sanitizer_test.go 39 KB

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