Locked-down page
+Nothing on this page may run a script.
+ +diff --git a/.gitignore b/.gitignore index bc32999..ea4e1c0 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,6 @@ a11y-regression.test.js .DS_Store .vscode/ .idea/ + +# Built by scripts/build-virtual-screen-reader.mjs (npm run build) +dist/ diff --git a/CLAUDE.md b/CLAUDE.md index d7eae1f..3dcb27b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1,15 +1,22 @@ # screen-reader-cli — project memory CLI for screen-reader accessibility testing. Three engines behind one -binary (`bin/cli.js`, commander, pure ESM, no build step): +binary (`bin/cli.js`, commander, pure ESM; one build step, `npm run build`, +run by `prepare` on `npm install`): 1. **scan** (`src/commands/scan.js` + `src/services/scanner.js`) — Playwright headless Chromium + axe-core + structure extraction → text/JSON/HTML report, generated tests, AI analysis. Detection is **axe-only**; rule ids in reports are axe ids (`image-alt`, `heading-order`, …). 2. **virtual** (`page`/`nav`/`speak`/`audit`/`repl` via `src/bridge.js`) — - Virtual Screen Reader bundle injected into the page (`bypassCSP: true` - is required for this). + Guidepup's Virtual Screen Reader, built into `dist/` as one plain script + (`scripts/build-virtual-screen-reader.mjs`) and evaluated into the page by + `injectVirtualScreenReader` — the one loader; every command goes through + it. Over the DevTools protocol, like `@axe-core/playwright` loads axe, so + it runs on pages whose CSP forbids scripts. Never switch the page's CSP + off to make it load. Pinned to exactly 0.32.1: 0.33.0's `start()` opens a + user-event session that can break typing in a React input + (guidepup/virtual-screen-reader#250). 3. **live** (`src/live-bridge.js`) — Guidepup drives VoiceOver (macOS) / NVDA (Windows). Throws on Linux by design. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ea2bba8..ad25a40 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -42,7 +42,9 @@ the one-time `npx @guidepup/setup`. ## Guidelines -- Keep the code style of the surrounding files (ES modules, no build step). +- Keep the code style of the surrounding files (ES modules). The one build + step, `npm run build`, bundles Guidepup's virtual screen reader into `dist/` + and runs by itself on `npm install`. - Add or update a test for behavior changes — `test/scan.test.js` shows the pattern of invoking the CLI as a subprocess against a fixture. - Never build a path into `node_modules` by hand. Use `resolveBundledAsset()` diff --git a/README.md b/README.md index c3d9991..898ae9e 100644 --- a/README.md +++ b/README.md @@ -587,10 +587,11 @@ The Virtual Screen Reader implements the same [W3C accessibility specifications] logged-in sessions included. Only use it against URLs you trust (your own staging environments); scanning an untrusted site with it exposes your authenticated sessions to that site. -- **Virtual mode disables the target page's Content-Security-Policy** - (`bypassCSP`) so the Virtual Screen Reader bundle can be injected. This is - required for the tool to work on CSP-strict sites, but it means the page runs - with weaker protections during the scan — again, point it at pages you trust. +- **The target page's Content-Security-Policy stays on.** The Virtual Screen + Reader is loaded as one plain script over the browser's DevTools protocol, + the way Deque's `@axe-core/playwright` loads axe, which a page's policy does + not govern — so it works on pages that forbid every script, and the page + runs with all of its own protections. - AI analysis (`--ai`) sends scan results (violation messages, selectors, page title/URL) to the provider you select. Use `--provider ollama` to keep everything local. diff --git a/docs/DEVLOG.md b/docs/DEVLOG.md index 48fd4b3..ab37c6c 100644 --- a/docs/DEVLOG.md +++ b/docs/DEVLOG.md @@ -6,6 +6,60 @@ Newest entries first. --- +## 2026-10-17 — 0.13.0: the page's security policy stays on + +A page can tell the browser which scripts it may run, with a +Content-Security-Policy. A strict one says "none from anywhere". Until now, +every virtual screen reader command asked the browser to ignore that policy, +because the reader was loaded as a JavaScript module, and a strict policy +refuses modules. So on a locked-down site the tool quietly switched the lock +off, and the page ran with weaker protections than any visitor gets. + +Now the lock stays on. Example, a page served with +`default-src 'none'; script-src 'none'`: + +- before (lock left on): `audit` stops at "Failed to fetch dynamically + imported module", and `scan` findings lose what the screen reader says; +- after: `audit` reads "main", "heading, Locked-down page, level 1", the + paragraph and "button"; `scan` says `screen reader says "button"` for the + nameless button. + +How: Guidepup's virtual screen reader is built into one plain script at +install time (`scripts/build-virtual-screen-reader.mjs`, esbuild, +`format: "iife"`) and shipped in the package as +`dist/guidepup-virtual-screen-reader.js`, with Guidepup's MIT licence beside +it. The CLI hands that script to the browser through the DevTools protocol +(`page.evaluate(source)`), which a page's policy does not govern. This is how +Deque's official `@axe-core/playwright` loads axe (`page.evaluate(this.script())`), +and how our own scan already loaded axe. One loader, `injectVirtualScreenReader` +in `src/bridge.js`, serves every command: `audit`, `page`, `nav`, `speak`, +`repl`, `screenshot`, `scan`, the dashboard and the MCP server. +`test/csp.test.js` serves the strict page over a local HTTP server and checks +`audit`, `scan` and `--record`; it fails on the old loader. + +Also fixed under the same policy: the `--record` overlay drew its box and +caption with inline `style="…"` markup, which a strict policy drops, so the +video lost its highlight. It now sets the same styles from script. + +**Pinned to Guidepup's virtual screen reader 0.32.1, exactly.** In 0.33.0, +`start()` opens a lasting user-event session that can break typing in a React +input afterwards +([guidepup/virtual-screen-reader#250](https://github.com/guidepup/virtual-screen-reader/issues/250)), +and this tool reads people's real pages. In 0.32.1 `start()` only sets up the +container, a page observer and a focus listener; the test suite checks that the +shipped script never calls `setup(`. The header-inside-a-card fix from +2026-09-27 is already in 0.32.1 (its test still passes). + +What changes for users: pages behave as they do for real visitors. A page whose +own policy blocks one of its scripts now has that script blocked under the tool +too, instead of running only during a scan. + +For contributors: there is now one build step, `npm run build`. It runs by +itself on `npm install` (`prepare`) and before `npm publish`, so nobody needs +to remember it; installing the published package never runs it. + +--- + ## 2026-10-16 — 0.12.0: live mode on macOS 27 Live mode could not start VoiceOver on macOS 27. Apple removed the @@ -139,7 +193,7 @@ in the terminal, the `--visual` report and `--json` (`element.announcement`). Entry below. Behaviour to know before upgrading: `scan` and the dashboard now open pages -with CSP bypassed, as `audit` always has, and each scan adds one virtual +with CSP bypassed, as `audit` always has (removed in 0.13.0, see that entry), and each scan adds one virtual screen reader walk of the page (capped at 2,000 steps / 15 s). --- @@ -171,8 +225,9 @@ saw. Four outcomes, each in `element.announcement`: Bounded twice, per the rule for waits in the pipeline: 2,000 steps or 15 s inside the page, and a 20 s wall-clock race outside it. Any failure leaves the findings without announcements rather than failing the scan. `scan` and the -dashboard now open their context with `bypassCSP: true`, as `audit` always -has — the bundle loads as a module and a strict CSP refused it. +dashboard now open their context with Playwright's CSP bypass switched on, as +`audit` always has — the bundle loads as a module and a strict CSP refused it. +(0.13.0 loads it as a plain script instead and the bypass is gone.) `test/fixtures/announce-edge.html` has a strict CSP and the img-in-button case. --- @@ -740,8 +795,9 @@ elements. Changes: documented around. - **Dogfooding as proof:** the site must pass `--fail-on minor`; the publish workflow must pass the full suite. The tool vouches for itself. -- **Known trade-offs:** `bypassCSP` is required for the virtual screen - reader injection (documented in README security notes); `--chrome-profile` +- **Known trade-offs:** switching off the page's CSP is required for the + virtual screen reader injection (documented in README security notes; no + longer true from 0.13.0, which loads it as a plain script); `--chrome-profile` reuses the real logged-in Chrome profile (only scan trusted URLs). ### Where things stand at end of session diff --git a/package-lock.json b/package-lock.json index e46b673..2bcd328 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,16 @@ { "name": "screen-reader-cli", - "version": "0.12.0", + "version": "0.13.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "screen-reader-cli", - "version": "0.12.0", + "version": "0.13.0", "license": "MIT", "dependencies": { "@guidepup/guidepup": "^0.36.3", - "@guidepup/virtual-screen-reader": "^0.33.0", + "@guidepup/virtual-screen-reader": "0.32.1", "axe-core": "^4.11.2", "commander": "^13.0.0", "playwright": "^1.52.0" @@ -20,6 +20,9 @@ "screenreader": "bin/cli.js", "sr": "bin/sr.js" }, + "devDependencies": { + "esbuild": "^0.28.2" + }, "engines": { "node": ">=20" } @@ -56,6 +59,448 @@ "node": ">=6.9.0" } }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz", + "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz", + "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz", + "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz", + "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz", + "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz", + "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz", + "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz", + "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz", + "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz", + "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz", + "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz", + "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz", + "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz", + "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz", + "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz", + "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz", + "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz", + "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz", + "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz", + "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz", + "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz", + "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz", + "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz", + "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz", + "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz", + "integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, "node_modules/@guidepup/guidepup": { "version": "0.36.3", "resolved": "https://registry.npmjs.org/@guidepup/guidepup/-/guidepup-0.36.3.tgz", @@ -68,9 +513,9 @@ } }, "node_modules/@guidepup/virtual-screen-reader": { - "version": "0.33.0", - "resolved": "https://registry.npmjs.org/@guidepup/virtual-screen-reader/-/virtual-screen-reader-0.33.0.tgz", - "integrity": "sha512-CXMkIx19oFZ7BS7TcvDibXn6A2RsEs8nVX9IijHW7HQ0qTGHgc2WErkaMi12YPQi1Zypxr36NfRYWgBxUNvMtQ==", + "version": "0.32.1", + "resolved": "https://registry.npmjs.org/@guidepup/virtual-screen-reader/-/virtual-screen-reader-0.32.1.tgz", + "integrity": "sha512-QoxyFmIkE4em+Up0VJ+cchpVndHbwMGUztbTWasNbjWfxT28Sf5XYq3U+ueM1sENk7AV4+0N7ahoYxjcFVAo8w==", "license": "MIT", "dependencies": { "@testing-library/dom": "^10.4.0", @@ -230,6 +675,48 @@ "integrity": "sha512-vdnCeZD+3wZ+8h8xXL/ZtBlvvoobOFyPzSiIfO6sGOZDqjFx4aLMAjZhl4rawj5xYz3UwP6Tgvyh0iH4IOCVnQ==", "license": "MIT" }, + "node_modules/esbuild": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz", + "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.28.2", + "@esbuild/android-arm": "0.28.2", + "@esbuild/android-arm64": "0.28.2", + "@esbuild/android-x64": "0.28.2", + "@esbuild/darwin-arm64": "0.28.2", + "@esbuild/darwin-x64": "0.28.2", + "@esbuild/freebsd-arm64": "0.28.2", + "@esbuild/freebsd-x64": "0.28.2", + "@esbuild/linux-arm": "0.28.2", + "@esbuild/linux-arm64": "0.28.2", + "@esbuild/linux-ia32": "0.28.2", + "@esbuild/linux-loong64": "0.28.2", + "@esbuild/linux-mips64el": "0.28.2", + "@esbuild/linux-ppc64": "0.28.2", + "@esbuild/linux-riscv64": "0.28.2", + "@esbuild/linux-s390x": "0.28.2", + "@esbuild/linux-x64": "0.28.2", + "@esbuild/netbsd-arm64": "0.28.2", + "@esbuild/netbsd-x64": "0.28.2", + "@esbuild/openbsd-arm64": "0.28.2", + "@esbuild/openbsd-x64": "0.28.2", + "@esbuild/openharmony-arm64": "0.28.2", + "@esbuild/sunos-x64": "0.28.2", + "@esbuild/win32-arm64": "0.28.2", + "@esbuild/win32-ia32": "0.28.2", + "@esbuild/win32-x64": "0.28.2" + } + }, "node_modules/fsevents": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", diff --git a/package.json b/package.json index aa928e4..89a893c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "screen-reader-cli", - "version": "0.12.0", + "version": "0.13.0", "description": "Screen reader testing CLI — scan pages for accessibility violations with axe-core + Virtual Screen Reader, drive real VoiceOver/NVDA, generate regression tests", "type": "module", "bin": { @@ -11,6 +11,7 @@ "files": [ "bin/", "src/", + "dist/", "README.md", "LICENSE" ], @@ -18,7 +19,9 @@ "node": ">=20" }, "scripts": { - "test": "node --test --test-concurrency=1 test/update-notice.test.js test/scan.test.js test/commands.test.js test/bridge.test.js test/audit.test.js test/user-agent.test.js test/dependency-resolution.test.js test/daemon.test.js test/live.test.js test/ai-analyzer.test.js test/dashboard.test.js test/report.test.js test/baseline.test.js test/mcp.test.js", + "build": "node scripts/build-virtual-screen-reader.mjs", + "prepare": "npm run build", + "test": "node --test --test-concurrency=1 test/update-notice.test.js test/scan.test.js test/commands.test.js test/bridge.test.js test/audit.test.js test/csp.test.js test/user-agent.test.js test/dependency-resolution.test.js test/daemon.test.js test/live.test.js test/ai-analyzer.test.js test/dashboard.test.js test/report.test.js test/baseline.test.js test/mcp.test.js", "test:e2e": "node --test test/e2e.test.js" }, "keywords": [ @@ -45,10 +48,13 @@ "homepage": "https://github.com/Elizabeth1979/screen-reader-cli#readme", "dependencies": { "@guidepup/guidepup": "^0.36.3", - "@guidepup/virtual-screen-reader": "^0.33.0", + "@guidepup/virtual-screen-reader": "0.32.1", "axe-core": "^4.11.2", "commander": "^13.0.0", "playwright": "^1.52.0" }, - "license": "MIT" + "license": "MIT", + "devDependencies": { + "esbuild": "^0.28.2" + } } diff --git a/scripts/build-virtual-screen-reader.mjs b/scripts/build-virtual-screen-reader.mjs new file mode 100644 index 0000000..06c87d5 --- /dev/null +++ b/scripts/build-virtual-screen-reader.mjs @@ -0,0 +1,51 @@ +// Builds Guidepup's virtual screen reader as one plain script, for +// src/bridge.js to load into a page. Guidepup publishes its browser build as an +// ES module, and a page's Content-Security-Policy can refuse to import a +// module. A plain script evaluated over the DevTools protocol runs on any page, +// whatever its policy — which is how Deque's official @axe-core/playwright +// loads axe (`page.evaluate(this.script())`). +// +// Runs on `npm install` in a clone and before `npm pack` / `npm publish` +// (the `prepare` script), so the published package ships the built file and +// installing it never needs esbuild. +import { copyFileSync, mkdirSync, readFileSync } from "node:fs"; +import { createRequire } from "node:module"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +import { build } from "esbuild"; + +const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); +const require = createRequire(path.join(REPO, "package.json")); + +const entry = require.resolve("@guidepup/virtual-screen-reader/browser.js"); +const packageDir = path.dirname( + require.resolve("@guidepup/virtual-screen-reader/package.json"), +); +const { version } = JSON.parse( + readFileSync(path.join(packageDir, "package.json"), "utf-8"), +); +const outDir = path.join(REPO, "dist"); +const outfile = path.join(outDir, "guidepup-virtual-screen-reader.js"); + +// Must match VSR_GLOBAL in src/bridge.js. +const GLOBAL_NAME = "__srCliGuidepupVirtualScreenReader"; + +mkdirSync(outDir, { recursive: true }); +await build({ + entryPoints: [entry], + bundle: true, + format: "iife", + // The page sees it as one global, named so no page script uses it. + globalName: GLOBAL_NAME, + platform: "browser", + banner: { + js: `/* @guidepup/virtual-screen-reader ${version}, MIT License: see guidepup-virtual-screen-reader.LICENSE */`, + }, + outfile, + logLevel: "warning", +}); +copyFileSync( + path.join(packageDir, "LICENSE"), + path.join(outDir, "guidepup-virtual-screen-reader.LICENSE"), +); diff --git a/src/bridge.js b/src/bridge.js index 27c27ab..9184edd 100644 --- a/src/bridge.js +++ b/src/bridge.js @@ -1,29 +1,44 @@ import fs from "node:fs"; -import { - resolveBundledAsset, - resolveDeviceOptions, - resolveTarget, -} from "./util.js"; - -// Read the VSR browser bundle once at module load time -const VSR_BUNDLE_PATH = resolveBundledAsset( - "@guidepup/virtual-screen-reader/browser.js", +import { resolveDeviceOptions, resolveTarget } from "./util.js"; + +// Guidepup's virtual screen reader, built as one plain script by +// scripts/build-virtual-screen-reader.mjs (`npm run build`, run by `prepare`) +// and shipped in the package. Read once, on first use, so `--help` still works +// in a checkout where it has not been built. +const VSR_SCRIPT_URL = new URL( + "../dist/guidepup-virtual-screen-reader.js", + import.meta.url, ); -const VSR_BUNDLE = fs.readFileSync(VSR_BUNDLE_PATH, "utf-8"); +// Must match GLOBAL_NAME in the build script. +const VSR_GLOBAL = "__srCliGuidepupVirtualScreenReader"; +let vsrScript = null; +function readVirtualScreenReader() { + if (vsrScript === null) { + try { + vsrScript = fs.readFileSync(VSR_SCRIPT_URL, "utf-8"); + } catch (err) { + if (err.code !== "ENOENT") throw err; + throw new Error( + "The virtual screen reader has not been built. Run `npm install` " + + "(or `npm run build`) in this checkout.", + ); + } + } + return vsrScript; +} -// Load the Virtual Screen Reader into the page as window.__vsr. Imported as an -// ESM module from a blob URL, so the page's CSP must be bypassed -// (bypassCSP: true on the context) or the import is refused. +// Load the Virtual Screen Reader into the page as window.__vsr — the one +// loader every command uses. The plain script is evaluated over the DevTools +// protocol, which a page's Content-Security-Policy does not govern, so it runs +// on a page that forbids every script while the page's own policy stays on. +// Same approach as Deque's official @axe-core/playwright, which loads axe with +// `page.evaluate(this.script())` (and as runAxe in services/scanner.js). export async function injectVirtualScreenReader(page) { - await page.evaluate(async (bundleSource) => { - if (window.__vsrLoaded) return; - const blob = new Blob([bundleSource], { type: "application/javascript" }); - const blobUrl = URL.createObjectURL(blob); - const module = await import(blobUrl); - URL.revokeObjectURL(blobUrl); - window.__vsr = module.virtual; - window.__vsrLoaded = true; - }, VSR_BUNDLE); + if (await page.evaluate(() => Boolean(window.__vsr))) return; + await page.evaluate(readVirtualScreenReader()); + await page.evaluate((name) => { + window.__vsr = window[name].virtual; + }, VSR_GLOBAL); } export async function createBridge(browser, opts = {}) { @@ -31,7 +46,6 @@ export async function createBridge(browser, opts = {}) { // desktop user agent: the traversal comes back clean because the mobile // branch never rendered. `opts.device` / `opts.userAgent` override it. const context = await browser.newContext({ - bypassCSP: true, ...resolveDeviceOptions(opts), ...(opts.recordVideo ? { recordVideo: opts.recordVideo } : {}), }); diff --git a/src/commands/scan.js b/src/commands/scan.js index 99e597e..1026fd4 100644 --- a/src/commands/scan.js +++ b/src/commands/scan.js @@ -173,15 +173,11 @@ export function scanCommand() { // effect — and it cannot be verified in this suite, which is headless. context = await chromium.launchPersistentContext(profileDir, { headless: false, - bypassCSP: true, userAgent: deviceOpts.userAgent, }); } else { browser = await chromium.launch({ headless: true }); context = await browser.newContext({ - // The virtual screen reader is loaded as a module; a page's CSP - // would refuse it and findings would lose their announcements. - bypassCSP: true, // Defaults to a real Chrome UA, since some sites (e.g. // Cloudflare-protected staging environments) block Playwright's // headless default. --device / --user-agent override it. diff --git a/src/recorder.js b/src/recorder.js index 731e62a..96ec1bd 100644 --- a/src/recorder.js +++ b/src/recorder.js @@ -12,14 +12,37 @@ export function recordVideoOptions(dir, viewport = DEFAULT_VIEWPORT) { return { dir, size: viewport }; } +// Styled through element.style, never a style="" attribute: a page whose +// Content-Security-Policy refuses inline styles drops the attribute (the +// overlay would be an invisible, unstyled box), but it does not govern styles +// set from script. export async function attachOverlay(page) { await page.evaluate(() => { const overlay = document.createElement("div"); overlay.id = "__sr-record"; overlay.setAttribute("aria-hidden", "true"); - overlay.innerHTML = - '
' + - ''; + const box = document.createElement("div"); + box.setAttribute("data-box", ""); + Object.assign(box.style, { + position: "absolute", + border: "4px solid #fc0", + borderRadius: "4px", + pointerEvents: "none", + zIndex: "2147483646", + }); + const caption = document.createElement("div"); + caption.setAttribute("data-caption", ""); + Object.assign(caption.style, { + position: "fixed", + inset: "auto 0 0 0", + padding: "14px 20px", + background: "#000", + color: "#fc0", + font: "600 20px/1.35 system-ui,sans-serif", + borderTop: "3px solid #fc0", + zIndex: "2147483647", + }); + overlay.append(box, caption); document.documentElement.appendChild(overlay); }); } diff --git a/src/services/scanner.js b/src/services/scanner.js index c218282..375acdd 100644 --- a/src/services/scanner.js +++ b/src/services/scanner.js @@ -20,8 +20,6 @@ export async function scanUrl(url, { device } = {}) { try { const context = await browser.newContext({ ...resolveDeviceOptions({ device }), - // Lets the virtual screen reader load, for per-finding announcements. - bypassCSP: true, }); const page = await context.newPage(); await page.goto(resolveTarget(url), { @@ -367,7 +365,7 @@ async function attachAnnouncements(page, violations) { } else if (skippedSet.has(sel)) v.element.announcement = null; } } catch { - // CSP refused the bundle, the walk timed out, or the page navigated + // The screen reader failed to start, the walk timed out, or the page navigated // away. Findings stand without announcements. } } diff --git a/src/util.js b/src/util.js index bd0df12..4614027 100644 --- a/src/util.js +++ b/src/util.js @@ -18,7 +18,7 @@ const require = createRequire(import.meta.url); * * Node's own resolver knows where the file landed in every one of those * layouts, so ask it instead of guessing. Passing a package's public entry - * point ("@guidepup/virtual-screen-reader/browser.js") rather than an internal + * point ("axe-core/axe.min.js") rather than an internal * build path also means a dependency reorganising its own lib/ directory * cannot break us. */ diff --git a/test/csp.test.js b/test/csp.test.js new file mode 100644 index 0000000..76b8c1b --- /dev/null +++ b/test/csp.test.js @@ -0,0 +1,160 @@ +import { describe, it, before, after } from "node:test"; +import assert from "node:assert/strict"; +import { execFile } from "node:child_process"; +import fs from "node:fs"; +import http from "node:http"; +import path from "node:path"; +import { promisify } from "node:util"; +import { fileURLToPath } from "node:url"; +import { chromium } from "playwright"; +import { attachOverlay } from "../src/recorder.js"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const REPO = path.resolve(__dirname, ".."); +const CLI = path.join(REPO, "bin/cli.js"); + +// The strictest policy a page can send: no scripts at all, from anywhere. The +// virtual screen reader must still read such a page — it is loaded as a plain +// script over the DevTools protocol, which the page's policy does not govern, +// the same way @axe-core/playwright loads axe. Imported as an ES module instead, +// it is refused here and the reader never starts. +const STRICT_CSP = "default-src 'none'; script-src 'none'"; + +const PAGE = ` + +Nothing on this page may run a script.
+ +