Refractoring Stage 7.1 Added sanitizer regression coverage for dangerous nodes and attributes, remote-image URL approval, and keeping remote images inert until explicit loading. Bumped to 0.4.37 (Android version code 437) and updated REFACTOR_PLAN.md.

This commit is contained in:
2026-10-16 23:22:46 +03:00
parent cfb6ddc2bb
commit cdf1b2dd62
4 changed files with 62 additions and 4 deletions
+57
View File
@@ -20,3 +20,60 @@ test('allows only bounded HTTPS remote image URLs without embedded credentials',
assert.equal(safeRemoteImageUrl('not a URL'), null);
assert.equal(safeRemoteImageUrl(null), null);
});
test('sanitizes parsed mail DOM and keeps approved remote images inert', () => {
let parsedHtml = '';
const createElement = (localName, attributes = {}, childNodes = []) => {
const values = new Map(Object.entries(attributes));
const element = {
nodeType: 1,
localName,
childNodes,
parentNode: null,
get attributes() { return [...values].map(([name, value]) => ({ name, value })); },
getAttribute(name) { return values.get(name) ?? null; },
setAttribute(name, value) { values.set(name, String(value)); },
removeAttribute(name) { values.delete(name); },
remove() {
if (this.parentNode) this.parentNode.childNodes.splice(this.parentNode.childNodes.indexOf(this), 1);
},
get innerHTML() {
const attributes = [...values].map(([name, value]) => ` ${name}="${value}"`).join('');
if (this.localName === 'img') return `<img${attributes}>`;
const contents = childNodes.map(child => child.nodeType === 3 ? child.textContent : child.innerHTML).join('');
return `<${this.localName}${attributes}>${contents}</${this.localName}>`;
}
};
for (const child of childNodes) child.parentNode = element;
return element;
};
const body = {
childNodes: [],
get innerHTML() { return this.childNodes.map(child => child.innerHTML).join(''); }
};
const browserRoot = {
Node: { ELEMENT_NODE: 1, TEXT_NODE: 3 },
DOMParser: class {
parseFromString(html) {
parsedHtml = html;
const url = html.match(/data-remote-src="([^"]+)"/)?.[1] || '';
body.childNodes = [
createElement('script', {}, [{ nodeType: 3, textContent: 'alert(1)' }]),
createElement('p', { onclick: 'alert(2)' }, [{ nodeType: 3, textContent: 'Hello' }]),
createElement('img', { src: url, 'data-remote-src': url, alt: 'pixel', onerror: 'alert(3)' })
];
body.childNodes.forEach(child => { child.parentNode = body; });
return { body };
}
}
};
const { html, remoteImageCount } = createMailContentSecurity(browserRoot).sanitizeMailHtml(
'<p onclick="alert(2)">Hello</p><script>alert(1)</script><img src="https://images.example.test/pixel.png" onerror="alert(3)"><a href="javascript:alert(4)">link</a>'
);
assert.doesNotMatch(parsedHtml, /<script\b|\shref\s*=|\ssrc\s*=/i);
assert.match(parsedHtml, /<img data-remote-src="https:\/\/images\.example\.test\/pixel\.png">/);
assert.equal(remoteImageCount, 1);
assert.equal(html, '<p>Hello</p><img data-remote-src="https://images.example.test/pixel.png" alt="pixel">');
assert.doesNotMatch(html, /onclick|onerror|javascript:|<script/);
});