Skip to content

Commit

Permalink
Fixes issue zammad#3344 - Allow mailto links in knowledge base (KB) a…
Browse files Browse the repository at this point in the history
…nd email signatures.
  • Loading branch information
zammad-sync authored and thorsteneckel committed Jan 4, 2021
1 parent 92e57e1 commit 2a6036f
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 51 deletions.
23 changes: 1 addition & 22 deletions lib/html_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.strict(string, external = false, timeout: true)
if node['href']
href = cleanup_target(node['href'], keep_spaces: true)
href_without_spaces = href.gsub(/[[:space:]]/, '')
if external && href_without_spaces.present? && !href_without_spaces.downcase.start_with?('//') && href_without_spaces.downcase !~ %r{^.{1,6}://.+?}
if external && href_without_spaces.present? && !href_without_spaces.downcase.start_with?('mailto:') && !href_without_spaces.downcase.start_with?('//') && href_without_spaces.downcase !~ %r{^.{1,6}://.+?}
node['href'] = "http://#{node['href']}"
href = node['href']
href_without_spaces = href.gsub(/[[:space:]]/, '')
Expand Down Expand Up @@ -176,16 +176,6 @@ def self.strict(string, external = false, timeout: true)
node.delete(attribute)
end

# remove mailto links
if node['href']
href = cleanup_target(node['href'])
if href =~ /mailto:(.*)$/i
text = Nokogiri::XML::Text.new($1, node.document)
node.add_next_sibling(text)
node.remove
Loofah::Scrubber::STOP
end
end
end

done = true
Expand Down Expand Up @@ -320,17 +310,6 @@ def self.cleanup_structure(string, type = 'all')

scrubber_cleanup = Loofah::Scrubber.new do |node|

# remove mailto links
if node['href']
href = cleanup_target(node['href'])
if href =~ /mailto:(.*)$/i
text = Nokogiri::XML::Text.new($1, node.document)
node.add_next_sibling(text)
node.remove
Loofah::Scrubber::STOP
end
end

# remove not needed new lines
if node.instance_of?(Nokogiri::XML::Text)
if !node.parent || (node.parent.name != 'pre' && node.parent.name != 'code') # rubocop:disable Style/SoleNestedConditional
Expand Down
18 changes: 10 additions & 8 deletions spec/lib/core_ext/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1199,23 +1199,23 @@
expect(<<~HTML.chomp.html2html_strict).to eq(<<~TEXT.chomp)
<a href="mailto:[email protected]" style="color: blue; text-decoration: underline; ">[email protected]</a>
HTML
[email protected]
<a href="mailto:[email protected]">[email protected]</a>
TEXT
end

it 'strips <a> tags (even with upcased "MAILTO:")' do
expect(<<~HTML.chomp.html2html_strict).to eq(<<~TEXT.chomp)
<a href="MAILTO:[email protected]" style="color: blue; text-decoration: underline; ">[email protected]</a>
HTML
[email protected]
<a href="MAILTO:[email protected]">[email protected]</a>
TEXT
end

it 'extracts destination address when it differs from <a> innertext' do
expect(<<~HTML.chomp.html2html_strict).to eq(<<~TEXT.chomp)
<a href="MAILTO:[email protected]" style="color: blue; text-decoration: underline; ">[email protected]</a>
HTML
[email protected]
<a href="MAILTO:[email protected]">[email protected]</a>
TEXT
end

Expand Down Expand Up @@ -1289,7 +1289,9 @@
<div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family: Arial, sans-serif; ">Mit freundlichem Gruß<span class="Apple-converted-space">&nbsp;</span><br><br>John Smith<br>Service und Support<br><br>Example Service AG &amp; Co.<o:p></o:p></span></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family: Arial, sans-serif; ">Management OHG<br>Someware-Str. 4<br>xxxxx Someware<br><br></span><span style="font-size: 10pt; font-family: Arial, sans-serif; "><o:p></o:p></span></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family: Arial, sans-serif; ">Tel.: +49 001 7601 462<br>Fax: +49 001 7601 472</span><span style="font-size: 10pt; font-family: Arial, sans-serif; "><o:p></o:p></span></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family: Arial, sans-serif; "><a href="mailto:[email protected]" style=color: blue; text-decoration: underline; ">[email protected]</a></span><span style="font-size: 10pt; font-family: Arial, sans-serif; "><o:p></o:p></span></div><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family: Arial, sans-serif; "><a href="http://www.example.com" style="color: blue; text-decoration: underline; ">www.example.com</a></span><span style="font-size: 10pt; font-family: Arial, sans-serif; "><o:p></o:p></span></div>
HTML
<div>Mit freundlichem Gruß<br><br>John Smith<br>Service und Support<br><br>Example Service AG &amp; Co. </div><div>Management OHG<br>Someware-Str. 4<br>xxxxx Someware<br><br>
</div><div>Tel.: +49 001 7601 462<br>Fax: +49 001 7601 472 </div><div>[email protected]</div><div>
</div><div>Tel.: +49 001 7601 462<br>Fax: +49 001 7601 472 </div><div>
<a href="mailto:[email protected]">[email protected]</a>
</div><div>
<a href="http://www.example.com" rel="nofollow noreferrer noopener" target="_blank">www.example.com</a>
</div>
TEXT
Expand Down Expand Up @@ -1321,7 +1323,7 @@
HTML
<div>
<p><span style="color:#1f497d;">Guten Morgen, Frau ABC,</span></p><p><span style="color:#1f497d;"><p>&nbsp;</p></span></p><p><span style="color:#1f497d;">vielen Dank für die Reservierung. Dabei allerdings die Sprache (Niederländisch) nicht erwähnt. Können Sie bitte dieses in Ihrer Reservierung vormerken?</span></p><p><span style="color:#1f497d;"><p>&nbsp;</p></span></p><p><span style="color:#1f497d;">Nochmals vielen Dank und herzliche Grüße </span></p><div>
<p><b><span style="color:#1f497d;"><p>&nbsp;</p></span></b></p><p><b><span style="color:#1f497d;">Anna Smith</span></b></p><p><b><span style="color:#1f497d;">art abc SEV GmbH</span></b></p><p><b><span style="color:#1f497d;">art abc TRAV</span></b></p><p><span style="color:#1f497d;">Marktstätte 123</span></p><p><span style="color:#1f497d;">123456 Dorten</span></p><p><span style="color:#1f497d;">T: +49 (0) 12345/1234560-1</span></p><p><span style="color:#1f497d;">T: +49 (0) 12345/1234560-0</span></p><p><span style="color:#1f497d;">F: +49 (0) 12345/1234560-2</span></p><p>[email protected]</p><p><a href="http://www.example.com/" rel="nofollow noreferrer noopener" target="_blank">www.example.com</a><span style="color:#1f497d;"> </span><a href="http://www.ABC.com/" rel="nofollow noreferrer noopener" target="_blank">www.ABC.com</a></p><p><span style="color:#1f497d;">Geschäftsführer Vor Nach, VorUndZu Nach - Amtsgericht Dort HRB 12345 - Ein Unternehmer der ABC Gruppe</span></p></div></div>
<p><b><span style="color:#1f497d;"><p>&nbsp;</p></span></b></p><p><b><span style="color:#1f497d;">Anna Smith</span></b></p><p><b><span style="color:#1f497d;">art abc SEV GmbH</span></b></p><p><b><span style="color:#1f497d;">art abc TRAV</span></b></p><p><span style="color:#1f497d;">Marktstätte 123</span></p><p><span style="color:#1f497d;">123456 Dorten</span></p><p><span style="color:#1f497d;">T: +49 (0) 12345/1234560-1</span></p><p><span style="color:#1f497d;">T: +49 (0) 12345/1234560-0</span></p><p><span style="color:#1f497d;">F: +49 (0) 12345/1234560-2</span></p><p><a href="mailto:[email protected]">[email protected]</a></p><p><a href="http://www.example.com/" rel="nofollow noreferrer noopener" target="_blank">www.example.com</a><span style="color:#1f497d;"> </span><a href="http://www.ABC.com/" rel="nofollow noreferrer noopener" target="_blank">www.ABC.com</a></p><p><span style="color:#1f497d;">Geschäftsführer Vor Nach, VorUndZu Nach - Amtsgericht Dort HRB 12345 - Ein Unternehmer der ABC Gruppe</span></p></div></div>
TEXT
end

Expand All @@ -1344,7 +1346,7 @@
HTML
<p><span style="color:#1f497d;"><p>&nbsp;</p></span></p><div>
<div>
<span class="js-signatureMarker"></span><p><b>Von:</b> Besucherbüro, MKuk [[email protected]] <br>
<span class="js-signatureMarker"></span><p><b>Von:</b> Besucherbüro, MKuk [<a href="mailto:[email protected]">mailto:[email protected]</a>] <br>
<b>Gesendet:</b> Freitag, 16. Dezember 2016 08:05<br>
<b>An:</b> 'Amaia Epalza'<br>
<b>Betreff:</b> AW: Gruppe vtb Kultuur // 28.06.2017</p></div></div><p>&nbsp;</p><p><b><span style="color:#1f497d;">Reservierungsbestätigung Führung Skulptur-Projekte 2017 am </span></b></p><p></p><p><span style="color:#1f497d;"> </span></p><p></p><p>Guten Morgen Frau Epalza,</p>
Expand Down Expand Up @@ -1473,7 +1475,7 @@
expect(<<~HTML.chomp.html2html_strict).to eq(<<~TEXT.chomp)
<div><div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0cm 0cm 0cm"><p class="MsoNormal"><b><span lang="DE" style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">Von:</span></b><span lang="DE" style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"> Martin Edenhofer via Zammad Helpdesk [mailto:<a href="mailto:[email protected]">[email protected]</a>] <br><b>Gesendet:</b>\u0020
HTML
#{marker}<p><b>Von:</b> Martin Edenhofer via Zammad Helpdesk [mailto:[email protected]] <br><b>Gesendet:</b> </p>
#{marker}<p><b>Von:</b> Martin Edenhofer via Zammad Helpdesk [mailto:<a href="mailto:[email protected]">[email protected]</a>] <br><b>Gesendet:</b> </p>
TEXT
end

Expand Down Expand Up @@ -1510,7 +1512,7 @@
<br class=""><div><blockquote type="cite" class=""><div class="">On 04 Mar 2017, at 14:47, Oliver Ruhm &lt;<a href="mailto:[email protected]" class="">[email protected]</a>&gt; wrote:</div><br class="Apple-interchange-newline">
HTML
<div>#{marker}<blockquote type="cite">
<div>On 04 Mar 2017, at 14:47, Oliver Ruhm &lt;[email protected]&gt; wrote:</div><br>
<div>On 04 Mar 2017, at 14:47, Oliver Ruhm &lt;<a href="mailto:[email protected]">[email protected]</a>&gt; wrote:</div><br>
</blockquote></div>
TEXT
end
Expand Down
98 changes: 96 additions & 2 deletions test/data/mail/mail001.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,106 @@
from: [email protected]
from_email: [email protected]
from_display_name: ''
to: [email protected]
subject: 'CI Daten für PublicView '
content_type: text/html
body: |-
<div>
<div>Hallo Martin,</div><p>&nbsp;</p><div>wie besprochen hier noch die Daten für die Intranetseite:</div><p>&nbsp;</p><div>Schriftart/-größe: Verdana 11 Pt wenn von Browser nicht unterstützt oder nicht vorhanden wird Arial 11 Pt genommen</div><div>Schriftfarbe: Schwarz</div><div>Farbe für die Balken in der Grafik: D7DDE9 (Blau)</div><p>&nbsp;</p><div>Wenn noch was fehlt oder du was brauchst sag mir Bescheid.</div><p>&nbsp;</p><div>Mit freundlichem Gruß<br><br>John Smith<br>Service und Support<br><br>Example Service AG &amp; Co. </div><div>Management OHG<br>Someware-Str. 4<br>xxxxx Someware<br><br>
</div><div>Tel.: +49 001 7601 462<br>Fax: +49 001 7601 472 </div><div>[email protected]</div><div>
</div><div>Tel.: +49 001 7601 462<br>Fax: +49 001 7601 472 </div><div>
<a href="mailto:[email protected]">[email protected]</a>
</div><div>
<a href="http://www.example.com" rel="nofollow noreferrer noopener" target="_blank">www.example.com</a>
</div><div>
<br>OHG mit Sitz in Someware<br>AG: Someware - HRA 4158<br>Geschäftsführung: Tilman Test, Klaus Jürgen Test, </div><div>Bernhard Test, Ulrich Test<br>USt-IdNr. DE 1010101010<br><br>Persönlich haftende geschäftsführende Gesellschafterin: </div><div>Marie Test Example Stiftung, Someware<br>Vorstand: Rolf Test<br><br>Persönlich haftende Gesellschafterin: </div><div>Example Service AG, Someware<br>AG: Someware - HRB xxx<br>Vorstand: Marie Test </div><p>&nbsp;</p></div>
content_type: text/html
attachments:
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
data: '<html><head><base href="x-msg://2849/"></head><body style="word-wrap: break-word;
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span"
style="border-collapse: separate; font-family: Helvetica; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal; line-height:
normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform:
none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing:
0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect:
none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size:
medium; "><div lang="DE" link="blue" vlink="purple"><div class="Section1" style="page:
Section1; "><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm;
margin-bottom: 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif; "><span
style="font-size: 10pt; font-family: Arial, sans-serif; ">Hallo Martin,<o:p></o:p></span></div><div
style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt;
font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt;
font-family: Arial, sans-serif; "><o:p>&nbsp;</o:p></span></div><div style="margin-top:
0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size:
11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family:
Arial, sans-serif; ">wie besprochen hier noch die Daten für die Intranetseite:<o:p></o:p></span></div><div
style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt;
font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt;
font-family: Arial, sans-serif; "><o:p>&nbsp;</o:p></span></div><div style="margin-top:
0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size:
11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family:
Arial, sans-serif; ">Schriftart/-größe: Verdana 11 Pt wenn von Browser nicht unterstützt
oder nicht vorhanden wird Arial 11 Pt genommen<o:p></o:p></span></div><div style="margin-top:
0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size:
11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family:
Arial, sans-serif; ">Schriftfarbe: Schwarz<o:p></o:p></span></div><div style="margin-top:
0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size:
11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family:
Arial, sans-serif; ">Farbe für die Balken in der Grafik: D7DDE9 (Blau)<o:p></o:p></span></div><div
style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt;
font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt;
font-family: Arial, sans-serif; "><o:p>&nbsp;</o:p></span></div><div style="margin-top:
0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size:
11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family:
Arial, sans-serif; ">Wenn noch was fehlt oder du was brauchst sag mir Bescheid.<o:p></o:p></span></div><div
style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt;
font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt;
font-family: Arial, sans-serif; "><o:p>&nbsp;</o:p></span></div><div style="margin-top:
0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size:
11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family:
Arial, sans-serif; ">Mit freundlichem Gruß<span class="Apple-converted-space">&nbsp;</span><br><br>John
Smith<br>Service und Support<br><br>Example Service AG &amp; Co.<o:p></o:p></span></div><div
style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt;
font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt;
font-family: Arial, sans-serif; ">Management OHG<br>Someware-Str. 4<br>xxxxx Someware<br><br></span><span
style="font-size: 10pt; font-family: Arial, sans-serif; "><o:p></o:p></span></div><div
style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt;
font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt;
font-family: Arial, sans-serif; ">Tel.: +49 001 7601 462<br>Fax: +49 001 7601
472</span><span style="font-size: 10pt; font-family: Arial, sans-serif; "><o:p></o:p></span></div><div
style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt;
font-size: 11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt;
font-family: Arial, sans-serif; "><a href="mailto:[email protected]" style="color:
blue; text-decoration: underline; ">[email protected]</a></span><span style="font-size:
10pt; font-family: Arial, sans-serif; "><o:p></o:p></span></div><div style="margin-top:
0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size:
11pt; font-family: Calibri, sans-serif; "><span style="font-size: 10pt; font-family:
Arial, sans-serif; "><a href="http://www.example.com" style="color: blue; text-decoration:
underline; ">www.example.com</a></span><span style="font-size: 10pt; font-family:
Arial, sans-serif; "><o:p></o:p></span></div><div style="margin-top: 0cm; margin-right:
0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family:
Calibri, sans-serif; "><span style="font-size: 8pt; font-family: Arial, sans-serif;
"><br>OHG mit Sitz in Someware<br>AG: Someware - HRA 4158<br>Geschäftsführung:
Tilman Test, Klaus Jürgen Test,</span><span style="font-size: 8pt; font-family:
Arial, sans-serif; "><o:p></o:p></span></div><div style="margin-top: 0cm; margin-right:
0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family:
Calibri, sans-serif; "><span style="font-size: 8pt; font-family: Arial, sans-serif;
">Bernhard Test, Ulrich Test<br>USt-IdNr. DE 1010101010<br><br>Persönlich haftende
geschäftsführende Gesellschafterin:</span><span style="font-size: 8pt; font-family:
Arial, sans-serif; "><o:p></o:p></span></div><div style="margin-top: 0cm; margin-right:
0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family:
Calibri, sans-serif; "><span style="font-size: 8pt; font-family: Arial, sans-serif;
">Marie Test Example Stiftung, Someware<br>Vorstand: Rolf Test<br><br>Persönlich
haftende Gesellschafterin:</span><span style="font-size: 8pt; font-family: Arial,
sans-serif; "><o:p></o:p></span></div><div style="margin-top: 0cm; margin-right:
0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 11pt; font-family:
Calibri, sans-serif; "><span style="font-size: 8pt; font-family: Arial, sans-serif;
">Example Service AG, Someware<br>AG: Someware - HRB xxx<br>Vorstand: Marie Test</span><span
style="font-size: 8pt; font-family: Arial, sans-serif; "><o:p></o:p></span></div><div
style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt;
font-size: 11pt; font-family: Calibri, sans-serif; "><o:p>&nbsp;</o:p></div></div></div></span></body></html>'
filename: message.html
preferences: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
content-alternative: true
original-format: true
Mime-Type: text/html
Charset: iso-8859-1
Loading

0 comments on commit 2a6036f

Please sign in to comment.