문제: provider를 띄우는 우리 단계(cgroup 강제 게이트, scope wrapper)는 provider 전에 exit 125로 멈추며 stderr에 이유를 쓴다. waitForProvider는 bwrap·prlimit·systemd-run·systemctl 접두 줄만 남기고 나머지를 provider 출력으로 보고 버린다. 그래서 FE-GATE-013의 exit=125 묶음 (ci-artifact-contract 671줄부터 18개)이 `exit=125, signal=none`만 남겨 원인을 볼 수 없었다. scope wrapper가 감독자 채널을 잃고 provider 전에 125로 끝나는 길은 아무 줄도 쓰지 않았다. 고른 방법: 줄 고르기를 scripts/lib/provider-sandbox-diagnostics.ts로 옮기고, 우리 단계가 쓰는 고정 문구를 접두가 아니라 끝까지 맞는 template 으로 받는다. cgroup 값은 max와 숫자만, 오류 코드는 errno·Node 오류 이름(E…, ERR_…)만 받고, 그 모양은 scope wrapper와 한 곳에서 정한다. 줄은 개행이 온 뒤에만 판정하고(조각난 줄이 잘린 채 남지 않게), 512자 넘는 줄은 나머지 조각까지 버리며, 8줄까지 둔다. scope wrapper는 provider 전에 멈출 때 `provider scope stopped before launch: <이유>`를 쓴다. 시험은 실제 gate shell과 그 limit 확인 함수를 돌려 나온 줄이 남는지 본다. gate 문구를 바꾸면 시험이 실패한다. 버린 대안: 허용 접두를 `provider `로 넓히기는 provider가 같은 접두로 비밀을 쓰면 실패 메시지에 싣는다. 실패 때 stderr 전부를 남기기는 provider 출력의 자격 증명을 CI 로그에 싣는다. 위험·한계: 기존 도구 접두 네 개(bwrap: 등)는 전처럼 접두만 보고 그 뒤는 512자까지 자유 문구다. liveness 채널 줄도 우리 고정 접두 뒤에 Node 오류 문구를 싣는다(출력 가능한 ASCII 200자까지). provider가 그 접두나 우리 template과 똑같은 줄을 쓰면 남는다. 검증: 노트북 감싸개에서 새 시험이 옛 허용 목록으로 13개 실패(RED), 고친 뒤 provider-sandbox-diagnostics·task3-selective-integration· test-timeout-declarations 통과. 변이 셋(끝 앵커 제거, 긴 줄 표시 제거, gate printf 문구 변경)에 시험이 각각 5·1·1개 실패. check:types·lint rc=0. desktop 시스템 컨테이너에서 이 파일 전체 4회 중 1회 125 묶음이 났고 메시지에 `sandbox reported: provider cgroup enforcement failed: cannot read memory.max`가 남았다(11건).
298 lines
12 KiB
TypeScript
298 lines
12 KiB
TypeScript
import { spawn } from "node:child_process";
|
|
import { writeFileSync } from "node:fs";
|
|
import { 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 { afterEach, describe, expect, it } from "vitest";
|
|
|
|
import { loadCiGateContract, parseCiGateContract } from "../../scripts/contracts/ci-gates.ts";
|
|
import { generateCiWorkflow } from "../../scripts/generate-ci-workflow.ts";
|
|
import { validatePackageScriptGraph } from "../../scripts/lib/package-script-graph.ts";
|
|
import { createProviderSandboxDiagnostics } from "../../scripts/lib/provider-sandbox-diagnostics.ts";
|
|
import { childProcessTimeout } from "../helpers/child-process-timeout.ts";
|
|
|
|
const roots: string[] = [];
|
|
/**
|
|
* Test watchdog only, as in tests/integration/provider-guardian-transaction.test.ts.
|
|
* It bounds a hang. The wrapper has no protocol deadline for EOF, and a loaded
|
|
* host can take longer than a second just to start a child.
|
|
*/
|
|
const PROCESS_WATCHDOG_MS = 5_000;
|
|
/** The extra pipe on which spawnLoadedScopeWrapper reports the wrapper loaded. */
|
|
const SCOPE_WRAPPER_LOADED_FD = 3;
|
|
|
|
afterEach(async () => {
|
|
await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
|
|
});
|
|
|
|
describe("selective Task 3 contract closure", () => {
|
|
it("builds a private offline aggregate-cgroup provider launch without argv secrets", async () => {
|
|
const {
|
|
encodeProviderBwrapInput,
|
|
encodeProviderScopeFrame,
|
|
formatProviderCgroupUnitName,
|
|
systemctlKillProviderArguments,
|
|
systemdRunProviderArguments,
|
|
} = await import("../../scripts/lib/provider-cgroup.ts");
|
|
const unit = formatProviderCgroupUnitName(
|
|
"vulnerability",
|
|
42,
|
|
"0123456789abcdef01234567",
|
|
);
|
|
const command = "node provider.mjs --token command-secret";
|
|
const credential = "credential-secret";
|
|
const launch = systemdRunProviderArguments(
|
|
unit,
|
|
1_800_000,
|
|
1_200,
|
|
"/trusted/node",
|
|
"/workspace/scripts/lib/provider-scope-wrapper.ts",
|
|
"/exact/report.json",
|
|
12,
|
|
34,
|
|
);
|
|
|
|
expect(launch).toEqual(expect.arrayContaining([
|
|
"--scope",
|
|
"--property=MemoryMax=1073741824",
|
|
"--property=MemorySwapMax=0",
|
|
"--property=TasksMax=64",
|
|
"--property=CPUQuota=100%",
|
|
"--property=KillMode=control-group",
|
|
"/trusted/node",
|
|
"/workspace/scripts/lib/provider-scope-wrapper.ts",
|
|
]));
|
|
expect(launch.join("\0")).not.toContain(command);
|
|
expect(launch.join("\0")).not.toContain(credential);
|
|
expect(
|
|
encodeProviderBwrapInput(
|
|
["--unshare-net", "--bind", "/exact/report.json", "/exact/report.json"],
|
|
{ PROVIDER_COMMAND: command, PROVIDER_CREDENTIAL: credential },
|
|
),
|
|
).toEqual(expect.any(Buffer));
|
|
expect(systemctlKillProviderArguments(unit)).toEqual([
|
|
"--user",
|
|
"kill",
|
|
"--kill-whom=all",
|
|
"--signal=SIGKILL",
|
|
unit,
|
|
]);
|
|
// `bwrap --args FD` stops parsing at the first non-option and never hands
|
|
// the remainder back, so a command placed in the args file is dropped and
|
|
// bubblewrap exits with its usage text. Refusing `--` in the option stream
|
|
// is what keeps that silent no-sandbox launch from returning.
|
|
expect(() =>
|
|
encodeProviderBwrapInput(
|
|
["--unshare-net", "--", "/usr/bin/prlimit"],
|
|
{ PROVIDER_COMMAND: command },
|
|
),
|
|
).toThrow(/terminate the option stream/u);
|
|
const frame = encodeProviderScopeFrame({
|
|
bwrapInput: Buffer.from("private-bwrap-vector\0"),
|
|
bwrapCommand: ["/usr/bin/prlimit", "--nofile=64:64", "--", "/bin/sh", "-eu", "-c", 'exec /bin/sh -eu -c "$PROVIDER_COMMAND"'],
|
|
reportPath: "/exact/report.json",
|
|
reportDev: 12,
|
|
reportIno: 34,
|
|
});
|
|
expect(frame.readUInt32BE(0)).toBe(frame.byteLength - 4);
|
|
expect(frame.subarray(4).toString("utf8")).toContain(
|
|
Buffer.from("private-bwrap-vector\0").toString("base64"),
|
|
);
|
|
expect(launch.join("\0")).not.toContain("private-bwrap-vector");
|
|
// The command vector rides on real argv, so it must never be able to carry
|
|
// the secret that the args file exists to hide.
|
|
expect(frame.subarray(4).toString("utf8")).not.toContain(credential);
|
|
expect(() =>
|
|
encodeProviderScopeFrame({
|
|
bwrapInput: Buffer.from("x\0"),
|
|
bwrapCommand: [],
|
|
reportPath: "/exact/report.json",
|
|
reportDev: 12,
|
|
reportIno: 34,
|
|
}),
|
|
).toThrow(/bwrap command is invalid/u);
|
|
expect(() =>
|
|
encodeProviderScopeFrame({
|
|
bwrapInput: Buffer.from("x\0"),
|
|
bwrapCommand: ["prlimit"],
|
|
reportPath: "/exact/report.json",
|
|
reportDev: 12,
|
|
reportIno: 34,
|
|
}),
|
|
).toThrow(/bwrap command is invalid/u);
|
|
});
|
|
|
|
it("removes only the pinned raw inode during parent-loss cleanup", async () => {
|
|
const { cleanupOwnedProviderReport } = await import(
|
|
"../../scripts/lib/provider-raw-cleanup.ts"
|
|
);
|
|
const root = await mkdtemp(path.join(tmpdir(), "provider-raw-cleanup-"));
|
|
roots.push(root);
|
|
const reportPath = path.join(root, "raw.json");
|
|
const originalPath = path.join(root, "original.json");
|
|
await writeFile(reportPath, "owned\n");
|
|
const identity = await lstat(reportPath);
|
|
await rename(reportPath, originalPath);
|
|
await writeFile(reportPath, "unrelated\n");
|
|
|
|
await expect(cleanupOwnedProviderReport({
|
|
reportPath,
|
|
reportDev: identity.dev,
|
|
reportIno: identity.ino,
|
|
})).resolves.toBe(false);
|
|
await expect(readFile(reportPath, "utf8")).resolves.toBe("unrelated\n");
|
|
|
|
await rm(reportPath);
|
|
await rename(originalPath, reportPath);
|
|
await expect(cleanupOwnedProviderReport({
|
|
reportPath,
|
|
reportDev: identity.dev,
|
|
reportIno: identity.ino,
|
|
})).resolves.toBe(true);
|
|
await expect(lstat(reportPath)).rejects.toMatchObject({ code: "ENOENT" });
|
|
await expect(readdir(root)).resolves.toEqual([]);
|
|
});
|
|
|
|
it("uses early liveness EOF to clean the pinned raw file without waiting for a command frame", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "provider-scope-eof-"));
|
|
roots.push(root);
|
|
const reportPath = path.join(root, "raw.json");
|
|
await writeFile(reportPath, "partial\n");
|
|
const identity = await lstat(reportPath);
|
|
const child = spawnLoadedScopeWrapper(await mkdtemp(path.join(tmpdir(), "provider-scope-loader-")), [
|
|
"1",
|
|
reportPath,
|
|
String(identity.dev),
|
|
String(identity.ino),
|
|
]);
|
|
const completion = waitForChildResult(child);
|
|
let stderr = "";
|
|
child.stderr!.setEncoding("utf8");
|
|
child.stderr!.on("data", (chunk: string) => { stderr += chunk; });
|
|
await waitForScopeWrapperLoaded(child);
|
|
// Time to exit on its own, which it must not do before EOF.
|
|
await new Promise<void>((resolve) => setTimeout(resolve, 75));
|
|
expect(child.exitCode).toBeNull();
|
|
await expect(readFile(reportPath, "utf8")).resolves.toBe("partial\n");
|
|
child.stdin!.end();
|
|
const result = await within(completion, PROCESS_WATCHDOG_MS, "provider scope EOF close");
|
|
expect(result).toEqual({ code: 125, signal: null });
|
|
// The supervisor keeps this fixed line, so its exit-125 report names the stage.
|
|
const diagnostics = createProviderSandboxDiagnostics();
|
|
diagnostics.consume("stderr", stderr);
|
|
expect(diagnostics.lines()).toEqual(["provider scope stopped before launch: supervisor channel ended"]);
|
|
await expect(lstat(reportPath)).rejects.toMatchObject({ code: "ENOENT" });
|
|
await expect(readdir(root)).resolves.toEqual([]);
|
|
}, childProcessTimeout(433));
|
|
|
|
it("tracks npm run-script dependencies instead of bypassing the graph", () => {
|
|
expect(validatePackageScriptGraph({ root: "npm run-script missing" }, "root"))
|
|
.toContain("package script missing: root -> missing");
|
|
});
|
|
|
|
// The count and gate-shape baseline (D-13, config/ci/contract-baseline.json)
|
|
// was deleted on 2026-11-08 (owner decision, design-docs repo-structure/
|
|
// README.md 3.3): the contract's own semantic checks hold its shape.
|
|
it("rejects orphan retention", async () => {
|
|
const canonical = await loadCiGateContract(process.cwd());
|
|
expect(canonical.retention.classes).toHaveLength(5);
|
|
|
|
const orphan = JSON.parse(JSON.stringify(canonical)) as Record<string, any>;
|
|
orphan.retention.classes.push({ id: "unused", policy: "never referenced" });
|
|
expect(() => parseCiGateContract(orphan)).toThrow(/five canonical retention|orphan retention/u);
|
|
});
|
|
|
|
it("rejects the retired validate-candidate-archive grammar", async () => {
|
|
const canonical = JSON.parse(
|
|
JSON.stringify(await loadCiGateContract(process.cwd())),
|
|
) as Record<string, any>;
|
|
canonical.jobs.find((job: Record<string, any>) => job.id === "vulnerability_provider")
|
|
.steps.splice(4, 0, {
|
|
kind: "validate-candidate-archive",
|
|
archivePath: ".release/candidate/release-candidate.tar.gz",
|
|
});
|
|
expect(() => parseCiGateContract(canonical)).toThrow(
|
|
/invalid discriminator|forbidden|canonical job step sequence/iu,
|
|
);
|
|
});
|
|
|
|
it("publishes a generated workflow as exactly 0644 under a restrictive umask", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "ci-workflow-mode-"));
|
|
roots.push(root);
|
|
await mkdir(path.join(root, ".gitea/workflows"), { recursive: true });
|
|
const previous = process.umask(0o777);
|
|
try {
|
|
const contract = await loadCiGateContract(process.cwd());
|
|
await generateCiWorkflow({ root, contract, check: false });
|
|
} finally {
|
|
process.umask(previous);
|
|
}
|
|
const target = path.join(root, ".gitea/workflows/quality-gates.yml");
|
|
const metadata = await lstat(target);
|
|
expect(metadata.mode & 0o777).toBe(0o644);
|
|
expect((await readFile(target, "utf8")).startsWith("# GENERATED FILE")).toBe(true);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 1103/10-01. Starts the unchanged scope wrapper through a loader that writes
|
|
* one byte to an extra pipe once the import has finished: by then the wrapper
|
|
* has opened its liveness channel and attached the EOF handler. Closing stdin
|
|
* at a fixed 75 ms put the child's whole start-up inside the EOF limit, and a
|
|
* loaded host failed the test with "EOF close timed out". The wrapper reads its
|
|
* launch identity from argv after the main module, so the loader takes the
|
|
* wrapper's place there.
|
|
*/
|
|
function spawnLoadedScopeWrapper(
|
|
loaderDirectory: string,
|
|
launchArguments: readonly string[],
|
|
): ReturnType<typeof spawn> {
|
|
roots.push(loaderDirectory);
|
|
const loader = path.join(loaderDirectory, "provider-scope-loaded.mjs");
|
|
writeFileSync(loader, [
|
|
'import { closeSync, writeSync } from "node:fs";',
|
|
`await import(${JSON.stringify(
|
|
pathToFileURL(path.resolve("scripts/lib/provider-scope-wrapper.ts")).href,
|
|
)});`,
|
|
`writeSync(${SCOPE_WRAPPER_LOADED_FD}, "1");`,
|
|
`closeSync(${SCOPE_WRAPPER_LOADED_FD});`,
|
|
"",
|
|
].join("\n"));
|
|
return spawn(process.execPath, [loader, ...launchArguments], {
|
|
stdio: ["pipe", "pipe", "pipe", "pipe"],
|
|
});
|
|
}
|
|
|
|
async function waitForScopeWrapperLoaded(child: ReturnType<typeof spawn>): Promise<void> {
|
|
const loaded = (child.stdio as readonly unknown[])[SCOPE_WRAPPER_LOADED_FD] as NodeJS.ReadableStream;
|
|
await within(new Promise<void>((resolve, reject) => {
|
|
loaded.once("data", () => resolve());
|
|
loaded.once("end", () => reject(new Error("provider scope wrapper exited before it loaded")));
|
|
}), PROCESS_WATCHDOG_MS, "provider scope wrapper start-up");
|
|
}
|
|
|
|
async function waitForChildResult(
|
|
child: ReturnType<typeof spawn>,
|
|
): Promise<Readonly<{ code: number | null; signal: NodeJS.Signals | null }>> {
|
|
return await new Promise((resolve, reject) => {
|
|
child.once("error", reject);
|
|
child.once("close", (code, signal) => resolve({ code, signal }));
|
|
});
|
|
}
|
|
|
|
async function within<T>(operation: Promise<T>, timeoutMs: number, label: string): Promise<T> {
|
|
let timer: NodeJS.Timeout | undefined;
|
|
try {
|
|
return await Promise.race([
|
|
operation,
|
|
new Promise<never>((_resolve, reject) => {
|
|
timer = setTimeout(() => reject(new Error(`${label} timed out`)), timeoutMs);
|
|
}),
|
|
]);
|
|
} finally {
|
|
if (timer) clearTimeout(timer);
|
|
}
|
|
}
|