Imported toolboxes could run scripts on the site origin through image data URLs, so only bitmap images are now accepted at import, display and opening. The Cards game and its fake admin login no longer turn on when NODE_ENV is missing, and the Node server now sends a CSP and the other security headers. The deployment script no longer runs files owned by the service account as root, the services cannot gain privileges through sudo, and the deployment key stays in a single workflow step with actions pinned by commit. The script and systemd units must still be reinstalled on the server after delivery. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
7 KiB
JavaScript
135 lines
7 KiB
JavaScript
// Rôle : vérifie que le workflow de livraison conserve ses barrières de validation et de provenance.
|
|
import assert from "node:assert/strict";
|
|
import { execFile } from "node:child_process";
|
|
import { readFile } from "node:fs/promises";
|
|
import { test } from "node:test";
|
|
import { promisify } from "node:util";
|
|
|
|
const deployWorkflowPath = ".forgejo/workflows/deploy-prod.yml";
|
|
const packageLockPath = "package-lock.json";
|
|
const remoteScriptPath = "scripts/deploy-sokko-g.sh";
|
|
const servicePath = "ops/systemd/sokko-g.service";
|
|
const validationWorkflowPath = ".forgejo/workflows/validate-main.yml";
|
|
const execFileAsync = promisify(execFile);
|
|
|
|
test("main pushes validate then automatically deploy the same current commit", async () => {
|
|
const workflow = await readFile(validationWorkflowPath, "utf8");
|
|
const validateJob = workflow.match(/ {2}validate:\n[\s\S]*?(?=\n {2}deploy:)/)?.[0] || "";
|
|
|
|
assert.ok(validateJob);
|
|
assert.match(workflow, /push:\s+[\s\S]*branches:\s+[\s\S]*- main/);
|
|
assert.match(workflow, /ref: \$\{\{ gitea\.sha \}\}/);
|
|
assert.match(workflow, /container: "mcr\.microsoft\.com\/playwright:v1\.62\.1-noble@sha256:[0-9a-f]{64}"/);
|
|
assert.match(workflow, /npm ci/);
|
|
assert.match(workflow, /npm run check:full/);
|
|
assert.match(workflow, /deploy:\s+[\s\S]*needs: validate/);
|
|
assert.match(workflow, /DEPLOY_COMMIT: \$\{\{ needs\.validate\.outputs\.commit \}\}/);
|
|
assert.match(workflow, />> "\$GITHUB_OUTPUT"/);
|
|
assert.match(workflow, /steps\.freshness\.outputs\.deploy == 'true'/);
|
|
assert.match(workflow, /current_commit[\s\S]*DEPLOY_COMMIT/);
|
|
assert.match(workflow, /deploy-contract/);
|
|
assert.match(workflow, /deploy-sokko-g --commit/);
|
|
assert.match(workflow, /\[webapps\.shinuwa\.fr\]:2911/);
|
|
assert.match(workflow, /shinuwa@webapps\.shinuwa\.fr/);
|
|
assert.match(workflow, /StrictHostKeyChecking=yes/);
|
|
assert.doesNotMatch(workflow, /workflow_dispatch:/);
|
|
assert.doesNotMatch(workflow, /StrictHostKeyChecking=no/);
|
|
assert.doesNotMatch(workflow, /192\.168\.0\.100/);
|
|
assert.doesNotMatch(workflow, /GITEA_OUTPUT/);
|
|
assert.doesNotMatch(validateJob, /secrets\./);
|
|
assert.doesNotMatch(validateJob, /\bssh\b/);
|
|
});
|
|
|
|
test("production deployment requires a validated exact commit and pinned SSH host", async () => {
|
|
const workflow = await readFile(deployWorkflowPath, "utf8");
|
|
|
|
assert.match(workflow, /workflow_dispatch:\s+[\s\S]*commit:/);
|
|
assert.doesNotMatch(workflow, /push:/);
|
|
assert.match(workflow, /REQUESTED_COMMIT[\s\S]*\^\[0-9a-f\]\{40\}\$/);
|
|
assert.match(workflow, /deploy:\s+[\s\S]*needs: validate/);
|
|
assert.match(workflow, /validate:\s+[\s\S]*container: "mcr\.microsoft\.com\/playwright:v1\.62\.1-noble@sha256:[0-9a-f]{64}"/);
|
|
assert.match(workflow, /npm ci/);
|
|
assert.match(workflow, /npm run check:full/);
|
|
assert.match(workflow, /git merge-base --is-ancestor "\$commit" origin\/main/);
|
|
assert.match(workflow, />> "\$GITHUB_OUTPUT"/);
|
|
assert.match(workflow, /DEPLOY_COMMIT: \$\{\{ needs\.validate\.outputs\.commit \}\}/);
|
|
assert.match(workflow, /deploy-contract/);
|
|
assert.match(workflow, /deploy-sokko-g --commit/);
|
|
assert.match(workflow, /\[webapps\.shinuwa\.fr\]:2911/);
|
|
assert.match(workflow, /shinuwa@webapps\.shinuwa\.fr/);
|
|
assert.match(workflow, /StrictHostKeyChecking=yes/);
|
|
assert.doesNotMatch(workflow, /StrictHostKeyChecking=no/);
|
|
assert.doesNotMatch(workflow, /192\.168\.0\.100/);
|
|
assert.doesNotMatch(workflow, /GITEA_OUTPUT/);
|
|
assert.doesNotMatch(workflow, /\$\{\{\s*forgejo\./);
|
|
});
|
|
|
|
test("validation containers match the locked Playwright release", async () => {
|
|
const [lockContents, validationWorkflow, deployWorkflow] = await Promise.all([
|
|
readFile(packageLockPath, "utf8"),
|
|
readFile(validationWorkflowPath, "utf8"),
|
|
readFile(deployWorkflowPath, "utf8")
|
|
]);
|
|
const lock = JSON.parse(lockContents);
|
|
const playwrightVersion = lock.packages["node_modules/@playwright/test"].version;
|
|
const expectedImage = `mcr.microsoft.com/playwright:v${playwrightVersion}-noble@sha256:`;
|
|
|
|
assert.ok(validationWorkflow.includes(expectedImage));
|
|
assert.ok(deployWorkflow.includes(expectedImage));
|
|
assert.doesNotMatch(validationWorkflow, /playwright install --with-deps/);
|
|
assert.doesNotMatch(deployWorkflow, /playwright install --with-deps/);
|
|
});
|
|
|
|
test("the versioned remote script builds and activates only the requested commit", async () => {
|
|
await execFileAsync("bash", ["-n", remoteScriptPath]);
|
|
const script = await readFile(remoteScriptPath, "utf8");
|
|
|
|
assert.match(script, /\$1 == "--commit"/);
|
|
assert.match(script, /\^\[0-9a-f\]\{40\}\$/);
|
|
assert.match(script, /flock --wait 900/);
|
|
assert.match(script, /merge-base --is-ancestor/);
|
|
assert.match(script, /worktree add --detach/);
|
|
assert.match(script, /env PATH="\$NODE_HOME\/bin:\/usr\/bin:\/bin" "\$NPM_BIN" --version/);
|
|
assert.match(script, /"\$NPM_BIN" --prefix "\$worktree" ci/);
|
|
assert.match(script, /"\$NPM_BIN" --prefix "\$worktree" run build/);
|
|
assert.match(script, /checkout --detach "\$requested_commit"/);
|
|
assert.match(script, /write_state previous-commit/);
|
|
assert.match(script, /systemctl is-active --quiet/);
|
|
assert.match(script, /systemctl show --property=ExecStart/);
|
|
assert.doesNotMatch(script, /git\s+pull/);
|
|
});
|
|
|
|
test("the production service uses the same pinned Node runtime", async () => {
|
|
const service = await readFile(servicePath, "utf8");
|
|
|
|
assert.match(service, /ExecStart=\/home\/shinuwa\/\.nvm\/versions\/node\/v24\.11\.1\/bin\/npm start/);
|
|
assert.match(service, /Environment=PATH=\/home\/shinuwa\/\.nvm\/versions\/node\/v24\.11\.1\/bin:/);
|
|
assert.doesNotMatch(service, /v26\.3\.0/);
|
|
});
|
|
|
|
test("the remote script never runs files owned by the service account as root", async () => {
|
|
const script = await readFile(remoteScriptPath, "utf8");
|
|
|
|
assert.match(script, /as_app_user "\$NODE_BIN" --version/);
|
|
assert.match(script, /as_app_user env PATH="\$NODE_HOME\/bin:\/usr\/bin:\/bin" "\$NPM_BIN" --version/);
|
|
assert.doesNotMatch(script, /\$\(\$NODE_BIN/);
|
|
assert.doesNotMatch(script, /\$\(env PATH=/);
|
|
assert.doesNotMatch(script, /chown -R/);
|
|
assert.doesNotMatch(script, /^\s*(?:mv .*(?:\$APP_ROOT|\$worktree|\$backup_dist)|rm -rf -- "\$APP_ROOT)/m);
|
|
});
|
|
|
|
test("the services cannot gain privileges through sudo", async () => {
|
|
for (const path of [servicePath, "ops/systemd/sokko-g-cards-backup.service"]) {
|
|
assert.match(await readFile(path, "utf8"), /^NoNewPrivileges=true$/m);
|
|
}
|
|
});
|
|
|
|
test("deployment secrets stay in one step and actions are pinned by commit", async () => {
|
|
for (const path of [deployWorkflowPath, validationWorkflowPath]) {
|
|
const workflow = await readFile(path, "utf8");
|
|
assert.equal(workflow.match(/secrets\.DEPLOY_SSH_KEY/g)?.length, 1);
|
|
assert.doesNotMatch(workflow.match(/ {2}deploy:\n[\s\S]*?\n {4}steps:/)?.[0] || "", /secrets\./);
|
|
assert.match(workflow, /- name: Configure the pinned production host key\n(?: {8}if: .*\n)? {8}env:\n {10}DEPLOY_SSH_KEY: \$\{\{ secrets\.DEPLOY_SSH_KEY \}\}/);
|
|
for (const [, reference] of workflow.matchAll(/uses: \S+@(\S+)/g)) assert.match(reference, /^[0-9a-f]{40}$/);
|
|
}
|
|
});
|