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).
This commit is contained in:
bchanot committed 2026-10-14 00:45:51 +02:00
1 parent 74fcb37564
commit 7be94d4ce6
5 files changed
+183 -7

No files matched your search

+100
View File
@@ -0,0 +1,100 @@
// 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)
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# R2 proof (review VI.2 R2, D-R13): the application image boots under the production CSP of
# DEPLOY.md. With 'wasm-unsafe-eval' the editor runs the Rust core (new report, zone click);
# without it the editor shows the degraded state and /login stays usable; /login and
# /app/account never request the core module. Runs the real image (SHEET_BUNDLE=draft) behind
# the image's own nginx with the header added, and drives headless Chromium.
#
# CHROME=/path/to/chrome PLAYWRIGHT_NODE_MODULES=/path/to/node_modules \
# bash docker/csp-proof/app-image.sh
#
# PLAYWRIGHT_NODE_MODULES: a node_modules holding playwright-core, outside the repository
# (no project dependency); the driver runs from a temporary directory linked to it.
#
# Result recorded in docs/development/RUST.md (R2 section).
set -euo pipefail
cd "$(dirname "$0")/../.."
work="$(mktemp -d)"
csp_line="$(grep -m1 '^add_header Content-Security-Policy' DEPLOY.md | sed -E 's/^add_header Content-Security-Policy "(.*)" always;$/\1/')"
without_csp="${csp_line/ \'wasm-unsafe-eval\'/}"
with_csp="${without_csp/script-src \'self\'/script-src \'self\' \'wasm-unsafe-eval\'}"
for variant in without with; do
if [ "$variant" = without ]; then csp="$without_csp"; else csp="$with_csp"; fi
sed "s/\${PORT}/8080/; s|^ # SPA fallback| add_header Content-Security-Policy \"$csp\" always;\n # SPA fallback|; s|^ add_header Cache-Control \"public, max-age=31536000, immutable\";| add_header Cache-Control \"public, max-age=31536000, immutable\";\n add_header Content-Security-Policy \"$csp\" always;|" docker/nginx.conf > "$work/csp-$variant.conf"
done
docker build --build-arg SHEET_BUNDLE=draft -t faunosteo:r2-csp-proof . >/dev/null 2>&1
cleanup() { docker rm -f faunosteo_r2csp_without faunosteo_r2csp_with >/dev/null 2>&1 || true; }
trap cleanup EXIT
cleanup
for variant in without with; do
port=8093
[ "$variant" = with ] && port=8094
docker run -d --rm --name "faunosteo_r2csp_$variant" -p "127.0.0.1:$port:8080" \
--entrypoint nginx \
-v "$work/csp-$variant.conf:/etc/nginx/conf.d/default.conf:ro" \
faunosteo:r2-csp-proof -g 'daemon off;' >/dev/null
done
sleep 1.5
cp docker/csp-proof/app-image.mjs "$work/"
ln -s "${PLAYWRIGHT_NODE_MODULES:?set PLAYWRIGHT_NODE_MODULES}" "$work/node_modules"
node "$work/app-image.mjs"
+3 -1
View File
@@ -11,8 +11,10 @@ server {
try_files $uri =404;
}
# Assets with long-lived cache (Vite hashes filenames)
# Assets with long-lived cache (Vite hashes filenames); the core module ships with a
# precompressed .wasm.gz sibling built into the image
location /assets/ {
gzip_static on;
add_header Cache-Control "public, max-age=31536000, immutable";
}