sanitizer_test.go 41 KB

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