자식 프로세스를 띄우는 Vitest 시험 91개(24파일)가 기본 timeout 10초에 기대고 있었다. 셋은 FE-GATE-013에서만 도는 tests/system 시험이다. 기본값은 프로세스 안에서 도는 시험에 맞춘 값이다. 부하가 높은 desktop에서는 자식 프로세스 시험이 평소보다 크게 느려진다. FE-GATE-020 drill 사본에서 평소 0.4~1.2초인 문서 시험이 10.6초 걸려 실패했다(2026-11-12). 가장 위험한 것은 tests/architecture/app-build-input .test.ts로, desktop에서 이미 5.7초까지 걸렸다. 무엇을 고칠지는 desktop 계측으로 정했다. - test:coverage 묶음(maxWorkers=4)과 test:unit을 세 번씩 돌렸다. 시험마다 띄운 자식 프로세스는 child_process 함수를 감싸 기록했고, 소요 시간은 JSON reporter로 받았다. - 시험마다 실제 timeout은 task.timeout으로 기록했다. - 자식 프로세스를 띄우는 시험 140개 중 88개가 기본값이었다. tests/system은 그 묶음에 없어서, 아래 검사가 잡은 셋을 따로 세 번 쟀다. 고친 것: - tests/helpers/child-process-timeout.ts의 childProcessTimeout(<잰 최대 ms>)는 그 15배를 30초 단위로 올린 값이다. 15배는 위 0.7초 → 10.6초에서 왔다. 시험마다 자기가 잰 최대값을 인자로 적어 근거가 호출 자리에 남는다. - describe 안의 기본값 시험이 모두 자식 프로세스를 띄우면 describe에, 일부만 띄우면 그 시험에 준다. 프로세스 안에서 도는 시험의 hang을 늦게 잡지 않으려는 것이다. - tests/setup.ts가 child_process 함수를 감싼다. 기본 timeout인 시험이 자식 프로세스를 띄우면 그 호출에서 실패시키고, helper 이름을 알린다. helper나 시험이 import한 스크립트가 띄워도 잡는다. util.promisify가 읽는 속성은 원래 함수에서 옮긴다. - docs/work/reference/testing.md 2절에 값 정하는 법과 이 검사를 적었다. 버린 것: - 전역 testTimeout 올리기: 프로세스 안 시험의 hang을 늦게 잡는다. - 시험마다 손으로 정한 숫자: 근거가 남지 않고, 이미 있는 값도 60_000·120_000처럼 근거가 없었다. - 자식 프로세스를 띄우지 않게 바꾸기: 검사 스크립트를 CLI 그대로 시험하는 것이 이 시험들의 목적이라 범위가 커진다. - 파일에 timeout 선언이 있는지만 보는 정적 검사: helper가 띄우는 경우와 같은 파일의 다른 시험을 놓친다. 위험: - hook(beforeAll 등)에서 띄우는 프로세스는 검사하지 않는다. 계측에서 hook이 띄운 곳은 둘이고, 둘 다 180초를 선언했다. - 같은 파일 안에서 it.concurrent로 도는 시험이 섞이면 어느 시험의 timeout인지 틀릴 수 있다. 지금은 그런 시험이 없다. - 다운스트림 시험이 기본값으로 자식 프로세스를 띄우면 test:unit 등이 실패한다. 메시지가 helper를 알린다. 검증(desktop): - 다섯 검사(test:unit·check:types·check:ci·lint·verify:documentation) rc=0. - 자식 프로세스를 띄우는 시험 파일 40개와 tests/scale: rc=0, 검사 위반 0. - tests/system: 115개 중 18개 실패. 모두 bwrap 호스트 제약이고 검사 위반은 0이다. - 변이: 검사를 끄면 tests/unit/child-process-timeout.test.ts가 실패한다. tracked-artifacts describe의 timeout을 지우면 그 시험이 검사 메시지로 실패한다. - 재현: CPU 10% 제한에서 app-build-input·registry-governance·hosting-probe ·design-system-rules를 돌렸다. main 650d22fa는 11개가 10초 timeout으로 실패했다. 이 커밋은 71개 모두 통과했고, 가장 긴 시험은 17.7초였다.
371 lines
15 KiB
TypeScript
371 lines
15 KiB
TypeScript
import { spawnSync } from "node:child_process";
|
|
import {
|
|
chmod,
|
|
lstat,
|
|
mkdir,
|
|
mkdtemp,
|
|
readFile,
|
|
readdir,
|
|
rename,
|
|
rm,
|
|
writeFile,
|
|
} from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import { pathToFileURL } from "node:url";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { PROMOTED_FILE_NAMES } from "../../scripts/contracts/promotion-artifacts.ts";
|
|
import { publishPrivatePromotionStaging } from "../../scripts/lib/promotion-stager.ts";
|
|
import { verifyExactPromotionBundle } from "../../scripts/lib/exact-promotion-bundle.ts";
|
|
import {
|
|
PROCESS_HEAVY_TIMEOUT_MS,
|
|
privatePromotionFiles,
|
|
syntheticSignedPromotionBundle,
|
|
} from "./security-followup-fixture.ts";
|
|
import { childProcessTimeout } from "../helpers/child-process-timeout.ts";
|
|
|
|
describe("security private promotion staging contracts", () => {
|
|
it("forces exact private staging modes in an isolated child with umask 077", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "promotion-umask-"));
|
|
try {
|
|
const stagerUrl = pathToFileURL(
|
|
path.join(process.cwd(), "scripts/lib/promotion-stager.ts"),
|
|
).href;
|
|
const contractsUrl = pathToFileURL(
|
|
path.join(process.cwd(), "scripts/contracts/promotion-artifacts.ts"),
|
|
).href;
|
|
const childPath = path.join(root, "umask-child.mjs");
|
|
const resultPath = path.join(root, "result.json");
|
|
await writeFile(resultPath, "{}\n", { mode: 0o600 });
|
|
await writeFile(
|
|
childPath,
|
|
[
|
|
`import { lstat, writeFile } from "node:fs/promises";`,
|
|
`import path from "node:path";`,
|
|
`import { createHash } from "node:crypto";`,
|
|
`import { cleanupFinalizedPromotion, publishPrivatePromotionStaging } from ${JSON.stringify(stagerUrl)};`,
|
|
`import { PROMOTED_FILE_NAMES } from ${JSON.stringify(contractsUrl)};`,
|
|
`process.umask(Number.parseInt(process.argv[2], 8));`,
|
|
`const runnerTempRoot = process.argv[3];`,
|
|
`const files = PROMOTED_FILE_NAMES.map((name) => { const bytes = Buffer.from(name); return { name, bytes, sha256: createHash("sha256").update(bytes).digest("hex") }; });`,
|
|
`const finalized = await publishPrivatePromotionStaging(runnerTempRoot, { id: "umask", attempt: 1 }, files, () => Buffer.alloc(16, 1));`,
|
|
`const directoryMode = (await lstat(finalized.stagingRoot)).mode & 0o777;`,
|
|
`const fileModes = await Promise.all(PROMOTED_FILE_NAMES.map(async (name) => (await lstat(path.join(finalized.stagingRoot, name))).mode & 0o777));`,
|
|
`await cleanupFinalizedPromotion({ runnerTempRoot, stagingRoot: finalized.stagingRoot, cleanupToken: finalized.cleanupToken, runnerTempIdentity: finalized.runnerTempIdentity, stagingIdentity: finalized.stagingIdentity });`,
|
|
`await writeFile(process.argv[4], JSON.stringify({ directoryMode, fileModes }));`,
|
|
].join("\n"),
|
|
);
|
|
const child = spawnSync(process.execPath, [childPath, "077", root, resultPath], {
|
|
cwd: root,
|
|
encoding: "utf8",
|
|
timeout: 30_000,
|
|
});
|
|
expect(child.status, `${child.stdout}\n${child.stderr}`).toBe(0);
|
|
expect(JSON.parse(await readFile(resultPath, "utf8"))).toEqual({
|
|
directoryMode: 0o700,
|
|
fileModes: [0o400, 0o400, 0o400, 0o400, 0o400],
|
|
});
|
|
} finally {
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
}, childProcessTimeout(2_361));
|
|
|
|
it("rejects a staged file unlinked and recreated after its original write", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "promotion-seal-recreate-"));
|
|
const files = privatePromotionFiles();
|
|
const token = `promotion-seal-1-${"11".repeat(16)}`;
|
|
try {
|
|
await expect(
|
|
publishPrivatePromotionStaging(
|
|
root,
|
|
{ id: "seal", attempt: 1 },
|
|
files,
|
|
() => Buffer.alloc(16, 0x11),
|
|
undefined,
|
|
async (name) => {
|
|
if (name !== PROMOTED_FILE_NAMES.at(-1)) return;
|
|
const first = path.join(root, token, PROMOTED_FILE_NAMES[0]);
|
|
await rm(first);
|
|
await writeFile(first, "replacement bytes\n", { mode: 0o400 });
|
|
},
|
|
),
|
|
).rejects.toThrow(/staged.*digest|inode|seal/u);
|
|
} finally {
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("rejects staged mode drift before returning the upload root", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "promotion-seal-mode-"));
|
|
const files = privatePromotionFiles();
|
|
const token = `promotion-seal-1-${"12".repeat(16)}`;
|
|
try {
|
|
await expect(
|
|
publishPrivatePromotionStaging(
|
|
root,
|
|
{ id: "seal", attempt: 1 },
|
|
files,
|
|
() => Buffer.alloc(16, 0x12),
|
|
undefined,
|
|
async (name) => {
|
|
if (name === PROMOTED_FILE_NAMES.at(-1)) {
|
|
await chmod(path.join(root, token, PROMOTED_FILE_NAMES[0]), 0o600);
|
|
}
|
|
},
|
|
),
|
|
).rejects.toThrow(/mode.*0400|staged.*mode|seal/u);
|
|
} finally {
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("rejects staging leaf replacement between mkdir and descriptor open", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "promotion-preopen-replace-"));
|
|
const files = privatePromotionFiles();
|
|
const token = `promotion-preopen-1-${"13".repeat(16)}`;
|
|
const displaced = path.join(root, `${token}-displaced`);
|
|
const replacementCanary = path.join(root, token, PROMOTED_FILE_NAMES[0]);
|
|
try {
|
|
await expect(
|
|
publishPrivatePromotionStaging(
|
|
root,
|
|
{ id: "preopen", attempt: 1 },
|
|
files,
|
|
() => Buffer.alloc(16, 0x13),
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
async (stagingRoot) => {
|
|
await rename(stagingRoot, displaced);
|
|
await mkdir(stagingRoot, { mode: 0o700 });
|
|
await writeFile(replacementCanary, "external replacement canary\n");
|
|
},
|
|
),
|
|
).rejects.toThrow(/staging leaf.*changed|mkdir.*open|identity/u);
|
|
await expect(readFile(replacementCanary, "utf8")).resolves.toBe(
|
|
"external replacement canary\n",
|
|
);
|
|
await expect(readdir(displaced)).resolves.toEqual([]);
|
|
} finally {
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("does not scan a crowded parent to recover an unverified pre-open leaf", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "promotion-preopen-bounded-"));
|
|
const files = privatePromotionFiles();
|
|
const token = `promotion-preopen-bound-1-${"14".repeat(16)}`;
|
|
const displaced = path.join(root, `${token}-displaced`);
|
|
const replacementCanary = path.join(root, token, PROMOTED_FILE_NAMES[0]);
|
|
try {
|
|
for (let offset = 0; offset < 4_097; offset += 128) {
|
|
await Promise.all(
|
|
Array.from(
|
|
{ length: Math.min(128, 4_097 - offset) },
|
|
(_, index) =>
|
|
mkdir(
|
|
path.join(
|
|
root,
|
|
`noise-${String(offset + index).padStart(4, "0")}`,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
let failure: unknown;
|
|
try {
|
|
await publishPrivatePromotionStaging(
|
|
root,
|
|
{ id: "preopen-bound", attempt: 1 },
|
|
files,
|
|
() => Buffer.alloc(16, 0x14),
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
async (stagingRoot) => {
|
|
await rename(stagingRoot, displaced);
|
|
await mkdir(stagingRoot, { mode: 0o700 });
|
|
await writeFile(replacementCanary, "external replacement canary\n");
|
|
},
|
|
);
|
|
} catch (error) {
|
|
failure = error;
|
|
}
|
|
expect(failure).toBeInstanceOf(Error);
|
|
expect(failure).not.toBeInstanceOf(AggregateError);
|
|
expect((failure as Error).message).toMatch(/staging leaf.*identity/i);
|
|
await expect(readdir(displaced)).resolves.toEqual([]);
|
|
await expect(readFile(replacementCanary, "utf8")).resolves.toBe(
|
|
"external replacement canary\n",
|
|
);
|
|
} finally {
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
}, 20_000);
|
|
|
|
it("leaves a non-empty moved original untouched after pre-open mismatch", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "promotion-preopen-nonempty-"));
|
|
const files = privatePromotionFiles();
|
|
const token = `promotion-preopen-nonempty-1-${"15".repeat(16)}`;
|
|
const displaced = path.join(root, `${token}-displaced`);
|
|
const ownedResidual = path.join(displaced, "owned-residual");
|
|
const replacementCanary = path.join(root, token, PROMOTED_FILE_NAMES[0]);
|
|
try {
|
|
let failure: unknown;
|
|
try {
|
|
await publishPrivatePromotionStaging(
|
|
root,
|
|
{ id: "preopen-nonempty", attempt: 1 },
|
|
files,
|
|
() => Buffer.alloc(16, 0x15),
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
async (stagingRoot) => {
|
|
await rename(stagingRoot, displaced);
|
|
await writeFile(ownedResidual, "owned residual\n");
|
|
await mkdir(stagingRoot, { mode: 0o700 });
|
|
await writeFile(replacementCanary, "external replacement canary\n");
|
|
},
|
|
);
|
|
} catch (error) {
|
|
failure = error;
|
|
}
|
|
expect(failure).toBeInstanceOf(Error);
|
|
expect(failure).not.toBeInstanceOf(AggregateError);
|
|
expect((failure as Error).message).toMatch(/staging leaf.*identity/i);
|
|
await expect(readFile(ownedResidual, "utf8")).resolves.toBe(
|
|
"owned residual\n",
|
|
);
|
|
await expect(readFile(replacementCanary, "utf8")).resolves.toBe(
|
|
"external replacement canary\n",
|
|
);
|
|
} finally {
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("does not search outside the parent for a moved unverified original", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "promotion-preopen-missing-"));
|
|
const outside = await mkdtemp(path.join(tmpdir(), "promotion-preopen-moved-"));
|
|
const files = privatePromotionFiles();
|
|
const token = `promotion-preopen-missing-1-${"16".repeat(16)}`;
|
|
const displaced = path.join(outside, token);
|
|
const replacementCanary = path.join(root, token, PROMOTED_FILE_NAMES[0]);
|
|
try {
|
|
let failure: unknown;
|
|
try {
|
|
await publishPrivatePromotionStaging(
|
|
root,
|
|
{ id: "preopen-missing", attempt: 1 },
|
|
files,
|
|
() => Buffer.alloc(16, 0x16),
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
async (stagingRoot) => {
|
|
await rename(stagingRoot, displaced);
|
|
await mkdir(stagingRoot, { mode: 0o700 });
|
|
await writeFile(replacementCanary, "external replacement canary\n");
|
|
},
|
|
);
|
|
} catch (error) {
|
|
failure = error;
|
|
}
|
|
expect(failure).toBeInstanceOf(Error);
|
|
expect(failure).not.toBeInstanceOf(AggregateError);
|
|
expect((failure as Error).message).toMatch(/staging leaf.*identity/i);
|
|
await expect(lstat(displaced)).resolves.toEqual(
|
|
expect.objectContaining({ dev: expect.any(Number), ino: expect.any(Number) }),
|
|
);
|
|
await expect(readFile(replacementCanary, "utf8")).resolves.toBe(
|
|
"external replacement canary\n",
|
|
);
|
|
} finally {
|
|
await rm(root, { recursive: true, force: true });
|
|
await rm(outside, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("rejects a fresh signed exact-five bundle replayed under a different expected run", async () => {
|
|
const fixture = syntheticSignedPromotionBundle();
|
|
await expect(
|
|
verifyExactPromotionBundle(fixture.files, {
|
|
...fixture.verification,
|
|
expected: {
|
|
...fixture.verification.expected,
|
|
run: { id: "different-run", attempt: 1 },
|
|
},
|
|
}),
|
|
).rejects.toThrow(/external expected run.*mismatch|expected promotion run/u);
|
|
});
|
|
|
|
it("requires every external expected identity variable at the exact promotion CLI", async () => {
|
|
const fixture = syntheticSignedPromotionBundle();
|
|
const root = await mkdtemp(path.join(tmpdir(), "promotion-replay-cli-"));
|
|
const bundleRoot = path.join(root, "bundle");
|
|
try {
|
|
await mkdir(bundleRoot);
|
|
for (const [name, bytes] of Object.entries(fixture.files)) {
|
|
await writeFile(path.join(bundleRoot, name), bytes);
|
|
}
|
|
await writeFile(path.join(root, "vulnerability.pem"), fixture.vulnerabilityPem);
|
|
await writeFile(path.join(root, "provenance.pem"), fixture.provenancePem);
|
|
const cliPath = path.join(process.cwd(), "scripts/verify-exact-promotion-bundle.ts");
|
|
const baseEnvironment: NodeJS.ProcessEnv = {
|
|
...process.env,
|
|
PROMOTION_BUNDLE_ROOT: bundleRoot,
|
|
VULNERABILITY_PUBLIC_KEY_PATH: "vulnerability.pem",
|
|
VULNERABILITY_KEY_ID: "synthetic-vulnerability",
|
|
PROVENANCE_PUBLIC_KEY_PATH: "provenance.pem",
|
|
PROVENANCE_KEY_ID: "synthetic-provenance",
|
|
EXPECTED_PROMOTION_RUN_ID: fixture.verification.expected.run.id,
|
|
EXPECTED_PROMOTION_RUN_ATTEMPT: String(
|
|
fixture.verification.expected.run.attempt,
|
|
),
|
|
EXPECTED_PROMOTION_SOURCE_REVISION:
|
|
fixture.verification.expected.sourceRevision,
|
|
EXPECTED_PROMOTION_ARCHIVE_SHA256:
|
|
fixture.verification.expected.archiveSha256,
|
|
};
|
|
const requiredExpected = [
|
|
"EXPECTED_PROMOTION_RUN_ID",
|
|
"EXPECTED_PROMOTION_RUN_ATTEMPT",
|
|
"EXPECTED_PROMOTION_SOURCE_REVISION",
|
|
"EXPECTED_PROMOTION_ARCHIVE_SHA256",
|
|
] as const;
|
|
for (const missing of requiredExpected) {
|
|
const environment = { ...baseEnvironment };
|
|
delete environment[missing];
|
|
const result = spawnSync(process.execPath, [cliPath], {
|
|
cwd: root,
|
|
encoding: "utf8",
|
|
env: environment,
|
|
});
|
|
expect(result.status, missing).not.toBe(0);
|
|
expect(result.stderr, missing).toContain(
|
|
`exact promotion verification environment is missing ${missing}`,
|
|
);
|
|
}
|
|
for (const [name, value, diagnostic] of [
|
|
["EXPECTED_PROMOTION_RUN_ID", "different-run", /external expected run.*mismatch/u],
|
|
["EXPECTED_PROMOTION_RUN_ATTEMPT", "2", /external expected run.*mismatch/u],
|
|
["EXPECTED_PROMOTION_SOURCE_REVISION", "f".repeat(40), /external expected source revision.*mismatch/u],
|
|
["EXPECTED_PROMOTION_ARCHIVE_SHA256", "0".repeat(64), /external expected archive digest.*mismatch/u],
|
|
] as const) {
|
|
const result = spawnSync(process.execPath, [cliPath], {
|
|
cwd: root,
|
|
encoding: "utf8",
|
|
env: { ...baseEnvironment, [name]: value },
|
|
});
|
|
expect(result.status, name).not.toBe(0);
|
|
expect(result.stderr, name).toMatch(diagnostic);
|
|
}
|
|
} finally {
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
}, PROCESS_HEAVY_TIMEOUT_MS);
|
|
});
|