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

fix: accept large security policy files (#1883)

Michael D'Angelo0b70ee90f406
diff --git a/plugins/codex-security/mcp-app/src/helpers/resolve-security-md.ts b/plugins/codex-security/mcp-app/src/helpers/resolve-security-md.ts
index b64fe8566..ffe4e2cc2 100644
--- a/plugins/codex-security/mcp-app/src/helpers/resolve-security-md.ts
+++ b/plugins/codex-security/mcp-app/src/helpers/resolve-security-md.ts
@@ -1,11 +1,9 @@
import { decodeUtf8 } from "./utf8";
import {
- closeSync,
lstatSync,
mkdirSync,
- openSync,
readdirSync,
- readSync,
+ readFileSync,
statSync,
writeFileSync,
type Stats,
@@ -21,7 +19,6 @@ import {
resolvePosixPath,
} from "./posix-path";
-const MAX_SECURITY_MD_BYTES = 1024 * 1024;
const windows = process.platform === "win32";
export const windowsFiles = () => windowsFileSystem(windowsBinding());
const encodePath = (path: string) =>
@@ -310,33 +307,9 @@ function listSecurityMd(repo: string, posixHome: string | undefined): string[] {
}
function readPolicy(path: Buffer, displayedPath: Buffer): string {
- const buffer = Buffer.alloc(MAX_SECURITY_MD_BYTES + 1);
- let length = 0;
- if (windows) {
- length = windowsFiles().readInto(path, buffer);
- } else {
- const file = openSync(path, "r");
- try {
- while (length < buffer.length) {
- const count = readSync(
- file,
- buffer,
- length,
- buffer.length - length,
- null,
- );
- if (count === 0) break;
- length += count;
- }
- } finally {
- closeSync(file);
- }
- }
- if (length > MAX_SECURITY_MD_BYTES) {
- throw new Error(`SECURITY.md exceeds 1 MiB: ${decodePath(displayedPath)}`);
- }
+ const buffer = windows ? windowsFiles().readFile(path) : readFileSync(path);
try {
- return decodeUtf8(buffer.subarray(0, length));
+ return decodeUtf8(buffer);
} catch {
throw new Error(
`SECURITY.md is not valid UTF-8: ${decodePath(displayedPath)}`,
diff --git a/plugins/codex-security/skills/define-security-policy/SKILL.md b/plugins/codex-security/skills/define-security-policy/SKILL.md
index a13fd4230..e3c52b56b 100644
--- a/plugins/codex-security/skills/define-security-policy/SKILL.md
+++ b/plugins/codex-security/skills/define-security-policy/SKILL.md
@@ -15,7 +15,7 @@ Confirm the repository or component the user wants to cover. Inventory policy pa
<plugin_dir>/scripts/launch_codex_security_mcp --helper resolve-security-md --repo <repo_root> --list
```
-On Windows, use `launch_codex_security_mcp.cmd` with the same arguments. The launcher reuses the plugin's configured or bundled Node runtime. It emits a sorted JSON array of repository-relative policy paths, escapes control characters unambiguously, includes linked policies without following directory links, and prunes Git metadata. Resolve each candidate within the repository and check the resolved regular file's byte size. Do not pass policies larger than 1 MiB to the resolver; report them so the user can decide how to proceed. The resolver enforces the same limit for regular files and repository-local symbolic links.
+On Windows, use `launch_codex_security_mcp.cmd` with the same arguments. The launcher reuses the plugin's configured or bundled Node runtime. It emits a sorted JSON array of repository-relative policy paths, escapes control characters unambiguously, includes linked policies without following directory links, and prunes Git metadata. The resolver reads regular UTF-8 policy files, including symbolic links whose targets remain within the repository.
Read `../../references/security-guidance.md`, then resolve the policy chain for the file or directory being reviewed:
diff --git a/sdk/typescript/src/security-policy.ts b/sdk/typescript/src/security-policy.ts
index a145fe015..17d385fc4 100644
--- a/sdk/typescript/src/security-policy.ts
+++ b/sdk/typescript/src/security-policy.ts
@@ -178,8 +178,6 @@ export interface SecurityPolicyDraft
const execFileAsync = promisify(execFile);
const MANIFEST_NAME = "policy-draft.json";
const ORIGINAL_NAME = "previous-SECURITY.md";
-// This is the input contract enforced by the resolve-security-md helper.
-const MAX_SECURITY_MD_BYTES = 1024 * 1024;
// The define-security-policy skill asks at most three questions at once.
const OWNER_QUESTION_BATCH_SIZE = 3;
@@ -262,21 +260,7 @@ async function readPolicyFile(
`Security policy must not be a hard-linked file: ${path}. Copy it to a separate file.`,
);
}
- validatePolicySize(metadata.size);
- const bytes = Buffer.allocUnsafe(MAX_SECURITY_MD_BYTES + 1);
- let length = 0;
- while (length < bytes.length) {
- const { bytesRead } = await file.read(
- bytes,
- length,
- bytes.length - length,
- null,
- );
- if (bytesRead === 0) break;
- length += bytesRead;
- }
- validatePolicySize(length);
- return decodePolicyText(bytes.subarray(0, length), path);
+ return decodePolicyText(await file.readFile(), path);
} finally {
await file.close();
}
@@ -1053,16 +1037,6 @@ function validatePolicyContent(
`The ${stage === "policy" ? "security policy" : stage.replace("_", " ")} must contain valid Unicode text.`,
);
}
- if (stage === "policy")
- validatePolicySize(Buffer.byteLength(content, "utf8"));
-}
-
-function validatePolicySize(size: number): void {
- if (size > MAX_SECURITY_MD_BYTES) {
- throw new CodexSecurityError(
- "SECURITY.md exceeds the policy resolver's 1 MiB limit.",
- );
- }
}
function decodePolicyText(bytes: Uint8Array, path: string): string {
diff --git a/sdk/typescript/tests-ts/api-policy.test.ts b/sdk/typescript/tests-ts/api-policy.test.ts
index eaf50aa5c..40defbf92 100644
--- a/sdk/typescript/tests-ts/api-policy.test.ts
+++ b/sdk/typescript/tests-ts/api-policy.test.ts
@@ -888,28 +888,37 @@ describe("CodexSecurity policy API", () => {
}
});
- test("validates descendant policy contents before starting Codex", async () => {
- for (const [content, message] of [
- [Buffer.alloc(1024 * 1024 + 1, "x"), "1 MiB"],
- [Buffer.from([0xff]), "UTF-8"],
- ] as const) {
- let prepared = false;
- const f = await setup({ onPrepare: () => (prepared = true) });
- const child = join(f.repository, "child");
- await mkdir(child);
- await writeFile(join(child, "SECURITY.md"), content);
- const options = { outputDir: f.outputDir };
- await expect(
- f.security.preflightPolicy(f.repository, options),
- ).rejects.toThrow(message);
- await expect(
- f.security.generatePolicy(f.repository, options),
- ).rejects.toThrow(message);
- expect(prepared).toBe(false);
- expect(f.threads).toHaveLength(0);
- expect(await readdir(f.outputDir)).toEqual([]);
- await f.security.close();
- }
+ test("rejects non-UTF-8 descendant policies before starting Codex", async () => {
+ let prepared = false;
+ const f = await setup({ onPrepare: () => (prepared = true) });
+ const child = join(f.repository, "child");
+ await mkdir(child);
+ await writeFile(join(child, "SECURITY.md"), Buffer.from([0xff]));
+ const options = { outputDir: f.outputDir };
+ await expect(
+ f.security.preflightPolicy(f.repository, options),
+ ).rejects.toThrow("UTF-8");
+ await expect(
+ f.security.generatePolicy(f.repository, options),
+ ).rejects.toThrow("UTF-8");
+ expect(prepared).toBe(false);
+ expect(f.threads).toHaveLength(0);
+ expect(await readdir(f.outputDir)).toEqual([]);
+ await f.security.close();
+ });
+
+ test("accepts descendant policies larger than 1 MiB before starting Codex", async () => {
+ const f = await setup();
+ const child = join(f.repository, "child");
+ await mkdir(child);
+ const content = `# Component policy\n${"é".repeat(1024 * 1024)}\n`;
+ await writeFile(join(child, "SECURITY.md"), content);
+ const options = { outputDir: f.outputDir };
+ await f.security.preflightPolicy(f.repository, options);
+ const draft = await f.security.generatePolicy(f.repository, options);
+ expect(draft.content).toBe(POLICY);
+ expect(await readFile(join(child, "SECURITY.md"), "utf8")).toBe(content);
+ await f.security.close();
});
test.each(["path", "directory"])(
diff --git a/sdk/typescript/tests-ts/security-policy-helper.test.ts b/sdk/typescript/tests-ts/security-policy-helper.test.ts
index ee1e8b655..4a08dbc5d 100644
--- a/sdk/typescript/tests-ts/security-policy-helper.test.ts
+++ b/sdk/typescript/tests-ts/security-policy-helper.test.ts
@@ -917,33 +917,51 @@ binding.readdir = function (...args) {
}
});
- test("rejects non-UTF-8 and oversized regular or linked policies", () => {
- for (const kind of ["non-utf8", "oversized", "linked"]) {
+ test("rejects non-UTF-8 regular or linked policies", () => {
+ for (const kind of ["regular", "linked"]) {
const { root } = fixture();
- const content =
- kind === "non-utf8"
- ? Buffer.from([0xff])
- : Buffer.alloc(1024 * 1024 + 1, "a");
- write(root, kind === "linked" ? "large.md" : "SECURITY.md", content);
+ write(
+ root,
+ kind === "linked" ? "policy.md" : "SECURITY.md",
+ Buffer.from([0xff]),
+ );
if (kind === "linked")
- symlinkSync(join(root, "large.md"), join(root, "SECURITY.md"), "file");
+ symlinkSync(join(root, "policy.md"), join(root, "SECURITY.md"), "file");
const result = resolve(root, ".");
expect(result.status, result.stderr).toBe(2);
- expect(result.stderr).toContain(
- kind === "non-utf8" ? "not valid UTF-8" : "exceeds 1 MiB",
- );
+ expect(result.stderr).toContain("not valid UTF-8");
expect(result.stdout).toBe("");
}
});
- test("accepts a policy exactly at the byte limit", () => {
- const { root } = fixture();
- const content = "a".repeat(1024 * 1024);
- write(root, "SECURITY.md", content);
- const result = resolve(root, ".");
- expect(result.status, result.stderr).toBe(0);
- expectGuidance(result.stdout, [["SECURITY.md", content]]);
- });
+ test.each(["regular", "linked"])(
+ "resolves complete root and nested %s policies larger than 1 MiB",
+ (kind) => {
+ const { root } = fixture();
+ const rootContent = `${"root policy é\n".repeat(100_000)}root policy end\n`;
+ const nestedContent = `${"nested policy 文\n".repeat(100_000)}nested policy end\n`;
+ for (const [path, content, target] of [
+ ["SECURITY.md", rootContent, "policies/root.md"],
+ ["component/SECURITY.md", nestedContent, "policies/component.md"],
+ ] as const) {
+ expect(Buffer.byteLength(content, "utf8")).toBeGreaterThan(1024 * 1024);
+ write(root, kind === "linked" ? target : path, content);
+ if (kind === "linked") {
+ mkdirSync(dirname(join(root, path)), { recursive: true });
+ symlinkSync(join(root, target), join(root, path), "file");
+ }
+ }
+ const rootResult = resolve(root, ".");
+ expect(rootResult.status, rootResult.stderr).toBe(0);
+ expectGuidance(rootResult.stdout, [["SECURITY.md", rootContent]]);
+ const nestedResult = resolve(root, "component");
+ expect(nestedResult.status, nestedResult.stderr).toBe(0);
+ expectGuidance(nestedResult.stdout, [
+ ["SECURITY.md", rootContent],
+ ["component/SECURITY.md", nestedContent],
+ ]);
+ },
+ );
test("preserves required and mutually exclusive helper arguments", () => {
const { root } = fixture();
diff --git a/sdk/typescript/tests-ts/security-policy.test.ts b/sdk/typescript/tests-ts/security-policy.test.ts
index 308cc5b96..f93bfbaaa 100644
--- a/sdk/typescript/tests-ts/security-policy.test.ts
+++ b/sdk/typescript/tests-ts/security-policy.test.ts
@@ -931,12 +931,7 @@ describe("security policy generation", () => {
}
test("retains completed evidence without saving invalid policy documents", async () => {
- for (const markdown of [
- "",
- " \n\t",
- "# Policy\n\ud800",
- `# Policy\n${"x".repeat(1024 * 1024)}`,
- ]) {
+ for (const markdown of ["", " \n\t", "# Policy\n\ud800"]) {
const f = await fixture();
await expect(
f.generate({
@@ -953,18 +948,31 @@ describe("security policy generation", () => {
}
});
- test("enforces the resolver byte limit on existing policies", async () => {
- const header = "# Policy\n";
- const maximum =
- header + "x".repeat(1024 * 1024 - Buffer.byteLength(header));
- const existing = await fixture();
- const target = join(existing.repository, "SECURITY.md");
- await writeFile(target, maximum);
- expect(await readSecurityPolicy(target)).toBe(maximum);
- await writeFile(target, `${maximum}x`);
- await expect(existing.generate()).rejects.toThrow("1 MiB limit");
- expect(await readdir(existing.outputDir)).toEqual([]);
- });
+ test.each([".", "component"])(
+ "reads and generates policies larger than 1 MiB at %s",
+ async (scope) => {
+ const f = await fixture();
+ const directory = join(f.repository, scope);
+ await mkdir(directory, { recursive: true });
+ const target = join(directory, "SECURITY.md");
+ const previous = `# Existing policy\n${"é".repeat(1024 * 1024)}\n`;
+ const proposed = `# Proposed policy\n${"λ".repeat(1024 * 1024)}\n`;
+ await writeFile(target, previous);
+ expect(await readSecurityPolicy(target)).toBe(previous);
+
+ const draft = await f.generate({
+ path: scope,
+ run: async (stage) => stageResult(stage, proposed),
+ });
+ expect(draft.previousContent).toBe(previous);
+ expect(
+ await readFile(join(f.outputDir, "previous-SECURITY.md"), "utf8"),
+ ).toBe(previous);
+ expect(draft.content).toBe(proposed);
+ expect(await readFile(draft.draftPath, "utf8")).toBe(proposed);
+ expect(await readSecurityPolicy(target)).toBe(previous);
+ },
+ );
test.each(["architecture", "threat_model"] as const)(
"rejects malformed Unicode in %s evidence before saving it",
@@ -993,7 +1001,7 @@ describe("security policy generation", () => {
},
);
- test("does not apply the policy byte limit to supporting evidence", async () => {
+ test("preserves supporting evidence larger than 1 MiB", async () => {
const f = await fixture();
const document = `# Evidence\n${"x".repeat(1024 * 1024)}`;
const draft = await f.generate({