sanitizer_test.go 42 KB

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