mirror of
https://github.com/bitbaum/sites.git
synced 2026-12-04 04:56:58 +01:00
The ask route refused any name classifyHost calls invalid, which includes every <tenant>.hirnli.orangecat.ch (two labels under the base) - so pointing Caddy's one ask at the engine would have stopped hirnli's certificates (the install script's parity check would have refused the switch instead). And a database error (tables not migrated, an outage) threw before the fallback was asked. The decision is now mayIssue() in src/lib/ask.ts, tested without a database. scripts/smoke.mjs checks a running engine: health, the published site by its free address and own domain, the preview (noindex), names nobody holds, the ask, and a form refused for a missing field. Run locally against Loki's 0140_sites.sql with a SELECT-only role: all checks pass. Claude-Session: https://claude.ai/code/session_019MDtmugQS9ffkEryKtQb5c Co-authored-by: Claude <noreply@anthropic.com>
152 lines
5.5 KiB
JavaScript
152 lines
5.5 KiB
JavaScript
// What the engine must answer, checked against a running engine — on the box
|
|
// after a deploy, or from anywhere against production.
|
|
//
|
|
// node scripts/smoke.mjs --site camille [--domain camille.ch] [--preview <site id>]
|
|
// [--absent <slug>] [--form muster:kontakt] [--engine http://127.0.0.1:4042]
|
|
//
|
|
// Without --engine every request goes to https://<host> as a visitor's would.
|
|
// With --engine it goes to that origin with the Host header set, which is how
|
|
// a check runs before Caddy routes the names (or on a laptop against a copy).
|
|
//
|
|
// Checks: the health probe; the published site by its free address (200,
|
|
// indexable unless the row says noindex) and by --domain; the preview
|
|
// address (200, always noindex); a name nobody holds, a malformed name and
|
|
// each --absent slug (404); Caddy's ask for the site (yes) and for a stranger
|
|
// (no, only with --engine: the public blocks hide /_sites/ask); and, with
|
|
// --form <slug>:<page>, a contact form sent with a missing field (303 back
|
|
// with problem=missing). Nothing is ever delivered: a form with a missing
|
|
// field is refused before any mail is composed.
|
|
//
|
|
// Exit 1 when anything differs, one line per check.
|
|
|
|
function args(argv) {
|
|
const out = { absent: [] };
|
|
for (let i = 0; i < argv.length; i += 2) {
|
|
const k = argv[i].replace(/^--/, '');
|
|
if (k === 'absent') out.absent.push(argv[i + 1]);
|
|
else out[k] = argv[i + 1];
|
|
}
|
|
return out;
|
|
}
|
|
|
|
const a = args(process.argv.slice(2));
|
|
if (!a.site) {
|
|
console.error(
|
|
'usage: smoke.mjs --site <slug> [--domain d] [--preview id] [--absent slug] [--form slug:page] [--engine url]',
|
|
);
|
|
process.exit(2);
|
|
}
|
|
const base = a.base ?? 'orangecat.ch';
|
|
const previewHost = `sites.${base}`;
|
|
|
|
async function get(host, path = '/', init = {}) {
|
|
const url = a.engine ? `${a.engine}${path}` : `https://${host}${path}`;
|
|
const headers = { ...(init.headers ?? {}) };
|
|
if (a.engine) headers['x-forwarded-host'] = host;
|
|
try {
|
|
const res = await fetch(url, {
|
|
redirect: 'manual',
|
|
...init,
|
|
headers,
|
|
signal: AbortSignal.timeout(20_000),
|
|
});
|
|
return {
|
|
status: res.status,
|
|
location: res.headers.get('location') ?? '',
|
|
body: await res.text(),
|
|
};
|
|
} catch (err) {
|
|
return { status: 0, location: '', body: String(err?.cause?.code ?? err) };
|
|
}
|
|
}
|
|
|
|
const robotsNoindex = (html) => /<meta name="robots" content="[^"]*noindex/.test(html);
|
|
let failed = 0;
|
|
function check(name, ok, detail) {
|
|
if (!ok) failed++;
|
|
console.log(`${ok ? 'ok ' : 'FAIL'} ${name}${detail ? ` — ${detail}` : ''}`);
|
|
}
|
|
|
|
if (a.engine) {
|
|
const r = await get('localhost');
|
|
check(
|
|
'health probe answers',
|
|
r.status === 200 && r.body.includes('sites engine'),
|
|
`HTTP ${r.status}`,
|
|
);
|
|
}
|
|
|
|
const free = `${a.site}.${base}`;
|
|
const pub = await get(free);
|
|
if (a.domain) {
|
|
check(
|
|
`${free} sends visitors to its own domain`,
|
|
pub.status === 308 && pub.location.startsWith(`https://${a.domain}/`),
|
|
`HTTP ${pub.status} ${pub.location}`,
|
|
);
|
|
const own = await get(a.domain);
|
|
check(`${a.domain} serves the published site`, own.status === 200, `HTTP ${own.status}`);
|
|
} else {
|
|
check(
|
|
`${free} serves the published site`,
|
|
pub.status === 200 && /<html/.test(pub.body),
|
|
`HTTP ${pub.status}`,
|
|
);
|
|
}
|
|
|
|
if (a.preview) {
|
|
const p = await get(previewHost, `/${a.preview}`);
|
|
check(`preview ${previewHost}/${a.preview} answers`, p.status === 200, `HTTP ${p.status}`);
|
|
check('preview is never indexed', p.status === 200 && robotsNoindex(p.body));
|
|
const wrong = await get(previewHost, '/00000000-0000-4000-8000-000000000000');
|
|
check('preview of an id nobody holds is 404', wrong.status === 404, `HTTP ${wrong.status}`);
|
|
}
|
|
|
|
const nobody = `nobody-${Date.now()}.${base}`;
|
|
if (a.engine) {
|
|
// Through Caddy a stranger never gets a certificate, so it cannot be fetched
|
|
// over https at all; the engine's own answer is what is checked here.
|
|
const n = await get(nobody);
|
|
check('a name nobody holds is 404', n.status === 404, `HTTP ${n.status}`);
|
|
const m = await get(`a.b.${base}`);
|
|
check('a malformed name is 404', m.status === 404, `HTTP ${m.status}`);
|
|
}
|
|
for (const slug of a.absent) {
|
|
const r = await get(`${slug}.${base}`);
|
|
// 0 = no certificate for it (ask said no): also not served.
|
|
check(
|
|
`${slug}.${base} is not served`,
|
|
r.status === 404 || (!a.engine && r.status === 0),
|
|
`HTTP ${r.status} ${r.status === 0 ? r.body : ''}`,
|
|
);
|
|
}
|
|
|
|
if (a.engine) {
|
|
const yes = await get('localhost', `/_sites/ask?domain=${encodeURIComponent(a.domain ?? free)}`);
|
|
check('ask: a certificate for the site', yes.status === 200, `HTTP ${yes.status}`);
|
|
const no = await get('localhost', `/_sites/ask?domain=${encodeURIComponent(nobody)}`);
|
|
check('ask: none for a stranger', no.status !== 200, `HTTP ${no.status}`);
|
|
} else {
|
|
const hidden = await get(a.domain ?? free, `/_sites/ask?domain=${encodeURIComponent(nobody)}`);
|
|
check('ask is not reachable from outside', hidden.status === 404, `HTTP ${hidden.status}`);
|
|
}
|
|
|
|
if (a.form) {
|
|
const [slug, page] = a.form.split(':');
|
|
const host = `${slug}.${base}`;
|
|
const body = new URLSearchParams({ _page: page, _form: page });
|
|
const r = await get(host, '/_sites/forms', {
|
|
method: 'POST',
|
|
body,
|
|
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
});
|
|
check(
|
|
`form on ${host}/${page}: a missing field comes back as a problem`,
|
|
r.status === 303 && r.location.includes('problem=missing') && r.location.startsWith('/'),
|
|
`HTTP ${r.status} ${r.location}`,
|
|
);
|
|
}
|
|
|
|
console.log(failed ? `${failed} check(s) failed` : 'all checks passed');
|
|
process.exit(failed ? 1 : 0);
|