commit
e092cb029c
252 files changed
+35037
No files matched your search
@@ -0,0 +1,75 @@
|
||||
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Serve an assembled site/ with a real Caddy under docs/Caddyfile's own rules, and hold what it answers: the
|
||||
// security headers on every answer, what a browser may keep, and where a path that is not a file goes. The tests
|
||||
// read the Caddyfile as text (tests/csp.test.mjs); only Caddy says what it does with it.
|
||||
//
|
||||
// pixi run caddy-check assemble site/ first (pixi run site), then this
|
||||
//
|
||||
// Caddy comes from `pixi exec` (conda-forge), so the first run needs the network; nothing else does. The site block
|
||||
// is the document's own, with two lines changed: the host (localhost, plain HTTP) and the root (./site).
|
||||
import { spawn } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
|
||||
const PORT = Number(process.env.CADDY_CHECK_PORT || 8767);
|
||||
const base = `http://127.0.0.1:${PORT}`;
|
||||
if (!fs.existsSync('site/index.html')) { console.error('site/ is not assembled: run `pixi run site` (or scripts/assemble.sh) first'); process.exit(2); }
|
||||
const doc = fs.readFileSync('docs/Caddyfile', 'utf8');
|
||||
const host = /^[a-z0-9.-]+\.[a-z]+ \{$/m.exec(doc);
|
||||
if (!host || !doc.includes('root * /srv/www/pplus')) { console.error('docs/Caddyfile no longer has the site block this script rewrites'); process.exit(2); }
|
||||
const local = `{\n admin off\n auto_https off\n}\n${doc.replace(host[0], `http://127.0.0.1:${PORT} {`).replace('root * /srv/www/pplus', `root * ${path.resolve('site')}`)}`;
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pplus-caddy-'));
|
||||
const file = path.join(dir, 'Caddyfile');
|
||||
fs.writeFileSync(file, local);
|
||||
|
||||
const caddy = spawn('pixi', ['exec', '-s', 'caddy>=2.8', '--', 'caddy', 'run', '--config', file, '--adapter', 'caddyfile'], { stdio: ['ignore', 'ignore', 'pipe'] });
|
||||
let log = '';
|
||||
caddy.stderr.on('data', (d) => { log += d; });
|
||||
const stop = () => { try { caddy.kill('SIGTERM'); } catch { /* already gone */ } fs.rmSync(dir, { recursive: true, force: true }); };
|
||||
process.on('exit', stop);
|
||||
|
||||
const get = (p, headers = {}) => fetch(base + p, { redirect: 'manual', headers });
|
||||
let up = false;
|
||||
for (let i = 0; i < 120 && !up; i++) { try { await get('/VERSION'); up = true; } catch { await new Promise((r) => setTimeout(r, 500)); } }
|
||||
if (!up) { console.error(`Caddy did not start:\n${log.slice(-2000)}`); process.exit(2); }
|
||||
|
||||
const problems = [];
|
||||
const SECURITY = ['content-security-policy', 'referrer-policy', 'x-content-type-options', 'permissions-policy'];
|
||||
/** One request, held to a status and to headers (a string must equal; a RegExp must match; null must be absent). */
|
||||
async function check(what, p, status, want = {}, reqHeaders = {}) {
|
||||
const r = await get(p, reqHeaders);
|
||||
await r.arrayBuffer();
|
||||
if (r.status !== status) problems.push(`${what}: ${p} answered ${r.status}, not ${status}`);
|
||||
for (const h of SECURITY) if (!r.headers.get(h)) problems.push(`${what}: ${p} has no ${h}`);
|
||||
for (const [h, v] of Object.entries(want)) {
|
||||
const got = r.headers.get(h);
|
||||
const ok = v === null ? got === null : v instanceof RegExp ? v.test(got ?? '') : got === v;
|
||||
if (!ok) problems.push(`${what}: ${p} has ${h}: ${got}, not ${v}`);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
const font = fs.readdirSync('site/fonts').find((f) => f.endsWith('.woff2'));
|
||||
const page = await check('the page', '/', 200, { 'cache-control': 'no-cache', 'content-type': /^text\/html/, etag: /./ });
|
||||
await check('the page by name', '/index.html', 200, { 'cache-control': 'no-cache' });
|
||||
const js = await check('a module', '/dist/main.js', 200, { 'cache-control': 'no-cache', 'content-type': /javascript/, etag: /./ });
|
||||
await check('a module, unchanged', '/dist/main.js', 304, { 'cache-control': 'no-cache' }, { 'if-none-match': js.headers.get('etag') });
|
||||
await check('a module of core', '/core/dist/services/pseudo.js', 200, { 'cache-control': 'no-cache' });
|
||||
await check('the stylesheet', '/styles.css', 200, { 'cache-control': 'no-cache', 'content-type': /^text\/css/ });
|
||||
await check('a font', `/fonts/${font}`, 200, { 'cache-control': 'public, max-age=2592000', 'content-type': 'font/woff2' });
|
||||
await check('VERSION', '/VERSION', 200, { 'cache-control': 'no-store' });
|
||||
await check('build.json', '/build.json', 200, { 'cache-control': 'no-cache', 'content-type': /json/ });
|
||||
for (const f of ['LICENSE.md', 'NOTICE.md', 'THIRD_PARTY.md']) await check('a notice', `/${f}`, 200, { 'content-type': 'text/plain; charset=utf-8' });
|
||||
// a path that is not a file: a page goes to /, a file is a plain 404, and neither is kept
|
||||
for (const p of ['/no/such/page', '/bridge', '/old/link?x=1']) await check('a missing page', p, 302, { location: '/', 'cache-control': 'no-store' });
|
||||
for (const p of ['/dist/gone.js', '/fonts/gone.woff2', '/styles/gone.css']) await check('a missing file', p, 404, { 'cache-control': 'no-store', 'content-type': /^text\/plain/ });
|
||||
// the CSP the server sends is the one the tests hold to index.html's inline scripts
|
||||
const csp = page.headers.get('content-security-policy') ?? '';
|
||||
for (const part of ["default-src 'self'", "frame-ancestors", 'https://godbolt.org']) if (!csp.includes(part)) problems.push(`the CSP Caddy sends has no ${part}`);
|
||||
|
||||
stop();
|
||||
if (problems.length) { console.error(problems.join('\n')); console.error(`\n${problems.length} thing(s) wrong`); process.exit(1); }
|
||||
console.log(`Caddy serves site/ as docs/Caddyfile says: security headers on every answer, caching per kind of file, a missing page sent to /, a missing file a plain 404`);
|
||||
process.exit(0);
|
||||
Reference in new issue
Block a user