Files
ep-mail-client/tests/mail-content-security.test.cjs
T

80 lines
3.9 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { createMailContentSecurity } = require('../mail-content-security.js');
test('exposes an immutable mail-content security API', () => {
const api = createMailContentSecurity({});
assert.equal(Object.isFrozen(api), true);
assert.equal(typeof api.safeRemoteImageUrl, 'function');
assert.equal(typeof api.sanitizeMailHtml, 'function');
});
test('allows only bounded HTTPS remote image URLs without embedded credentials', () => {
const { safeRemoteImageUrl } = createMailContentSecurity({});
assert.equal(safeRemoteImageUrl('https://images.example.test/pixel.png'), 'https://images.example.test/pixel.png');
assert.equal(safeRemoteImageUrl('HTTPs://images.example.test/pixel.png'), 'https://images.example.test/pixel.png');
assert.equal(safeRemoteImageUrl('http://images.example.test/pixel.png'), null);
assert.equal(safeRemoteImageUrl('data:image/png;base64,AAAA'), null);
assert.equal(safeRemoteImageUrl('https://user:pass@images.example.test/pixel.png'), null);
assert.equal(safeRemoteImageUrl(`https://images.example.test/${'x'.repeat(2050)}`), null);
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/);
});