Files
bchanot 7be94d4ce6 chore(docker): R2 application image with the Rust core — wasm stage, precompressed module, CSP proof on the image
Dockerfile stage 0 (rust:1.99.0-bookworm, wasm32 target, wasm-bindgen-cli
at the builder's pinned version, cargo cache mounts) builds the shipped
module with scripts/wasm-build.sh (WASM_BUILD_SHIPPED_ONLY=1); the Node
stage copies packages/core-wasm (manifest + pkg/); the runner gzips the
module and nginx serves it with gzip_static (application/wasm, 1,180,153
bytes raw, 372,275 served). docker/csp-proof/app-image.sh runs the real
image behind the DEPLOY.md CSP in headless Chromium: with
'wasm-unsafe-eval' a report is created and a zone click is applied by the
core with no script or wasm violation; without it the editor shows the
degraded notice, stays put and leaves the draft untouched; /login,
/app/account and / never request the module (15/15 checks).
2026-10-14 00:45:51 +02:00

101 lines
4.7 KiB
JavaScript

// Driven by docker/csp-proof/app-image.sh: 8093 = DEPLOY.md CSP without 'wasm-unsafe-eval',
// 8094 = with it. Prints one line per check; exits 1 when one fails.
import { chromium } from 'playwright-core'
const WITHOUT = 'http://127.0.0.1:8093'
const WITH = 'http://127.0.0.1:8094'
const failures = []
const check = (name, ok, detail = '') => {
console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail ? ` — ${detail}` : ''}`)
if (!ok) failures.push(name)
}
const browser = await chromium.launch({ executablePath: process.env.CHROME, headless: true })
async function page(base) {
// The app picks its language from the browser: the checks read the French strings.
const p = await browser.newPage({ locale: 'fr-FR' })
const core = []
const violations = []
p.on('request', (r) => {
if (/faunosteo_wasm/.test(r.url())) core.push(r.url())
})
p.on('console', (m) => {
// Script and WebAssembly refusals only: the footer's external credit logo is refused by
// img-src on every page since Step 6, a known, separate matter.
const text = m.text()
if (/Content Security Policy/i.test(text) && !/Loading the image/.test(text)) violations.push(text)
})
p.on('dialog', (d) => d.accept())
// A caught compile error logs nothing: the violation event is the browser's own record.
await p.addInitScript(() => {
window.__csp = []
document.addEventListener('securitypolicyviolation', (e) =>
window.__csp.push(`${e.effectiveDirective} ${e.blockedURI}`),
)
})
return { p, core, violations, base }
}
async function quietPages(base, label) {
for (const path of ['/login', '/app/account', '/']) {
const s = await page(base)
const response = await s.p.goto(`${base}${path}`)
await s.p.waitForTimeout(700)
check(`${label} ${path} loads without the core`, response.ok() && s.core.length === 0, `${s.core.length} core requests`)
await s.p.close()
}
}
async function newReport(s) {
await s.p.goto(`${s.base}/new`)
// The scope disclaimer is acknowledged once per tab; it would cover the drawing.
await s.p.evaluate(() => sessionStorage.setItem('osteo:disclaimer-shown', '1'))
await s.p.fill('#prac-name', 'Dr. Proof')
await s.p.fill('#an-name', 'Mistral')
await s.p.fill('#own-name', 'Owner')
await s.p.getByRole('button', { name: 'Démarrer le bilan' }).click()
}
// With the keyword: the editor runs the core.
const ok = await page(WITH)
const csp = (await (await ok.p.goto(`${WITH}/`)).headers())['content-security-policy']
check('with: CSP header carries wasm-unsafe-eval', csp.includes("'wasm-unsafe-eval'"))
await newReport(ok)
await ok.p.waitForURL('**/editor', { timeout: 10_000 }).catch(() => {})
check('with: new report created by the core, editor open', ok.p.url().endsWith('/editor'))
await ok.p.locator('[data-anno-id="c7-t1"]').first().dispatchEvent('click')
const chip = ok.p.getByRole('button', { name: /Retirer .*C7-T1/ })
check('with: zone click applied by the core', (await chip.count()) > 0)
const wasmEncoding = await ok.p.evaluate(async () => {
const url = performance.getEntriesByType('resource').map((e) => e.name).find((n) => n.endsWith('.wasm'))
const response = await fetch(url, { headers: { 'Accept-Encoding': 'gzip' } })
return { type: response.headers.get('content-type'), size: (await response.arrayBuffer()).byteLength }
})
check('with: module served as application/wasm', wasmEncoding.type === 'application/wasm', JSON.stringify(wasmEncoding))
const okEvents = (await ok.p.evaluate(() => window.__csp)).filter((v) => !v.startsWith('img-src'))
check('with: no CSP violation', ok.violations.length === 0 && okEvents.length === 0, [...ok.violations, ...okEvents].join(' | '))
await ok.p.close()
// Without the keyword: degraded state, nothing lost, auth pages usable.
await quietPages(WITHOUT, 'without:')
await quietPages(WITH, 'with:')
const ko = await page(WITHOUT)
await ko.p.goto(`${WITHOUT}/`)
const draft = JSON.stringify({ version: 3 })
await ko.p.evaluate((d) => sessionStorage.setItem('osteo:draft', d), draft)
await ko.p.goto(`${WITHOUT}/editor`)
await ko.p.waitForTimeout(1500)
const notice = await ko.p.getByText('Le moteur de calcul de Faunosteo').count()
check('without: editor shows the degraded notice', notice > 0)
check('without: editor stays on /editor', ko.p.url().endsWith('/editor'), ko.p.url())
const kept = await ko.p.evaluate(() => sessionStorage.getItem('osteo:draft'))
check('without: the draft is left untouched', kept === draft)
const koEvents = await ko.p.evaluate(() => window.__csp)
check('without: the browser refused the module (CSP)', koEvents.some((v) => v.includes('wasm-eval')), koEvents.join(' | '))
await ko.p.close()
await browser.close()
console.log(failures.length === 0 ? 'ALL PASS' : `FAILED: ${failures.join(', ')}`)
process.exit(failures.length === 0 ? 0 : 1)