OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(coverage): validate deferred source paths (#1573)
Peter1e3afc8ee406
diff --git a/plugins/codex-security/tests/test_finalize_scan_contract.py b/plugins/codex-security/tests/test_finalize_scan_contract.pyindex b2931cae6..9e395e061 100644--- a/plugins/codex-security/tests/test_finalize_scan_contract.py+++ b/plugins/codex-security/tests/test_finalize_scan_contract.py@@ -2525,17 +2525,29 @@ The extraction root is not enforced.FINALIZER.finalize_scan(self.scan_dir)def test_rejects_unsafe_deferred_paths(self) -> None:-- for path in ("../../outside.ts", "/outside.ts", r"C:\outside.ts"):- with self.subTest(path=path):+ for path in (+ "",+ "../../outside.ts",+ "src/../outside.ts",+ "src/..",+ "/outside.ts",+ "//server/share",+ "C:/outside.ts",+ "c:outside.ts",+ "src\\outside.ts",+ "src/\u0000outside.ts",+ "src/\u0001outside.ts",+ "src/\toutside.ts",+ "src/outside.ts\n",+ "src/\routside.ts",+ "src/\u2028/../outside.ts",+ ):+ with self.subTest(path=repr(path)):self.coverage["deferred"] = [{"id": "review", "reason": "Review is incomplete.", "paths": [path]}]self.write_scan()- with self.assertRaisesRegex(- FINALIZER.ContractError,- r"deferred\[0\]\.paths\[0\]: expected a safe repository-relative POSIX path",- ):+ with self.assertRaisesRegex(FINALIZER.ContractError, r"coverage.*deferred"):FINALIZER.finalize_scan(self.scan_dir)def test_accepts_safe_code_evidence_and_deferred_paths(self) -> None:@@ -2550,7 +2562,27 @@ The extraction root is not enforced.}]- for path in (".", "src", "src/extract.py", "src/a:b.py"):+ for path in (+ ".",+ "./",+ "././",+ "src",+ "src/parser.ts",+ "./src/parser.ts",+ "src/./parser.ts",+ "src//parser.ts",+ "src/",+ "src:parser.ts",+ "src/a:b.py",+ "src/.hidden",+ "src/na\u00efve file.ts",+ "src/\u007fparser.ts",+ "src/\u2028parser.ts",+ "src/\u2029parser.ts",+ " ",+ "\u0085",+ "\ufeff",+ ):with self.subTest(path=path):self.coverage["deferred"] = [{"id": "review", "reason": "Review is incomplete.", "paths": [path]}diff --git a/sdk/typescript/src/contract.ts b/sdk/typescript/src/contract.tsindex 1927fa553..6b6acb7c8 100644--- a/sdk/typescript/src/contract.ts+++ b/sdk/typescript/src/contract.ts@@ -162,7 +162,7 @@ export async function loadContractWithScanDirectory();}- validateCanonicalContract(manifest, findings);+ validateCanonicalContract(manifest, findings, coverage);await validateSeal(scanDir,@@ -204,6 +204,7 @@ function normalizePersistedFindings(payload: unknown): unknown {function validateCanonicalContract(manifest: ScanManifest,findings: FindingsDocument,+ coverage: CoverageDocument,): void {const remote = manifest.scan.target.remote;if (remote !== undefined) {@@ -259,6 +260,20 @@ function validateCanonicalContract(}}+ for (const [index, deferred] of coverage.deferred.entries()) {+ for (const [pathIndex, path] of (deferred.paths ?? []).entries()) {+ const context = `coverage.deferred[${index}].paths[${pathIndex}]`;+ try {+ safeRelativePath(path, context, true);+ } catch (error) {+ throw new ContractValidationError(+ `${context}: expected a safe repository-relative POSIX path.`,+ { cause: error },+ );+ }+ }+ }+const findingIds = new Set<string>();for (const [findingIndex, finding] of findings.findings.entries()) {const context = `findings.findings[${findingIndex}]`;@@ -674,12 +689,16 @@ async function verifyScanRoot(}}-function safeRelativePath(value: string, context: string): string {+function safeRelativePath(+ value: string,+ context: string,+ allowDot = false,+): string {const parts = value.split("/");if (value.length === 0 ||Buffer.from(value, "utf8").toString("utf8") !== value ||- value === "." ||+ (!allowDot && value === ".") ||value.startsWith("/") ||/^[A-Za-z]:/.test(value) ||parts.includes("..") ||@@ -692,7 +711,7 @@ function safeRelativePath(value: string, context: string): string {}const normalized = posix.normalize(value).replace(/\/+$/, "");if (- normalized === "." ||+ (!allowDot && normalized === ".") ||normalized.startsWith("../") ||isAbsolute(normalized)) {diff --git a/sdk/typescript/tests-ts/contract.test.ts b/sdk/typescript/tests-ts/contract.test.tsindex 3c7592224..730f46604 100644--- a/sdk/typescript/tests-ts/contract.test.ts+++ b/sdk/typescript/tests-ts/contract.test.ts@@ -885,6 +885,79 @@ describe("canonical scan contract", () => {},);+ test("preserves accepted paths in sealed deferred coverage", async () => {+ const paths = [+ ".",+ "./",+ "././",+ "src",+ "src/parser.ts",+ "./src/parser.ts",+ "src/./parser.ts",+ "src//parser.ts",+ "src/",+ "src:parser.ts",+ "src/a:b.py",+ "src/.hidden",+ "src/na\u00efve file.ts",+ "src/\u007fparser.ts",+ "src/\u2028parser.ts",+ "src/\u2029parser.ts",+ " ",+ "\u0085",+ "\ufeff",+ ];+ const scanDir = await copyExample();+ const coveragePath = join(scanDir, "coverage.json");+ const coverage = await readJson(coveragePath);+ coverage["deferred"] = [+ { id: "review", reason: "Review is incomplete.", paths },+ ];+ await writeJson(coveragePath, coverage);+ await reseal(scanDir);+ const sealedBytes = await readFile(coveragePath);++ await expect(+ loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),+ ).resolves.toMatchObject({+ coverage: { deferred: [{ paths }] },+ });+ expect(await readFile(coveragePath)).toEqual(sealedBytes);+ });++ test("rejects unsafe paths in sealed deferred coverage", async () => {+ for (const path of [+ "",+ "../../outside.ts",+ "src/../outside.ts",+ "src/..",+ "/outside.ts",+ "//server/share",+ "C:/outside.ts",+ "c:outside.ts",+ "src\\outside.ts",+ "src/\u0000outside.ts",+ "src/\u0001outside.ts",+ "src/\toutside.ts",+ "src/outside.ts\n",+ "src/\routside.ts",+ "src/\u2028/../outside.ts",+ ]) {+ const scanDir = await copyExample();+ const coveragePath = join(scanDir, "coverage.json");+ const coverage = await readJson(coveragePath);+ coverage["deferred"] = [+ { id: "review", reason: "Review is incomplete.", paths: [path] },+ ];+ await writeJson(coveragePath, coverage);+ await reseal(scanDir);++ await expect(+ loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),+ ).rejects.toThrow(/coverage.*deferred/);+ }+ });+test.each([-1, 0])("agrees with Python on a finding end-line offset of %i",async (offset) => {