sanitizer_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package sanitizer // import "miniflux.app/reader/sanitizer"
  5. import "testing"
  6. func TestValidInput(t *testing.T) {
  7. input := `<p>This is a <strong>text</strong> with an image: <img src="http://example.org/" alt="Test" loading="lazy">.</p>`
  8. output := Sanitize("http://example.org/", input)
  9. if input != output {
  10. t.Errorf(`Wrong output: "%s" != "%s"`, input, output)
  11. }
  12. }
  13. func TestSelfClosingTags(t *testing.T) {
  14. input := `<p>This <br> is a <strong>text</strong> <br/>with an image: <img src="http://example.org/" alt="Test" loading="lazy"/>.</p>`
  15. output := Sanitize("http://example.org/", input)
  16. if input != output {
  17. t.Errorf(`Wrong output: "%s" != "%s"`, input, output)
  18. }
  19. }
  20. func TestTable(t *testing.T) {
  21. input := `<table><tr><th>A</th><th colspan="2">B</th></tr><tr><td>C</td><td>D</td><td>E</td></tr></table>`
  22. output := Sanitize("http://example.org/", input)
  23. if input != output {
  24. t.Errorf(`Wrong output: "%s" != "%s"`, input, output)
  25. }
  26. }
  27. func TestRelativeURL(t *testing.T) {
  28. input := `This <a href="/test.html">link is relative</a> and this image: <img src="../folder/image.png"/>`
  29. expected := `This <a href="http://example.org/test.html" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">link is relative</a> and this image: <img src="http://example.org/folder/image.png" loading="lazy"/>`
  30. output := Sanitize("http://example.org/", input)
  31. if expected != output {
  32. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  33. }
  34. }
  35. func TestProtocolRelativeURL(t *testing.T) {
  36. input := `This <a href="//static.example.org/index.html">link is relative</a>.`
  37. expected := `This <a href="https://static.example.org/index.html" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">link is relative</a>.`
  38. output := Sanitize("http://example.org/", input)
  39. if expected != output {
  40. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  41. }
  42. }
  43. func TestInvalidTag(t *testing.T) {
  44. input := `<p>My invalid <b>tag</b>.</p>`
  45. expected := `<p>My invalid tag.</p>`
  46. output := Sanitize("http://example.org/", input)
  47. if expected != output {
  48. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  49. }
  50. }
  51. func TestVideoTag(t *testing.T) {
  52. input := `<p>My valid <video src="videofile.webm" autoplay poster="posterimage.jpg">fallback</video>.</p>`
  53. expected := `<p>My valid <video src="http://example.org/videofile.webm" poster="http://example.org/posterimage.jpg" controls>fallback</video>.</p>`
  54. output := Sanitize("http://example.org/", input)
  55. if expected != output {
  56. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  57. }
  58. }
  59. func TestAudioAndSourceTag(t *testing.T) {
  60. input := `<p>My music <audio controls="controls"><source src="foo.wav" type="audio/wav"></audio>.</p>`
  61. expected := `<p>My music <audio controls><source src="http://example.org/foo.wav" type="audio/wav"></audio>.</p>`
  62. output := Sanitize("http://example.org/", input)
  63. if expected != output {
  64. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  65. }
  66. }
  67. func TestUnknownTag(t *testing.T) {
  68. input := `<p>My invalid <unknown>tag</unknown>.</p>`
  69. expected := `<p>My invalid tag.</p>`
  70. output := Sanitize("http://example.org/", input)
  71. if expected != output {
  72. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  73. }
  74. }
  75. func TestInvalidNestedTag(t *testing.T) {
  76. input := `<p>My invalid <b>tag with some <em>valid</em> tag</b>.</p>`
  77. expected := `<p>My invalid tag with some <em>valid</em> tag.</p>`
  78. output := Sanitize("http://example.org/", input)
  79. if expected != output {
  80. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  81. }
  82. }
  83. func TestInvalidIFrame(t *testing.T) {
  84. input := `<iframe src="http://example.org/"></iframe>`
  85. expected := ``
  86. output := Sanitize("http://example.org/", input)
  87. if expected != output {
  88. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  89. }
  90. }
  91. func TestInvalidURLScheme(t *testing.T) {
  92. input := `<p>This link is <a src="file:///etc/passwd">not valid</a></p>`
  93. expected := `<p>This link is not valid</p>`
  94. output := Sanitize("http://example.org/", input)
  95. if expected != output {
  96. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  97. }
  98. }
  99. func TestAPTURIScheme(t *testing.T) {
  100. input := `<p>This link is <a href="apt:some-package?channel=test">valid</a></p>`
  101. expected := `<p>This link is <a href="apt:some-package?channel=test" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  102. output := Sanitize("http://example.org/", input)
  103. if expected != output {
  104. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  105. }
  106. }
  107. func TestBitcoinURIScheme(t *testing.T) {
  108. input := `<p>This link is <a href="bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W">valid</a></p>`
  109. expected := `<p>This link is <a href="bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  110. output := Sanitize("http://example.org/", input)
  111. if expected != output {
  112. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  113. }
  114. }
  115. func TestCallToURIScheme(t *testing.T) {
  116. input := `<p>This link is <a href="callto:12345679">valid</a></p>`
  117. expected := `<p>This link is <a href="callto:12345679" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  118. output := Sanitize("http://example.org/", input)
  119. if expected != output {
  120. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  121. }
  122. }
  123. func TestFeedURIScheme(t *testing.T) {
  124. input := `<p>This link is <a href="feed://example.com/rss.xml">valid</a></p>`
  125. expected := `<p>This link is <a href="feed://example.com/rss.xml" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  126. output := Sanitize("http://example.org/", input)
  127. if expected != output {
  128. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  129. }
  130. input = `<p>This link is <a href="feed:https://example.com/rss.xml">valid</a></p>`
  131. expected = `<p>This link is <a href="feed:https://example.com/rss.xml" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  132. output = Sanitize("http://example.org/", input)
  133. if expected != output {
  134. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  135. }
  136. }
  137. func TestGeoURIScheme(t *testing.T) {
  138. input := `<p>This link is <a href="geo:13.4125,103.8667">valid</a></p>`
  139. expected := `<p>This link is <a href="geo:13.4125,103.8667" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  140. output := Sanitize("http://example.org/", input)
  141. if expected != output {
  142. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  143. }
  144. }
  145. func TestItunesURIScheme(t *testing.T) {
  146. input := `<p>This link is <a href="itms://itunes.com/apps/my-app-name">valid</a></p>`
  147. expected := `<p>This link is <a href="itms://itunes.com/apps/my-app-name" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  148. output := Sanitize("http://example.org/", input)
  149. if expected != output {
  150. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  151. }
  152. input = `<p>This link is <a href="itms-apps://itunes.com/apps/my-app-name">valid</a></p>`
  153. expected = `<p>This link is <a href="itms-apps://itunes.com/apps/my-app-name" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  154. output = Sanitize("http://example.org/", input)
  155. if expected != output {
  156. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  157. }
  158. }
  159. func TestMagnetURIScheme(t *testing.T) {
  160. input := `<p>This link is <a href="magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&amp;xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7">valid</a></p>`
  161. expected := `<p>This link is <a href="magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&amp;xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  162. output := Sanitize("http://example.org/", input)
  163. if expected != output {
  164. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  165. }
  166. }
  167. func TestMailtoURIScheme(t *testing.T) {
  168. input := `<p>This link is <a href="mailto:jsmith@example.com?subject=A%20Test&amp;body=My%20idea%20is%3A%20%0A">valid</a></p>`
  169. expected := `<p>This link is <a href="mailto:jsmith@example.com?subject=A%20Test&amp;body=My%20idea%20is%3A%20%0A" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  170. output := Sanitize("http://example.org/", input)
  171. if expected != output {
  172. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  173. }
  174. }
  175. func TestNewsURIScheme(t *testing.T) {
  176. input := `<p>This link is <a href="news://news.server.example/*">valid</a></p>`
  177. expected := `<p>This link is <a href="news://news.server.example/*" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  178. output := Sanitize("http://example.org/", input)
  179. if expected != output {
  180. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  181. }
  182. input = `<p>This link is <a href="news:example.group.this">valid</a></p>`
  183. expected = `<p>This link is <a href="news:example.group.this" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  184. output = Sanitize("http://example.org/", input)
  185. if expected != output {
  186. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  187. }
  188. input = `<p>This link is <a href="nntp://news.server.example/example.group.this">valid</a></p>`
  189. expected = `<p>This link is <a href="nntp://news.server.example/example.group.this" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  190. output = Sanitize("http://example.org/", input)
  191. if expected != output {
  192. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  193. }
  194. }
  195. func TestRTMPURIScheme(t *testing.T) {
  196. input := `<p>This link is <a href="rtmp://mycompany.com/vod/mp4:mycoolvideo.mov">valid</a></p>`
  197. expected := `<p>This link is <a href="rtmp://mycompany.com/vod/mp4:mycoolvideo.mov" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  198. output := Sanitize("http://example.org/", input)
  199. if expected != output {
  200. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  201. }
  202. }
  203. func TestSIPURIScheme(t *testing.T) {
  204. input := `<p>This link is <a href="sip:+1-212-555-1212:1234@gateway.com;user=phone">valid</a></p>`
  205. expected := `<p>This link is <a href="sip:+1-212-555-1212:1234@gateway.com;user=phone" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  206. output := Sanitize("http://example.org/", input)
  207. if expected != output {
  208. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  209. }
  210. input = `<p>This link is <a href="sips:alice@atlanta.com?subject=project%20x&amp;priority=urgent">valid</a></p>`
  211. expected = `<p>This link is <a href="sips:alice@atlanta.com?subject=project%20x&amp;priority=urgent" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  212. output = Sanitize("http://example.org/", input)
  213. if expected != output {
  214. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  215. }
  216. }
  217. func TestSkypeURIScheme(t *testing.T) {
  218. input := `<p>This link is <a href="skype:echo123?call">valid</a></p>`
  219. expected := `<p>This link is <a href="skype:echo123?call" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  220. output := Sanitize("http://example.org/", input)
  221. if expected != output {
  222. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  223. }
  224. }
  225. func TestSpotifyURIScheme(t *testing.T) {
  226. input := `<p>This link is <a href="spotify:track:2jCnn1QPQ3E8ExtLe6INsx">valid</a></p>`
  227. expected := `<p>This link is <a href="spotify:track:2jCnn1QPQ3E8ExtLe6INsx" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  228. output := Sanitize("http://example.org/", input)
  229. if expected != output {
  230. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  231. }
  232. }
  233. func TestSteamURIScheme(t *testing.T) {
  234. input := `<p>This link is <a href="steam://settings/account">valid</a></p>`
  235. expected := `<p>This link is <a href="steam://settings/account" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  236. output := Sanitize("http://example.org/", input)
  237. if expected != output {
  238. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  239. }
  240. }
  241. func TestSubversionURIScheme(t *testing.T) {
  242. input := `<p>This link is <a href="svn://example.org">valid</a></p>`
  243. expected := `<p>This link is <a href="svn://example.org" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  244. output := Sanitize("http://example.org/", input)
  245. if expected != output {
  246. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  247. }
  248. input = `<p>This link is <a href="svn+ssh://example.org">valid</a></p>`
  249. expected = `<p>This link is <a href="svn+ssh://example.org" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  250. output = Sanitize("http://example.org/", input)
  251. if expected != output {
  252. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  253. }
  254. }
  255. func TestTelURIScheme(t *testing.T) {
  256. input := `<p>This link is <a href="tel:+1-201-555-0123">valid</a></p>`
  257. expected := `<p>This link is <a href="tel:+1-201-555-0123" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  258. output := Sanitize("http://example.org/", input)
  259. if expected != output {
  260. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  261. }
  262. }
  263. func TestWebcalURIScheme(t *testing.T) {
  264. input := `<p>This link is <a href="webcal://example.com/calendar.ics">valid</a></p>`
  265. expected := `<p>This link is <a href="webcal://example.com/calendar.ics" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  266. output := Sanitize("http://example.org/", input)
  267. if expected != output {
  268. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  269. }
  270. }
  271. func TestXMPPURIScheme(t *testing.T) {
  272. input := `<p>This link is <a href="xmpp:user@host?subscribe&amp;type=subscribed">valid</a></p>`
  273. expected := `<p>This link is <a href="xmpp:user@host?subscribe&amp;type=subscribed" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  274. output := Sanitize("http://example.org/", input)
  275. if expected != output {
  276. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  277. }
  278. }
  279. func TestBlacklistedLink(t *testing.T) {
  280. input := `<p>This image is not valid <img src="https://stats.wordpress.com/some-tracker"></p>`
  281. expected := `<p>This image is not valid </p>`
  282. output := Sanitize("http://example.org/", input)
  283. if expected != output {
  284. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  285. }
  286. }
  287. func TestPixelTracker(t *testing.T) {
  288. input := `<p><img src="https://tracker1.example.org/" height="1" width="1"> and <img src="https://tracker2.example.org/" height="1" width="1"/></p>`
  289. expected := `<p> and </p>`
  290. output := Sanitize("http://example.org/", input)
  291. if expected != output {
  292. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  293. }
  294. }
  295. func TestXmlEntities(t *testing.T) {
  296. input := `<pre>echo "test" &gt; /etc/hosts</pre>`
  297. expected := `<pre>echo &#34;test&#34; &gt; /etc/hosts</pre>`
  298. output := Sanitize("http://example.org/", input)
  299. if expected != output {
  300. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  301. }
  302. }
  303. func TestEspaceAttributes(t *testing.T) {
  304. input := `<td rowspan="<b>test</b>">test</td>`
  305. expected := `<td rowspan="&lt;b&gt;test&lt;/b&gt;">test</td>`
  306. output := Sanitize("http://example.org/", input)
  307. if expected != output {
  308. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  309. }
  310. }
  311. func TestReplaceYoutubeURL(t *testing.T) {
  312. input := `<iframe src="http://www.youtube.com/embed/test123?version=3&#038;rel=1&#038;fs=1&#038;autohide=2&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent"></iframe>`
  313. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123?version=3&amp;rel=1&amp;fs=1&amp;autohide=2&amp;showsearch=0&amp;showinfo=1&amp;iv_load_policy=1&amp;wmode=transparent" sandbox="allow-scripts allow-same-origin allow-popups" loading="lazy"></iframe>`
  314. output := Sanitize("http://example.org/", input)
  315. if expected != output {
  316. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  317. }
  318. }
  319. func TestReplaceSecureYoutubeURL(t *testing.T) {
  320. input := `<iframe src="https://www.youtube.com/embed/test123"></iframe>`
  321. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123" sandbox="allow-scripts allow-same-origin allow-popups" loading="lazy"></iframe>`
  322. output := Sanitize("http://example.org/", input)
  323. if expected != output {
  324. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  325. }
  326. }
  327. func TestReplaceSecureYoutubeURLWithParameters(t *testing.T) {
  328. input := `<iframe src="https://www.youtube.com/embed/test123?rel=0&amp;controls=0"></iframe>`
  329. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&amp;controls=0" sandbox="allow-scripts allow-same-origin allow-popups" loading="lazy"></iframe>`
  330. output := Sanitize("http://example.org/", input)
  331. if expected != output {
  332. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  333. }
  334. }
  335. func TestReplaceYoutubeURLAlreadyReplaced(t *testing.T) {
  336. input := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&amp;controls=0" sandbox="allow-scripts allow-same-origin"></iframe>`
  337. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&amp;controls=0" sandbox="allow-scripts allow-same-origin allow-popups" loading="lazy"></iframe>`
  338. output := Sanitize("http://example.org/", input)
  339. if expected != output {
  340. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  341. }
  342. }
  343. func TestReplaceProtocolRelativeYoutubeURL(t *testing.T) {
  344. input := `<iframe src="//www.youtube.com/embed/Bf2W84jrGqs" width="560" height="314" allowfullscreen="allowfullscreen"></iframe>`
  345. expected := `<iframe src="https://www.youtube-nocookie.com/embed/Bf2W84jrGqs" width="560" height="314" allowfullscreen="allowfullscreen" sandbox="allow-scripts allow-same-origin allow-popups" loading="lazy"></iframe>`
  346. output := Sanitize("http://example.org/", input)
  347. if expected != output {
  348. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  349. }
  350. }
  351. func TestReplaceIframeURL(t *testing.T) {
  352. input := `<iframe src="https://player.vimeo.com/video/123456?title=0&amp;byline=0"></iframe>`
  353. expected := `<iframe src="https://player.vimeo.com/video/123456?title=0&amp;byline=0" sandbox="allow-scripts allow-same-origin allow-popups" loading="lazy"></iframe>`
  354. output := Sanitize("http://example.org/", input)
  355. if expected != output {
  356. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  357. }
  358. }
  359. func TestReplaceNoScript(t *testing.T) {
  360. input := `<p>Before paragraph.</p><noscript>Inside <code>noscript</code> tag with an image: <img src="http://example.org/" alt="Test" loading="lazy"></noscript><p>After paragraph.</p>`
  361. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  362. output := Sanitize("http://example.org/", input)
  363. if expected != output {
  364. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  365. }
  366. }
  367. func TestReplaceScript(t *testing.T) {
  368. input := `<p>Before paragraph.</p><script type="text/javascript">alert("1");</script><p>After paragraph.</p>`
  369. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  370. output := Sanitize("http://example.org/", input)
  371. if expected != output {
  372. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  373. }
  374. }
  375. func TestReplaceStyle(t *testing.T) {
  376. input := `<p>Before paragraph.</p><style>body { background-color: #ff0000; }</style><p>After paragraph.</p>`
  377. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  378. output := Sanitize("http://example.org/", input)
  379. if expected != output {
  380. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  381. }
  382. }