sanitizer_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sanitizer // import "miniflux.app/v2/internal/reader/sanitizer"
  4. import (
  5. "os"
  6. "testing"
  7. "miniflux.app/v2/internal/config"
  8. )
  9. func TestMain(m *testing.M) {
  10. config.Opts = config.NewOptions()
  11. exitCode := m.Run()
  12. os.Exit(exitCode)
  13. }
  14. func BenchmarkSanitize(b *testing.B) {
  15. var testCases = map[string][]string{
  16. "miniflux_github.html": {"https://github.com/miniflux/v2", ""},
  17. "miniflux_wikipedia.html": {"https://fr.wikipedia.org/wiki/Miniflux", ""},
  18. }
  19. for filename := range testCases {
  20. data, err := os.ReadFile("testdata/" + filename)
  21. if err != nil {
  22. b.Fatalf(`Unable to read file %q: %v`, filename, err)
  23. }
  24. testCases[filename][1] = string(data)
  25. }
  26. for range b.N {
  27. for _, v := range testCases {
  28. Sanitize(v[0], v[1])
  29. }
  30. }
  31. }
  32. func TestValidInput(t *testing.T) {
  33. input := `<p>This is a <strong>text</strong> with an image: <img src="http://example.org/" alt="Test" loading="lazy">.</p>`
  34. output := Sanitize("http://example.org/", input)
  35. if input != output {
  36. t.Errorf(`Wrong output: "%s" != "%s"`, input, output)
  37. }
  38. }
  39. func TestImgWithWidthAndHeightAttribute(t *testing.T) {
  40. input := `<img src="https://example.org/image.png" width="10" height="20">`
  41. expected := `<img src="https://example.org/image.png" width="10" height="20" loading="lazy">`
  42. output := Sanitize("http://example.org/", input)
  43. if output != expected {
  44. t.Errorf(`Wrong output: %s`, output)
  45. }
  46. }
  47. func TestImgWithWidthAndHeightAttributeLargerThanMinifluxLayout(t *testing.T) {
  48. input := `<img src="https://example.org/image.png" width="1200" height="675">`
  49. expected := `<img src="https://example.org/image.png" loading="lazy">`
  50. output := Sanitize("http://example.org/", input)
  51. if output != expected {
  52. t.Errorf(`Wrong output: %s`, output)
  53. }
  54. }
  55. func TestImgWithIncorrectWidthAndHeightAttribute(t *testing.T) {
  56. input := `<img src="https://example.org/image.png" width="10px" height="20px">`
  57. expected := `<img src="https://example.org/image.png" loading="lazy">`
  58. output := Sanitize("http://example.org/", input)
  59. if output != expected {
  60. t.Errorf(`Wrong output: %s`, output)
  61. }
  62. }
  63. func TestImgWithTextDataURL(t *testing.T) {
  64. input := `<img src="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" alt="Example">`
  65. expected := ``
  66. output := Sanitize("http://example.org/", input)
  67. if output != expected {
  68. t.Errorf(`Wrong output: %s`, output)
  69. }
  70. }
  71. func TestImgWithDataURL(t *testing.T) {
  72. input := `<img src="data:image/gif;base64,test" alt="Example">`
  73. expected := `<img src="data:image/gif;base64,test" alt="Example" loading="lazy">`
  74. output := Sanitize("http://example.org/", input)
  75. if output != expected {
  76. t.Errorf(`Wrong output: %s`, output)
  77. }
  78. }
  79. func TestImgWithSrcset(t *testing.T) {
  80. input := `<img srcset="example-320w.jpg, example-480w.jpg 1.5x, example-640w.jpg 2x, example-640w.jpg 640w" src="example-640w.jpg" alt="Example">`
  81. expected := `<img srcset="http://example.org/example-320w.jpg, http://example.org/example-480w.jpg 1.5x, http://example.org/example-640w.jpg 2x, http://example.org/example-640w.jpg 640w" src="http://example.org/example-640w.jpg" alt="Example" loading="lazy">`
  82. output := Sanitize("http://example.org/", input)
  83. if output != expected {
  84. t.Errorf(`Wrong output: %s`, output)
  85. }
  86. }
  87. func TestSourceWithSrcsetAndMedia(t *testing.T) {
  88. input := `<picture><source media="(min-width: 800px)" srcset="elva-800w.jpg"></picture>`
  89. expected := `<picture><source media="(min-width: 800px)" srcset="http://example.org/elva-800w.jpg"></picture>`
  90. output := Sanitize("http://example.org/", input)
  91. if output != expected {
  92. t.Errorf(`Wrong output: %s`, output)
  93. }
  94. }
  95. func TestMediumImgWithSrcset(t *testing.T) {
  96. input := `<img alt="Image for post" class="t u v ef aj" src="https://miro.medium.com/max/5460/1*aJ9JibWDqO81qMfNtqgqrw.jpeg" srcset="https://miro.medium.com/max/552/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 276w, https://miro.medium.com/max/1000/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 500w" sizes="500px" width="2730" height="3407">`
  97. expected := `<img alt="Image for post" src="https://miro.medium.com/max/5460/1*aJ9JibWDqO81qMfNtqgqrw.jpeg" srcset="https://miro.medium.com/max/552/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 276w, https://miro.medium.com/max/1000/1*aJ9JibWDqO81qMfNtqgqrw.jpeg 500w" sizes="500px" loading="lazy">`
  98. output := Sanitize("http://example.org/", input)
  99. if output != expected {
  100. t.Errorf(`Wrong output: %s`, output)
  101. }
  102. }
  103. func TestSelfClosingTags(t *testing.T) {
  104. input := `<p>This <br> is a <strong>text</strong> <br/>with an image: <img src="http://example.org/" alt="Test" loading="lazy"/>.</p>`
  105. output := Sanitize("http://example.org/", input)
  106. if input != output {
  107. t.Errorf(`Wrong output: "%s" != "%s"`, input, output)
  108. }
  109. }
  110. func TestTable(t *testing.T) {
  111. input := `<table><tr><th>A</th><th colspan="2">B</th></tr><tr><td>C</td><td>D</td><td>E</td></tr></table>`
  112. output := Sanitize("http://example.org/", input)
  113. if input != output {
  114. t.Errorf(`Wrong output: "%s" != "%s"`, input, output)
  115. }
  116. }
  117. func TestRelativeURL(t *testing.T) {
  118. input := `This <a href="/test.html">link is relative</a> and this image: <img src="../folder/image.png"/>`
  119. 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"/>`
  120. output := Sanitize("http://example.org/", input)
  121. if expected != output {
  122. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  123. }
  124. }
  125. func TestProtocolRelativeURL(t *testing.T) {
  126. input := `This <a href="//static.example.org/index.html">link is relative</a>.`
  127. expected := `This <a href="https://static.example.org/index.html" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">link is relative</a>.`
  128. output := Sanitize("http://example.org/", input)
  129. if expected != output {
  130. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  131. }
  132. }
  133. func TestInvalidTag(t *testing.T) {
  134. input := `<p>My invalid <b>tag</b>.</p>`
  135. expected := `<p>My invalid tag.</p>`
  136. output := Sanitize("http://example.org/", input)
  137. if expected != output {
  138. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  139. }
  140. }
  141. func TestVideoTag(t *testing.T) {
  142. input := `<p>My valid <video src="videofile.webm" autoplay poster="posterimage.jpg">fallback</video>.</p>`
  143. expected := `<p>My valid <video src="http://example.org/videofile.webm" poster="http://example.org/posterimage.jpg" controls>fallback</video>.</p>`
  144. output := Sanitize("http://example.org/", input)
  145. if expected != output {
  146. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  147. }
  148. }
  149. func TestAudioAndSourceTag(t *testing.T) {
  150. input := `<p>My music <audio controls="controls"><source src="foo.wav" type="audio/wav"></audio>.</p>`
  151. expected := `<p>My music <audio controls><source src="http://example.org/foo.wav" type="audio/wav"></audio>.</p>`
  152. output := Sanitize("http://example.org/", input)
  153. if expected != output {
  154. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  155. }
  156. }
  157. func TestUnknownTag(t *testing.T) {
  158. input := `<p>My invalid <unknown>tag</unknown>.</p>`
  159. expected := `<p>My invalid tag.</p>`
  160. output := Sanitize("http://example.org/", input)
  161. if expected != output {
  162. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  163. }
  164. }
  165. func TestInvalidNestedTag(t *testing.T) {
  166. input := `<p>My invalid <b>tag with some <em>valid</em> tag</b>.</p>`
  167. expected := `<p>My invalid tag with some <em>valid</em> tag.</p>`
  168. output := Sanitize("http://example.org/", input)
  169. if expected != output {
  170. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  171. }
  172. }
  173. func TestInvalidIFrame(t *testing.T) {
  174. input := `<iframe src="http://example.org/"></iframe>`
  175. expected := ``
  176. output := Sanitize("http://example.com/", input)
  177. if expected != output {
  178. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  179. }
  180. }
  181. func TestIFrameWithChildElements(t *testing.T) {
  182. input := `<iframe src="https://www.youtube.com/"><p>test</p></iframe>`
  183. expected := `<iframe src="https://www.youtube.com/" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  184. output := Sanitize("http://example.com/", input)
  185. if expected != output {
  186. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  187. }
  188. }
  189. func TestAnchorLink(t *testing.T) {
  190. input := `<p>This link is <a href="#some-anchor">an anchor</a></p>`
  191. expected := `<p>This link is <a href="#some-anchor">an anchor</a></p>`
  192. output := Sanitize("http://example.org/", input)
  193. if expected != output {
  194. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  195. }
  196. }
  197. func TestInvalidURLScheme(t *testing.T) {
  198. input := `<p>This link is <a src="file:///etc/passwd">not valid</a></p>`
  199. expected := `<p>This link is not valid</p>`
  200. output := Sanitize("http://example.org/", input)
  201. if expected != output {
  202. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  203. }
  204. }
  205. func TestAPTURIScheme(t *testing.T) {
  206. input := `<p>This link is <a href="apt:some-package?channel=test">valid</a></p>`
  207. expected := `<p>This link is <a href="apt:some-package?channel=test" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  208. output := Sanitize("http://example.org/", input)
  209. if expected != output {
  210. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  211. }
  212. }
  213. func TestBitcoinURIScheme(t *testing.T) {
  214. input := `<p>This link is <a href="bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W">valid</a></p>`
  215. expected := `<p>This link is <a href="bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  216. output := Sanitize("http://example.org/", input)
  217. if expected != output {
  218. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  219. }
  220. }
  221. func TestCallToURIScheme(t *testing.T) {
  222. input := `<p>This link is <a href="callto:12345679">valid</a></p>`
  223. expected := `<p>This link is <a href="callto:12345679" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  224. output := Sanitize("http://example.org/", input)
  225. if expected != output {
  226. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  227. }
  228. }
  229. func TestFeedURIScheme(t *testing.T) {
  230. input := `<p>This link is <a href="feed://example.com/rss.xml">valid</a></p>`
  231. expected := `<p>This link is <a href="feed://example.com/rss.xml" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  232. output := Sanitize("http://example.org/", input)
  233. if expected != output {
  234. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  235. }
  236. input = `<p>This link is <a href="feed:https://example.com/rss.xml">valid</a></p>`
  237. expected = `<p>This link is <a href="feed:https://example.com/rss.xml" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  238. output = Sanitize("http://example.org/", input)
  239. if expected != output {
  240. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  241. }
  242. }
  243. func TestGeoURIScheme(t *testing.T) {
  244. input := `<p>This link is <a href="geo:13.4125,103.8667">valid</a></p>`
  245. expected := `<p>This link is <a href="geo:13.4125,103.8667" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  246. output := Sanitize("http://example.org/", input)
  247. if expected != output {
  248. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  249. }
  250. }
  251. func TestItunesURIScheme(t *testing.T) {
  252. input := `<p>This link is <a href="itms://itunes.com/apps/my-app-name">valid</a></p>`
  253. 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>`
  254. output := Sanitize("http://example.org/", input)
  255. if expected != output {
  256. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  257. }
  258. input = `<p>This link is <a href="itms-apps://itunes.com/apps/my-app-name">valid</a></p>`
  259. 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>`
  260. output = Sanitize("http://example.org/", input)
  261. if expected != output {
  262. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  263. }
  264. }
  265. func TestMagnetURIScheme(t *testing.T) {
  266. input := `<p>This link is <a href="magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&amp;xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7">valid</a></p>`
  267. 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>`
  268. output := Sanitize("http://example.org/", input)
  269. if expected != output {
  270. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  271. }
  272. }
  273. func TestMailtoURIScheme(t *testing.T) {
  274. 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>`
  275. 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>`
  276. output := Sanitize("http://example.org/", input)
  277. if expected != output {
  278. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  279. }
  280. }
  281. func TestNewsURIScheme(t *testing.T) {
  282. input := `<p>This link is <a href="news://news.server.example/*">valid</a></p>`
  283. expected := `<p>This link is <a href="news://news.server.example/*" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  284. output := Sanitize("http://example.org/", input)
  285. if expected != output {
  286. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  287. }
  288. input = `<p>This link is <a href="news:example.group.this">valid</a></p>`
  289. expected = `<p>This link is <a href="news:example.group.this" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  290. output = Sanitize("http://example.org/", input)
  291. if expected != output {
  292. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  293. }
  294. input = `<p>This link is <a href="nntp://news.server.example/example.group.this">valid</a></p>`
  295. 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>`
  296. output = Sanitize("http://example.org/", input)
  297. if expected != output {
  298. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  299. }
  300. }
  301. func TestRTMPURIScheme(t *testing.T) {
  302. input := `<p>This link is <a href="rtmp://mycompany.com/vod/mp4:mycoolvideo.mov">valid</a></p>`
  303. 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>`
  304. output := Sanitize("http://example.org/", input)
  305. if expected != output {
  306. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  307. }
  308. }
  309. func TestSIPURIScheme(t *testing.T) {
  310. input := `<p>This link is <a href="sip:+1-212-555-1212:1234@gateway.com;user=phone">valid</a></p>`
  311. 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>`
  312. output := Sanitize("http://example.org/", input)
  313. if expected != output {
  314. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  315. }
  316. input = `<p>This link is <a href="sips:alice@atlanta.com?subject=project%20x&amp;priority=urgent">valid</a></p>`
  317. 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>`
  318. output = Sanitize("http://example.org/", input)
  319. if expected != output {
  320. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  321. }
  322. }
  323. func TestSkypeURIScheme(t *testing.T) {
  324. input := `<p>This link is <a href="skype:echo123?call">valid</a></p>`
  325. expected := `<p>This link is <a href="skype:echo123?call" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  326. output := Sanitize("http://example.org/", input)
  327. if expected != output {
  328. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  329. }
  330. }
  331. func TestSpotifyURIScheme(t *testing.T) {
  332. input := `<p>This link is <a href="spotify:track:2jCnn1QPQ3E8ExtLe6INsx">valid</a></p>`
  333. expected := `<p>This link is <a href="spotify:track:2jCnn1QPQ3E8ExtLe6INsx" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  334. output := Sanitize("http://example.org/", input)
  335. if expected != output {
  336. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  337. }
  338. }
  339. func TestSteamURIScheme(t *testing.T) {
  340. input := `<p>This link is <a href="steam://settings/account">valid</a></p>`
  341. expected := `<p>This link is <a href="steam://settings/account" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  342. output := Sanitize("http://example.org/", input)
  343. if expected != output {
  344. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  345. }
  346. }
  347. func TestSubversionURIScheme(t *testing.T) {
  348. input := `<p>This link is <a href="svn://example.org">valid</a></p>`
  349. expected := `<p>This link is <a href="svn://example.org" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  350. output := Sanitize("http://example.org/", input)
  351. if expected != output {
  352. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  353. }
  354. input = `<p>This link is <a href="svn+ssh://example.org">valid</a></p>`
  355. expected = `<p>This link is <a href="svn+ssh://example.org" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  356. output = Sanitize("http://example.org/", input)
  357. if expected != output {
  358. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  359. }
  360. }
  361. func TestTelURIScheme(t *testing.T) {
  362. input := `<p>This link is <a href="tel:+1-201-555-0123">valid</a></p>`
  363. expected := `<p>This link is <a href="tel:+1-201-555-0123" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  364. output := Sanitize("http://example.org/", input)
  365. if expected != output {
  366. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  367. }
  368. }
  369. func TestWebcalURIScheme(t *testing.T) {
  370. input := `<p>This link is <a href="webcal://example.com/calendar.ics">valid</a></p>`
  371. expected := `<p>This link is <a href="webcal://example.com/calendar.ics" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">valid</a></p>`
  372. output := Sanitize("http://example.org/", input)
  373. if expected != output {
  374. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  375. }
  376. }
  377. func TestXMPPURIScheme(t *testing.T) {
  378. input := `<p>This link is <a href="xmpp:user@host?subscribe&amp;type=subscribed">valid</a></p>`
  379. 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>`
  380. output := Sanitize("http://example.org/", input)
  381. if expected != output {
  382. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  383. }
  384. }
  385. func TestBlacklistedLink(t *testing.T) {
  386. input := `<p>This image is not valid <img src="https://stats.wordpress.com/some-tracker"></p>`
  387. expected := `<p>This image is not valid </p>`
  388. output := Sanitize("http://example.org/", input)
  389. if expected != output {
  390. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  391. }
  392. }
  393. func TestPixelTracker(t *testing.T) {
  394. input := `<p><img src="https://tracker1.example.org/" height="1" width="1"> and <img src="https://tracker2.example.org/" height="1" width="1"/></p>`
  395. expected := `<p> and </p>`
  396. output := Sanitize("http://example.org/", input)
  397. if expected != output {
  398. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  399. }
  400. }
  401. func TestXmlEntities(t *testing.T) {
  402. input := `<pre>echo "test" &gt; /etc/hosts</pre>`
  403. expected := `<pre>echo &#34;test&#34; &gt; /etc/hosts</pre>`
  404. output := Sanitize("http://example.org/", input)
  405. if expected != output {
  406. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  407. }
  408. }
  409. func TestEspaceAttributes(t *testing.T) {
  410. input := `<td rowspan="<b>test</b>">test</td>`
  411. expected := `<td rowspan="&lt;b&gt;test&lt;/b&gt;">test</td>`
  412. output := Sanitize("http://example.org/", input)
  413. if expected != output {
  414. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  415. }
  416. }
  417. func TestReplaceYoutubeURL(t *testing.T) {
  418. 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>`
  419. 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 allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  420. output := Sanitize("http://example.org/", input)
  421. if expected != output {
  422. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  423. }
  424. }
  425. func TestReplaceSecureYoutubeURL(t *testing.T) {
  426. input := `<iframe src="https://www.youtube.com/embed/test123"></iframe>`
  427. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  428. output := Sanitize("http://example.org/", input)
  429. if expected != output {
  430. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  431. }
  432. }
  433. func TestReplaceSecureYoutubeURLWithParameters(t *testing.T) {
  434. input := `<iframe src="https://www.youtube.com/embed/test123?rel=0&amp;controls=0"></iframe>`
  435. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&amp;controls=0" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  436. output := Sanitize("http://example.org/", input)
  437. if expected != output {
  438. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  439. }
  440. }
  441. func TestReplaceYoutubeURLAlreadyReplaced(t *testing.T) {
  442. input := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&amp;controls=0" sandbox="allow-scripts allow-same-origin"></iframe>`
  443. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&amp;controls=0" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  444. output := Sanitize("http://example.org/", input)
  445. if expected != output {
  446. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  447. }
  448. }
  449. func TestReplaceProtocolRelativeYoutubeURL(t *testing.T) {
  450. input := `<iframe src="//www.youtube.com/embed/Bf2W84jrGqs" width="560" height="314" allowfullscreen="allowfullscreen"></iframe>`
  451. expected := `<iframe src="https://www.youtube-nocookie.com/embed/Bf2W84jrGqs" width="560" height="314" allowfullscreen="allowfullscreen" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  452. output := Sanitize("http://example.org/", input)
  453. if expected != output {
  454. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  455. }
  456. }
  457. func TestReplaceYoutubeURLWithCustomURL(t *testing.T) {
  458. os.Clearenv()
  459. os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://invidious.custom/embed/")
  460. var err error
  461. parser := config.NewParser()
  462. config.Opts, err = parser.ParseEnvironmentVariables()
  463. if err != nil {
  464. t.Fatalf(`Parsing failure: %v`, err)
  465. }
  466. input := `<iframe src="https://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>`
  467. expected := `<iframe src="https://invidious.custom/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 allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  468. output := Sanitize("http://example.org/", input)
  469. if expected != output {
  470. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  471. }
  472. }
  473. func TestReplaceIframeURL(t *testing.T) {
  474. input := `<iframe src="https://player.vimeo.com/video/123456?title=0&amp;byline=0"></iframe>`
  475. expected := `<iframe src="https://player.vimeo.com/video/123456?title=0&amp;byline=0" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  476. output := Sanitize("http://example.org/", input)
  477. if expected != output {
  478. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  479. }
  480. }
  481. func TestReplaceNoScript(t *testing.T) {
  482. 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>`
  483. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  484. output := Sanitize("http://example.org/", input)
  485. if expected != output {
  486. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  487. }
  488. }
  489. func TestReplaceScript(t *testing.T) {
  490. input := `<p>Before paragraph.</p><script type="text/javascript">alert("1");</script><p>After paragraph.</p>`
  491. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  492. output := Sanitize("http://example.org/", input)
  493. if expected != output {
  494. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  495. }
  496. }
  497. func TestReplaceStyle(t *testing.T) {
  498. input := `<p>Before paragraph.</p><style>body { background-color: #ff0000; }</style><p>After paragraph.</p>`
  499. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  500. output := Sanitize("http://example.org/", input)
  501. if expected != output {
  502. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  503. }
  504. }