Fix the audit's concurrency, stale state and delivery findings
Share uploads now reserve their slot, bytes and per-IP count before the disk check, so simultaneous uploads can no longer exceed the limits. Writes to a share's metadata run one after another, and a revocation can no longer be undone by a pending read count. The TCG page ignores a state reload that started before or during a mutation, the last mutation wins, and returning to the page triggers a single reload. The card cache confirms writes only once the IndexedDB transaction completes and shows only the current player's copy. The administration panel loads on demand, which shrinks the TCG chunk from 311.6 to 191.8 kB. A new /api/health route answers 503 when the cards or shares API failed to start. The deploy script ships the commit's production dependencies with the build, waits up to 30 s for a healthy server instead of trusting systemctl is-active, and restores node_modules on rollback. Both Forgejo workflows check the route on the public site. The updated script applies once reinstalled on the server. Unused helpers, the ignored join role and duplicate button selectors are removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0571a935d6
commit
067a4a8fd6
20 changed files with 391 additions and 73 deletions
|
|
@ -99,6 +99,21 @@ test("the versioned remote script builds and activates only the requested commit
|
|||
assert.doesNotMatch(script, /git\s+pull/);
|
||||
});
|
||||
|
||||
// Le service ne doit jamais tourner avec les dépendances d'une livraison précédente, ni être déclaré livré sans avoir démarré.
|
||||
test("the remote script ships the commit's dependencies and waits for a healthy server", async () => {
|
||||
const script = await readFile(remoteScriptPath, "utf8");
|
||||
|
||||
assert.match(script, /"\$NPM_BIN" --prefix "\$worktree" prune --omit=dev/);
|
||||
assert.match(script, /as_app_user mv "\$APP_ROOT\/node_modules" "\$backup_node_modules"/);
|
||||
assert.match(script, /as_app_user mv "\$worktree\/node_modules" "\$APP_ROOT\/node_modules"/);
|
||||
assert.match(script, /as_app_user mv "\$backup_node_modules" "\$APP_ROOT\/node_modules"/);
|
||||
assert.match(script, /\/api\/health/);
|
||||
assert.match(script, /systemctl restart "\$SERVICE_NAME" \|\| ! wait_until_healthy/);
|
||||
for (const workflow of [validationWorkflowPath, deployWorkflowPath]) {
|
||||
assert.match(await readFile(workflow, "utf8"), /sokkog\.shinuwa\.fr\/api\/health \| grep -Fq '"ok":true'/);
|
||||
}
|
||||
});
|
||||
|
||||
test("the production service uses the same pinned Node runtime", async () => {
|
||||
const service = await readFile(servicePath, "utf8");
|
||||
|
||||
|
|
@ -115,7 +130,7 @@ test("the remote script never runs files owned by the service account as root",
|
|||
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);
|
||||
assert.doesNotMatch(script, /^\s*(?:mv .*(?:\$APP_ROOT|\$worktree|\$backup_dist|\$backup_node_modules)|rm -rf -- "\$APP_ROOT)/m);
|
||||
});
|
||||
|
||||
test("the services cannot gain privileges through sudo", async () => {
|
||||
|
|
|
|||
|
|
@ -88,6 +88,18 @@ test("a Sokko TCG configuration failure disables the game and keeps the site run
|
|||
assert.match(await rawRequest(port, {}), /^HTTP\/1\.1 200 /);
|
||||
const config = await fetch(`http://127.0.0.1:${port}/api/cards/config`).then((response) => response.json());
|
||||
assert.equal(config.enabled, false);
|
||||
// La livraison s'appuie sur cette route pour refuser une version dont le jeu n'a pas démarré.
|
||||
const health = await fetch(`http://127.0.0.1:${port}/api/health`);
|
||||
assert.equal(health.status, 503);
|
||||
assert.deepEqual(await health.json(), { ok: false, unavailable: ["cards"] });
|
||||
});
|
||||
|
||||
test("the health route answers when every requested API started, including a deliberately disabled one", async (t) => {
|
||||
const port = await listen(t, createCardsApi({ enabled: false, env: { NODE_ENV: "test" } }));
|
||||
const health = await fetch(`http://127.0.0.1:${port}/api/health`);
|
||||
assert.equal(health.status, 200);
|
||||
assert.equal(health.headers.get("cache-control"), "no-store");
|
||||
assert.deepEqual(await health.json(), { ok: true, unavailable: [] });
|
||||
});
|
||||
|
||||
test("SIGTERM closes the database cleanly so no write-ahead log stays behind", async (t) => {
|
||||
|
|
|
|||
|
|
@ -293,6 +293,59 @@ test("concurrent uploads are capped and a stalled upload is cut off", async (t)
|
|||
assert.equal((await upload(base, payload())).status, 201);
|
||||
});
|
||||
|
||||
// Toutes les vérifications attendent ensemble le disque : sans réservation préalable, chacune validerait la même place.
|
||||
test("simultaneous uploads cannot share the last slot while the disk check is pending", async (t) => {
|
||||
// Le disque ne répond qu'une fois les trois requêtes arrivées : chacune attend sa vérification ou a déjà été refusée.
|
||||
let waiting = 0;
|
||||
let refused = 0;
|
||||
let openDisk;
|
||||
let diskOpen;
|
||||
const statfs = async () => {
|
||||
waiting += 1;
|
||||
await diskOpen;
|
||||
return { bavail: TB, bsize: 1 };
|
||||
};
|
||||
const warn = console.warn;
|
||||
console.warn = (message) => { if (String(message).startsWith("Toolbox share refused")) refused += 1; };
|
||||
t.after(() => { console.warn = warn; });
|
||||
const releaseDiskChecks = async (count) => {
|
||||
await waitFor(() => waiting + refused >= count);
|
||||
openDisk();
|
||||
};
|
||||
|
||||
const cases = [
|
||||
{ limits: { maxConcurrentUploads: 1 }, code: "SHARE_BUSY" },
|
||||
{ limits: { maxActiveShares: 1 }, code: "SHARE_STORAGE_FULL" },
|
||||
{ limits: { maxTotalBytes: 300 }, code: "SHARE_STORAGE_FULL" },
|
||||
{ limits: { dailyBytes: 300 }, code: "SHARE_DAILY_QUOTA" },
|
||||
{ limits: { ipDailyUploads: 1 }, code: "SHARE_IP_QUOTA" },
|
||||
{ limits: { ipDailyBytes: 300 }, code: "SHARE_IP_QUOTA" }
|
||||
];
|
||||
for (const { limits, code } of cases) {
|
||||
const { base, sharesApi } = await setup(t, { statfs, limits });
|
||||
waiting = 0;
|
||||
refused = 0;
|
||||
diskOpen = new Promise((resolvePromise) => { openDisk = resolvePromise; });
|
||||
const responses = [upload(base, payload(256)), upload(base, payload(256)), upload(base, payload(256))];
|
||||
await releaseDiskChecks(3);
|
||||
const statuses = await Promise.all(responses.map(async (response) => {
|
||||
const result = await response;
|
||||
return { status: result.status, code: (await result.json()).error?.code };
|
||||
}));
|
||||
assert.equal(statuses.filter(({ status }) => status === 201).length, 1, `${JSON.stringify(limits)}: ${JSON.stringify(statuses)}`);
|
||||
assert.ok(statuses.filter(({ status }) => status !== 201).every((entry) => entry.code === code), `${JSON.stringify(limits)}: ${JSON.stringify(statuses)}`);
|
||||
assert.equal(sharesApi.store.count, 1);
|
||||
}
|
||||
});
|
||||
|
||||
test("a refused disk check releases its reservation", async (t) => {
|
||||
let free = 0;
|
||||
const { base } = await setup(t, { statfs: async () => ({ bavail: free, bsize: 1 }), limits: { maxConcurrentUploads: 1, minFreeBytes: 1000 } });
|
||||
assert.equal((await upload(base, payload())).status, 507);
|
||||
free = TB;
|
||||
assert.equal((await upload(base, payload())).status, 201);
|
||||
});
|
||||
|
||||
test("a reusable share stops answering after its maximum number of reads", async (t) => {
|
||||
const { base } = await setup(t, { limits: { maxReads: 2 } });
|
||||
const share = await created(base, payload());
|
||||
|
|
|
|||
39
tests/toolbox-shares-store.test.mjs
Normal file
39
tests/toolbox-shares-store.test.mjs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
// Rôle : vérifie que les métadonnées d'un envoi de toolbox restent justes sur disque quand plusieurs lectures ou une révocation se croisent.
|
||||
import assert from "node:assert/strict";
|
||||
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { test } from "node:test";
|
||||
import { ShareStore } from "../services/shares/store.js";
|
||||
|
||||
const ID = "AAAAAAAAAAAAAAAAAAAAAA";
|
||||
|
||||
async function storeWithShare(t) {
|
||||
const dir = await mkdtemp(join(tmpdir(), "sokko-share-store-"));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const now = Date.UTC(2026, 10, 20, 12);
|
||||
const store = new ShareStore({ dir, now: () => now });
|
||||
await writeFile(store.partPath(ID), Buffer.alloc(64, 1));
|
||||
await store.commit(ID, { createdAt: now, expiresAt: now + 60_000, size: 64, singleUse: false, reads: 0, deleteTokenHash: "00" });
|
||||
return { dir, store, now };
|
||||
}
|
||||
|
||||
test("simultaneous reads of a reusable share all reach the file on disk", async (t) => {
|
||||
const { dir, store, now } = await storeWithShare(t);
|
||||
const results = await Promise.allSettled(Array.from({ length: 10 }, () => store.recordRead(ID)));
|
||||
assert.deepEqual(results.filter(({ status }) => status === "rejected").map(({ reason }) => reason.code), []);
|
||||
assert.equal(JSON.parse(readFileSync(store.metaPath(ID), "utf8")).reads, 10);
|
||||
assert.deepEqual(readdirSync(dir).filter((name) => name.endsWith(".tmp")), []);
|
||||
assert.equal(new ShareStore({ dir, now: () => now }).get(ID).reads, 10);
|
||||
});
|
||||
|
||||
test("a revocation is not undone by a read still being written", async (t) => {
|
||||
const { dir, store, now } = await storeWithShare(t);
|
||||
const reads = [store.recordRead(ID), store.recordRead(ID)];
|
||||
await store.remove(ID);
|
||||
await Promise.all(reads);
|
||||
assert.equal(existsSync(store.metaPath(ID)), false);
|
||||
assert.equal(existsSync(store.binPath(ID)), false);
|
||||
assert.equal(new ShareStore({ dir, now: () => now }).count, 0);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue