61 lines
3.8 KiB
JavaScript
61 lines
3.8 KiB
JavaScript
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
// The Content-Security-Policy in docs/Caddyfile is the one the site runs under: these tests hold it to index.html
|
|
// (every inline script is allowed by its hash, nothing loads from another origin) and to what the app sends.
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import { createHash } from 'node:crypto';
|
|
import { GODBOLT_BASE } from '../core/dist/mojo/generated/pin.js';
|
|
|
|
const caddy = readFileSync(new URL('../docs/Caddyfile', import.meta.url), 'utf8');
|
|
const html = readFileSync(new URL('../index.html', import.meta.url), 'utf8');
|
|
const header = (name) => { const m = new RegExp(`^\\s*${name}\\s+"([^"]*)"`, 'm').exec(caddy); return m ? m[1] : null; };
|
|
const csp = header('Content-Security-Policy');
|
|
const directive = (name) => (csp ?? '').split(';').map((d) => d.trim().split(/\s+/)).find((d) => d[0] === name)?.slice(1) ?? null;
|
|
|
|
test('the Caddy block sets a CSP and the other security headers', () => {
|
|
assert.ok(csp, 'Content-Security-Policy');
|
|
assert.equal(header('Referrer-Policy'), 'no-referrer');
|
|
assert.equal(header('X-Content-Type-Options'), 'nosniff');
|
|
assert.match(header('Permissions-Policy') ?? '', /camera=\(\)/);
|
|
assert.doesNotMatch(caddy, /^\s*X-Frame-Options\s/m, 'course pages embed the app: framing is decided by frame-ancestors alone');
|
|
for (const d of ['default-src', 'script-src', 'style-src', 'font-src', 'connect-src', 'object-src', 'base-uri', 'frame-ancestors']) assert.ok(directive(d), d);
|
|
assert.deepEqual(directive('object-src'), ["'none'"]);
|
|
});
|
|
|
|
test('every inline script of index.html is allowed by its hash, and scripts allow nothing looser', () => {
|
|
const inline = [...html.matchAll(/<script(?![^>]*\bsrc=)[^>]*>([\s\S]*?)<\/script>/g)].map((m) => m[1]);
|
|
assert.ok(inline.length >= 2, 'the theme script and the import map');
|
|
const allowed = directive('script-src');
|
|
for (const body of inline) {
|
|
const hash = `'sha256-${createHash('sha256').update(body, 'utf8').digest('base64')}'`;
|
|
assert.ok(allowed.includes(hash), `index.html changed an inline script: put ${hash} in script-src (docs/Caddyfile)`);
|
|
}
|
|
for (const loose of ["'unsafe-inline'", "'unsafe-eval'", '*', 'data:', 'https:']) assert.ok(!allowed.includes(loose), `script-src ${loose}`);
|
|
});
|
|
|
|
test('the page loads nothing from another origin (the fonts are the site\'s own)', () => {
|
|
const refs = [...html.matchAll(/\b(?:href|src)="([^"]+)"/g)].map((m) => m[1]).filter((u) => !u.startsWith('data:'));
|
|
assert.ok(refs.length > 3);
|
|
for (const u of refs) assert.ok(!/^(https?:)?\/\//.test(u), `index.html loads ${u}`);
|
|
assert.doesNotMatch(html, /fonts\.googleapis|fonts\.gstatic/);
|
|
assert.deepEqual(directive('font-src'), ["'self'"]);
|
|
});
|
|
|
|
test('connect-src allows the compile backend and nothing else outside the site', () => {
|
|
const origin = new URL(GODBOLT_BASE).origin;
|
|
const allowed = directive('connect-src');
|
|
assert.ok(allowed.includes(origin), `Compile & run posts to ${origin}`);
|
|
assert.deepEqual(allowed.filter((s) => s !== "'self'"), [origin]);
|
|
});
|
|
|
|
test('the fonts the page names are in the repository, with their license', () => {
|
|
const css = readFileSync(new URL('../fonts/plex.css', import.meta.url), 'utf8');
|
|
const files = [...css.matchAll(/url\("\.\/([^"]+)"\)/g)].map((m) => m[1]);
|
|
assert.equal(files.length, 7);
|
|
for (const f of files) assert.ok(readFileSync(new URL(`../fonts/${f}`, import.meta.url)).length > 10000, f);
|
|
assert.match(readFileSync(new URL('../fonts/LICENSE.txt', import.meta.url), 'utf8'), /SIL Open Font License, Version 1\.1/);
|
|
assert.match(readFileSync(new URL('../scripts/assemble.sh', import.meta.url), 'utf8'), /\bfonts\b/, 'assemble.sh copies fonts/ into the site');
|
|
});
|