Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions scripts/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { join, resolve } from "node:path";

const root = resolve(import.meta.dir, "..");
const script = join(root, "scripts/snapshot.sh");
const warning = "::warning::Snapshots are not encrypted; anyone who can read this repository can download them.";

// Stands in for the broker: the OIDC value names the repository, and the key is derived from that alone.
let recoveringRequests = 0;
Expand Down Expand Up @@ -41,11 +42,11 @@ const runnerTemp = home({});
// The broker answers on this process, so the script must run without blocking the event loop.
async function run(args: string[], repo = "23", extraEnv: Record<string, string> = {}) {
const proc = Bun.spawn(["bash", script, ...args], {
env: { ...process.env, RUNNER_TEMP: runnerTemp, GITHUB_ACTION_PATH: root, BROKER: `http://127.0.0.1:${server.port}`, ACTIONS_ID_TOKEN_REQUEST_URL: `http://127.0.0.1:${server.port}/oidc?repo=${repo}&`, ACTIONS_ID_TOKEN_REQUEST_TOKEN: "runner", ENCRYPT_SNAPSHOTS: "true", ...extraEnv },
env: { ...process.env, RUNNER_TEMP: runnerTemp, GITHUB_ACTION_PATH: root, GITHUB_ENV: "", CODE_REVIEW_PLAINTEXT_SNAPSHOT_WARNED: "", BROKER: `http://127.0.0.1:${server.port}`, ACTIONS_ID_TOKEN_REQUEST_URL: `http://127.0.0.1:${server.port}/oidc?repo=${repo}&`, ACTIONS_ID_TOKEN_REQUEST_TOKEN: "runner", ENCRYPT_SNAPSHOTS: "true", ...extraEnv },
stdout: "pipe", stderr: "pipe",
});
const [exitCode, stderr] = await Promise.all([proc.exited, new Response(proc.stderr).text()]);
return { exitCode, stderr };
const [exitCode, stdout, stderr] = await Promise.all([proc.exited, new Response(proc.stdout).text(), new Response(proc.stderr).text()]);
return { exitCode, stdout, stderr };
}

test("save encrypts the session and load restores it", async () => {
Expand Down Expand Up @@ -105,12 +106,54 @@ test("encryption is off unless the repository asks for it, and needs no broker",
const archive = join(home({}), "snapshot.bin");
const saved = await run(["save", archive, source, "last-thread-id"], "23", { ENCRYPT_SNAPSHOTS: "false", BROKER: "" });
expect(saved.exitCode).toBe(0);
expect(saved.stdout).toContain(warning);
expect(readFileSync(archive).subarray(0, 7).toString()).toBe("CRSNP0\n");
const target = home({});
expect((await run(["load", archive, target], "23", { BROKER: "" })).exitCode).toBe(0);
expect(readFileSync(join(target, "last-thread-id"), "utf8")).toBe("thread-1\n");
});

test.each(["session", "events"])("plaintext archives warn once per job when %s is saved first", async first => {
const source = home({ "last-thread-id": "thread-1\n", "codex.jsonl": "review event\n", "first-env": "OTHER=value\n", "next-env": "" });
const session = ["save", join(source, "snapshot.bin"), source];
const events = ["save", join(source, "codex.jsonl.bin"), source, "codex.jsonl"];
const [firstSave, nextSave] = first === "session" ? [session, events] : [events, session];
const env = { ENCRYPT_SNAPSHOTS: "false", BROKER: "", GITHUB_ENV: join(source, "first-env") };
const saved = await run(firstSave, "23", env);
expect(saved.exitCode).toBe(0);
expect(saved.stdout.split("\n").filter(line => line === warning)).toHaveLength(1);
// Each Actions step has a new env file; the runner carries its additions into the next process.
const persisted = Object.fromEntries(readFileSync(env.GITHUB_ENV, "utf8").trim().split("\n").map(line => line.split("=")));
expect(persisted.OTHER).toBe("value");
const next = await run(nextSave, "23", { ...env, ...persisted, GITHUB_ENV: join(source, "next-env") });
expect(next.exitCode).toBe(0);
expect(next.stdout).not.toContain(warning);
expect(readFileSync(join(source, "next-env"), "utf8")).toBe("");
for (const archive of [session[1], events[1]]) expect(readFileSync(archive).subarray(0, 7).toString()).toBe("CRSNP0\n");
// A fresh job on the same runner must warn again, even when its source files are reused.
const fresh = await run(nextSave, "23", { ...env, GITHUB_ENV: join(source, "next-env") });
expect(fresh.exitCode).toBe(0);
expect(fresh.stdout).toContain(warning);
});

test("failed and encrypted saves leave the first plaintext warning available", async () => {
const source = home({ "last-thread-id": "thread-1\n", "codex.jsonl": "review event\n", "github-env": "" });
const env = { GITHUB_ENV: join(source, "github-env") };
const archive = join(source, "snapshot.bin");
const failed = await run(["save", archive, home({})], "23", { ...env, ENCRYPT_SNAPSHOTS: "false" });
expect(failed.exitCode).not.toBe(0);
expect(failed.stdout).not.toContain(warning);
expect(readFileSync(env.GITHUB_ENV, "utf8")).toBe("");
const encrypted = await run(["save", archive, source], "23", env);
expect(encrypted.exitCode).toBe(0);
expect(encrypted.stdout).not.toContain(warning);
expect(readFileSync(archive).subarray(0, 7).toString()).toBe("CRSNP1\n");
expect(readFileSync(env.GITHUB_ENV, "utf8")).toBe("");
const plain = await run(["save", join(source, "codex.jsonl.bin"), source, "codex.jsonl"], "23", { ...env, ENCRYPT_SNAPSHOTS: "false" });
expect(plain.exitCode).toBe(0);
expect(plain.stdout).toContain(warning);
});

test("turning encryption off still restores what an encrypted save wrote", async () => {
const source = home({ "last-thread-id": "thread-1\n" });
const archive = join(home({}), "snapshot.bin");
Expand Down
8 changes: 6 additions & 2 deletions scripts/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// artifacts can download them. The broker derives the key from the repository alone, so changing the App,
// the provider or the model never strands an older snapshot.
// Usage: snapshot.ts save <archive> <dir> [path...] | snapshot.ts load <archive> <dir>
import { existsSync, mkdirSync, rmSync } from "node:fs";
import { appendFileSync, existsSync, mkdirSync, rmSync } from "node:fs";
import { join } from "node:path";

const [command, archive, dir, ...paths] = process.argv.slice(2);
Expand Down Expand Up @@ -49,7 +49,11 @@ if (command === "save") {
if (await tar.exited) throw new Error("Could not read the session into an archive");
const key = encryption === "true" ? await fetchKey() : null;
if (!key) {
console.log("::warning::Snapshots are not encrypted; anyone who can read this repository can download them.");
if (process.env.CODE_REVIEW_PLAINTEXT_SNAPSHOT_WARNED !== "true") {
console.log("::warning::Snapshots are not encrypted; anyone who can read this repository can download them.");
// Session and debug archives are saved in separate steps; the marker lasts only for this job.
if (process.env.GITHUB_ENV) appendFileSync(process.env.GITHUB_ENV, "CODE_REVIEW_PLAINTEXT_SNAPSHOT_WARNED=true\n");
}
await Bun.write(archive, Buffer.concat([plainMark, plain]));
} else {
const iv = crypto.getRandomValues(new Uint8Array(12));
Expand Down
Loading