sanitizer_test.go 27 KB

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