~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
test(security): real-browser mXSS coverage in Chromium and WebKit (#6338)
Shoaib Latif8bcd8f3db248
diff --git a/.github/workflows/security-mxss-browser-tests.yml b/.github/workflows/security-mxss-browser-tests.ymlnew file mode 100644index 0000000000..8a67b30489--- /dev/null+++ b/.github/workflows/security-mxss-browser-tests.yml@@ -0,0 +1,107 @@+# Real-browser mXSS regression tests (#6295).+#+# Why this is its own workflow rather than a job inside+# `playwright-webkit-tests.yml`: that workflow has no `pull_request` trigger —+# it runs on `workflow_call` (release pipeline), `workflow_dispatch` and a daily+# schedule. A job added there can never report a result against a PR, so the+# three-browser run would be unverifiable on the change that introduces it.+#+# This spec needs no application server, no authentication and no Vite build:+# it loads the production sanitizer modules and its fixture page from disk via+# `setContent()`. That is what makes a cheap, self-contained PR-triggered job+# possible here.+name: Security mXSS Browser Tests++on:+ pull_request:+ paths:+ - 'tests/ui_tests/playwright/tests/security-mxss-reparse.spec.js'+ - 'tests/ui_tests/playwright/tests/helpers/mxss-fixture.html'+ - 'tests/ui_tests/playwright/playwright.mxss.config.js'+ - 'tests/ui_tests/playwright/package.json'+ - 'tests/ui_tests/playwright/package-lock.json'+ - 'src/local_deep_research/web/static/js/security/**'+ - 'src/local_deep_research/web/static/js/services/ui.js'+ # The spec loads the application's rendering dependencies (DOMPurify,+ # marked, katex, marked-katex-extension) from the repository root+ # node_modules and mirrors the KaTeX registration in app.js, so root+ # dependency or bootstrap changes can change what is under test.+ - 'package.json'+ - 'package-lock.json'+ - 'src/local_deep_research/web/static/js/app.js'+ - '.github/workflows/security-mxss-browser-tests.yml'+ workflow_dispatch:+ schedule:+ - cron: '30 2 * * *' # Daily, offset from the other Playwright suites++permissions: {} # Minimal top-level for OSSF Scorecard Token-Permissions++jobs:+ mxss-three-browser:+ name: mXSS reparse spec (Chromium, Firefox, WebKit)+ runs-on: ubuntu-latest+ permissions:+ contents: read++ steps:+ - name: Harden the runner (Audit all outbound calls)+ uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1+ with:+ egress-policy: audit++ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1+ with:+ persist-credentials: false++ - name: Setup Node.js+ uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0+ with:+ node-version: '24'+ cache: 'npm'+ cache-dependency-path: |+ package-lock.json+ tests/ui_tests/playwright/package-lock.json++ # The spec loads DOMPurify, marked, katex and marked-katex-extension from+ # the repository root node_modules — the versions the application ships —+ # so the root dependency tree has to be present. No Vite build is needed:+ # the spec reads the production modules off disk.+ - name: Install application dependencies (npm)+ run: npm ci++ - name: Install Playwright dependencies (npm)+ working-directory: tests/ui_tests/playwright+ run: npm ci++ - name: Install Playwright browsers+ uses: nick-fields/retry@ad984534de44a9489a53aefd81eb77f87c70dc60 # v4.0.0+ with:+ timeout_minutes: 10+ max_attempts: 2+ retry_on: error+ shell: bash+ command: |+ cd tests/ui_tests/playwright+ npx playwright install chromium firefox webkit --with-deps++ - name: Run security-mxss-reparse spec across Chromium, Firefox and WebKit+ working-directory: tests/ui_tests/playwright+ env:+ CI: true+ # `playwright.mxss.config.js` declares the same three desktop browsers+ # with no `dependencies: ['setup']`, no stored auth state and no+ # `webServer` — the spec needs none of them. Selecting a project from the+ # main config would pull in `auth.setup.js`, which visits `/auth/login`.+ run: |+ npx playwright test \+ --config=playwright.mxss.config.js \+ --reporter=html,json,junit++ - name: Upload Playwright Report+ if: always()+ uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1+ with:+ name: playwright-report-mxss-three-browser+ path: |+ tests/ui_tests/playwright/playwright-report/+ tests/ui_tests/playwright/test-results/diff --git a/changelog.d/+6295-real-browser-mxss-coverage.security.md b/changelog.d/+6295-real-browser-mxss-coverage.security.mdnew file mode 100644index 0000000000..593426744d--- /dev/null+++ b/changelog.d/+6295-real-browser-mxss-coverage.security.md@@ -0,0 +1,15 @@+- Security tests now cover the Markdown and HTML render paths in real+ browsers. A new Playwright spec drives `safeSetInnerHTML`, `safeSetHTML`,+ `renderMarkdown` and `sanitizeHtml` through five mXSS payload families in+ Chromium, Firefox and WebKit, and re-checks every result after a+ serialize/reparse round trip for executable elements, `on*=` handlers and+ `javascript:` / `vbscript:` / `data:text/html` hrefs. The rendering+ dependencies are the application's locked ones (DOMPurify, marked, katex+ and marked-katex-extension, loaded from the root lockfile) and the KaTeX+ extension is registered the way `app.js` registers it, so the positive+ control renders real TeX and asserts KaTeX's generated MathML survives+ reparsing — alongside a table, a link and emphasis it must keep, and the+ tags the strict allow-list must still strip. Dependency versions are+ asserted against the root lockfile so drift fails the suite instead of+ quietly changing what is under test. The existing happy-dom suite is+ unchanged; it documents its own gap on SVG/MathML foreign-content parsing.diff --git a/docs/ci/workflow-status.md b/docs/ci/workflow-status.mdindex c7707eed6f..6f040f7c6f 100644--- a/docs/ci/workflow-status.md+++ b/docs/ci/workflow-status.md@@ -163,3 +163,14 @@ the table above.Self-gates until the Milvus backend and the `milvus` optional-dependencygroup land (#5331). Partial setups fail; only an entirely absent featureis skipped.++- `security-mxss-browser-tests.yml` — real-browser mXSS regression tests+ for the markdown rendering path (#6295). PR, manual, schedule (daily+ 02:30 UTC). Path-filtered to the spec, its fixture,+ `playwright.mxss.config.js`, the Playwright `package*.json`, the+ sanitizer and service sources under test+ (`web/static/js/security/**`, `web/static/js/services/ui.js`,+ `web/static/js/app.js`), the root `package*.json`, and the workflow+ itself. Needs no application server, no auth and no secrets: the spec+ loads the production sanitizer modules and its fixture page from disk+ via `setContent()`, so it is safe to run on fork PRs.diff --git a/tests/ui_tests/playwright/playwright.config.js b/tests/ui_tests/playwright/playwright.config.jsindex f64cddf578..74c03c19cf 100644--- a/tests/ui_tests/playwright/playwright.config.js+++ b/tests/ui_tests/playwright/playwright.config.js@@ -26,6 +26,10 @@ const authFile = path.join(__dirname, '.auth/user.json');* in `npm run test:notes`.*/const NOTES_SPECS = /\/tests\/notes\//;+// Real-browser sanitizer regression (security/mxss-reparse) targets desktop+// browsers via a fixture HTML loaded from disk; mobile/visual-regression+// projects have no value running it.+const MXSS_SPECS = /\/tests\/security-mxss-reparse/;/*** See https://playwright.dev/docs/test-configuration@@ -101,7 +105,7 @@ export default defineConfig({{name: 'iPhone SE',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['iPhone SE'],// Additional settings for thorough testing@@ -115,7 +119,7 @@ export default defineConfig({{name: 'iPhone 14',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['iPhone 14'],hasTouch: true,@@ -128,7 +132,7 @@ export default defineConfig({{name: 'iPhone 14 Pro Max',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['iPhone 14 Pro Max'],hasTouch: true,@@ -141,7 +145,7 @@ export default defineConfig({{name: 'Pixel 5',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['Pixel 5'],hasTouch: true,@@ -154,7 +158,7 @@ export default defineConfig({{name: 'Galaxy S23',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {viewport: { width: 360, height: 780 },deviceScaleFactor: 3,@@ -172,7 +176,7 @@ export default defineConfig({{name: 'iPad Mini',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['iPad Mini'],hasTouch: true,@@ -184,7 +188,7 @@ export default defineConfig({{name: 'iPad Pro 11',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['iPad Pro 11'],hasTouch: true,@@ -200,7 +204,7 @@ export default defineConfig({{name: 'iPhone SE Landscape',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['iPhone SE landscape'],hasTouch: true,@@ -212,7 +216,7 @@ export default defineConfig({{name: 'iPhone 14 Landscape',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['iPhone 14 landscape'],hasTouch: true,@@ -228,7 +232,7 @@ export default defineConfig({{name: 'Desktop Chrome',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['Desktop Chrome'],storageState: authFile,@@ -238,7 +242,7 @@ export default defineConfig({{name: 'Desktop Firefox',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['Desktop Firefox'],storageState: authFile,@@ -248,7 +252,7 @@ export default defineConfig({{name: 'Desktop Safari',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['Desktop Safari'],storageState: authFile,@@ -262,7 +266,7 @@ export default defineConfig({{name: 'Mobile Safari',dependencies: ['setup'],- testIgnore: NOTES_SPECS,+ testIgnore: [NOTES_SPECS, MXSS_SPECS],use: {...devices['iPhone 14'],browserName: 'webkit', // This uses WebKit, closer to real Safari@@ -281,6 +285,7 @@ export default defineConfig({name: 'notes-paths',dependencies: ['setup'],testMatch: /\/tests\/notes\/[^/]+\.spec\.js$/,+ testIgnore: MXSS_SPECS,use: {...devices['Desktop Chrome'],storageState: authFile,diff --git a/tests/ui_tests/playwright/playwright.mxss.config.js b/tests/ui_tests/playwright/playwright.mxss.config.jsnew file mode 100644index 0000000000..c577f14a0a--- /dev/null+++ b/tests/ui_tests/playwright/playwright.mxss.config.js@@ -0,0 +1,64 @@+/**+ * Playwright configuration for the real-browser mXSS regression spec.+ *+ * Scope: `tests/security-mxss-reparse.spec.js` only (issue #6295).+ *+ * Why a separate config rather than reusing `playwright.config.js`:+ * every project in the main config declares `dependencies: ['setup']` and+ * loads `.auth/user.json`. Playwright runs project dependencies even when a+ * specific spec is selected, and `auth.setup.js` visits `/auth/login`, so+ * selecting any main-config project requires the Flask dev server to be up.+ *+ * This spec needs neither. It loads the production sanitizer modules off disk+ * and its fixture page via `setContent()`, so it must not inherit the+ * authentication dependency, the stored auth state, or the `webServer` block.+ * Running it under this config is what makes the three-browser CI job+ * (`security-mxss-reparse-three-browser` in+ * `.github/workflows/playwright-webkit-tests.yml`) self-contained.+ */++import { defineConfig, devices } from '@playwright/test';++export default defineConfig({+ testDir: './tests',++ // Only this spec. Path-anchored so it cannot pick up an unrelated file+ // that happens to share the basename.+ testMatch: /\/tests\/security-mxss-reparse\.spec\.js$/,++ // The spec's tests share a page-level contract (document-wide executable+ // surface scan) and are cheap; serial keeps the failure output readable.+ fullyParallel: false,+ workers: 1,++ // A retry is worth having on CI: browser-parser timing differs across+ // Chromium/Firefox/WebKit and a flake here would be indistinguishable+ // from a real sanitizer regression without one.+ retries: process.env.CI ? 1 : 0,++ reporter: process.env.CI ? [['list'], ['html', { open: 'never' }]] : 'list',++ use: {+ trace: 'retain-on-failure',+ },++ projects: [+ {+ // No `dependencies` and no `storageState`: this spec needs neither+ // the auth setup nor a running application server.+ name: 'mxss-chromium',+ use: { ...devices['Desktop Chrome'] },+ },+ {+ name: 'mxss-firefox',+ use: { ...devices['Desktop Firefox'] },+ },+ {+ name: 'mxss-webkit',+ use: { ...devices['Desktop Safari'] },+ },+ ],++ // Deliberately no `webServer`. The fixture is loaded from disk via+ // setContent(); nothing here talks to the application.+});diff --git a/tests/ui_tests/playwright/tests/helpers/mxss-fixture.html b/tests/ui_tests/playwright/tests/helpers/mxss-fixture.htmlnew file mode 100644index 0000000000..618c95d3e9--- /dev/null+++ b/tests/ui_tests/playwright/tests/helpers/mxss-fixture.html@@ -0,0 +1,34 @@+<!DOCTYPE html>+<!--+ mXSS reparse-real-browser fixture (used by security-mxss-reparse.spec.js).++ A self-contained sandbox the Playwright spec loads via setContent (file://+ is blocked by Playwright's default context for security). The fixture+ exposes:+ - #mxss-target: the test bed writes sanitized output here via+ window.safeSetInnerHTML, window.safeSetHTML, or window.ui.renderMarkdown.+ - #mxss-bypass-target: the negative control writes raw unsanitized HTML+ here to confirm the browser parses an executable surface (so a silently+ broken DOMPurify load cannot make the suite pass).+ - #trusted-loader: the spec relocates inline <script> wrappers into this+ subtree after production modules are loaded. The executable-surface+ detector accepts <script src="..."> elements only inside this subtree,+ so trusted CDN-style loaders are not flagged while a hostile+ <script src="..."> that survived sanitization in #mxss-target IS flagged.++ The browser's foreign-content parser is the system under test. The HTML+ here is incidental — it just provides containers.+-->+<html lang="en">+ <head>+ <meta charset="utf-8" />+ <title>mXSS reparse-real-browser fixture</title>+ </head>+ <body>+ <main>+ <div id="mxss-target"></div>+ <div id="mxss-bypass-target"></div>+ </main>+ <div id="trusted-loader" style="display: none"></div>+ </body>+</html>diff --git a/tests/ui_tests/playwright/tests/security-mxss-reparse.spec.js b/tests/ui_tests/playwright/tests/security-mxss-reparse.spec.jsnew file mode 100644index 0000000000..b8a9b1a1b3--- /dev/null+++ b/tests/ui_tests/playwright/tests/security-mxss-reparse.spec.js@@ -0,0 +1,972 @@+/**+ * Real-browser mXSS regression coverage (#6295).+ *+ * What this file establishes+ * --------------------------+ * The happy-dom fixture at `tests/js/security/mxss-reparse-invariant.test.js`+ * guards the application's sanitize → parse → serialize → reparse invariant+ * against regressions in the *simulated* DOM. That suite cannot exercise+ * the browser's foreign-content parser — happy-dom has no MathML tree+ * construction and tracks SVG namespace only partially — so a passing test+ * there does not by itself prove the sanitizer is robust against mXSS under a+ * real browser. See the long header in that file for the gory details.+ *+ * This Playwright spec complements the happy-dom suite. It loads DOMPurify+ * from the application's *installed* dependency (the lockfile-pinned version,+ * not a floating CDN URL), then loads the unmodified production sanitizer+ * (`window.safeSetInnerHTML` from `security/xss-protection.js`,+ * `window.ui.renderMarkdown` and `safeSetHTML` from `services/ui.js`), and+ * drives them through each payload in a real Chromium / Firefox / WebKit+ * page. After every render the spec exercises the mXSS mXSS round trip:+ *+ * serialize(sanitizer-output)+ * → insert into a fresh element+ * → reparse (round-trip)+ * → re-walk for executable surfaces+ *+ * Also runs the closures the production app actually uses for "I trust this+ * Markdown": `renderMarkdown` (which feeds DOMPurify a richer config than the+ * strict one used by safeSetInnerHTML — it adds `semantics` and `annotation`,+ * which is what enables MathML/KaTeX in the production render path) and+ * `sanitizeHtml` (the underlying DOMPurify.sanitize wrapper).+ *+ * Scope (per the issue's acceptance criteria)+ * -------------------------------------------+ * 1. Five payload families (foreign-content confusion, rawtext/RCDATA+ * breakout, foster-parenting/table repair, deep-nesting flattening,+ * serialization instability), each as a single payload with a sentinel.+ * 2. Four production code paths exercised with every payload:+ * a. `window.safeSetInnerHTML` — strict `SANITIZE_CONFIG` allow-list;+ * b. `window.safeSetHTML` — services/ui.js wrapper (prefers DOMPurify);+ * c. `window.ui.renderMarkdown` — Markdown → DOMPurify *permissive* config+ * (default allow-list plus `semantics`/`annotation`), the path the+ * application uses to keep tables and KaTeX-emitted MathML alive;+ * d. `window.sanitizeHtml` — the DOMPurify wrapper the other setters+ * delegate to, driven with the same payloads so its config is a tested+ * contract rather than an implementation detail.+ * All four run against the application's *locked* dependencies, loaded from+ * the repository root `node_modules` (DOMPurify, marked, katex,+ * marked-katex-extension) rather than the Playwright package's own versions.+ * 3. The serialize → insert-into-fresh-element → reparse → re-walk round trip+ * runs for every case, and the instrumented trees are walked individually:+ * the post-render DOM, the first reparsed root and the second reparsed root+ * each get the full executable-surface assertion set. The reparse roots are+ * detached, so they are scanned by walking the root itself rather than by a+ * document-wide query, which cannot see them.+ * 4. Benign coverage in both directions:+ * a. A renderer control drives real Markdown — a GFM table plus actual TeX+ * (`$\sqrt{x^2}$` and a display integral) — through `renderMarkdown`+ * with `marked-katex-extension` registered exactly as `app.js` does it,+ * then asserts KaTeX generated its MathML (`<math>`, `<semantics>`,+ * `<msqrt>`, the TeX carried in `<annotation>`) and that it survives the+ * reparse with no executable surface alongside. Because the math is TeX,+ * this can catch a failure to *generate* math, which pre-built MathML+ * cannot.+ * b. An additional control supplies pre-built MathML as markup and asserts+ * it survives the renderer and the reparse.+ * c. A strict audit asserts `safeSetInnerHTML` and `sanitizeHtml` strip+ * table/svg/math, the opposite of what `renderMarkdown` preserves. Both+ * directions are pinned so "correctly rejected" and "never produced"+ * cannot be confused with each other.+ * 5. Dependency parity: `production dependency versions match the root+ * lockfile` compares the installed versions of DOMPurify, marked, katex and+ * marked-katex-extension against the resolved versions in the root+ * `package-lock.json`, checks every dependency build resolves inside the+ * repository root, and cross-checks the runtime versions DOMPurify and KaTeX+ * report in the browser. Version drift fails the suite instead of silently+ * changing what is under test.+ * 6. Two independent detector controls keep the safety assertions honest:+ * a. a bypass that asserts the scanner *does* report `script[src]` and+ * `iframe[src]` in a payload root, `on*=` handlers and `javascript:`+ * hrefs — if it returns empty, the vector loop's assertions are vacuous;+ * b. a forged-`id="trusted-loader"` payload that asserts the loader+ * exception cannot be claimed by a payload-supplied ancestor id, in a+ * payload root, in a detached reparse tree, and in the document scan.+ * 7. A negative control renders an unsanitized payload, forces the handler to+ * run and asserts an alert sentinel actually fires, so a silently broken+ * DOMPurify load cannot make the suite pass.+ * What this spec does NOT establish+ * --------------------------------+ * It does not perform PDF preview insertion in this round. The issue's PDF+ * preview acceptance criterion is paired with #6220's follow-up cleanup;+ * a separate fixture & spec round can wire the same DOMPurify invocation+ * through `services/pdf.js` when that work lands.+ */++import { test, expect } from '@playwright/test';+import path from 'path';+import fs from 'fs';++const SPEC_DIR = process.cwd();+// REPO_ROOT is three levels up: tests/ui_tests/playwright -> tests/ui_tests -> tests -> .+const REPO_ROOT = path.resolve(SPEC_DIR, '..', '..', '..');+const FIXTURE_PATH = path.join(SPEC_DIR, 'tests', 'helpers', 'mxss-fixture.html');++// Production modules we load into the page from disk.+const UI_SCRIPT_PATH = path.join(+ REPO_ROOT, 'src', 'local_deep_research', 'web', 'static', 'js', 'services', 'ui.js'+);+const XSS_SCRIPT_PATH = path.join(+ REPO_ROOT, 'src', 'local_deep_research', 'web', 'static', 'js', 'security', 'xss-protection.js'+);+const SAFE_LOGGER_SCRIPT_PATH = path.join(+ REPO_ROOT, 'src', 'local_deep_research', 'web', 'static', 'js', 'security', 'safe-logger.js'+);+const UI_SCRIPT = fs.readFileSync(UI_SCRIPT_PATH, 'utf8');+const XSS_SCRIPT = fs.readFileSync(XSS_SCRIPT_PATH, 'utf8');+const SAFE_LOGGER_SCRIPT = fs.readFileSync(SAFE_LOGGER_SCRIPT_PATH, 'utf8');++// ---------------------------------------------------------------------------+// Production rendering dependencies.+//+// These load from the REPO ROOT's node_modules — the versions the root+// lockfile resolves and the application actually ships. An earlier revision+// loaded `marked` from the Playwright package's own dev deps, which pinned 15.x+// while the root lockfile resolves 18.x, so the spec was exercising a parser+// the application never uses. Loading from the root also makes+// `marked-katex-extension` and `katex` available at the versions `app.js`+// initialises, which is what lets the benign control render real TeX instead of+// asserting on pre-built MathML it supplied itself.+//+// `production dependency versions match the root lockfile` below asserts the+// loaded versions against that lockfile, so drift fails the suite rather than+// silently changing what is under test.+// ---------------------------------------------------------------------------+const APP_NODE_MODULES = path.join(REPO_ROOT, 'node_modules');++const loadDepFile = (relative) => {+ const full = path.join(APP_NODE_MODULES, relative);+ if (!fs.existsSync(full)) {+ throw new Error(+ `missing production dependency build: ${relative}\n` ++ 'Run `npm ci` at the repository root before this spec.'+ );+ }+ return full;+};++// UMD builds throughout: addScriptTag({path}) injects a classic <script>, not+// <script type="module">, so the ESM entry points would not execute.+const DOMPURIFY_PATH = loadDepFile('dompurify/dist/purify.min.js');+const MARKED_PATH = loadDepFile('marked/lib/marked.umd.js');+const KATEX_PATH = loadDepFile('katex/dist/katex.min.js');+const MARKED_KATEX_PATH = loadDepFile('marked-katex-extension/lib/index.umd.js');++// The versions the root lockfile resolves — the parity contract asserted below.+const LOCKED_VERSIONS = (() => {+ const lock = JSON.parse(+ fs.readFileSync(path.join(REPO_ROOT, 'package-lock.json'), 'utf8')+ );+ const packages = lock.packages || {};+ const out = {};+ for (const name of ['dompurify', 'marked', 'katex', 'marked-katex-extension']) {+ const entry = packages[`node_modules/${name}`];+ if (!entry || !entry.version) {+ throw new Error(`package-lock.json has no resolved version for ${name}`);+ }+ out[name] = entry.version;+ }+ return out;+})();++// Same payload corpus the happy-dom suite uses, lifted wholesale so any+// regression in either suite surfaces in both.+const VECTORS = {+ foreignContent: {+ family: 'foreign-content confusion',+ sentinel: 'foreign-sentinel',+ payload: '<p>foreign-sentinel</p><svg><p><style><!--</style><img src=x onerror=alert(1)>'+ + '<math><mtext><table><mglyph><style><!--</style><img src=x onerror=alert(1)>'+ + '</table></mtext></math>',+ },+ rawtextRcdata: {+ family: 'rawtext/RCDATA breakout',+ sentinel: 'rawtext-sentinel',+ payload: '<p>rawtext-sentinel</p><style><a title="</style><img src=x onerror=alert(1)">'+ + '<noscript><p title="</noscript><a href="vbscript:alert(1)" onclick="alert(1)">bad</a>">'+ + '<textarea><a title="</textarea><a href="data:text/html,%3Cscript%3Ealert(1)%3C/script%3E" '+ + 'onfocus="alert(1)">bad</a>">',+ },+ fosterParenting: {+ family: 'foster-parenting/table repair',+ sentinel: 'foster-sentinel',+ payload: '<p>foster-sentinel</p><table><caption><svg><foreignObject><table><tr><td>'+ + '<img src=x onerror=alert(1)></td></tr></table></foreignObject></svg></caption></table>'+ + '<form><table><form><tr><td onmouseover=alert(1)>'+ + '<a href=javascript:alert(1)>bad</a></td></tr></form></table></form>',+ },+ deepNesting: {+ family: 'deep-nesting flattening',+ sentinel: 'deep-sentinel',+ payload: '<p>deep-sentinel</p>' + '<div><svg>'.repeat(100)+ + '<a href="javascript:alert(1)"><img src=x onerror=alert(1)></a>'+ + '</svg></div>'.repeat(100),+ },+ serialization: {+ family: 'serialization instability',+ sentinel: 'serialization-sentinel',+ payload: '<p>serialization-sentinel</p><div title="prefix<!--suffix">'+ + '<a title="<script src=x>" href="javascript:alert(1)">bad</a></div><!--',+ },+};++/**+ * Page-side executable-surface scanner.+ *+ * `loadFixture()` injects this source into the page so the SAME walker can be+ * run against two different roots:+ *+ * 1. `document` — the live page, for the post-render assertion; and+ * 2. each reparsed root — the detached `<div>`s the serialize → reparse+ * round trip produces. Those trees are never attached to the document,+ * so a `document.querySelectorAll` walk cannot see them. Walking each+ * reparsed root explicitly is what makes "still safe after reparsing" an+ * actual assertion rather than an assumption.+ *+ * Trust model: the only executable element tolerated is a `<script>` nested+ * inside `#trusted-loader` — the subtree holding the loaders this spec+ * injected. A `src` attribute never grants trust; a `script[src]` or+ * `iframe[src]` that survived sanitization into a payload root is precisely+ * the leak class under test.+ */+const MXSS_SCAN_SOURCE = `+window.__mxssTrustedLoaders = window.__mxssTrustedLoaders || [];++window.__mxssScan = function (root, options) {+ const opts = options || {};+ // Trust for loader <script> elements is OPT-IN per scan. Payload-root scans+ // never pass it, so nothing executable is ever tolerated inside a payload+ // tree. Only the whole-document scan opts in.+ const trustLoaders = opts.trustLoaders === true;+ const trustedLoaders = window.__mxssTrustedLoaders || [];+ const scope = root || document;+ const all = Array.from(scope.querySelectorAll('*'));+ const badElements = [];+ const badAttrs = [];+ const badHrefs = [];+ for (const el of all) {+ const tag = el.tagName.toLowerCase();+ const isInTargetContainer = (() => {+ let n = el.parentElement;+ while (n) {+ if (n.id === 'mxss-target' || n.id === 'mxss-bypass-target') return true;+ n = n.parentElement;+ }+ return false;+ })();+ if (tag === 'script' || tag === 'iframe' || tag === 'object' || tag === 'embed') {+ // Trust is IDENTITY-based: the element must be one of the exact+ // nodes loadFixture() recorded after injecting the production+ // loaders. Walking ancestors looking for id === 'trusted-loader' is+ // deliberately NOT used, because a payload can supply its own+ // <div id="trusted-loader"> wrapper inside #mxss-target and an+ // ID-based check would wave the scripts inside it straight through.+ // A payload can forge an attribute; it cannot forge identity.+ const isRecordedLoader = trustedLoaders.indexOf(el) !== -1;+ const isTrustedLoaderScript = tag === 'script'+ && trustLoaders+ && isRecordedLoader+ && !isInTargetContainer;+ if (!isTrustedLoaderScript) {+ badElements.push({+ tag,+ parentId: el.parentElement ? el.parentElement.id : '',+ inTargetContainer: isInTargetContainer,+ src: (el.getAttribute('src') || '').trim(),+ textLen: (el.textContent || '').length,+ });+ }+ }+ for (const name of el.getAttributeNames()) {+ if (/^on/i.test(name)) {+ badAttrs.push({+ tag,+ name,+ parentId: el.parentElement ? el.parentElement.id : '',+ });+ }+ }+ if (el.hasAttribute('href')) {+ const href = (el.getAttribute('href') || '').replace(/[\\x00-\\x20\\x7F]/g, '');+ if (/^(?:javascript:|vbscript:|data:text\\/html)/i.test(href)) {+ badHrefs.push({+ tag,+ href,+ parentId: el.parentElement ? el.parentElement.id : '',+ });+ }+ }+ }+ return { badElements, badAttrs, badHrefs };+};+`;++/**+ * Runs the scanner over the live document. This is the only call site that+ * opts into the loader exception (`trustLoaders: true`); payload-root scans+ * never do, so nothing executable is tolerated inside a payload tree.+ */+async function detectExecutableSurface(page) {+ return page.evaluate(() => window.__mxssScan(document, { trustLoaders: true }));+}++async function loadFixture(page) {+ // Read the fixture HTML from disk and serve it directly to the page.+ // `file://` is blocked by Playwright's default context for security,+ // so we inline the file via setContent() with a baseURL of about:blank.+ const fixtureHtml = fs.readFileSync(FIXTURE_PATH, 'utf8');+ await page.setContent(fixtureHtml);++ // DOMPurify from the lockfile-pinned installed dep, NOT from a CDN.+ // addScriptTag({path}) injects the file as a classic <script>; the+ // UMD build attaches DOMPurify to globalThis (== window in a browser),+ // which matches what app.js does after Vite bundles the application.+ await page.addScriptTag({ path: DOMPURIFY_PATH });+ // app.js publishes the global under window.DOMPurify; production code reads+ // it that way too. Belt-and-braces: re-alias if the global didn't land+ // under window.* directly (some bundlers attach to globalThis.* only).+ await page.evaluate(() => {+ if (typeof DOMPurify === 'undefined') return;+ if (typeof window.DOMPurify === 'undefined') {+ window.DOMPurify = DOMPurify;+ }+ });++ // `marked`, `katex` and `marked-katex-extension` reach `services/ui.js` as+ // globals from the Vite bundle in production, and `app.js` registers the+ // KaTeX extension on `marked`. We load the same locked builds and repeat+ // that registration, so `renderMarkdown` renders real TeX through the+ // application's own math pipeline rather than merely preserving markup a+ // test supplied. Order matters: the extension's UMD factory takes `katex`+ // as its dependency, so `katex` must be on the page first.+ await page.addScriptTag({ path: MARKED_PATH });+ await page.addScriptTag({ path: KATEX_PATH });+ await page.addScriptTag({ path: MARKED_KATEX_PATH });+ await page.evaluate(() => {+ // Mirrors src/local_deep_research/web/static/js/app.js:62.+ marked.use(window.markedKatex({ throwOnError: false, errorColor: 'currentColor' }));+ });++ // Production modules — load off disk so the test bed matches what+ // production loads, no Vite/bundler involvement.+ // safe-logger.js must come before services/ui.js because ui.js's+ // `renderMarkdown` calls `SafeLogger.warn(...)`. xss-protection.js and+ // services/ui.js don't have an inter-dep order requirement.+ await page.addScriptTag({ content: SAFE_LOGGER_SCRIPT });+ await page.addScriptTag({ content: UI_SCRIPT });+ await page.addScriptTag({ content: XSS_SCRIPT });++ // The executable-surface scanner itself, injected as page-side source so+ // the round-trip assertions can run it against detached reparse roots.+ // Injected before the trusted-loader relocation below so its wrapper+ // <script> element is relocated along with the other loaders.+ await page.addScriptTag({ content: MXSS_SCAN_SOURCE });++ // addScriptTag with `content` creates a real <script> element. After it+ // executes, the wrapper element stays in the DOM, with the production+ // source text as its inline body. We move all such wrappers into a+ // `#trusted-loader` subtree and tag #trusted-loader on each, so+ // detectExecutableSurface's loader-aware filter accepts them.+ await page.evaluate(() => {+ let trusted = document.getElementById('trusted-loader');+ if (!trusted) {+ trusted = document.createElement('div');+ trusted.id = 'trusted-loader';+ trusted.style.display = 'none';+ document.body.appendChild(trusted);+ }+ for (const s of Array.from(document.querySelectorAll('script'))) {+ // Only wrapper scripts we created (have inline text body); move+ // them out of the test-target's tree.+ const hasInline = (s.textContent || '').trim().length > 0;+ if (hasInline) {+ trusted.appendChild(s);+ }+ }+ // Record the EXACT loader nodes. The scanner's trust check is+ // identity-based against this list, never an `id === 'trusted-loader'`+ // ancestor walk — a payload can inject its own element carrying that+ // ID into #mxss-target, and an ID-based check would then treat the+ // scripts inside it as trusted.+ window.__mxssTrustedLoaders = Array.from(trusted.querySelectorAll('script'));+ });++ // Confirm the production surface is in place. If this throws, the test+ // bed is broken and we want a clear failure, not mysterious null refs.+ const surface = await page.evaluate(() => ({+ hasSafeSetInnerHTML: typeof window.safeSetInnerHTML === 'function',+ hasSafeSetHTML: typeof window.safeSetHTML === 'function',+ hasRenderMarkdown:+ typeof (window.ui && window.ui.renderMarkdown) === 'function',+ hasSanitizeHtml: typeof window.sanitizeHtml === 'function',+ hasDOMPurify: typeof window.DOMPurify !== 'undefined',+ domPurifyVersion:+ window.DOMPurify && (window.DOMPurify.version || 'unknown'),+ hasMarked: typeof window.marked !== 'undefined',+ hasKatex: typeof window.katex !== 'undefined',+ hasMarkedKatex: typeof window.markedKatex !== 'undefined',+ katexVersion: window.katex && (window.katex.version || 'unknown'),+ }));+ expect(surface.hasSafeSetInnerHTML, 'production safeSetInnerHTML did not attach').toBe(true);+ expect(surface.hasSafeSetHTML, 'production safeSetHTML did not attach').toBe(true);+ expect(+ surface.hasRenderMarkdown,+ 'production window.ui.renderMarkdown did not attach'+ ).toBe(true);+ expect(surface.hasSanitizeHtml, 'production sanitizeHtml did not attach').toBe(true);+ expect(surface.hasDOMPurify, 'DOMPurify did not load').toBe(true);+ // The rendering dependencies the application initialises. Without these the+ // benign math control would silently degrade to "supplied markup survives".+ expect(surface.hasMarked, 'marked did not load').toBe(true);+ expect(surface.hasKatex, 'katex did not load').toBe(true);+ expect(surface.hasMarkedKatex, 'marked-katex-extension did not load').toBe(true);++ // Clear targets between tests.+ await page.evaluate(() => {+ const t = document.getElementById('mxss-target');+ const b = document.getElementById('mxss-bypass-target');+ if (t) t.innerHTML = '';+ if (b) b.innerHTML = '';+ });+}++test.beforeEach(async ({ page }) => {+ page.dialogCount = 0;+ page.on('dialog', async (dialog) => {+ page.dialogCount += 1;+ console.error(+ `[mXSS spec] UNEXPECTED DIALOG: type=${dialog.type()} message=${dialog.message()}`+ );+ try { await dialog.dismiss(); } catch (_) { /* already auto-dismissed */ }+ });+});++test.afterEach(async ({ page }, testInfo) => {+ // Tests that deliberately trigger a dialog (negative control) opt out+ // via this annotation.+ const expectedAnnotation = testInfo.annotations.find(+ (a) => a.type === 'expectedDialogs' && a.description === 'true'+ );+ if (!expectedAnnotation) {+ expect(+ page.dialogCount,+ 'an alert(1) fired under the page — DOMPurify let an mXSS through'+ ).toBe(0);+ }+});++/**+ * Drives a payload through `setter`, then performs the reparse round-trip the+ * happy-dom suite cannot model:+ *+ * setter(payload)+ * → serialize the result+ * → parse into a fresh, DETACHED element+ * → serialize again → parse into a second fresh element+ * → walk EACH tree for executable surface+ *+ * The reparsed trees are deliberately never attached to the document. They are+ * scanned by walking the reparse root itself (`window.__mxssScan(root)`) rather+ * than by a document-wide query, because a document-scoped walk cannot see a+ * detached tree at all. Asserting only that the sentinel text survived would+ * say nothing about whether an executable surface came back after reparsing.+ *+ * Returns the sentinel texts plus the findings for all three trees.+ */+async function applyAndRoundTrip(page, setterName, rawPayload, targetElementId) {+ return page.evaluate(+ ({ setter, payload, targetId }) => {+ const target = document.getElementById(targetId);+ if (setter === 'safeSetInnerHTML') {+ window.safeSetInnerHTML(target, payload, true);+ } else if (setter === 'safeSetHTML') {+ window.safeSetHTML(target, payload);+ } else if (setter === 'renderMarkdown') {+ // renderMarkdown returns sanitized HTML; assigning innerHTML+ // triggers the same browser parser path production hits.+ const sanitized = window.ui.renderMarkdown(payload);+ // eslint-disable-next-line no-unsanitized/property -- intentional: exercising the renderer's own sanitized output through the browser parser, exactly as production does.+ target.innerHTML = sanitized;+ } else if (setter === 'sanitizeHtml') {+ // The raw DOMPurify wrapper the other setters delegate to.+ const sanitized = window.sanitizeHtml(payload);+ // eslint-disable-next-line no-unsanitized/property -- intentional: sanitizeHtml output through the browser parser, exactly as production does.+ target.innerHTML = sanitized;+ } else {+ throw new Error(`unknown setter: ${setter}`);+ }++ // Round trip: serialize → detached fresh element → reparse.+ const reparseHost = document.createElement('div');+ // eslint-disable-next-line no-unsanitized/property -- intentional: this is the serialize → reparse hop under test.+ reparseHost.innerHTML = target.outerHTML;+ const reparsedSerialized = reparseHost.innerHTML;+ const reparseHost2 = document.createElement('div');+ // eslint-disable-next-line no-unsanitized/property -- second reparse; a no-op for plain HTML serialization, which is what makes it a stability check.+ reparseHost2.innerHTML = reparsedSerialized;++ return {+ targetText: target.textContent,+ reparse1Text: reparseHost.textContent,+ reparse2Text: reparseHost2.textContent,+ // Walk every tree explicitly — the two reparse roots are+ // detached, so a document-scoped scan would miss them.+ targetSurface: window.__mxssScan(target),+ reparse1Surface: window.__mxssScan(reparseHost),+ reparse2Surface: window.__mxssScan(reparseHost2),+ };+ },+ { setter: setterName, payload: rawPayload, targetId: targetElementId }+ );+}++/**+ * Every production path that turns untrusted text into DOM. `sanitizeHtml` is+ * the DOMPurify wrapper the other setters delegate to; driving it with the+ * same payloads is what makes its config a tested contract rather than an+ * implementation detail.+ */+const SETTERS = ['safeSetInnerHTML', 'safeSetHTML', 'renderMarkdown', 'sanitizeHtml'];++for (const vector of Object.values(VECTORS)) {+ for (const setter of SETTERS) {+ test(`mXSS via ${setter}: ${vector.family}`, async ({ page }) => {+ await loadFixture(page);+ const result = await applyAndRoundTrip(page, setter, vector.payload, 'mxss-target');++ expect(+ result.targetText,+ `sentinel "${vector.sentinel}" must survive ${setter}`+ ).toContain(vector.sentinel);+ expect(+ result.reparse1Text,+ `sentinel "${vector.sentinel}" must survive the first reparse`+ ).toContain(vector.sentinel);+ expect(+ result.reparse2Text,+ `sentinel "${vector.sentinel}" must survive the second reparse`+ ).toContain(vector.sentinel);++ // All three trees get the full invariant set: the post-render DOM+ // and BOTH reparsed roots (which are detached and therefore walked+ // directly by the scanner).+ for (const [tree, surface] of [+ ['post-render', result.targetSurface],+ ['first reparse', result.reparse1Surface],+ ['second reparse', result.reparse2Surface],+ ]) {+ expect(+ surface.badElements,+ `${tree} (${setter}): executable elements survived sanitization`+ ).toEqual([]);+ expect(+ surface.badAttrs,+ `${tree} (${setter}): on*= event-handler attributes survived sanitization`+ ).toEqual([]);+ expect(+ surface.badHrefs,+ `${tree} (${setter}): javascript:/vbscript:/data:text/html hrefs survived sanitization`+ ).toEqual([]);+ }+ });+ }+}++test('production dependency versions match the root lockfile', async ({ page }) => {+ // Regression guard for the review finding at 933587fb3: an earlier revision+ // loaded `marked` from the Playwright package's own dev deps (15.x) while+ // the root lockfile resolves 18.x, so the suite exercised a parser the+ // application does not ship. Drift must fail here rather than silently+ // change what is under test.+ for (const name of Object.keys(LOCKED_VERSIONS)) {+ const manifest = path.join(APP_NODE_MODULES, name, 'package.json');+ const installed = JSON.parse(fs.readFileSync(manifest, 'utf8')).version;+ expect(+ installed,+ `installed ${name} must match the version the root lockfile resolves`+ ).toBe(LOCKED_VERSIONS[name]);+ }++ // Every dependency build must resolve inside the repository root, never+ // inside the Playwright package's own node_modules.+ for (const depPath of [DOMPURIFY_PATH, MARKED_PATH, KATEX_PATH, MARKED_KATEX_PATH]) {+ expect(+ depPath.startsWith(APP_NODE_MODULES + path.sep),+ `${depPath} must be loaded from the repository root node_modules`+ ).toBe(true);+ }++ // The two libraries that expose a version at runtime must agree with the+ // lockfile inside the browser as well.+ await loadFixture(page);+ const runtime = await page.evaluate(() => ({+ dompurify: window.DOMPurify && window.DOMPurify.version,+ katex: window.katex && window.katex.version,+ }));+ expect(+ runtime.dompurify,+ 'DOMPurify runtime version must match the root lockfile'+ ).toBe(LOCKED_VERSIONS.dompurify);+ expect(runtime.katex, 'KaTeX runtime version must match the root lockfile').toBe(+ LOCKED_VERSIONS.katex+ );+});++test('benign renderer control: GFM table and real TeX render through renderMarkdown', async ({ page }) => {+ await loadFixture(page);++ // The math below is TeX, not pre-built MathML. `marked-katex-extension` is+ // registered on `marked` exactly as app.js does it, so this drives the+ // application's own TeX → KaTeX pipeline. Handing the renderer ready-made+ // MathML would only prove that markup the test itself wrote survives, and+ // could not catch a failure to generate math at all.+ //+ // This is the positive half of the foreign-content story: `renderMarkdown`+ // runs the permissive DOMPurify configuration (default allow-list plus+ // `semantics`/`annotation`), which is what keeps real Markdown — including+ // the MathML KaTeX emits — alive. The strict `SANITIZE_CONFIG` used by+ // `safeSetInnerHTML` and `sanitizeHtml` strips the same tags; that strip is+ // pinned separately in the strict-allow-list audit below.+ const markdown = [+ 'GFM table:',+ '',+ '| Col A | Col B |',+ '| --- | --- |',+ '| a1 | b1 |',+ '',+ 'Inline math: $\\sqrt{x^2}$ and a display equation:',+ '',+ '$$\\int_0^1 x^2 \\, dx$$',+ '',+ 'Inline [external link](https://example.com/path).',+ '',+ 'A *emphasis* and a **strong** word.',+ '',+ ].join('\n');++ const result = await page.evaluate((md) => {+ const target = document.getElementById('mxss-target');+ const sanitized = window.ui.renderMarkdown(md);+ // eslint-disable-next-line no-unsanitized/property -- intentional: renderMarkdown's own sanitized output is the subject under test.+ target.innerHTML = sanitized;++ // Same serialize → reparse round trip the vector loop uses, so the+ // preservation claim covers the reparsed tree too.+ const reparseHost = document.createElement('div');+ // eslint-disable-next-line no-unsanitized/property -- serialize → reparse hop under test.+ reparseHost.innerHTML = target.outerHTML;++ return {+ firstRender: sanitized,+ reparsed: reparseHost.innerHTML,+ reparsedText: reparseHost.textContent,+ reparseSurface: window.__mxssScan(reparseHost),+ };+ }, markdown);++ for (const [tree, html] of [['first render', result.firstRender], ['reparse', result.reparsed]]) {+ // GFM table survived.+ expect(html, `${tree}: GFM table must survive renderMarkdown`).toMatch(/<table[\s>]/i);+ expect(html, `${tree}: table header cell must survive`).toMatch(/<th[\s>]*>\s*Col A\s*<\/th>/i);+ expect(html, `${tree}: table body cell must survive`).toMatch(/<td[\s>]*>\s*a1\s*<\/td>/i);++ // KaTeX actually rendered the TeX — the generated foreign content, not+ // markup we supplied.+ expect(html, `${tree}: KaTeX must have produced its wrapper`).toMatch(/class="katex"/i);+ expect(html, `${tree}: KaTeX MathML must survive the renderer`).toMatch(/<math[\s>]/i);+ expect(html, `${tree}: <semantics> must survive the renderer`).toMatch(/<semantics[\s>]/i);+ expect(html, `${tree}: the display equation must produce a sqrt/radical node`).toMatch(/<msqrt[\s>]/i);+ expect(+ html,+ `${tree}: the TeX source must be carried through in <annotation>`+ ).toMatch(/<annotation[^>]*encoding="application\/x-tex"[^>]*>[^<]*\\sqrt/i);++ // Ordinary Markdown still works alongside the foreign content.+ expect(html, `${tree}: emphasis must survive`).toMatch(/<em>emphasis<\/em>/);+ expect(html, `${tree}: strong must survive`).toMatch(/<strong>strong<\/strong>/);+ expect(+ html,+ `${tree}: renderMarkdown's link rewriter must add target=_blank + rel=noopener`+ ).toMatch(/<a [^>]*target="_blank"[^>]*rel="[^"]*noopener[^"]*"[^>]*href="https:\/\/example\.com\/path"/i);+ }++ // Preserving foreign content must not have preserved an executable surface+ // with it.+ expect(+ result.reparseSurface.badElements,+ 'reparsed benign tree must not contain executable elements'+ ).toEqual([]);+ expect(+ result.reparseSurface.badAttrs,+ 'reparsed benign tree must not contain on*= handlers'+ ).toEqual([]);+ expect(+ result.reparseSurface.badHrefs,+ 'reparsed benign tree must not contain executable hrefs'+ ).toEqual([]);+});++test('benign renderer control (additional): pre-built MathML supplied as markup survives', async ({ page }) => {+ // Additional coverage kept alongside the TeX control above. This asserts the+ // narrower property that MathML written directly into the source survives+ // the renderer and the reparse — useful when the KaTeX pipeline changes,+ // but on its own it cannot show that math is generated.+ await loadFixture(page);++ const markdown = [+ '<span class="katex"><span class="katex-mathml">'+ + '<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow>'+ + '<msqrt><mi>x</mi></msqrt></mrow>'+ + '<annotation encoding="application/x-tex">\\sqrt{x}</annotation>'+ + '</semantics></math></span></span>',+ '',+ ].join('\n');++ const result = await page.evaluate((md) => {+ const target = document.getElementById('mxss-target');+ const sanitized = window.ui.renderMarkdown(md);+ // eslint-disable-next-line no-unsanitized/property -- intentional: renderMarkdown's sanitized output is the subject under test.+ target.innerHTML = sanitized;+ const reparseHost = document.createElement('div');+ // eslint-disable-next-line no-unsanitized/property -- serialize → reparse hop under test.+ reparseHost.innerHTML = target.outerHTML;+ return {+ reparsed: reparseHost.innerHTML,+ reparseSurface: window.__mxssScan(reparseHost),+ };+ }, markdown);++ expect(result.reparsed, 'supplied MathML <math> must survive').toMatch(/<math[\s>]/i);+ expect(result.reparsed, 'supplied <semantics> must survive').toMatch(/<semantics[\s>]/i);+ expect(result.reparsed, 'supplied <msqrt> must survive').toMatch(/<msqrt[\s>]/i);+ expect(result.reparsed, 'supplied <annotation> must survive').toMatch(/<annotation[\s>]/i);+ expect(+ result.reparseSurface.badElements,+ 'reparsed tree must not contain executable elements'+ ).toEqual([]);+ expect(result.reparseSurface.badAttrs).toEqual([]);+ expect(result.reparseSurface.badHrefs).toEqual([]);+});++test('strict allow-list audit: safeSetInnerHTML and sanitizeHtml both strip table/svg/math', async ({ page }) => {+ // The strict `SANITIZE_CONFIG` is allowed to strip table/svg/math, and it+ // must keep doing so — these two entry points are the ones that receive+ // untrusted HTML, as opposed to `renderMarkdown`, whose permissive config+ // is asserted to PRESERVE the same tags in the benign renderer control+ // above. Pinning both directions is what makes either claim meaningful:+ // a strip-only suite cannot tell "correctly rejected" from "never+ // produced", and a preserve-only suite cannot tell "correctly kept" from+ // "never sanitized".+ await loadFixture(page);++ const benign = [+ '<h1>Heading preserved</h1>',+ '<p>Inline <em>em</em>, <strong>strong</strong>, '+ + '<a href="https://example.com/path">external link</a> stays.</p>',+ '<p>But <table><tbody><tr><td>table</td></tr></tbody></table>, '+ + '<svg><circle r="1"></circle></svg> and '+ + '<math><mrow><mi>x</mi></mrow></math> are stripped by the strict allow-list.</p>',+ ].join('');++ const results = await page.evaluate((payload) => {+ const target = document.getElementById('mxss-target');++ window.safeSetInnerHTML(target, payload, true);+ const viaSetter = target.innerHTML;++ const viaSanitizeHtml = window.sanitizeHtml(payload);++ return { viaSetter, viaSanitizeHtml };+ }, benign);++ for (const [entryPoint, html] of [+ ['safeSetInnerHTML', results.viaSetter],+ ['sanitizeHtml', results.viaSanitizeHtml],+ ]) {+ expect(html, `${entryPoint}: heading preserved`).toMatch(/<h1>Heading preserved<\/h1>/);+ expect(html, `${entryPoint}: em preserved`).toMatch(/<em>em<\/em>/);+ expect(html, `${entryPoint}: strong preserved`).toMatch(/<strong>strong<\/strong>/);+ expect(html, `${entryPoint}: safe external href preserved`).toMatch(+ /href="https:\/\/example\.com\/path"/+ );+ expect(html, `${entryPoint}: table stripped by the strict allow-list`).not.toMatch(/<table[\s>]/);+ expect(html, `${entryPoint}: svg stripped by the strict allow-list`).not.toMatch(/<svg[\s>]/);+ expect(html, `${entryPoint}: math stripped by the strict allow-list`).not.toMatch(/<math[\s>]/);+ }++ const surface = await detectExecutableSurface(page);+ expect(surface.badElements).toEqual([]);+ expect(surface.badAttrs).toEqual([]);+ expect(surface.badHrefs).toEqual([]);+});++test('detector self-test: a deliberate bypass yields the findings the safety assertions reject', async ({ page }) => {+ // The vector loop's assertions are only meaningful if the scanner would+ // actually report the surfaces they forbid. This control bypasses every+ // sanitizer and asserts the scanner flags each class the loop checks —+ // including the `script[src]` / `iframe[src]` cases that an earlier+ // revision of the detector accepted because it trusted a non-empty `src`.+ //+ // If this ever comes back empty, the loop's assertions are vacuous and+ // that is the bug, not a green suite.+ //+ // Inert by construction: `about:blank` avoids outbound requests, scripts+ // inserted via innerHTML do not execute, the onerror handler is `void 0`,+ // and the javascript: href is never clicked.+ await loadFixture(page);++ const findings = await page.evaluate(() => {+ const target = document.getElementById('mxss-bypass-target');+ // eslint-disable-next-line no-unsanitized/property -- bypass control: every sanitizer is deliberately skipped here.+ target.innerHTML = [+ '<script src="about:blank"></script>',+ '<iframe src="about:blank"></iframe>',+ '<img src=x onerror="void 0">',+ '<a href="javascript:void 0">x</a>',+ ].join('');+ return window.__mxssScan(target);+ });++ expect(+ findings.badElements.map((e) => e.tag).sort(),+ 'scanner must flag script[src] and iframe[src] inside a payload root'+ ).toEqual(['iframe', 'script']);+ expect(+ findings.badElements.map((e) => e.src),+ 'the src-only cases must be reported with their src recorded'+ ).toEqual(['about:blank', 'about:blank']);+ expect(+ findings.badElements.every((e) => e.inTargetContainer === true),+ 'both findings must be attributed to the payload root'+ ).toBe(true);+ expect(+ findings.badAttrs.some((a) => /^on/i.test(a.name)),+ 'scanner must flag on*= handler attributes'+ ).toBe(true);+ expect(+ findings.badHrefs.some((h) => /^javascript:/i.test(h.href)),+ 'scanner must flag javascript: hrefs'+ ).toBe(true);+});++test('detector self-test: a forged trusted-loader id does not grant script trust', async ({ page }) => {+ // Trust must not be inferable from an ancestor's `id`. A payload can inject+ // `<div id="trusted-loader"><script …></div>` into a payload root, and an+ // ID-based ancestor walk would wave that script straight through — in the+ // live document and in a detached reparse tree alike. Trust is identity+ // against the nodes loadFixture() recorded, so a forged id buys nothing.+ //+ // Inert by construction: `about:blank` src, and scripts inserted via+ // innerHTML do not execute.+ await loadFixture(page);++ const findings = await page.evaluate(() => {+ const target = document.getElementById('mxss-bypass-target');+ target.innerHTML =+ '<div id="trusted-loader"><script src="about:blank"></script></div>';++ // A detached reparse tree carrying the same forged ancestor id.+ const host = document.createElement('div');+ // eslint-disable-next-line no-unsanitized/property -- serialize → reparse hop under test.+ host.innerHTML = target.outerHTML;++ return {+ payloadRoot: window.__mxssScan(target),+ detachedReparse: window.__mxssScan(host),+ // The whole-document scan opts into the loader exception, so this+ // also proves the exception stayed narrow: only the recorded+ // loader nodes are exempt, not anything wearing the id.+ documentScan: window.__mxssScan(document, { trustLoaders: true }),+ };+ });++ expect(+ findings.payloadRoot.badElements.map((e) => e.tag),+ 'a forged trusted-loader id must NOT grant script trust in a payload root'+ ).toContain('script');+ expect(+ findings.payloadRoot.badElements.map((e) => e.src),+ 'the forged-id script must be reported with its src recorded'+ ).toContain('about:blank');+ expect(+ findings.payloadRoot.badElements.every((e) => e.inTargetContainer === true),+ 'the finding must be attributed to the payload root'+ ).toBe(true);+ expect(+ findings.detachedReparse.badElements.map((e) => e.tag),+ 'the forged id must not grant trust in a detached reparse tree either'+ ).toContain('script');+ expect(+ findings.documentScan.badElements.map((e) => e.tag),+ 'even the loader-exception scan must report it — only recorded loader nodes are trusted'+ ).toContain('script');+});++test('negative control: bypassing safeSetInnerHTML fires an alert(1) we actually catch', async ({ page }) => {+ // Strengthens the negative control so a silently-broken DOMPurify load+ // cannot make the suite pass. We:+ // 1. Render an unsanitized hostile payload directly via innerHTML.+ // 2. Verify the browser DID parse an executable surface (onerror=+ // survived serializing).+ // 3. Force the onerror handler to run by dispatching an error event and+ // asserting the polyfilled window.alert received the exact message+ // the bypass handler was told to send.+ test.setTimeout(10000);+ test.info().annotations.push({ type: 'expectedDialogs', description: 'true' });+ await loadFixture(page);++ // `beforeEach` already reset page.dialogCount and installed the+ // 'dialog' listener for this test. Here we polyfill window.alert+ // instead, so the alert the bypass handler calls is directly+ // observable without depending on a real browser dialog.++ // Set the bypass via DOM (no Sanitizer in the path). Use an inline+ // event-handler that calls window.____mxssDialogMarker() so the spec+ // doesn't depend on the browser's default behavior of an onerror calling+ // alert — this guarantees the dialog fires regardless of image-load+ // timing.+ await page.evaluate(() => {+ // Stash a marker so the dialog handler can compare its message.+ window.__mxssProbeMessage = 'mxss-bypass-probe';+ window.alert = (msg) => {+ // We never reach the real DOMPurify survival path here — this+ // alert is fired by the unsanitized bypass handler. Counting+ // it is the negative control's whole point.+ if (msg === window.__mxssProbeMessage) window.__mxssDialogFired = true;+ };+ const target = document.getElementById('mxss-bypass-target');+ target.innerHTML =+ '<img src=x onerror="window.alert(window.__mxssProbeMessage)">';+ // Force the onerror handler to run by dispatching an Event. The+ // browser's natural "missing image fires onerror" timing is+ // variable across Chromium/Firefox/WebKit; dispatching explicitly+ // makes the negative control deterministic.+ const img = target.querySelector('img');+ if (img) {+ img.dispatchEvent(new window.ErrorEvent('error', { message: 'bypass' }));+ }+ });+ // Give the dispatched event a tick.+ await page.waitForTimeout(50);++ // Verify the negative control actually executed: the dialogCount is+ // incremented by the outer page.on('dialog') listener if a real alert+ // dialog fires. Our polyfilled window.alert is a separate signal.+ const fired = await page.evaluate(() => window.__mxssDialogFired === true);+ expect(+ fired,+ 'bypass handler did NOT call window.alert — the negative control is broken'+ ).toBe(true);++ // Sanity check: an executable surface is still in the DOM (the bypass+ // rendered it because we never sanitized).+ const bypassHtml = await page.evaluate(+ () => document.getElementById('mxss-bypass-target').innerHTML+ );+ expect(+ bypassHtml,+ 'bypass produced an executable attribute (proves the test bed can produce the surface it defends against)'+ ).toMatch(/onerror=/);++ // Suppress the afterEach's dialog-count assertion for this test (annotated).+});