| // Copyright 2026 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| suite('ContentProcessing.sanitizeLinks', function() { | |
| let testContainer; | |
| setup(function() { | |
| testContainer = document.createElement('div'); | |
| document.body.appendChild(testContainer); | |
| }); | |
| teardown(function() { | |
| document.body.removeChild(testContainer); | |
| testContainer = null; | |
| }); | |
| test( | |
| 'sanitizeLinks should keep valid http/https links and open in new tab', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<a href="http://example.com">HTTP Link</a>' + | |
| '<a href="https://example.com">HTTPS Link</a>'; | |
| sanitizeLinks(testContainer); | |
| const links = testContainer.querySelectorAll('a'); | |
| assert.equal(links.length, 2); | |
| assert.equal(links[0].getAttribute('href'), 'http://example.com'); | |
| assert.equal(links[0].target, '_blank'); | |
| assert.equal(links[1].getAttribute('href'), 'https://example.com'); | |
| assert.equal(links[1].target, '_blank'); | |
| }); | |
| test('sanitizeLinks should remove javascript links', async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<a href="javascript:void(0)">JS Link</a>'; | |
| sanitizeLinks(testContainer); | |
| const links = testContainer.querySelectorAll('a'); | |
| assert.equal(links.length, 0); | |
| assert.equal(testContainer.innerHTML, 'JS Link'); | |
| }); | |
| test( | |
| 'sanitizeLinks should keep #in-page links and not open in new tab', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<a href="#anchor">Anchor Link</a>'; | |
| sanitizeLinks(testContainer); | |
| const links = testContainer.querySelectorAll('a'); | |
| assert.equal(links.length, 1); | |
| assert.equal(links[0].getAttribute('href'), '#anchor'); | |
| // Should NOT be _blank. | |
| assert.notEqual(links[0].target, '_blank'); | |
| }); | |
| test( | |
| 'sanitizeLinks should keep #in-page links with path-like characters', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<a href="#foo/bar">Hash Path Link</a>'; | |
| sanitizeLinks(testContainer); | |
| const links = testContainer.querySelectorAll('a'); | |
| assert.equal(links.length, 1); | |
| assert.equal(links[0].getAttribute('href'), '#foo/bar'); | |
| assert.notEqual(links[0].target, '_blank'); | |
| }); | |
| test('sanitizeLinks should remove empty hash links', async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<a href="#">Empty Hash</a>'; | |
| sanitizeLinks(testContainer); | |
| const links = testContainer.querySelectorAll('a'); | |
| assert.equal(links.length, 0); | |
| assert.equal(testContainer.innerHTML, 'Empty Hash'); | |
| }); | |
| test('sanitizeLinks should remove mailto links', async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = | |
| '<a href="mailto:user@example.com">Mailto Link</a>'; | |
| sanitizeLinks(testContainer); | |
| const links = testContainer.querySelectorAll('a'); | |
| assert.equal(links.length, 0); | |
| assert.equal(testContainer.innerHTML, 'Mailto Link'); | |
| }); | |
| test( | |
| 'sanitizeLinks should remove javascript SVG links with xlink:href', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<svg><a xlink:href="javascript:void(0)">' + | |
| '<text>SVG JS Link</text></a></svg>'; | |
| sanitizeLinks(testContainer); | |
| const links = testContainer.querySelectorAll('a'); | |
| assert.equal(links.length, 0); | |
| assert.equal( | |
| testContainer.querySelector('svg').innerHTML, | |
| '<text>SVG JS Link</text>'); | |
| }); | |
| test( | |
| 'sanitizeLinks should keep valid http/https SVG links with xlink:href ' + | |
| 'and open in new tab', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<svg><a xlink:href="https://example.com">' + | |
| '<text>SVG HTTPS Link</text></a></svg>'; | |
| sanitizeLinks(testContainer); | |
| const links = testContainer.querySelectorAll('a'); | |
| assert.equal(links.length, 1); | |
| assert.equal( | |
| links[0].getAttributeNS('http://www.w3.org/1999/xlink', 'href'), | |
| 'https://example.com'); | |
| assert.equal(links[0].getAttribute('target'), '_blank'); | |
| assert.equal(links[0].target.baseVal, '_blank'); | |
| assert.equal(links[0].target.animVal, '_blank'); | |
| }); | |
| test( | |
| 'sanitizeLinks should strip inline SVG SMIL animations targeting href', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = | |
| '<svg xmlns:xlink="http://www.w3.org/1999/xlink">' + | |
| '<a id="a-href" href="https://example.com/">' + | |
| '<set attributeName="href" to="chrome-distiller://forged" ' + | |
| 'begin="0s"/>' + | |
| '<animateMotion path="M0,0 L10,10" begin="0s" dur="10s"/>' + | |
| '<animateTransform attributeName="transform" type="scale" ' + | |
| 'from="1" to="2" begin="0s" dur="10s"/>' + | |
| '<animateColor attributeName="fill" to="red" begin="0s"/>' + | |
| '<text>SVG Link</text>' + | |
| '</a>' + | |
| '<a id="a-xlink" xlink:href="https://example.com/">' + | |
| '<animate attributeName="xlink:href" ' + | |
| 'values="javascript:alert(1)" begin="0s" dur="10s"/>' + | |
| '<text>XLink SVG Link</text>' + | |
| '</a></svg>'; | |
| const svg = testContainer.querySelector('svg'); | |
| const hrefLink = svg.querySelector('#a-href'); | |
| const xlinkLink = svg.querySelector('#a-xlink'); | |
| // Confirm SMIL actively overrides animVal prior to sanitization. | |
| svg.setCurrentTime(1); | |
| assert.equal(hrefLink.href.animVal, 'chrome-distiller://forged'); | |
| assert.equal(xlinkLink.href.animVal, 'javascript:alert(1)'); | |
| sanitizeLinks(testContainer); | |
| // Clearing must take effect synchronously before any future timeline | |
| // tick as well as after advancing the SVG clock. | |
| assert.equal(hrefLink.href.animVal, 'https://example.com/'); | |
| assert.equal(xlinkLink.href.animVal, 'https://example.com/'); | |
| svg.setCurrentTime(2); | |
| assert.deepEqual( | |
| Array.from(svg.querySelectorAll('*')).map(el => el.localName), | |
| ['a', 'text', 'a', 'text']); | |
| assert.equal(hrefLink.href.baseVal, 'https://example.com/'); | |
| assert.equal(hrefLink.href.animVal, 'https://example.com/'); | |
| assert.equal(hrefLink.target.animVal, '_blank'); | |
| assert.equal(xlinkLink.href.animVal, 'https://example.com/'); | |
| assert.equal(xlinkLink.target.animVal, '_blank'); | |
| }); | |
| test( | |
| 'sanitizeLinks should strip external SVG SMIL animations targeting ' + | |
| 'anchor by id', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = | |
| '<svg id="svg1" xmlns:xlink="http://www.w3.org/1999/xlink">' + | |
| '<a id="svg-link" href="https://example.com/">' + | |
| '<text>SVG Link</text></a>' + | |
| '<set href="#svg-link" attributeName="href" ' + | |
| 'to="chrome-distiller://forged" begin="0s"/>' + | |
| '</svg>' + | |
| '<svg id="svg2" xmlns:xlink="http://www.w3.org/1999/xlink">' + | |
| '<animate xlink:href="#svg-link" attributeName="target" ' + | |
| 'values="_self" begin="0s" dur="10s"/>' + | |
| '</svg>'; | |
| const svg1 = testContainer.querySelector('#svg1'); | |
| const svg2 = testContainer.querySelector('#svg2'); | |
| const link = testContainer.querySelector('#svg-link'); | |
| // Confirm external SMIL actively mutates href and target before | |
| // sanitization. | |
| svg1.setCurrentTime(1); | |
| svg2.setCurrentTime(1); | |
| assert.equal(link.href.animVal, 'chrome-distiller://forged'); | |
| assert.equal(link.target.animVal, '_self'); | |
| sanitizeLinks(testContainer); | |
| assert.equal(link.href.animVal, 'https://example.com/'); | |
| assert.equal(link.target.animVal, '_blank'); | |
| svg1.setCurrentTime(2); | |
| svg2.setCurrentTime(2); | |
| assert.deepEqual( | |
| Array.from(svg1.querySelectorAll('*')).map(el => el.localName), | |
| ['a', 'text']); | |
| assert.deepEqual( | |
| Array.from(svg2.querySelectorAll('*')).map(el => el.localName), []); | |
| assert.equal(link.href.animVal, 'https://example.com/'); | |
| assert.equal(link.target.animVal, '_blank'); | |
| }); | |
| test( | |
| 'sanitizeLinks should unwrap bare SVG anchors targeted by SMIL', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<svg>' + | |
| '<a id="bare-svg-link"><text>Bare SVG Link</text></a>' + | |
| '<set href="#bare-svg-link" attributeName="href" ' + | |
| 'to="chrome-distiller://forged" begin="0s"/>' + | |
| '</svg>'; | |
| const svg = testContainer.querySelector('svg'); | |
| svg.setCurrentTime(1); | |
| assert.equal( | |
| svg.querySelector('#bare-svg-link').href.animVal, | |
| 'chrome-distiller://forged'); | |
| sanitizeLinks(testContainer); | |
| svg.setCurrentTime(2); | |
| assert.deepEqual( | |
| Array.from(svg.querySelectorAll('*')).map(el => el.localName), | |
| ['text']); | |
| assert.equal(svg.innerHTML, '<text>Bare SVG Link</text>'); | |
| }); | |
| test( | |
| 'sanitizeLinks should not remove non-SVG elements named set or animate', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = | |
| '<set>HTML Set</set><animate>HTML Animate</animate>'; | |
| sanitizeLinks(testContainer); | |
| assert.equal(testContainer.querySelectorAll('set').length, 1); | |
| assert.equal(testContainer.querySelectorAll('animate').length, 1); | |
| }); | |
| test( | |
| 'sanitizeLinks should strip SVG discard and xlink:show, and remove ' + | |
| 'target on fragment SVG links', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = | |
| '<svg xmlns:xlink="http://www.w3.org/1999/xlink">' + | |
| '<a id="ext" href="https://example.com" xlink:show="replace">' + | |
| '<text>External SVG</text></a>' + | |
| '<a id="frag" href="#sec" target="_blank" xlink:show="new">' + | |
| '<text>Fragment SVG</text></a>' + | |
| '<discard href="#ext" begin="0s"/>' + | |
| '</svg>'; | |
| sanitizeLinks(testContainer); | |
| assert.equal(testContainer.querySelectorAll('discard').length, 0); | |
| const ext = testContainer.querySelector('#ext'); | |
| assert.isNotNull(ext); | |
| assert.equal(ext.getAttribute('target'), '_blank'); | |
| assert.isNull( | |
| ext.getAttributeNS('http://www.w3.org/1999/xlink', 'show')); | |
| const frag = testContainer.querySelector('#frag'); | |
| assert.isNotNull(frag); | |
| assert.isNull(frag.getAttribute('target')); | |
| assert.isNull( | |
| frag.getAttributeNS('http://www.w3.org/1999/xlink', 'show')); | |
| }); | |
| test( | |
| 'sanitizeLinks should unwrap SVG anchors with empty, unsafe, or ' + | |
| 'conflicting href and xlink:href', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = | |
| '<svg xmlns:xlink="http://www.w3.org/1999/xlink">' + | |
| '<a href="" xlink:href="https://example.com">' + | |
| '<text>Empty Href</text></a>' + | |
| '<a href="https://example.com" xlink:href="javascript:alert(1)">' + | |
| '<text>Mixed Href</text></a>' + | |
| '<a href="#sec" xlink:href="https://example.com">' + | |
| '<text>Conflicting Href</text></a>' + | |
| '<a href="https://example.com" xlink:href="https://example.com">' + | |
| '<text>Valid Both</text></a>' + | |
| '</svg>'; | |
| sanitizeLinks(testContainer); | |
| const svg = testContainer.querySelector('svg'); | |
| const links = svg.querySelectorAll('a'); | |
| assert.equal(links.length, 1); | |
| assert.equal(links[0].textContent, 'Valid Both'); | |
| assert.equal(links[0].getAttribute('target'), '_blank'); | |
| assert.isTrue(svg.textContent.includes('Empty Href')); | |
| assert.isTrue(svg.textContent.includes('Mixed Href')); | |
| assert.isTrue(svg.textContent.includes('Conflicting Href')); | |
| }); | |
| test( | |
| 'sanitizeLinks should keep placeholder HTML anchors without href and ' + | |
| 'unwrap empty href', | |
| async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<a id="placeholder">Section</a>' + | |
| '<a href="">Empty</a>'; | |
| sanitizeLinks(testContainer); | |
| const links = testContainer.querySelectorAll('a'); | |
| assert.equal(links.length, 1); | |
| assert.equal(links[0].id, 'placeholder'); | |
| assert.isTrue(testContainer.textContent.includes('Empty')); | |
| }); | |
| }); | |
| suite('ContentProcessing.removeExtraneousElementsFrom', function() { | |
| let testContainer; | |
| setup(function() { | |
| testContainer = document.createElement('div'); | |
| document.body.appendChild(testContainer); | |
| }); | |
| teardown(function() { | |
| document.body.removeChild(testContainer); | |
| testContainer = null; | |
| }); | |
| test('removes UI labels', async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<div>Advertisement</div>' + | |
| '<p>Sponsored</p>' + | |
| '<span>Supported by</span>' + | |
| '<article>Skip Advertisement</article>' + | |
| '<div>Legitimate content with advertisement inside a long paragraph ' + | |
| 'that exceeds the forty character limit by quite a bit.</div>'; | |
| removeExtraneousElementsFrom(testContainer); | |
| const remainingDivs = testContainer.querySelectorAll('div'); | |
| assert.equal(remainingDivs.length, 1); | |
| assert.isTrue(remainingDivs[0].textContent.includes('Legitimate content')); | |
| assert.equal(testContainer.querySelectorAll('p').length, 0); | |
| assert.equal(testContainer.querySelectorAll('span').length, 0); | |
| assert.equal(testContainer.querySelectorAll('article').length, 0); | |
| }); | |
| test('removes accessibility announcements', async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<div aria-live="polite">Loading...</div>' + | |
| '<div role="alert">Error</div>' + | |
| '<div role="status">Saved</div>' + | |
| '<div role="log">Log entry</div>' + | |
| '<div aria-live="polite">This is a very long live blog entry ' + | |
| 'that should not be removed because it is well over the two ' + | |
| 'hundred character limit. It contains actual content that users ' + | |
| 'will want to read during a live event. We are adding more text ' + | |
| 'to ensure it surpasses the length threshold reliably.</div>'; | |
| removeExtraneousElementsFrom(testContainer); | |
| const remainingDivs = testContainer.querySelectorAll('div'); | |
| assert.equal(remainingDivs.length, 1); | |
| assert.isTrue(remainingDivs[0].textContent.includes('very long live blog')); | |
| }); | |
| test('removes visually hidden empty elements', async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<div style="width: 0; height: 0;"></div>' + | |
| '<span style="width: 0; height: 0;"> </span>' + | |
| '<div style="width: 0; height: 0;">Text</div>' + | |
| '<div style="width: 10px; height: 0;"></div>' + | |
| '<img style="width: 0; height: 0;">' + | |
| '<div style="float: left; height: 0;"></div>'; | |
| // Simulate getBoundingClientRect for testing | |
| const elements = testContainer.querySelectorAll('*'); | |
| for (const el of elements) { | |
| el.getBoundingClientRect = function() { | |
| const style = this.getAttribute('style') || ''; | |
| return { | |
| width: style.includes('width: 0') ? 0 : 10, | |
| height: style.includes('height: 0') ? 0 : 10, | |
| }; | |
| }; | |
| } | |
| removeExtraneousElementsFrom(testContainer); | |
| // The first two divs/spans are 0x0 and empty, so they are removed. | |
| // The third has text, so it stays. | |
| // The fourth has width 10, so it stays. | |
| // The fifth is an img (not in allowlist), so it stays. | |
| // The sixth has width 10 (simulated float), so it stays. | |
| const remainingElements = testContainer.querySelectorAll('*'); | |
| assert.equal(remainingElements.length, 4); | |
| assert.equal(remainingElements[0].tagName, 'DIV'); | |
| assert.equal(remainingElements[0].textContent, 'Text'); | |
| assert.equal(remainingElements[1].tagName, 'DIV'); | |
| assert.equal(remainingElements[2].tagName, 'IMG'); | |
| assert.equal(remainingElements[3].tagName, 'DIV'); | |
| }); | |
| test('removes nested extraneous elements correctly', async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = | |
| '<div id="keep1">This is a very long paragraph that will definitely ' + | |
| 'exceed the one hundred character limit to ensure parent is kept. ' + | |
| 'Keep Me' + | |
| ' <div id="remove1">Advertisement' + | |
| ' <div id="child1">Nested Child</div>' + | |
| ' </div>' + | |
| '</div>' + | |
| '<div id="keep2">Keep Me Too</div>'; | |
| removeExtraneousElementsFrom(testContainer); | |
| // #remove1 and its child #child1 should be gone. | |
| assert.isNull(document.getElementById('remove1')); | |
| assert.isNull(document.getElementById('child1')); | |
| // #keep1 and #keep2 should remain. | |
| assert.isNotNull(document.getElementById('keep1')); | |
| assert.isNotNull(document.getElementById('keep2')); | |
| assert.isTrue(testContainer.textContent.includes('Keep Me')); | |
| assert.isTrue(testContainer.textContent.includes('Keep Me Too')); | |
| assert.isFalse(testContainer.textContent.includes('Advertisement')); | |
| assert.isFalse(testContainer.textContent.includes('Nested Child')); | |
| }); | |
| test('removes the first child element correctly', async function() { | |
| const {assert} = await import('./index.js'); | |
| testContainer.innerHTML = '<div id="remove1">Advertisement</div>' + | |
| '<div id="keep1">Keep Me</div>'; | |
| removeExtraneousElementsFrom(testContainer); | |
| assert.isNull(document.getElementById('remove1')); | |
| assert.isNotNull(document.getElementById('keep1')); | |
| assert.equal(testContainer.textContent.trim(), 'Keep Me'); | |
| }); | |
| }); |