sanitize_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package email
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestSanitizeHTML(t *testing.T) {
  7. cid := map[string]string{"logo@x": "data:image/png;base64,AAAA"}
  8. tests := []struct {
  9. name string
  10. input string
  11. allowRemote bool
  12. contains []string
  13. absent []string
  14. remote bool
  15. }{
  16. {
  17. name: "script and handlers removed",
  18. input: `<p onclick="x()" onmouseover='y()'>Hi</p><script>alert(1)</script><noscript>n</noscript>`,
  19. absent: []string{"onclick", "onmouseover", "<script", "alert(1)", "<noscript"},
  20. },
  21. {
  22. name: "dangerous links dropped, safe links get a new tab",
  23. input: `<a href="javascript:alert(1)">a</a><a href=" JaVa&#10;script:alert(2)">b</a><a href="https://ok.test/">c</a><a href="/system/x">d</a><a href="mailto:a@b.c">e</a>`,
  24. contains: []string{`href="https://ok.test/"`, `target="_blank"`, `rel="noopener noreferrer nofollow"`, `href="mailto:a@b.c"`},
  25. absent: []string{"javascript", "/system/x"},
  26. },
  27. {
  28. name: "remote image blocked by default",
  29. input: `<img src="https://t.test/p.gif" width="1">`,
  30. contains: []string{`data-remote-src="https://t.test/p.gif"`},
  31. absent: []string{` src="https://t.test/p.gif"`},
  32. remote: true,
  33. },
  34. {
  35. name: "remote image allowed",
  36. input: `<img src="https://t.test/p.gif">`,
  37. allowRemote: true,
  38. contains: []string{`src="https://t.test/p.gif"`},
  39. remote: true,
  40. },
  41. {
  42. name: "cid image resolved",
  43. input: `<img src="cid:LOGO@x">`,
  44. contains: []string{`src="data:image/png;base64,AAAA"`},
  45. },
  46. {
  47. name: "style urls and imports blocked",
  48. input: `<style>@import url("https://evil.test/a.css"); .a{background:url(https://t.test/bg.png)}</style><div style="background-image:url('https://t.test/x.png');color:red">x</div>`,
  49. contains: []string{"color:red", "background:none", "background-image:none"},
  50. absent: []string{"@import", "t.test", "evil.test"},
  51. remote: true,
  52. },
  53. {
  54. name: "frames, forms and meta removed",
  55. input: `<meta http-equiv="refresh" content="0;url=https://x.test"><iframe src="https://x.test"></iframe><form action="https://x.test"><input name="p"><button>Go</button></form><base href="https://x.test/">`,
  56. absent: []string{"<iframe", "<meta", "<input", "<form", "action=", "<base", "x.test"},
  57. },
  58. {
  59. name: "form contents are kept",
  60. input: `<form><p>Inside</p><button>Label</button></form>`,
  61. contains: []string{"<p>Inside</p>", "Label"},
  62. },
  63. {
  64. name: "svg and comments removed",
  65. input: `<svg onload="x()"><script>1</script></svg><!--[if mso]><table></table><![endif]-->ok`,
  66. absent: []string{"<svg", "onload", "[if mso]"},
  67. },
  68. {
  69. name: "style element cannot be closed early",
  70. input: `<style>p{color:red}</style><p>x</p>`,
  71. absent: []string{"behavior"},
  72. },
  73. {
  74. name: "legacy css expressions neutralised",
  75. input: `<div style="width:expression(alert(1));behavior:url(x.htc)">x</div>`,
  76. absent: []string{"expression(", "behavior:"},
  77. contains: []string{"blocked-"},
  78. },
  79. }
  80. for _, test := range tests {
  81. t.Run(test.name, func(t *testing.T) {
  82. result := sanitizeHTML(test.input, sanitizeOptions{allowRemote: test.allowRemote, cid: cid})
  83. for _, want := range test.contains {
  84. if !strings.Contains(result.html, want) {
  85. t.Errorf("missing %q in %s", want, result.html)
  86. }
  87. }
  88. for _, unwanted := range test.absent {
  89. if strings.Contains(result.html, unwanted) {
  90. t.Errorf("unexpected %q in %s", unwanted, result.html)
  91. }
  92. }
  93. if result.hasRemote != test.remote {
  94. t.Errorf("hasRemote = %v, want %v", result.hasRemote, test.remote)
  95. }
  96. })
  97. }
  98. }
  99. func TestHTMLToText(t *testing.T) {
  100. tests := []struct {
  101. name string
  102. input string
  103. withLinks bool
  104. want string
  105. }{
  106. {"paragraphs", "<p>Hello</p><p>World</p>", false, "Hello\n\nWorld"},
  107. {"line breaks and entities", "Tom &amp; Jerry<br>next&nbsp;line", false, "Tom & Jerry\nnext line"},
  108. {"head and style skipped", "<html><head><title>T</title><style>p{}</style></head><body>Body</body></html>", false, "Body"},
  109. {"lists", "<ul><li>One</li><li>Two</li></ul>", false, "- One\n- Two"},
  110. {"links appended", `<a href="https://a.test/x">Click</a>`, true, "Click (https://a.test/x)"},
  111. {"bare link not duplicated", `<a href="https://a.test/">https://a.test/</a>`, true, "https://a.test/"},
  112. {"preformatted", "<pre>a b\n c</pre>", false, "a b\n c"},
  113. }
  114. for _, test := range tests {
  115. t.Run(test.name, func(t *testing.T) {
  116. if got := htmlToText(test.input, test.withLinks); got != test.want {
  117. t.Errorf("htmlToText = %q, want %q", got, test.want)
  118. }
  119. })
  120. }
  121. }
  122. func TestPreviewText(t *testing.T) {
  123. text := "Thanks!\n> quoted line\n> another\nSee you"
  124. if got := previewText(text, 100); got != "Thanks! See you" {
  125. t.Errorf("previewText = %q", got)
  126. }
  127. if got := previewText(strings.Repeat("a", 50), 10); got != strings.Repeat("a", 10)+"…" {
  128. t.Errorf("truncation = %q", got)
  129. }
  130. }
  131. func TestUnflowText(t *testing.T) {
  132. flowed := "This is a long \r\nline that wraps.\r\n Stuffed\r\n-- \r\nSig"
  133. want := "This is a long line that wraps.\nStuffed\n-- \nSig"
  134. if got := unflowText(flowed, false); got != want {
  135. t.Errorf("unflowText = %q, want %q", got, want)
  136. }
  137. if got := unflowText("ab \r\ncd", true); got != "abcd" {
  138. t.Errorf("delsp unflow = %q", got)
  139. }
  140. }