Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

refactor!: compute policy diffs without Python (#1837)

Michael D'Angelo8f02b49da206
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index cf80a3042..2e0844b55 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -377,7 +377,16 @@ try {
`preflightPolicy()` checks local inputs. `previewPolicy()` previews the supplied
in-memory draft and escapes terminal controls; editing its saved file does not
change that object. `securityPolicyDiff()` returns a raw diff for files and other
-non-terminal uses, with an optional Python interpreter.
+non-terminal uses. Both preview methods compute the diff in Node without
+discovering or launching Python; `pythonPath` and `PYTHON` do not affect previews.
+
+The optional second argument to `securityPolicyDiff(draft, python, signal)` is
+deprecated. Interpreter paths, including paths returned by a resolver callback,
+no longer select an executable or get validated. For compatibility, an explicitly
+supplied resolver is still awaited when the draft changes, and any error it throws
+propagates. Omit that argument for new code, or pass `undefined` when supplying a
+cancellation signal. Source freshness is checked before and after computing the
+diff.
`generatePolicy()` accepts `auth`, `path`, `knowledgeBasePaths`, `outputDir`,
`maxCostUsd`, `signal`, and progress/cost callbacks. `answerQuestions` receives
diff --git a/sdk/typescript/scripts/package-dist-files.mjs b/sdk/typescript/scripts/package-dist-files.mjs
index 5f73d814c..a7c8df98b 100644
--- a/sdk/typescript/scripts/package-dist-files.mjs
+++ b/sdk/typescript/scripts/package-dist-files.mjs
@@ -80,6 +80,7 @@ const distModules = [
"scan-logs",
"security-policy",
"security-policy-cli",
+ "security-policy-diff",
"standalone-validation-prompt",
"validation-records",
"suggest-owners",
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index bd5c74ea7..9db967bc5 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -248,7 +248,6 @@ import {
} from "./runtime.js";
import {
enclosingGitWorktreeRoot,
- enclosingGitWorktreeRoots,
normalizeRepository,
normalizeTarget,
gitMarkerRoot,
@@ -1255,22 +1254,7 @@ export class CodexSecurity {
...(options.signal === undefined ? [] : [options.signal]),
]);
return formatSecurityPolicyText(
- await securityPolicyDiff(
- draft,
- async () =>
- await (
- this.#dependencies.resolvePluginPython ?? resolvePluginPython
- )({
- configuredPath: this.config.pythonPath,
- environment: this.#dependencies.environment,
- protectedRoot:
- (await enclosingGitWorktreeRoots(draft.repository, signal)).at(
- -1,
- ) ?? draft.repository,
- signal,
- }),
- signal,
- ),
+ await securityPolicyDiff(draft, undefined, signal),
true,
);
});
diff --git a/sdk/typescript/src/security-policy-diff.ts b/sdk/typescript/src/security-policy-diff.ts
new file mode 100644
index 000000000..d6701ea9a
--- /dev/null
+++ b/sdk/typescript/src/security-policy-diff.ts
@@ -0,0 +1,214 @@
+import { setImmediate } from "node:timers/promises";
+
+type Match = [a: number, b: number, size: number];
+type Range = [aStart: number, aEnd: number, bStart: number, bEnd: number];
+type Opcode = [equal: boolean, ...Range];
+type Work<T> = Generator<void, T>;
+
+function* lines(text: string): Work<string[]> {
+ const result: string[] = [];
+ let start = 0;
+ for (let end = 0; end < text.length; end++) {
+ if (text.charCodeAt(end) === 10) {
+ result.push(text.slice(start, end + 1));
+ start = end + 1;
+ }
+ yield;
+ }
+ if (start < text.length) result.push(text.slice(start));
+ return result;
+}
+
+// Match Python's SequenceMatcher(None, a, b), including its default autojunk
+// heuristic. A different longest-common-subsequence algorithm changes the diff.
+function* matchingBlocks(a: string[], b: string[]): Work<Match[]> {
+ const positions = new Map<string, number[]>();
+ for (let j = 0; j < b.length; j++) {
+ const line = b[j]!;
+ const indices = positions.get(line);
+ if (indices) indices.push(j);
+ else positions.set(line, [j]);
+ yield;
+ }
+ if (b.length >= 200) {
+ const threshold = Math.floor(b.length / 100) + 1;
+ for (const [line, indices] of positions) {
+ if (indices.length > threshold) positions.delete(line);
+ yield;
+ }
+ }
+
+ const pending: (Range | Match)[] = [[0, a.length, 0, b.length]];
+ const matches: Match[] = [];
+ while (pending.length) {
+ const range = pending.pop()!;
+ if (range.length === 3) {
+ const previous = matches.at(-1);
+ if (
+ previous &&
+ previous[0] + previous[2] === range[0] &&
+ previous[1] + previous[2] === range[1]
+ ) {
+ previous[2] += range[2];
+ } else {
+ matches.push(range);
+ }
+ yield;
+ continue;
+ }
+
+ const [alo, ahi, blo, bhi] = range;
+ let bestA = alo;
+ let bestB = blo;
+ let size = 0;
+ let lengths = new Map<number, number>();
+ for (let i = alo; i < ahi; i++) {
+ const next = new Map<number, number>();
+ for (const j of positions.get(a[i]!) ?? []) {
+ yield;
+ if (j < blo) continue;
+ if (j >= bhi) break;
+ const length = (lengths.get(j - 1) ?? 0) + 1;
+ next.set(j, length);
+ if (length > size) {
+ bestA = i - length + 1;
+ bestB = j - length + 1;
+ size = length;
+ }
+ }
+ lengths = next;
+ yield;
+ }
+ // Popular lines may extend a match, even though they cannot anchor one.
+ while (bestA > alo && bestB > blo && a[bestA - 1] === b[bestB - 1]) {
+ bestA--;
+ bestB--;
+ size++;
+ yield;
+ }
+ while (
+ bestA + size < ahi &&
+ bestB + size < bhi &&
+ a[bestA + size] === b[bestB + size]
+ ) {
+ size++;
+ yield;
+ }
+ if (size) {
+ // Visit matches in order, avoiding a blocking sort of the result.
+ if (bestA + size < ahi && bestB + size < bhi) {
+ pending.push([bestA + size, ahi, bestB + size, bhi]);
+ }
+ pending.push([bestA, bestB, size]);
+ if (alo < bestA && blo < bestB) {
+ pending.push([alo, bestA, blo, bestB]);
+ }
+ }
+ yield;
+ }
+ matches.push([a.length, b.length, 0]);
+ return matches;
+}
+
+function formatRange(start: number, end: number): string {
+ const length = end - start;
+ return length === 1
+ ? String(start + 1)
+ : `${length ? start + 1 : start},${length}`;
+}
+
+function* unifiedDiff(
+ before: string,
+ after: string,
+ fromfile: string,
+ tofile: string,
+): Work<string> {
+ const a = yield* lines(before);
+ const b = yield* lines(after);
+ const matches = yield* matchingBlocks(a, b);
+ const codes: Opcode[] = [];
+ let i = 0;
+ let j = 0;
+ for (const [ai, bj, size] of matches) {
+ if (i < ai || j < bj) codes.push([false, i, ai, j, bj]);
+ i = ai + size;
+ j = bj + size;
+ if (size) codes.push([true, ai, i, bj, j]);
+ yield;
+ }
+ const first = codes[0];
+ const last = codes.at(-1);
+ if (first?.[0]) {
+ first[1] = Math.max(first[1], first[2] - 3);
+ first[3] = Math.max(first[3], first[4] - 3);
+ }
+ if (last?.[0]) {
+ last[2] = Math.min(last[2], last[1] + 3);
+ last[4] = Math.min(last[4], last[3] + 3);
+ }
+
+ const output: string[] = [];
+ function* emit(group: Opcode[]): Work<void> {
+ if (!group.length || (group.length === 1 && group[0]![0])) return;
+ if (!output.length) output.push(`--- ${fromfile}\n+++ ${tofile}\n`);
+ const first = group[0]!;
+ const last = group.at(-1)!;
+ output.push(
+ `@@ -${formatRange(first[1], last[2])} +${formatRange(first[3], last[4])} @@\n`,
+ );
+ for (const [equal, aStart, aEnd, bStart, bEnd] of group) {
+ for (let index = aStart; index < aEnd; index++) {
+ const line = a[index]!;
+ output.push((equal ? " " : "-") + line);
+ if (!line.endsWith("\n")) {
+ output.push("\n\\ No newline at end of file\n");
+ }
+ yield;
+ }
+ if (!equal) {
+ for (let index = bStart; index < bEnd; index++) {
+ const line = b[index]!;
+ output.push("+" + line);
+ if (!line.endsWith("\n")) {
+ output.push("\n\\ No newline at end of file\n");
+ }
+ yield;
+ }
+ }
+ yield;
+ }
+ }
+
+ let group: Opcode[] = [];
+ for (const [equal, aStart, aEnd, bStart, bEnd] of codes) {
+ if (equal && aEnd - aStart > 6) {
+ group.push([true, aStart, aStart + 3, bStart, bStart + 3]);
+ yield* emit(group);
+ group = [[true, aEnd - 3, aEnd, bEnd - 3, bEnd]];
+ } else {
+ group.push([equal, aStart, aEnd, bStart, bEnd]);
+ }
+ yield;
+ }
+ yield* emit(group);
+ return output.join("");
+}
+
+export async function securityPolicyUnifiedDiff(
+ before: string,
+ after: string,
+ fromfile: string,
+ tofile: string,
+ signal?: AbortSignal,
+): Promise<string> {
+ const work = unifiedDiff(before, after, fromfile, tofile);
+ for (;;) {
+ signal?.throwIfAborted();
+ // Bound synchronous work, including the quadratic inner matching loop.
+ for (let operation = 0; operation < 4096; operation++) {
+ const result = work.next();
+ if (result.done) return result.value;
+ }
+ await setImmediate();
+ }
+}
diff --git a/sdk/typescript/src/security-policy.ts b/sdk/typescript/src/security-policy.ts
index 58039722c..a145fe015 100644
--- a/sdk/typescript/src/security-policy.ts
+++ b/sdk/typescript/src/security-policy.ts
@@ -32,7 +32,8 @@ import type { ScanCost } from "./cost.js";
import type { ThreatModel } from "./models.js";
import { writeThreatModel } from "./artifact-export.js";
import { CodexSecurityError, InvalidTargetError } from "./errors.js";
-import { resolvePluginPython, type ProcessEnvironment } from "./runtime.js";
+import type { ProcessEnvironment } from "./runtime.js";
+import { securityPolicyUnifiedDiff } from "./security-policy-diff.js";
import {
nullIfMissingFile,
nullIfMissingPath,
@@ -975,8 +976,12 @@ export async function runSecurityPolicyStages(options: {
};
}
-/** Raw unified diff. A Python resolver is called only when there is a change.
- * Use CodexSecurity.previewPolicy() for terminal output. */
+/**
+ * Raw unified diff computed in Node. Use CodexSecurity.previewPolicy() for terminal output.
+ * @param python Deprecated interpreter selection. Strings and resolver results no longer
+ * select an executable. An explicit resolver is still awaited when there is a change,
+ * and its errors propagate.
+ */
export async function securityPolicyDiff(
draft: SecurityPolicyDraft,
python?: string | (() => Promise<string>),
@@ -986,67 +991,17 @@ export async function securityPolicyDiff(
const target = await resolveDraftTarget(draft, signal);
await requireUnchangedSecurityPolicy(target, draft, signal);
if (draft.previousContent === draft.content) return "";
- const selectedPython = typeof python === "function" ? await python() : python;
- const interpreter =
- selectedPython ??
- (await resolvePluginPython({
- protectedRoot:
- (await enclosingGitWorktreeRoots(draft.repository, signal)).at(-1) ??
- draft.repository,
- signal,
- }));
+ if (typeof python === "function") await python();
const label = relative(draft.repository, draft.targetPath)
.split(sep)
.join("/");
- const script = [
- "import difflib, json, sys",
- "before, after, fromfile, tofile = json.loads(sys.stdin.buffer.read().decode('utf-8'))",
- "def lines(text):",
- " parts = text.split('\\n')",
- " return [part + '\\n' for part in parts[:-1]] + ([parts[-1]] if parts[-1] else [])",
- "for line in difflib.unified_diff(lines(before), lines(after), fromfile=fromfile, tofile=tofile):",
- " sys.stdout.buffer.write(line.encode('utf-8'))",
- " if not line.endswith('\\n'): sys.stdout.buffer.write(b'\\n\\\\ No newline at end of file\\n')",
- ].join("\n");
- const diff = await new Promise<string>((resolve, reject) => {
- let inputError: Error | undefined;
- const child = execFile(
- interpreter,
- ["-I", "-c", script],
- {
- encoding: "utf8",
- maxBuffer: Infinity,
- signal,
- },
- (error, stdout) => {
- const failure = error ?? inputError;
- if (failure === undefined) resolve(stdout);
- else reject(failure);
- },
- );
- const releaseFailedInput = () => {
- if (inputError === undefined) return;
- if (child.exitCode === null && child.signalCode === null) {
- if (child.pid !== undefined) reject(inputError);
- return;
- }
- child.stdout!.destroy();
- child.stderr!.destroy();
- };
- child.once("exit", releaseFailedInput);
- child.stdin!.on("error", (error: Error) => {
- inputError = error;
- releaseFailedInput();
- });
- child.stdin!.end(
- JSON.stringify([
- draft.previousContent ?? "",
- draft.content,
- draft.previousContent === null ? "/dev/null" : diffLabel(`a/${label}`),
- diffLabel(`b/${label}`),
- ]),
- );
- });
+ const diff = await securityPolicyUnifiedDiff(
+ draft.previousContent ?? "",
+ draft.content,
+ draft.previousContent === null ? "/dev/null" : diffLabel(`a/${label}`),
+ diffLabel(`b/${label}`),
+ signal,
+ );
await requireUnchangedSecurityPolicy(
await resolveDraftTarget(draft, signal),
draft,
diff --git a/sdk/typescript/tests-ts/api-policy.test.ts b/sdk/typescript/tests-ts/api-policy.test.ts
index 5b6f30489..eaf50aa5c 100644
--- a/sdk/typescript/tests-ts/api-policy.test.ts
+++ b/sdk/typescript/tests-ts/api-policy.test.ts
@@ -471,7 +471,7 @@ describe("CodexSecurity policy API", () => {
await f.security.close();
});
- test("uses the client's Python and renders preview controls visibly", async () => {
+ test("renders preview controls without resolving the client's Python", async () => {
const content = `${POLICY}\n\u001b]52;c;c3ludGhldGlj\u0007\u202eOwner note\n`;
const f = await setup({
config: { pythonPath: "configured-policy-python" },
@@ -495,7 +495,7 @@ describe("CodexSecurity policy API", () => {
python.mockRestore();
}
const preview = await f.security.previewPolicy(draft);
- expect(f.pythonSelections).toHaveLength(2);
+ expect(f.pythonSelections).toHaveLength(1);
for (const selection of f.pythonSelections)
expect(selection).toMatchObject({
configuredPath: "configured-policy-python",
diff --git a/sdk/typescript/tests-ts/security-policy-diff.test.ts b/sdk/typescript/tests-ts/security-policy-diff.test.ts
new file mode 100644
index 000000000..7d27afbee
--- /dev/null
+++ b/sdk/typescript/tests-ts/security-policy-diff.test.ts
@@ -0,0 +1,152 @@
+import { expect, test } from "bun:test";
+import { securityPolicyUnifiedDiff } from "../src/security-policy-diff.js";
+
+const header = "--- a/SECURITY.md\n+++ b/SECURITY.md\n";
+const missingNewline = "\n\\ No newline at end of file\n";
+const diff = (before: string, after: string, signal?: AbortSignal) =>
+ securityPolicyUnifiedDiff(
+ before,
+ after,
+ "a/SECURITY.md",
+ "b/SECURITY.md",
+ signal,
+ );
+
+// Expected output comes from the original LF-only Python difflib wrapper.
+test.each([
+ ["empty files", "", "", ""],
+ ["unchanged unterminated content", "same", "same", ""],
+ ["insertion into an empty file", "", "added\n", "@@ -0,0 +1 @@\n+added\n"],
+ ["deletion of the entire file", "old\n", "", "@@ -1 +0,0 @@\n-old\n"],
+ [
+ "missing final newlines on both changed lines",
+ "old",
+ "new",
+ `@@ -1 +1 @@\n-old${missingNewline}+new${missingNewline}`,
+ ],
+ [
+ "adding a final newline",
+ "same",
+ "same\n",
+ `@@ -1 +1 @@\n-same${missingNewline}+same\n`,
+ ],
+ [
+ "removing a final newline",
+ "same\n",
+ "same",
+ `@@ -1 +1 @@\n-same\n+same${missingNewline}`,
+ ],
+ [
+ "retaining CR and Unicode line separators inside LF-delimited lines",
+ "old\r\nretained\rline\u2028\u2029\nend",
+ "new\r\nretained\rline\u2028\u2029\nend",
+ `@@ -1,3 +1,3 @@\n-old\r\n+new\r\n retained\rline\u2028\u2029\n end${missingNewline}`,
+ ],
+ [
+ "breaking matching ties by earliest position in the old file",
+ "A\nB\n",
+ "B\nA\n",
+ "@@ -1,2 +1,2 @@\n+B\n A\n-B\n",
+ ],
+ [
+ "then breaking matching ties by earliest position in the new file",
+ "A\nB\n",
+ "X\nA\nB\nY\nA\nB\n",
+ "@@ -1,2 +1,6 @@\n+X\n A\n B\n+Y\n+A\n+B\n",
+ ],
+ [
+ "finding the longest match before extending a common prefix",
+ "A\nB\n",
+ "A\nC\nA\nB\n",
+ "@@ -1,2 +1,4 @@\n+A\n+C\n A\n B\n",
+ ],
+ [
+ "disabling autojunk below 200 new-file lines",
+ "A\n".repeat(198),
+ "B\n" + "A\n".repeat(198),
+ "@@ -1,3 +1,4 @@\n+B\n" + " A\n".repeat(3),
+ ],
+ [
+ "enabling autojunk at 200 new-file lines",
+ "A\n".repeat(199),
+ "B\n" + "A\n".repeat(199),
+ "@@ -1,199 +1,200 @@\n" + "-A\n".repeat(199) + "+B\n" + "+A\n".repeat(199),
+ ],
+ [
+ "keeping three occurrences eligible at 200 lines",
+ "R\n".repeat(3),
+ "X\n" + "R\n".repeat(3) + "F\n".repeat(196),
+ "@@ -1,3 +1,200 @@\n+X\n" + " R\n".repeat(3) + "+F\n".repeat(196),
+ ],
+ [
+ "excluding four occurrences as popular at 200 lines",
+ "R\n".repeat(4),
+ "X\n" + "R\n".repeat(4) + "F\n".repeat(195),
+ "@@ -1,4 +1,200 @@\n" +
+ "-R\n".repeat(4) +
+ "+X\n" +
+ "+R\n".repeat(4) +
+ "+F\n".repeat(195),
+ ],
+ [
+ "extending popular lines on both sides of a rare matching anchor",
+ "R\n".repeat(99) + "A\n" + "R\n".repeat(100),
+ "X\n" + "R\n".repeat(99) + "A\n" + "R\n".repeat(100),
+ "@@ -1,3 +1,4 @@\n+X\n" + " R\n".repeat(3),
+ ],
+])("%s", async (_name, before, after, expected) => {
+ expect(await diff(before, after)).toBe(expected ? header + expected : "");
+});
+
+test.each([6, 7])(
+ "groups changes separated by %d unchanged lines with three lines of context",
+ async (count) => {
+ const context = Array.from({ length: count }, (_, i) => `${i + 1}\n`);
+ const before = "old\n" + context.join("") + "old\n";
+ const after = "new\n" + context.join("") + "new\n";
+ const expected =
+ count === 6
+ ? "@@ -1,8 +1,8 @@\n-old\n+new\n" +
+ context.map((line) => " " + line).join("") +
+ "-old\n+new\n"
+ : "@@ -1,4 +1,4 @@\n-old\n+new\n 1\n 2\n 3\n" +
+ "@@ -6,4 +6,4 @@\n 5\n 6\n 7\n-old\n+new\n";
+ expect(await diff(before, after)).toBe(header + expected);
+ },
+);
+
+test("honors an already aborted signal", async () => {
+ const reason = new Error("cancelled");
+ await expect(diff("old\n", "new\n", AbortSignal.abort(reason))).rejects.toBe(
+ reason,
+ );
+});
+
+test("allows cancellation inside repetitive-line matching", async () => {
+ const controller = new AbortController();
+ const reason = new Error("cancelled");
+ // Tokenization and indexing fit within one work batch; matching takes many.
+ const result = diff(
+ "A\n".repeat(199),
+ "B\n" + "A\n".repeat(198),
+ controller.signal,
+ );
+ setImmediate(() => controller.abort(reason));
+ await expect(result).rejects.toBe(reason);
+});
+
+test("keeps the event loop responsive while completing repetitive-line matching", async () => {
+ let heartbeatRan = false;
+ const result = diff("A\n".repeat(198), "B\n" + "A\n".repeat(198));
+ const heartbeat = new Promise<void>((resolve) => {
+ setImmediate(() => {
+ heartbeatRan = true;
+ resolve();
+ });
+ });
+ const output = await result;
+ const yieldedBeforeCompletion = heartbeatRan;
+ await heartbeat;
+ expect(yieldedBeforeCompletion).toBe(true);
+ expect(output).toBe(header + "@@ -1,3 +1,4 @@\n+B\n" + " A\n".repeat(3));
+});
diff --git a/sdk/typescript/tests-ts/security-policy.test.ts b/sdk/typescript/tests-ts/security-policy.test.ts
index 5168e3d05..308cc5b96 100644
--- a/sdk/typescript/tests-ts/security-policy.test.ts
+++ b/sdk/typescript/tests-ts/security-policy.test.ts
@@ -1042,6 +1042,28 @@ describe("security policy preview", () => {
expect(await readdir(f.repository)).toEqual([]);
});
+ test.each([null, "", "# Old policy\n"])(
+ "preserves missing and empty policy behavior (%j)",
+ async (before) => {
+ const f = await fixture();
+ if (before !== null)
+ await writeFile(join(f.repository, "SECURITY.md"), before);
+ const draft = await f.generate();
+ const diff = await securityPolicyDiff({ ...draft, content: "" });
+ expect(diff).toBe(
+ before
+ ? "--- a/SECURITY.md\n+++ b/SECURITY.md\n@@ -1 +0,0 @@\n-# Old policy\n"
+ : "",
+ );
+ if (before === "") {
+ expect(await securityPolicyDiff(draft)).toStartWith(
+ "--- a/SECURITY.md\n+++ b/SECURITY.md\n",
+ );
+ }
+ expect(await readSecurityPolicy(draft.targetPath)).toBe(before);
+ },
+ );
+
test("rejects preview after a component changes Git roots", async () => {
for (const change of ["add", "remove"] as const) {
const f = await fixture();
@@ -1085,7 +1107,7 @@ describe("security policy preview", () => {
expect(diff.match(/No newline at end of file/gu)).toHaveLength(2);
});
- test("preserves native launch errors and settles failed diff input", async () => {
+ test("previews with Python unavailable in a Node process", async () => {
const f = await fixture();
const draft = await f.generate();
const source = new URL("../src/security-policy.ts", import.meta.url);
@@ -1098,14 +1120,7 @@ describe("security policy preview", () => {
expect(built.success).toBe(true);
const module = join(f.root, "policy.mjs");
await writeFile(module, await built.outputs[0]!.text());
- const ignoreInput = join(f.root, "ignore-input");
- const deniedInterpreter = join(f.root, "denied-interpreter");
- if (process.platform !== "win32") {
- await writeFile(ignoreInput, "#!/bin/sh\nexit 0\n", { mode: 0o700 });
- await writeFile(deniedInterpreter, "#!/bin/sh\nexit 0\n", {
- mode: 0o600,
- });
- }
+ const missingPython = join(f.root, "missing-python");
const result = JSON.parse(
execFileSync(
nodeCommand().command,
@@ -1113,89 +1128,66 @@ describe("security policy preview", () => {
"--input-type=module",
"--eval",
`
-import { readFileSync, writeFileSync, existsSync } from "node:fs";
-import { setTimeout as delay } from "node:timers/promises";
+import { readFileSync } from "node:fs";
const { securityPolicyDiff } = await import(process.argv[1]);
const draft = JSON.parse(readFileSync(0, "utf8"));
-const failures = [];
-for (const [interpreter, size] of [[process.execPath, 100], [process.argv[3], 900000], [process.argv[4], 900000], [process.argv[2], 900000]].filter(([interpreter]) => interpreter)) {
- try { await securityPolicyDiff({ ...draft, content: '# Policy\\n' + 'x'.repeat(size) }, interpreter); failures.push(null); }
- catch (error) { failures.push({ code: error.code, message: error.message }); }
-}
-if (process.argv[2]) {
- const wrapper = process.argv[2];
- const release = wrapper + '-held';
- const diagnostic = ' synthetic café 日本語 😀 error\\n ';
- const holder = "const fs = require('fs'); process.send('ready'); const timer = setInterval(() => { if (fs.existsSync(process.argv[1])) { clearInterval(timer); fs.writeFileSync(process.argv[1] + '.done', 'done'); process.exit(0); } }, 10); setTimeout(() => process.exit(2), 10000).unref();";
- const launcher = [
- '#!' + process.execPath,
- "require('fs').closeSync(0);",
- "const child = require('child_process').spawn(process.execPath, ['-e', " + JSON.stringify(holder) + ", " + JSON.stringify(release) + "], { stdio: ['ignore', 1, 2, 'ipc'] });",
- "child.once('message', () => process.stderr.write(" + JSON.stringify(diagnostic) + ", () => process.exit(0)));",
- ].join('\\n');
- writeFileSync(wrapper, launcher, { mode: 0o700 });
- const task = securityPolicyDiff({ ...draft, content: '# Policy\\n' + 'x'.repeat(900000) }, wrapper).then(
- () => ({ code: null }), error => ({ code: error.code, message: error.message }));
- let outcome;
- try {
- outcome = await Promise.race([task, delay(2000).then(() => null)]);
- } finally {
- writeFileSync(release, 'release');
- await task;
- while (!existsSync(release + '.done')) await delay(10);
- }
- failures.push({ ...outcome, settledBeforeRelease: outcome !== null });
- for (const live of [true, false]) {
- const release = wrapper + '-live-' + live;
- const pid = release + '.pid';
- const finish = "require('fs').writeFileSync(" + JSON.stringify(release + '.done') + ", 'done'); process.stderr.write(" + JSON.stringify(diagnostic) + ", () => process.exit(7));";
- const body = live
- ? "const fs=require('fs'); fs.writeFileSync(" + JSON.stringify(pid) + ", String(process.pid)); fs.closeSync(0); const timer=setInterval(() => { if (fs.existsSync(" + JSON.stringify(release) + ")) { clearInterval(timer); " + finish + " } }, 10);"
- : "process.stdin.resume(); process.stdin.once('end', () => { " + finish + " });";
- writeFileSync(wrapper, ['#!' + process.execPath, body].join('\\n'), { mode: 0o700 });
- const task = securityPolicyDiff({ ...draft, content: '# Policy\\n' + 'x'.repeat(900000) }, wrapper).then(
- () => ({ code: null }), error => ({ code: error.code, message: error.message }));
- let outcome;
- let childAlive = false;
- try {
- outcome = await Promise.race([task, delay(2000).then(() => null)]);
- if (live) {
- process.kill(Number(readFileSync(pid, 'utf8')), 0);
- childAlive = true;
- }
- } finally {
- writeFileSync(release, 'release');
- await task;
- while (!existsSync(release + '.done')) await delay(10);
- }
- failures.push({ ...outcome, settledBeforeRelease: outcome !== null, childAlive, preservedDiagnostic: live || outcome?.message.endsWith(diagnostic) });
- }
-}
-console.log(JSON.stringify(failures));`,
+const defaultDiff = await securityPolicyDiff(draft);
+const explicitDiff = await securityPolicyDiff(draft, process.env.PYTHON);
+console.log(JSON.stringify({ defaultDiff, explicitDiff }));`,
pathToFileURL(module).href,
- process.platform === "win32" ? "" : ignoreInput,
- join(f.root, "missing-interpreter"),
- process.platform === "win32" ? "" : deniedInterpreter,
],
- { encoding: "utf8", input: JSON.stringify(draft), timeout: 20_000 },
+ {
+ encoding: "utf8",
+ input: JSON.stringify(draft),
+ env: {
+ ...process.env,
+ PATH: "",
+ PYTHON: missingPython,
+ CODEX_SECURITY_PYTHON_COMMAND: missingPython,
+ },
+ timeout: 20_000,
+ },
),
);
- expect(result[0].code).toBe(9);
- expect(result[0].message).toContain("bad option: -I");
- expect(result[1].code).toBe("ENOENT");
- if (process.platform !== "win32") {
- expect(result[2].code).toBe("EACCES");
- expect(result[3].code).toBe("EPIPE");
- expect(result.slice(4)).toMatchObject([
- { code: "EPIPE", settledBeforeRelease: true },
- { code: "EPIPE", settledBeforeRelease: true, childAlive: true },
- { code: 7, settledBeforeRelease: true, preservedDiagnostic: true },
- ]);
- }
+ expect(result.defaultDiff).toContain("--- /dev/null\n+++ b/SECURITY.md\n");
+ expect(result.explicitDiff).toBe(result.defaultDiff);
expect(await readdir(f.repository)).toEqual([]);
});
- test("preserves UTF-8 text and CRLF content independently of Python's locale", async () => {
+ test("awaits explicit legacy resolvers and propagates their errors", async () => {
+ const f = await fixture();
+ const draft = await f.generate();
+ const resolver = mock(async () => join(f.root, "missing-python"));
+ expect(await securityPolicyDiff(draft, resolver)).toBe(
+ await securityPolicyDiff(draft),
+ );
+ expect(resolver).toHaveBeenCalledTimes(1);
+ const failure = new Error("Synthetic resolver failure");
+ await expect(
+ securityPolicyDiff(draft, async () => {
+ throw failure;
+ }),
+ ).rejects.toBe(failure);
+ });
+
+ test("honors cancellation supplied through the public diff API", async () => {
+ const f = await fixture();
+ const draft = await f.generate();
+ const controller = new AbortController();
+ const reason = new Error("Preview cancelled");
+ await expect(
+ securityPolicyDiff(
+ draft,
+ async () => {
+ controller.abort(reason);
+ return "unused-python";
+ },
+ controller.signal,
+ ),
+ ).rejects.toBe(reason);
+ });
+
+ test("preserves UTF-8 text and CRLF content", async () => {
const f = await fixture();
await writeFile(
join(f.repository, "SECURITY.md"),