gameservers-control/test/storage.test.js
Shinuwa d74f80fbf9
All checks were successful
Deploy Gameservers Control / check (push) Successful in 6s
Deploy Gameservers Control / deploy (push) Has been skipped
feat(security): harden HTTP, storage, status reading and launches
- security headers on every response (strict CSP, nosniff, frame DENY,
  no-referrer, HSTS behind TLS proxy) and no x-powered-by; hero image set
  through the CSSOM so the CSP needs no inline styles
- error handler: 400 without stack trace for malformed URLs/JSON and multer
  errors, 500 logged in full
- image uploads: type detected from the file signature (PNG/JPEG/WebP), not
  the client MIME type; multer part limits
- storage: atomic JSON writes (tmp + rename) with mode 600, dirs 700; refuse
  to start when groups.json is corrupted instead of recreating admin; initial
  admin password written to a 600 file instead of the logs
- status file read asynchronously in the background and cached, one read at
  a time with a 3 s timeout, so a hung NFS mount never blocks requests;
  cleanup refused when the status is stale or empty
- launches: 10 min cooldown after a successful start (status lags behind),
  ssh BatchMode/ConnectTimeout/IdentitiesOnly, remote stdout/stderr kept in
  the server log instead of the API response

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 12:46:53 +01:00

54 lines
2.1 KiB
JavaScript

import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
const storageModule = new URL("../src/services/storage.js", import.meta.url).href;
// Chaque cas tourne dans un processus a part: la config (STORAGE_DIR) est lue a l'import.
async function runEnsureStorageLayout(storageDir) {
return execFileAsync(
process.execPath,
[
"--input-type=module",
"-e",
`const m = await import(${JSON.stringify(storageModule)}); await m.ensureStorageLayout();`,
],
{ env: { ...process.env, STORAGE_DIR: storageDir } }
);
}
function mode(file) {
return fs.statSync(file).mode & 0o777;
}
test("first start writes private files and the admin password to a file, not the logs", async () => {
const storageDir = fs.mkdtempSync(path.join(os.tmpdir(), "gsc-storage-"));
const { stdout } = await runEnsureStorageLayout(storageDir);
const passwordFile = path.join(storageDir, "initial-admin-password.txt");
const password = fs.readFileSync(passwordFile, "utf8").trim();
assert.ok(password.length >= 12);
assert.equal(stdout.includes(password), false);
assert.equal(mode(passwordFile), 0o600);
assert.equal(mode(path.join(storageDir, "groups.json")), 0o600);
assert.equal(mode(path.join(storageDir, "servers.json")), 0o600);
assert.deepEqual(
fs.readdirSync(storageDir).filter((name) => name.endsWith(".tmp")),
[]
);
});
test("refuses to start when groups.json is corrupted instead of recreating admin", async () => {
const storageDir = fs.mkdtempSync(path.join(os.tmpdir(), "gsc-storage-"));
const groupsFile = path.join(storageDir, "groups.json");
fs.writeFileSync(groupsFile, '[{"id": "admin", "passwordHash": ');
await assert.rejects(runEnsureStorageLayout(storageDir));
assert.equal(fs.readFileSync(groupsFile, "utf8"), '[{"id": "admin", "passwordHash": ');
assert.equal(fs.existsSync(path.join(storageDir, "initial-admin-password.txt")), false);
});