From cdf1b2dd62249844a4ee92a4cfc669983b718bce Mon Sep 17 00:00:00 2001 From: Tero Date: Fri, 16 Oct 2026 23:22:46 +0300 Subject: [PATCH] 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. --- REFACTOR_PLAN.md | 3 +- package-lock.json | 4 +- package.json | 2 +- tests/mail-content-security.test.cjs | 57 ++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/REFACTOR_PLAN.md b/REFACTOR_PLAN.md index 30b3315..4e7345f 100644 --- a/REFACTOR_PLAN.md +++ b/REFACTOR_PLAN.md @@ -153,7 +153,7 @@ This is a gradual destination, not a requirement to move everything at once. IPC - Electron Builder and the desktop Actions artifact name derive from `package.json`. - Android `versionName` derives from the same package version. Its current `versionCode` mapping is `major * 10000 + minor * 100 + patch`; keep minor and patch below 100, and revise the mapping before reaching that boundary to avoid collisions. - `scripts/build-all.ps1` uses the package version for APK naming. Before a release, bump once with `npm run bump:build`, verify the lockfile and computed Android version code, and build every target without another bump. -- Latest version check: `package.json` and both lockfile version fields are `0.4.36`; Android derives `versionName 0.4.36` and `versionCode 436`. The user confirms the previous artifact works; this bump is for the next build. +- Latest version check: `package.json` and both lockfile version fields are `0.4.37`; Android derives `versionName 0.4.37` and `versionCode 437`. The user confirms the previous artifact works; this bump is for the next build. ## Progress log @@ -192,5 +192,6 @@ This is a gradual destination, not a requirement to move everything at once. IPC - Step 6 at version `0.4.34` removes legacy global renderer-state accessors; the state module now exposes only `window.epMailState` to the renderer. - Step 6 at version `0.4.35` consolidates the mail API factory under `window.epMailFeatures.modules`, removing the separate `window.epMailUiModules` global. - Step 6 at version `0.4.36` consolidates the HTML sanitization and remote-image policy API under `window.epMailFeatures.contentSecurity`. +- Step 7 at version `0.4.37` adds focused sanitizer coverage for dangerous DOM nodes/attributes and verifies that remote images remain data-only pending explicit loading. - The current IPC/DAV validation changes pass the full suite: 108 tests. - While work continued, `package.json` and `package-lock.json` reflected a version bump to `0.4.6`; those changes were preserved. diff --git a/package-lock.json b/package-lock.json index 5e7933c..9703d81 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ep-mail", - "version": "0.4.36", + "version": "0.4.37", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ep-mail", - "version": "0.4.36", + "version": "0.4.37", "dependencies": { "@capacitor/android": "^8.5.2", "@capacitor/core": "^8.5.2", diff --git a/package.json b/package.json index 446daa7..ca78d30 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ep-mail", - "version": "0.4.36", + "version": "0.4.37", "private": true, "description": "Eternity Project mail client", "homepage": "https://git.eternityproject.fi/thuttu/ep-mail-client", diff --git a/tests/mail-content-security.test.cjs b/tests/mail-content-security.test.cjs index e4de857..4df31b2 100644 --- a/tests/mail-content-security.test.cjs +++ b/tests/mail-content-security.test.cjs @@ -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 ``; + const contents = childNodes.map(child => child.nodeType === 3 ? child.textContent : child.innerHTML).join(''); + return `<${this.localName}${attributes}>${contents}`; + } + }; + 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( + '

Hello

link' + ); + + assert.doesNotMatch(parsedHtml, //); + assert.equal(remoteImageCount, 1); + assert.equal(html, '

Hello

pixel'); + assert.doesNotMatch(html, /onclick|onerror|javascript:|