sanitizer_test.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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 sanitizeHTMLWithDefaultOptions(baseURL, rawHTML string) string {
  12. return SanitizeHTML(baseURL, rawHTML, &SanitizerOptions{
  13. OpenLinksInNewTab: true,
  14. })
  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 b.Loop() {
  29. for _, v := range testCases {
  30. sanitizeHTMLWithDefaultOptions(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 := sanitizeHTMLWithDefaultOptions("", 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 := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  55. if input != output {
  56. t.Errorf(`Wrong output: "%s" != "%s"`, input, output)
  57. }
  58. }
  59. func TestImgSanitization(t *testing.T) {
  60. baseURL := "http://example.org/"
  61. testCases := []struct {
  62. name string
  63. input string
  64. expected string
  65. }{
  66. {
  67. name: "width-and-height-attributes",
  68. input: `<img src="https://example.org/image.png" width="10" height="20">`,
  69. expected: `<img src="https://example.org/image.png" width="10" height="20" loading="lazy">`,
  70. },
  71. {
  72. name: "invalid-width-and-height-attributes",
  73. input: `<img src="https://example.org/image.png" width="10px" height="20px">`,
  74. expected: `<img src="https://example.org/image.png" loading="lazy">`,
  75. },
  76. {
  77. name: "invalid-width-attribute",
  78. input: `<img src="https://example.org/image.png" width="10px" height="20">`,
  79. expected: `<img src="https://example.org/image.png" height="20" loading="lazy">`,
  80. },
  81. {
  82. name: "empty-width-and-height-attributes",
  83. input: `<img src="https://example.org/image.png" width="" height="">`,
  84. expected: `<img src="https://example.org/image.png" loading="lazy">`,
  85. },
  86. {
  87. name: "invalid-height-attribute",
  88. input: `<img src="https://example.org/image.png" width="10" height="20px">`,
  89. expected: `<img src="https://example.org/image.png" width="10" loading="lazy">`,
  90. },
  91. {
  92. name: "negative-width-attribute",
  93. input: `<img src="https://example.org/image.png" width="-10" height="20">`,
  94. expected: `<img src="https://example.org/image.png" height="20" loading="lazy">`,
  95. },
  96. {
  97. name: "negative-height-attribute",
  98. input: `<img src="https://example.org/image.png" width="10" height="-20">`,
  99. expected: `<img src="https://example.org/image.png" width="10" loading="lazy">`,
  100. },
  101. {
  102. name: "text-data-url",
  103. input: `<img src="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" alt="Example">`,
  104. expected: ``,
  105. },
  106. {
  107. name: "image-data-url",
  108. input: `<img src="data:image/gif;base64,test" alt="Example">`,
  109. expected: `<img src="data:image/gif;base64,test" alt="Example" loading="lazy">`,
  110. },
  111. {
  112. name: "srcset-attribute",
  113. 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">`,
  114. 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">`,
  115. },
  116. {
  117. name: "srcset-attribute-without-src",
  118. input: `<img srcset="example-320w.jpg, example-480w.jpg 1.5x, example-640w.jpg 2x, example-640w.jpg 640w" alt="Example">`,
  119. 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" alt="Example" loading="lazy">`,
  120. },
  121. {
  122. name: "fetchpriority-high",
  123. input: `<img src="https://example.org/image.png" fetchpriority="high">`,
  124. expected: `<img src="https://example.org/image.png" fetchpriority="high" loading="lazy">`,
  125. },
  126. {
  127. name: "fetchpriority-low",
  128. input: `<img src="https://example.org/image.png" fetchpriority="low">`,
  129. expected: `<img src="https://example.org/image.png" fetchpriority="low" loading="lazy">`,
  130. },
  131. {
  132. name: "fetchpriority-auto",
  133. input: `<img src="https://example.org/image.png" fetchpriority="auto">`,
  134. expected: `<img src="https://example.org/image.png" fetchpriority="auto" loading="lazy">`,
  135. },
  136. {
  137. name: "fetchpriority-invalid",
  138. input: `<img src="https://example.org/image.png" fetchpriority="invalid">`,
  139. expected: `<img src="https://example.org/image.png" loading="lazy">`,
  140. },
  141. {
  142. name: "decoding-sync",
  143. input: `<img src="https://example.org/image.png" decoding="sync">`,
  144. expected: `<img src="https://example.org/image.png" decoding="sync" loading="lazy">`,
  145. },
  146. {
  147. name: "decoding-async",
  148. input: `<img src="https://example.org/image.png" decoding="async">`,
  149. expected: `<img src="https://example.org/image.png" decoding="async" loading="lazy">`,
  150. },
  151. {
  152. name: "decoding-auto",
  153. input: `<img src="https://example.org/image.png" decoding="auto">`,
  154. expected: `<img src="https://example.org/image.png" decoding="auto" loading="lazy">`,
  155. },
  156. {
  157. name: "decoding-invalid",
  158. input: `<img src="https://example.org/image.png" decoding="invalid">`,
  159. expected: `<img src="https://example.org/image.png" loading="lazy">`,
  160. },
  161. }
  162. for _, tc := range testCases {
  163. t.Run(tc.name, func(t *testing.T) {
  164. output := sanitizeHTMLWithDefaultOptions(baseURL, tc.input)
  165. if output != tc.expected {
  166. t.Errorf(`Wrong output for input %q: expected %q, got %q`, tc.input, tc.expected, output)
  167. }
  168. })
  169. }
  170. }
  171. func TestNonImgWithFetchPriorityAttribute(t *testing.T) {
  172. input := `<p fetchpriority="high">Text</p>`
  173. expected := `<p>Text</p>`
  174. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  175. if output != expected {
  176. t.Errorf(`Wrong output: expected %q, got %q`, expected, output)
  177. }
  178. }
  179. func TestNonImgWithDecodingAttribute(t *testing.T) {
  180. input := `<p decoding="async">Text</p>`
  181. expected := `<p>Text</p>`
  182. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  183. if output != expected {
  184. t.Errorf(`Wrong output: expected %q, got %q`, expected, output)
  185. }
  186. }
  187. func TestSourceWithSrcsetAndMedia(t *testing.T) {
  188. input := `<picture><source media="(min-width: 800px)" srcset="elva-800w.jpg"></picture>`
  189. expected := `<picture><source media="(min-width: 800px)" srcset="http://example.org/elva-800w.jpg"></picture>`
  190. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  191. if output != expected {
  192. t.Errorf(`Wrong output: %s`, output)
  193. }
  194. }
  195. func TestMediumImgWithSrcset(t *testing.T) {
  196. 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">`
  197. 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" width="2730" height="3407" loading="lazy">`
  198. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  199. if output != expected {
  200. t.Errorf(`Wrong output: %s`, output)
  201. }
  202. }
  203. func TestSelfClosingTags(t *testing.T) {
  204. input := `<p>This <br> is a <strong>text</strong> <br>with an image: <img src="http://example.org/" alt="Test" loading="lazy">.</p>`
  205. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  206. if input != output {
  207. t.Errorf(`Wrong output: "%s" != "%s"`, input, output)
  208. }
  209. }
  210. func TestTable(t *testing.T) {
  211. input := `<table><tr><th>A</th><th colspan="2">B</th></tr><tr><td>C</td><td>D</td><td>E</td></tr></table>`
  212. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  213. if input != output {
  214. t.Errorf(`Wrong output: "%s" != "%s"`, input, output)
  215. }
  216. }
  217. func TestRelativeURL(t *testing.T) {
  218. input := `This <a href="/test.html">link is relative</a> and this image: <img src="../folder/image.png">`
  219. expected := `This <a href="http://example.org/test.html" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">link is relative</a> and this image: <img src="http://example.org/folder/image.png" loading="lazy">`
  220. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  221. if expected != output {
  222. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  223. }
  224. }
  225. func TestProtocolRelativeURL(t *testing.T) {
  226. input := `This <a href="//static.example.org/index.html">link is relative</a>.`
  227. expected := `This <a href="https://static.example.org/index.html" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">link is relative</a>.`
  228. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  229. if expected != output {
  230. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  231. }
  232. }
  233. func TestInvalidTag(t *testing.T) {
  234. input := `<p>My invalid <z>tag</z>.</p>`
  235. expected := `<p>My invalid tag.</p>`
  236. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  237. if expected != output {
  238. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  239. }
  240. }
  241. func TestVideoTag(t *testing.T) {
  242. input := `<p>My valid <video src="videofile.webm" autoplay poster="posterimage.jpg">fallback</video>.</p>`
  243. expected := `<p>My valid <video src="http://example.org/videofile.webm" poster="http://example.org/posterimage.jpg" controls>fallback</video>.</p>`
  244. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  245. if expected != output {
  246. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  247. }
  248. }
  249. func TestAudioAndSourceTag(t *testing.T) {
  250. input := `<p>My music <audio controls="controls"><source src="foo.wav" type="audio/wav"></audio>.</p>`
  251. expected := `<p>My music <audio controls><source src="http://example.org/foo.wav" type="audio/wav"></audio>.</p>`
  252. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  253. if expected != output {
  254. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  255. }
  256. }
  257. func TestUnknownTag(t *testing.T) {
  258. input := `<p>My invalid <unknown>tag</unknown>.</p>`
  259. expected := `<p>My invalid tag.</p>`
  260. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  261. if expected != output {
  262. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  263. }
  264. }
  265. func TestInvalidNestedTag(t *testing.T) {
  266. input := `<p>My invalid <z>tag with some <em>valid</em> tag</z>.</p>`
  267. expected := `<p>My invalid tag with some <em>valid</em> tag.</p>`
  268. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  269. if expected != output {
  270. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  271. }
  272. }
  273. func TestInvalidIFrame(t *testing.T) {
  274. config.Opts = config.NewConfigOptions()
  275. input := `<iframe src="http://example.org/"></iframe>`
  276. expected := ``
  277. output := sanitizeHTMLWithDefaultOptions("http://example.com/", input)
  278. if expected != output {
  279. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  280. }
  281. }
  282. func TestSameDomainIFrame(t *testing.T) {
  283. config.Opts = config.NewConfigOptions()
  284. input := `<iframe src="http://example.com/test"></iframe>`
  285. expected := ``
  286. output := sanitizeHTMLWithDefaultOptions("http://example.com/", input)
  287. if expected != output {
  288. t.Errorf(`Wrong output: %q != %q`, expected, output)
  289. }
  290. }
  291. func TestInvidiousIFrame(t *testing.T) {
  292. config.Opts = config.NewConfigOptions()
  293. input := `<iframe src="https://yewtu.be/watch?v=video_id"></iframe>`
  294. expected := `<iframe src="https://yewtu.be/watch?v=video_id" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  295. output := sanitizeHTMLWithDefaultOptions("http://example.com/", input)
  296. if expected != output {
  297. t.Errorf(`Wrong output: %q != %q`, expected, output)
  298. }
  299. }
  300. func TestCustomYoutubeEmbedURL(t *testing.T) {
  301. os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://www.invidious.custom/embed/")
  302. defer os.Clearenv()
  303. var err error
  304. if config.Opts, err = config.NewConfigParser().ParseEnvironmentVariables(); err != nil {
  305. t.Fatalf(`Parsing failure: %v`, err)
  306. }
  307. input := `<iframe src="https://www.invidious.custom/embed/1234"></iframe>`
  308. expected := `<iframe src="https://www.invidious.custom/embed/1234" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  309. output := sanitizeHTMLWithDefaultOptions("http://example.com/", input)
  310. if expected != output {
  311. t.Errorf(`Wrong output: %q != %q`, expected, output)
  312. }
  313. }
  314. func TestIFrameWithChildElements(t *testing.T) {
  315. config.Opts = config.NewConfigOptions()
  316. input := `<iframe src="https://www.youtube.com/"><p>test</p></iframe>`
  317. expected := `<iframe src="https://www.youtube.com/" referrerpolicy="strict-origin-when-cross-origin" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  318. output := sanitizeHTMLWithDefaultOptions("http://example.com/", input)
  319. if expected != output {
  320. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  321. }
  322. }
  323. func TestIFrameWithReferrerPolicy(t *testing.T) {
  324. config.Opts = config.NewConfigOptions()
  325. input := `<iframe src="https://www.youtube.com/embed/test123" referrerpolicy="strict-origin-when-cross-origin"></iframe>`
  326. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123" referrerpolicy="strict-origin-when-cross-origin" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  327. output := sanitizeHTMLWithDefaultOptions("http://example.com/", input)
  328. if expected != output {
  329. t.Errorf(`Wrong output: %q != %q`, expected, output)
  330. }
  331. }
  332. func TestLinkWithTarget(t *testing.T) {
  333. input := `<p>This link is <a href="http://example.org/index.html">an anchor</a></p>`
  334. expected := `<p>This link is <a href="http://example.org/index.html" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">an anchor</a></p>`
  335. output := SanitizeHTML("http://example.org/", input, &SanitizerOptions{OpenLinksInNewTab: true})
  336. if expected != output {
  337. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  338. }
  339. }
  340. func TestLinkWithNoTarget(t *testing.T) {
  341. input := `<p>This link is <a href="http://example.org/index.html">an anchor</a></p>`
  342. expected := `<p>This link is <a href="http://example.org/index.html" rel="noopener noreferrer" referrerpolicy="no-referrer">an anchor</a></p>`
  343. output := SanitizeHTML("http://example.org/", input, &SanitizerOptions{OpenLinksInNewTab: false})
  344. if expected != output {
  345. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  346. }
  347. }
  348. func TestAnchorLink(t *testing.T) {
  349. input := `<p>This link is <a href="#some-anchor">an anchor</a></p>`
  350. expected := `<p>This link is <a href="#some-anchor">an anchor</a></p>`
  351. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  352. if expected != output {
  353. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  354. }
  355. }
  356. func TestInvalidURLScheme(t *testing.T) {
  357. input := `<p>This link is <a src="file:///etc/passwd">not valid</a></p>`
  358. expected := `<p>This link is not valid</p>`
  359. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  360. if expected != output {
  361. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  362. }
  363. }
  364. func TestURISchemes(t *testing.T) {
  365. baseURL := "http://example.org/"
  366. testCases := []struct {
  367. name string
  368. input string
  369. expected string
  370. }{
  371. {
  372. name: "apt",
  373. input: `<p>This link is <a href="apt:some-package?channel=test">valid</a></p>`,
  374. expected: `<p>This link is <a href="apt:some-package?channel=test" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  375. },
  376. {
  377. name: "bitcoin",
  378. input: `<p>This link is <a href="bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W">valid</a></p>`,
  379. expected: `<p>This link is <a href="bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  380. },
  381. {
  382. name: "callto",
  383. input: `<p>This link is <a href="callto:12345679">valid</a></p>`,
  384. expected: `<p>This link is <a href="callto:12345679" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  385. },
  386. {
  387. name: "feed-double-slash",
  388. input: `<p>This link is <a href="feed://example.com/rss.xml">valid</a></p>`,
  389. expected: `<p>This link is <a href="feed://example.com/rss.xml" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  390. },
  391. {
  392. name: "feed-https",
  393. input: `<p>This link is <a href="feed:https://example.com/rss.xml">valid</a></p>`,
  394. expected: `<p>This link is <a href="feed:https://example.com/rss.xml" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  395. },
  396. {
  397. name: "geo",
  398. input: `<p>This link is <a href="geo:13.4125,103.8667">valid</a></p>`,
  399. expected: `<p>This link is <a href="geo:13.4125,103.8667" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  400. },
  401. {
  402. name: "itms",
  403. input: `<p>This link is <a href="itms://itunes.com/apps/my-app-name">valid</a></p>`,
  404. expected: `<p>This link is <a href="itms://itunes.com/apps/my-app-name" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  405. },
  406. {
  407. name: "itms-apps",
  408. input: `<p>This link is <a href="itms-apps://itunes.com/apps/my-app-name">valid</a></p>`,
  409. expected: `<p>This link is <a href="itms-apps://itunes.com/apps/my-app-name" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  410. },
  411. {
  412. name: "magnet",
  413. input: `<p>This link is <a href="magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&amp;xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7">valid</a></p>`,
  414. expected: `<p>This link is <a href="magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&amp;xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  415. },
  416. {
  417. name: "mailto",
  418. 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>`,
  419. 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" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  420. },
  421. {
  422. name: "news-double-slash",
  423. input: `<p>This link is <a href="news://news.server.example/*">valid</a></p>`,
  424. expected: `<p>This link is <a href="news://news.server.example/*" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  425. },
  426. {
  427. name: "news-single-colon",
  428. input: `<p>This link is <a href="news:example.group.this">valid</a></p>`,
  429. expected: `<p>This link is <a href="news:example.group.this" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  430. },
  431. {
  432. name: "nntp",
  433. input: `<p>This link is <a href="nntp://news.server.example/example.group.this">valid</a></p>`,
  434. expected: `<p>This link is <a href="nntp://news.server.example/example.group.this" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  435. },
  436. {
  437. name: "rtmp",
  438. input: `<p>This link is <a href="rtmp://mycompany.com/vod/mp4:mycoolvideo.mov">valid</a></p>`,
  439. expected: `<p>This link is <a href="rtmp://mycompany.com/vod/mp4:mycoolvideo.mov" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  440. },
  441. {
  442. name: "sip",
  443. input: `<p>This link is <a href="sip:+1-212-555-1212:1234@gateway.com;user=phone">valid</a></p>`,
  444. expected: `<p>This link is <a href="sip:+1-212-555-1212:1234@gateway.com;user=phone" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  445. },
  446. {
  447. name: "sips",
  448. input: `<p>This link is <a href="sips:alice@atlanta.com?subject=project%20x&amp;priority=urgent">valid</a></p>`,
  449. expected: `<p>This link is <a href="sips:alice@atlanta.com?subject=project%20x&amp;priority=urgent" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  450. },
  451. {
  452. name: "skype",
  453. input: `<p>This link is <a href="skype:echo123?call">valid</a></p>`,
  454. expected: `<p>This link is <a href="skype:echo123?call" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  455. },
  456. {
  457. name: "spotify",
  458. input: `<p>This link is <a href="spotify:track:2jCnn1QPQ3E8ExtLe6INsx">valid</a></p>`,
  459. expected: `<p>This link is <a href="spotify:track:2jCnn1QPQ3E8ExtLe6INsx" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  460. },
  461. {
  462. name: "steam",
  463. input: `<p>This link is <a href="steam://settings/account">valid</a></p>`,
  464. expected: `<p>This link is <a href="steam://settings/account" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  465. },
  466. {
  467. name: "svn",
  468. input: `<p>This link is <a href="svn://example.org">valid</a></p>`,
  469. expected: `<p>This link is <a href="svn://example.org" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  470. },
  471. {
  472. name: "svn-ssh",
  473. input: `<p>This link is <a href="svn+ssh://example.org">valid</a></p>`,
  474. expected: `<p>This link is <a href="svn+ssh://example.org" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  475. },
  476. {
  477. name: "tel",
  478. input: `<p>This link is <a href="tel:+1-201-555-0123">valid</a></p>`,
  479. expected: `<p>This link is <a href="tel:+1-201-555-0123" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  480. },
  481. {
  482. name: "webcal",
  483. input: `<p>This link is <a href="webcal://example.com/calendar.ics">valid</a></p>`,
  484. expected: `<p>This link is <a href="webcal://example.com/calendar.ics" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  485. },
  486. {
  487. name: "xmpp",
  488. input: `<p>This link is <a href="xmpp:user@host?subscribe&amp;type=subscribed">valid</a></p>`,
  489. expected: `<p>This link is <a href="xmpp:user@host?subscribe&amp;type=subscribed" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">valid</a></p>`,
  490. },
  491. }
  492. for _, tc := range testCases {
  493. t.Run(tc.name, func(t *testing.T) {
  494. output := sanitizeHTMLWithDefaultOptions(baseURL, tc.input)
  495. if tc.expected != output {
  496. t.Errorf(`Wrong output for input %q: expected %q, got %q`, tc.input, tc.expected, output)
  497. }
  498. })
  499. }
  500. }
  501. func TestBlacklistedLink(t *testing.T) {
  502. input := `<p>This image is not valid <img src="https://stats.wordpress.com/some-tracker"></p>`
  503. expected := `<p>This image is not valid </p>`
  504. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  505. if expected != output {
  506. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  507. }
  508. }
  509. func TestLinkWithTrackers(t *testing.T) {
  510. input := `<p>This link has trackers <a href="https://example.com/page?utm_source=newsletter">Test</a></p>`
  511. expected := `<p>This link has trackers <a href="https://example.com/page" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">Test</a></p>`
  512. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  513. if expected != output {
  514. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  515. }
  516. }
  517. func TestImageSrcWithTrackers(t *testing.T) {
  518. input := `<p>This image has trackers <img src="https://example.org/?id=123&utm_source=newsletter&utm_medium=email&fbclid=abc123"></p>`
  519. expected := `<p>This image has trackers <img src="https://example.org/?id=123" loading="lazy"></p>`
  520. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  521. if expected != output {
  522. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  523. }
  524. }
  525. func Test1x1PixelTracker(t *testing.T) {
  526. input := `<p><img src="https://tracker1.example.org/" height="1" width="1"> and <img src="https://tracker2.example.org/" height="1" width="1"/></p>`
  527. expected := `<p> and </p>`
  528. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  529. if expected != output {
  530. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  531. }
  532. }
  533. func Test0x0PixelTracker(t *testing.T) {
  534. input := `<p><img src="https://tracker1.example.org/" height="0" width="0"> and <img src="https://tracker2.example.org/" height="0" width="0"/></p>`
  535. expected := `<p> and </p>`
  536. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  537. if expected != output {
  538. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  539. }
  540. }
  541. func TestXmlEntities(t *testing.T) {
  542. input := `<pre>echo "test" &gt; /etc/hosts</pre>`
  543. expected := `<pre>echo &#34;test&#34; &gt; /etc/hosts</pre>`
  544. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  545. if expected != output {
  546. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  547. }
  548. }
  549. func TestEspaceAttributes(t *testing.T) {
  550. input := `<td rowspan="<b>injection</b>">text</td>`
  551. expected := `text`
  552. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  553. if expected != output {
  554. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  555. }
  556. }
  557. func TestReplaceYoutubeURL(t *testing.T) {
  558. os.Clearenv()
  559. var err error
  560. config.Opts, err = config.NewConfigParser().ParseEnvironmentVariables()
  561. if err != nil {
  562. t.Fatalf(`Parsing failure: %v`, err)
  563. }
  564. 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>`
  565. 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" referrerpolicy="strict-origin-when-cross-origin" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  566. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  567. if expected != output {
  568. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  569. }
  570. }
  571. func TestReplaceSecureYoutubeURL(t *testing.T) {
  572. os.Clearenv()
  573. var err error
  574. config.Opts, err = config.NewConfigParser().ParseEnvironmentVariables()
  575. if err != nil {
  576. t.Fatalf(`Parsing failure: %v`, err)
  577. }
  578. input := `<iframe src="https://www.youtube.com/embed/test123"></iframe>`
  579. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123" referrerpolicy="strict-origin-when-cross-origin" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  580. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  581. if expected != output {
  582. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  583. }
  584. }
  585. func TestReplaceSecureYoutubeURLWithParameters(t *testing.T) {
  586. os.Clearenv()
  587. var err error
  588. config.Opts, err = config.NewConfigParser().ParseEnvironmentVariables()
  589. if err != nil {
  590. t.Fatalf(`Parsing failure: %v`, err)
  591. }
  592. input := `<iframe src="https://www.youtube.com/embed/test123?rel=0&amp;controls=0"></iframe>`
  593. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&amp;controls=0" referrerpolicy="strict-origin-when-cross-origin" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  594. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  595. if expected != output {
  596. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  597. }
  598. }
  599. func TestReplaceYoutubeURLAlreadyReplaced(t *testing.T) {
  600. os.Clearenv()
  601. var err error
  602. config.Opts, err = config.NewConfigParser().ParseEnvironmentVariables()
  603. if err != nil {
  604. t.Fatalf(`Parsing failure: %v`, err)
  605. }
  606. input := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&amp;controls=0" sandbox="allow-scripts allow-same-origin"></iframe>`
  607. expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&amp;controls=0" referrerpolicy="strict-origin-when-cross-origin" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  608. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  609. if expected != output {
  610. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  611. }
  612. }
  613. func TestReplaceProtocolRelativeYoutubeURL(t *testing.T) {
  614. os.Clearenv()
  615. var err error
  616. config.Opts, err = config.NewConfigParser().ParseEnvironmentVariables()
  617. if err != nil {
  618. t.Fatalf(`Parsing failure: %v`, err)
  619. }
  620. input := `<iframe src="//www.youtube.com/embed/Bf2W84jrGqs" width="560" height="314" allowfullscreen="allowfullscreen"></iframe>`
  621. expected := `<iframe src="https://www.youtube-nocookie.com/embed/Bf2W84jrGqs" width="560" height="314" allowfullscreen="allowfullscreen" referrerpolicy="strict-origin-when-cross-origin" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  622. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  623. if expected != output {
  624. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  625. }
  626. }
  627. func TestReplaceYoutubeURLWithCustomURL(t *testing.T) {
  628. defer os.Clearenv()
  629. os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://invidious.custom/embed/")
  630. var err error
  631. config.Opts, err = config.NewConfigParser().ParseEnvironmentVariables()
  632. if err != nil {
  633. t.Fatalf(`Parsing failure: %v`, err)
  634. }
  635. 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>`
  636. 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" referrerpolicy="strict-origin-when-cross-origin" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  637. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  638. if expected != output {
  639. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  640. }
  641. }
  642. func TestVimeoIframeRewriteWithQueryString(t *testing.T) {
  643. input := `<iframe src="https://player.vimeo.com/video/123456?title=0&amp;byline=0"></iframe>`
  644. 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>`
  645. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  646. if expected != output {
  647. t.Errorf(`Wrong output: %q != %q`, expected, output)
  648. }
  649. }
  650. func TestVimeoIframeRewriteWithoutQueryString(t *testing.T) {
  651. input := `<iframe src="https://player.vimeo.com/video/123456"></iframe>`
  652. expected := `<iframe src="https://player.vimeo.com/video/123456?dnt=1" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" loading="lazy"></iframe>`
  653. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  654. if expected != output {
  655. t.Errorf(`Wrong output: %q != %q`, expected, output)
  656. }
  657. }
  658. func TestReplaceNoScript(t *testing.T) {
  659. 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>`
  660. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  661. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  662. if expected != output {
  663. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  664. }
  665. }
  666. func TestReplaceScript(t *testing.T) {
  667. input := `<p>Before paragraph.</p><script type="text/javascript">alert("1");</script><p>After paragraph.</p>`
  668. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  669. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  670. if expected != output {
  671. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  672. }
  673. }
  674. func TestReplaceStyle(t *testing.T) {
  675. input := `<p>Before paragraph.</p><style>body { background-color: #ff0000; }</style><p>After paragraph.</p>`
  676. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  677. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  678. if expected != output {
  679. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  680. }
  681. }
  682. func TestHiddenParagraph(t *testing.T) {
  683. input := `<p>Before paragraph.</p><p hidden>This should <em>not</em> appear in the <strong>output</strong></p><p>After paragraph.</p>`
  684. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  685. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  686. if expected != output {
  687. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  688. }
  689. }
  690. func TestAttributesAreStripped(t *testing.T) {
  691. input := `<p style="color: red;">Some text.<hr style="color: blue"/>Test.</p>`
  692. expected := `<p>Some text.</p><hr>Test.<p></p>`
  693. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  694. if expected != output {
  695. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  696. }
  697. }
  698. func TestMathML(t *testing.T) {
  699. input := `<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>x</mi><mn>2</mn></msup></math>`
  700. expected := `<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>x</mi><mn>2</mn></msup></math>`
  701. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  702. if expected != output {
  703. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  704. }
  705. }
  706. func TestInvalidMathMLXMLNamespace(t *testing.T) {
  707. input := `<math xmlns="http://example.org"><msup><mi>x</mi><mn>2</mn></msup></math>`
  708. expected := `<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>x</mi><mn>2</mn></msup></math>`
  709. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  710. if expected != output {
  711. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  712. }
  713. }
  714. func TestBlockedResourcesSubstrings(t *testing.T) {
  715. input := `<p>Before paragraph.</p><img src="http://stats.wordpress.com/something.php" alt="Blocked Resource"><p>After paragraph.</p>`
  716. expected := `<p>Before paragraph.</p><p>After paragraph.</p>`
  717. output := sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  718. if expected != output {
  719. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  720. }
  721. input = `<p>Before paragraph.</p><img src="http://twitter.com/share?text=This+is+google+a+search+engine&url=https%3A%2F%2Fwww.google.com" alt="Blocked Resource"><p>After paragraph.</p>`
  722. expected = `<p>Before paragraph.</p><p>After paragraph.</p>`
  723. output = sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  724. if expected != output {
  725. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  726. }
  727. input = `<p>Before paragraph.</p><img src="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.google.com%[title]=This+Is%2C+Google+a+search+engine" alt="Blocked Resource"><p>After paragraph.</p>`
  728. expected = `<p>Before paragraph.</p><p>After paragraph.</p>`
  729. output = sanitizeHTMLWithDefaultOptions("http://example.org/", input)
  730. if expected != output {
  731. t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
  732. }
  733. }
  734. func TestAttrLowerCase(t *testing.T) {
  735. baseURL := "http://example.org/"
  736. testCases := []struct {
  737. name string
  738. input string
  739. expected string
  740. }{
  741. {
  742. name: "href-and-hidden-mixed-case",
  743. input: `<a HrEF="http://example.com" HIddEN>test</a>`,
  744. expected: ``,
  745. },
  746. {
  747. name: "href-mixed-case",
  748. input: `<a HrEF="http://example.com">test</a>`,
  749. expected: `<a href="http://example.com" rel="noopener noreferrer" referrerpolicy="no-referrer" target="_blank">test</a>`,
  750. },
  751. }
  752. for _, tc := range testCases {
  753. tc := tc
  754. t.Run(tc.name, func(t *testing.T) {
  755. output := sanitizeHTMLWithDefaultOptions(baseURL, tc.input)
  756. if tc.expected != output {
  757. t.Errorf(`Wrong output for input %q: expected %q, got %q`, tc.input, tc.expected, output)
  758. }
  759. })
  760. }
  761. }