From 870d55b44f32fede317e1f78489827ca4926d4ec Mon Sep 17 00:00:00 2001 From: Schluffe Date: Fri, 30 Oct 2026 13:39:16 +0100 Subject: [PATCH] Set Content-Security-Policy --- README.md | 3 ++- TODO.md | 7 +++++++ e2e.spec.ts | 11 +++++++++++ index.html | 4 ++++ package.json | 2 +- pnpm-lock.yaml | 44 ++++++++++++++++++++++---------------------- vite.config.ts | 21 ++++++++++++++++++++- 7 files changed, 67 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 98eb50f..e89ba2f 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,8 @@ Then serve the `dist/` folder, with these requirements: HellCon must reach it unchanged, so don't rewrite them to `index.html`. Recommended, as response headers, a `` can't set them (`index.html` sets -`Referrer-Policy: no-referrer` itself): +`Referrer-Policy: no-referrer` and the rest of its Content-Security-Policy itself, which keeps out +scripts and styles from elsewhere but allows connections to any tracker): ``` Content-Security-Policy: frame-ancestors 'none' diff --git a/TODO.md b/TODO.md index 2b7f631..78a868f 100644 --- a/TODO.md +++ b/TODO.md @@ -15,6 +15,13 @@ deployments on different versions, the receiver only sees "No sender with this code". Open how a mismatch would be reported, e.g. by trying the previous version's swarm too. +- Keep a transfer alive on phones: while a share waits or a transfer runs, hold a Screen Wake Lock + (`navigator.wakeLock.request('screen')`), else a phone that dims and locks suspends the tab and + the connection dies. The browser releases it whenever the page is hidden, so it has to be + requested again on `visibilitychange`. In the same phases, a `beforeunload` handler makes the + browser ask before a reload or a closed tab ends the share or transfer. Both go once it's done, + stopped or failed. + ## Open observations - Firefox ignores an errored service worker response stream: when the sender vanishes, the receiver diff --git a/e2e.spec.ts b/e2e.spec.ts index 17e3e00..a0b51f2 100644 --- a/e2e.spec.ts +++ b/e2e.spec.ts @@ -152,6 +152,7 @@ async function open( page.on('websocket', (ws) => ws.url().startsWith(base.replace('http', 'ws')) || trackers.push(ws)); page.on('pageerror', (error) => errors.push(`${name}: ${error.message}`)); await page.addInitScript(trackPeers); + await page.addInitScript(failOnCsp); if (init) await page.addInitScript(init); await page.goto(base); return page; @@ -187,6 +188,14 @@ async function hash(path: string) { const log = (page: Page) => page.locator('#log').textContent(); +// Init script for every page: whatever index.html's Content-Security-Policy blocks fails the test, the +// browsers only log it. +function failOnCsp() { + document.addEventListener('securitypolicyviolation', ({ violatedDirective, blockedURI }) => { + throw new Error(`Content-Security-Policy blocked ${blockedURI || 'inline'} by ${violatedDirective}`); + }); +} + // Init script for every page: keeps its peer connections in window.peers, so a test can check that // they were closed. It runs in the hidden frames too, only the app's own window counts. function trackPeers() { @@ -1241,7 +1250,9 @@ for (const name of Object.keys(BROWSERS)) { const [sender, receiver] = await Promise.all(profiles.map((profile) => profile.newPage())); for (const page of [sender!, receiver!]) { sockets.set(page, []); + page.on('pageerror', (error) => errors.push(`${name}: ${error.message}`)); await page.addInitScript(trackPeers); + await page.addInitScript(failOnCsp); } await Promise.all([sender!.goto(url), receiver!.goto(url)]); await transfer('offline', sender!, receiver!, source, LARGE_SIZE); diff --git a/index.html b/index.html index 6ae8026..8a1ad24 100644 --- a/index.html +++ b/index.html @@ -6,6 +6,10 @@ + HellCon diff --git a/package.json b/package.json index 0f46311..7ef5a3d 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "devDependencies": { "@biomejs/biome": "2.6.3", "@playwright/test": "^1.64.1", - "@types/node": "^26.6.5", + "@types/node": "^26.6.6", "@vitejs/plugin-vue": "^6.0.10", "typescript": "~6.0.3", "vite": "^8.3.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6064d86..b0f8748 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -177,20 +177,20 @@ importers: specifier: ^1.64.1 version: 1.64.1 '@types/node': - specifier: ^26.6.5 - version: 26.6.5 + specifier: ^26.6.6 + version: 26.6.6 '@vitejs/plugin-vue': specifier: ^6.0.10 - version: 6.0.10(vite@8.3.5(@types/node@26.6.5))(vue@3.5.44(typescript@6.0.3)) + version: 6.0.10(vite@8.3.5(@types/node@26.6.6))(vue@3.5.44(typescript@6.0.3)) typescript: specifier: ~6.0.3 version: 6.0.3 vite: specifier: ^8.3.5 - version: 8.3.5(@types/node@26.6.5) + version: 8.3.5(@types/node@26.6.6) vitest: specifier: ^5.0.5 - version: 5.0.5(@types/node@26.6.5)(vite@8.3.5(@types/node@26.6.5)) + version: 5.0.5(@types/node@26.6.6)(vite@8.3.5(@types/node@26.6.6)) vue-tsc: specifier: ^3.3.13 version: 3.3.13(typescript@6.0.3) @@ -409,11 +409,11 @@ packages: '@types/deep-eql@4.0.2': resolution: {integrity: sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==} - '@types/estree@1.0.9': - resolution: {integrity: sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==} + '@types/estree@1.0.10': + resolution: {integrity: sha512-lTyOMnOcmRBzdCv6/4zUZCrEhV0xrqQiUZBZtVcbmLHBXKLnN8q89v/wKRF/Q0HbwVsqbIG3o5HDoFl9sduM+g==} - '@types/node@26.6.5': - resolution: {integrity: sha512-uMzsv9stDdofIZBx4UGh4Z9ZgXnGC9seRDscilBcTHAeOFk67MZg/k9bWVfO+bk4qZmG+l52hRnLwUiH2LnJTg==} + '@types/node@26.6.6': + resolution: {integrity: sha512-MzQj+8Kx3XI40OmUAE6Qoh58ZMNstNNMwn2nfd3qWsfe7nDNSqJNzes4njhz6AHwrSHOffSLwXU6yb474Q4aog==} '@vitejs/plugin-vue@6.0.10': resolution: {integrity: sha512-b80aUQh3n+qEzzZm5oQp3rT2QImeCx58K6eFl/RkJ3d65KW0kTuv5lNsLRVAjbW9YRUpo/wzmvzMgV34bTAItA==} @@ -922,26 +922,26 @@ snapshots: '@types/deep-eql@4.0.2': {} - '@types/estree@1.0.9': {} + '@types/estree@1.0.10': {} - '@types/node@26.6.5': + '@types/node@26.6.6': dependencies: undici-types: 8.9.0 - '@vitejs/plugin-vue@6.0.10(vite@8.3.5(@types/node@26.6.5))(vue@3.5.44(typescript@6.0.3))': + '@vitejs/plugin-vue@6.0.10(vite@8.3.5(@types/node@26.6.6))(vue@3.5.44(typescript@6.0.3))': dependencies: '@rolldown/pluginutils': 1.0.1 - vite: 8.3.5(@types/node@26.6.5) + vite: 8.3.5(@types/node@26.6.6) vue: 3.5.44(typescript@6.0.3) - '@vitest/mocker@5.0.5(vite@8.3.5(@types/node@26.6.5))': + '@vitest/mocker@5.0.5(vite@8.3.5(@types/node@26.6.6))': dependencies: '@jridgewell/trace-mapping': 0.3.31 '@vitest/spy': 5.0.5 estree-walker: 3.0.3 magic-string: 1.4.5 optionalDependencies: - vite: 8.3.5(@types/node@26.6.5) + vite: 8.3.5(@types/node@26.6.6) '@vitest/spy@5.0.5': {} @@ -1041,7 +1041,7 @@ snapshots: estree-walker@3.0.3: dependencies: - '@types/estree': 1.0.9 + '@types/estree': 1.0.10 expect-type@1.4.0: {} @@ -1171,7 +1171,7 @@ snapshots: undici-types@8.9.0: {} - vite@8.3.5(@types/node@26.6.5): + vite@8.3.5(@types/node@26.6.6): dependencies: lightningcss: 1.33.0 picomatch: 4.0.7 @@ -1179,13 +1179,13 @@ snapshots: rolldown: 1.2.15 tinyglobby: 0.2.17 optionalDependencies: - '@types/node': 26.6.5 + '@types/node': 26.6.6 fsevents: 2.3.3 - vitest@5.0.5(@types/node@26.6.5)(vite@8.3.5(@types/node@26.6.5)): + vitest@5.0.5(@types/node@26.6.6)(vite@8.3.5(@types/node@26.6.6)): dependencies: '@types/chai': 5.2.3 - '@vitest/mocker': 5.0.5(vite@8.3.5(@types/node@26.6.5)) + '@vitest/mocker': 5.0.5(vite@8.3.5(@types/node@26.6.6)) chai: 6.3.0 es-module-lexer: 2.3.2 expect-type: 1.4.0 @@ -1196,10 +1196,10 @@ snapshots: tinybench: 6.2.2 tinyexec: 1.3.1 tinyglobby: 0.2.17 - vite: 8.3.5(@types/node@26.6.5) + vite: 8.3.5(@types/node@26.6.6) why-is-node-running: 3.2.1 optionalDependencies: - '@types/node': 26.6.5 + '@types/node': 26.6.6 transitivePeerDependencies: - msw diff --git a/vite.config.ts b/vite.config.ts index 788926a..2abb6c3 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -2,8 +2,27 @@ import vue from '@vitejs/plugin-vue'; import { defineConfig } from 'vite'; +const DEV_STYLE_SRC = "style-src 'self'"; + export default defineConfig({ - plugins: [vue()], + plugins: [ + vue(), + // index.html's Content-Security-Policy allows no inline styles, the dev server injects the CSS + // through JavaScript. A nonce lets only those style elements through, so the dev server enforces + // the same policy as the production build, which links the CSS as files. + { + name: 'dev-csp', + apply: 'serve', + config: () => ({ html: { cspNonce: 'dev' } }), + transformIndexHtml(html) { + if (!html.includes(DEV_STYLE_SRC)) throw new Error(`dev-csp: index.html has no "${DEV_STYLE_SRC}"`); + return html.replace(DEV_STYLE_SRC, `${DEV_STYLE_SRC} 'nonce-dev'`); + }, + }, + ], + // The dev server would inline small images like gitea.svg as data: URLs, which index.html's + // Content-Security-Policy blocks. The production build links them as files either way. + build: { assetsInlineLimit: 0 }, // Playwright runs e2e.spec.ts and backend/, which Vitest would pick up too. test: { include: ['src/**/*.test.ts'] }, });