Files
faunosteo/scripts/browser/r4-app-csp.mjs
T

369 lines
13 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// CSP proof of the application's import worker (R4.3d, plan §B.1.5, DEPLOY.md
// OD-1): the server's two policies, the three engines, one reference PDF
// picked on Home, a fresh browser per engine and policy.
// with DEPLOY.md's policy, 'wasm-unsafe-eval' in script-src: the
// import opens the editor; no violation at all (the footer's
// external image, the one known violation until R4.10, is bundled).
// without the keyword removed: the inline line « Le moteur de lecture n'a
// pas pu être chargé. Réessayez l'import. », the button enabled
// again, no page error, and a second pick of the same file shows
// the same line (the page stays usable).
// Violations: the document's `securitypolicyviolation` events (the
// init-script probe) and every console message of the page and its
// workers. A worker's own `securitypolicyviolation` event fires in the
// worker's scope, out of the harness's reach without changing the
// application's worker, so both policies run a second time with
// `report-uri` appended (the enforced directives unchanged): the reports
// the browser POSTs name what was blocked and where, workers included. Each
// browser runs under the 4 GiB RSS watchdog (not a guaranteed memory
// limit). Exit code 1 when a run is not as expected.
//
// PLAYWRIGHT_NODE_MODULES=... node scripts/browser/r4-app-csp.mjs \
// [--engines chromium,firefox,webkit] [--out <file>]
import { readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { parseArgs } from 'node:util'
import {
IMPORT_ERROR,
PICK_LABEL,
finalState,
firstEnd,
resultOf,
settle,
} from './import-baseline.mjs'
import { checkBuild } from './lib/app-build.mjs'
import { delay, firstLine, isStop, untilStopped } from './lib/async.mjs'
import { hostFacts, memAvailableBytes } from './lib/host.mjs'
import { sha256 } from './lib/inputs.mjs'
import { countErrorLines, probedPage } from './lib/page-probes.mjs'
import {
FIXTURES_DIR,
REPO_ROOT,
RESULTS_DIR,
relativeToRepo,
} from './lib/paths.mjs'
import {
ENGINES,
closeEngine,
launchEngine,
loadPlaywright,
} from './lib/playwright.mjs'
import { resultsJson } from './lib/results-json.mjs'
import { watchdogFacts } from './lib/watchdog.mjs'
import { startServer } from './serve.mjs'
const REFERENCE = join(FIXTURES_DIR, 'pdf/r3/demo.fr.logos.pdf')
const FR_DICT = join(REPO_ROOT, 'apps/app/src/i18n/fr.ts')
// The two policies as served, then the same with `report-uri` appended:
// the browser POSTs each violation report to the server, workers included.
const PASSES = [
{ variant: 'with', reporting: false },
{ variant: 'without', reporting: false },
{ variant: 'with', reporting: true },
{ variant: 'without', reporting: true },
]
const CSP_TEXT = /content.security.policy|refused to|unsafe-eval|\bCSP\b/i
const LATE_VIOLATIONS_MS = 2000
const RETRY_TIMEOUT_MS = 30_000
const MAX_CONSOLE = 40
const today = () => new Date().toISOString().slice(0, 10)
const log = (line) => console.error(`[${new Date().toISOString()}] ${line}`)
const pathOf = (url) => {
try {
return new URL(url).pathname
} catch {
return url
}
}
function options() {
const { values } = parseArgs({
options: {
engines: { type: 'string', default: ENGINES.join(',') },
out: {
type: 'string',
default: join(RESULTS_DIR, `${today()}-r4-csp.json`),
},
},
})
const engines = values.engines.split(',').filter(Boolean)
const unknown = engines.filter((e) => !ENGINES.includes(e))
if (unknown.length) throw new Error(`unknown engines: ${unknown.join(', ')}`)
return { engines, out: values.out }
}
const pickButton = (page) => page.getByRole('button', { name: PICK_LABEL })
/** Home in a fresh context, every console message and worker recorded. */
async function cspTab(session, baseUrl) {
const context = await session.browser.newContext({ locale: 'fr-FR' })
const seed = { draft: null, locale: 'fr', skipDisclaimer: false }
const { page, events } = await probedPage(context, seed)
const seen = { console: [], workers: [], crashed: false }
page.on('console', (m) => {
const worker = m.worker()
const text = m.text().slice(0, 400)
seen.console.push({
type: m.type(),
worker: worker && pathOf(worker.url()),
text,
})
})
page.on('worker', (w) => {
const row = { path: pathOf(w.url()), closed: false }
seen.workers.push(row)
w.on('close', () => (row.closed = true))
})
page.on('crash', () => (seen.crashed = true))
await page.goto(`${baseUrl}/`)
await pickButton(page).and(page.locator(':enabled')).waitFor()
return { context, page, events, seen }
}
async function buttonEnabled(page) {
return pickButton(page)
.and(page.locator(':enabled'))
.waitFor({ timeout: 5000 })
.then(() => true)
.catch(() => false)
}
/** One pick, as import-baseline.mjs measures it, then 2 s for late events. */
async function firstImport(session, tab) {
const since = await tab.page.evaluate(() => performance.now())
const start = performance.now()
const first = await untilStopped(session, firstEnd(tab, REFERENCE))
const durationMs = first.at - start
if (isStop(first) || first.kind === 'timeout' || first.kind === 'error') {
return { outcome: first.kind, error: first.error ?? null, durationMs }
}
await settle(tab, first)
await delay(LATE_VIOLATIONS_MS)
const final = await finalState(tab, since)
return { outcome: resultOf(final), durationMs, ...final }
}
/** A second pick of the same file: the inline line must come back. */
async function retryImport(tab) {
const { page } = tab
await page.evaluate(countErrorLines)
await page.setInputFiles('input[type="file"]', REFERENCE)
const shown = await page
.waitForFunction(() => window.__errorLines > 0, null, {
timeout: RETRY_TIMEOUT_MS,
})
.then(() => true)
.catch(() => false)
const errorText = shown
? await page.locator(IMPORT_ERROR).textContent({ timeout: 1000 })
: null
return { shown, errorText, buttonEnabledAfter: await buttonEnabled(page) }
}
/** The violations and console messages that name the policy. */
function violationsOf(row, seen) {
return {
documentViolations: row.cspViolations ?? [],
consoleCsp: seen.console.filter((m) => CSP_TEXT.test(m.text)),
}
}
/** One `report-uri` report, the fields that name what was blocked where. */
function reportFacts(report) {
const r = report['csp-report'] ?? report
return {
documentPath: r['document-uri'] && pathOf(r['document-uri']),
effectiveDirective: r['effective-directive'],
violatedDirective: r['violated-directive'],
blockedUri: r['blocked-uri'],
sourceFile: r['source-file'] && pathOf(r['source-file']),
disposition: r.disposition,
unparsed: r.unparsed,
}
}
/** What each policy must show (header comment); empty when as expected. */
function problemsOf(variant, row, coreUnavailable) {
const problems = []
if (row.pageErrors?.length) problems.push('page errors')
if (row.crashed) problems.push('page crashed')
if (variant === 'with') {
if (row.outcome !== 'editor') problems.push(`outcome ${row.outcome}`)
if (row.documentViolations.length) problems.push('violations')
if (row.consoleCsp.length) problems.push('CSP console messages')
if (row.reports?.length) problems.push('reports')
return problems
}
if (row.errorText !== coreUnavailable) problems.push('inline line')
if (!row.buttonEnabledAfter) problems.push('button not enabled again')
if (row.retry?.errorText !== coreUnavailable) problems.push('retry line')
if (!row.retry?.buttonEnabledAfter) problems.push('retry: button')
return problems
}
async function cspRun(session, baseUrl, variant, coreUnavailable) {
const tab = await cspTab(session, baseUrl)
try {
const row = await firstImport(session, tab)
if (variant === 'without' && row.outcome === 'error') {
row.buttonEnabledAfter = await buttonEnabled(tab.page)
row.retry = await retryImport(tab)
}
const { seen } = tab
const full = { ...row, ...violationsOf(row, seen), crashed: seen.crashed }
full.workers = seen.workers
full.console = seen.console.slice(0, MAX_CONSOLE)
full.consoleCount = seen.console.length
delete full.cspViolations
return full
} finally {
await tab.context.close().catch(() => undefined)
}
}
/** One engine's run; the reports are read once its browser is closed. */
async function engineRun(pw, engine, { server, pass, coreUnavailable }) {
const session = await launchEngine(pw, engine)
const head = { ...pass, engine, engineVersion: session.version }
let row
try {
row = await cspRun(session, server.url, pass.variant, coreUnavailable)
} catch (error) {
row = { outcome: 'error', error: firstLine(error) }
} finally {
await closeEngine(session)
}
if (pass.reporting) row.reports = server.reports.splice(0).map(reportFacts)
if (!row.error) row.problems = problemsOf(pass.variant, row, coreUnavailable)
return { ...head, ...row }
}
async function passRuns(pw, pass, engines, coreUnavailable) {
const server = await startServer({
csp: pass.variant,
reportViolations: pass.reporting,
})
const runs = []
try {
for (const engine of engines) {
const run = await engineRun(pw, engine, { server, pass, coreUnavailable })
runs.push(run)
log(
`${policyName(pass)} ${engine}: ${run.outcome} ` +
`${run.problems ?? run.error}`,
)
}
} finally {
server.close()
}
return { csp: server.csp, runs }
}
const policyName = (pass) =>
pass.reporting ? `${pass.variant} + report-uri` : pass.variant
const asExpected = (r) => Array.isArray(r.problems) && r.problems.length === 0
function summary(result) {
const line = (cells) => `| ${cells.join(' | ')} |`
const header = [
'Policy',
'Engine',
'Outcome',
'Inline line',
'Button enabled',
'Retry line',
'Document violations',
'CSP console messages',
'report-uri reports',
'Workers created / closed',
'Page errors',
'Verdict',
]
const reportCell = (r) =>
r.reporting
? (r.reports ?? [])
.map(
(x) =>
`[${x.documentPath}] ${x.effectiveDirective} ${x.blockedUri}`,
)
.join('; ') || 'none'
: '–'
const rows = result.runs.map((r) =>
line([
policyName(r),
r.engine,
r.outcome,
r.errorText ?? '–',
String(r.buttonEnabledAfter ?? '–'),
r.retry?.errorText ?? '–',
(r.documentViolations ?? []).join('; ') || 'none',
(r.consoleCsp ?? [])
.map((m) => `[${m.worker ?? 'page'}] ${m.text}`)
.join('; ') || 'none',
reportCell(r),
r.workers
? `${r.workers.length} / ${r.workers.filter((w) => w.closed).length}`
: '–',
String(r.pageErrors?.length ?? '–'),
asExpected(r)
? 'as expected'
: `NOT AS EXPECTED: ${r.problems ?? r.error}`,
]),
)
return [line(header), line(header.map(() => '---')), ...rows].join('\n')
}
async function main() {
const opts = options()
const build = checkBuild()
const { pw, version } = loadPlaywright()
const { fr } = await import(pathToFileURL(FR_DICT).href)
const coreUnavailable = fr['home.import.error_core_unavailable']
const result = {
harness: 'scripts/browser/r4-app-csp.mjs',
startedAt: new Date().toISOString(),
watchdog: watchdogFacts(),
host: hostFacts(),
build,
playwrightCore: version,
reference: {
file: relativeToRepo(REFERENCE),
bytes: readFileSync(REFERENCE).length,
sha256: sha256(readFileSync(REFERENCE)),
},
expected: { without: coreUnavailable, violations: 'none' },
observation:
"document: the init-script probe's securitypolicyviolation events; " +
'console: every message of the page and its workers; reporting ' +
'passes: the same enforced directives plus report-uri, the reports ' +
"the browser POSTs (a worker's own violation events fire in its " +
"scope, out of the harness's reach otherwise)",
policies: {},
runs: [],
}
for (const pass of PASSES) {
const { csp, runs } = await passRuns(
pw,
pass,
opts.engines,
coreUnavailable,
)
result.policies[policyName(pass)] = csp
result.runs.push(...runs)
}
result.endedAt = new Date().toISOString()
result.host.memAvailableBytesAtEnd = memAvailableBytes()
result.engines = Object.fromEntries(
result.runs.map((r) => [r.engine, r.engineVersion]),
)
writeFileSync(opts.out, `${resultsJson(result)}\n`)
log(`results written to ${opts.out}`)
console.log(summary(result))
if (!result.runs.every(asExpected)) process.exitCode = 1
}
await main()