76 lines
4.0 KiB
JavaScript
76 lines
4.0 KiB
JavaScript
(function installMailContentSecurity(root) {
|
|
if (typeof module === 'object' && module.exports) {
|
|
module.exports = { createMailContentSecurity };
|
|
return;
|
|
}
|
|
Object.defineProperty(root, 'epMailMailContentSecurity', {
|
|
value: createMailContentSecurity(root),
|
|
enumerable: false
|
|
});
|
|
|
|
function createMailContentSecurity(browserRoot) {
|
|
function escapeHtml(value) {
|
|
return String(value).replace(/[&<>"']/g, character => ({
|
|
'&': '&', '<': '<', '>': '>', '"': '"', "'": '''
|
|
})[character]);
|
|
}
|
|
|
|
function safeRemoteImageUrl(value) {
|
|
if (typeof value !== 'string' || value.length > 2048) return null;
|
|
try {
|
|
const url = new URL(value);
|
|
return url.protocol === 'https:' && url.hostname && !url.username && !url.password ? url.href : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function sanitizeMailHtml(value) {
|
|
const html = String(value || '').slice(0, 100000);
|
|
const inertImages = html
|
|
.replace(/<(script|style|iframe|frame|object|embed|svg|math|form|noscript|template|video|audio|source|picture|track)\b[\s\S]*?<\/\1\s*>/gi, '')
|
|
.replace(/<(?:script|style|iframe|frame|object|embed|svg|math|form|noscript|template|video|audio|source|picture|track|link|base|meta)\b[^>]*>/gi, '')
|
|
.replace(/<!--[\s\S]*?-->/g, '')
|
|
.replace(/<img\b[^>]*>/gi, tag => {
|
|
const source = tag.match(/\bsrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s/>]+))/i);
|
|
const alt = tag.match(/\balt\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s/>]+))/i);
|
|
const remote = source && (source[1] || source[2] || source[3] || '').trim();
|
|
const alternative = alt && (alt[1] || alt[2] || alt[3] || '');
|
|
return `<img${remote ? ` data-remote-src="${escapeHtml(remote)}"` : ''}${alternative ? ` alt="${escapeHtml(alternative)}"` : ''}>`;
|
|
})
|
|
.replace(/\s(?:src|srcset|background|poster|action|formaction|href|xlink:href|srcdoc|dynsrc|lowsrc|style)\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/gi, '');
|
|
const document = new browserRoot.DOMParser().parseFromString(inertImages, 'text/html');
|
|
const allowed = new Set(['a', 'p', 'div', 'span', 'br', 'hr', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'b', 'strong', 'i', 'em', 'u', 's', 'del', 'blockquote', 'pre', 'code', 'ul', 'ol', 'li', 'table', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td', 'img']);
|
|
const discard = new Set(['script', 'style', 'iframe', 'frame', 'object', 'embed', 'svg', 'math', 'form', 'input', 'button', 'textarea', 'select', 'option', 'link', 'base', 'meta', 'video', 'audio', 'source', 'picture', 'track']);
|
|
let remoteImageCount = 0;
|
|
const clean = parent => {
|
|
for (const node of [...parent.childNodes]) {
|
|
if (node.nodeType !== browserRoot.Node.ELEMENT_NODE) {
|
|
if (node.nodeType !== browserRoot.Node.TEXT_NODE) node.remove();
|
|
continue;
|
|
}
|
|
const name = node.localName.toLowerCase();
|
|
if (discard.has(name)) { node.remove(); continue; }
|
|
if (!allowed.has(name)) { node.replaceWith(...node.childNodes); continue; }
|
|
for (const attribute of [...node.attributes]) {
|
|
const key = attribute.name.toLowerCase();
|
|
const keep = (name === 'img' && (key === 'alt' || key === 'data-remote-src')) ||
|
|
(name === 'td' || name === 'th') && ['colspan', 'rowspan'].includes(key) && /^\d{1,2}$/.test(attribute.value);
|
|
if (!keep) node.removeAttribute(attribute.name);
|
|
}
|
|
if (name === 'img') {
|
|
const url = safeRemoteImageUrl(node.getAttribute('data-remote-src'));
|
|
if (url) { node.setAttribute('data-remote-src', url); remoteImageCount += 1; }
|
|
else node.removeAttribute('data-remote-src');
|
|
}
|
|
clean(node);
|
|
}
|
|
};
|
|
clean(document.body);
|
|
return { html: document.body.innerHTML, remoteImageCount };
|
|
}
|
|
|
|
return Object.freeze({ safeRemoteImageUrl, sanitizeMailHtml });
|
|
}
|
|
})(typeof window === 'undefined' ? globalThis : window);
|