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(artifacts): share relative path validation for draft and sealed scans (#2478)

Michael D'Angeloa0d193e30967
diff --git a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
index 7c4b8717d..a21241876 100644
--- a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
@@ -1,4 +1,5 @@
import type { JsonObject } from "./types.js";
+import { normalizedScanRelativePath } from "./contract-values.js";
import {
coverageReceiptComponents,
validateRetainedCoverage,
@@ -7,7 +8,7 @@ import {
import { isRecord as isObject } from "./record.js";
import { createHash, randomUUID } from "node:crypto";
import { promises as fs } from "node:fs";
-import { basename, dirname, isAbsolute, join, posix, sep } from "node:path";
+import { basename, dirname, join, posix, sep } from "node:path";
import { isDeepStrictEqual } from "node:util";
import * as z from "zod/v4";
import type { ArtifactContext } from "./artifact-context.js";
@@ -2815,31 +2816,6 @@ export async function getCodexSecurityCompletedScan(
};
}
-function safeRelativePath(value: string, context: string): string {
- const parts = value.split("/");
- if (
- value.length === 0 ||
- Buffer.from(value, "utf8").toString("utf8") !== value ||
- value === "." ||
- value.startsWith("/") ||
- /^[A-Za-z]:/.test(value) ||
- parts.includes("..") ||
- value.includes("\\") ||
- /[\u0000-\u001f]/u.test(value)
- ) {
- throw new Error(`${context}: expected a safe scan-relative POSIX path.`);
- }
- const normalized = posix.normalize(value).replace(/\/+$/, "");
- if (
- normalized === "." ||
- normalized.startsWith("../") ||
- isAbsolute(normalized)
- ) {
- throw new Error(`${context}: expected a safe scan-relative POSIX path.`);
- }
- return normalized;
-}
-
/** Admit file-authored drafts through the current contract after removing host fields. */
export function parseCanonicalScanDraft(input: {
scanId?: string;
@@ -2880,10 +2856,10 @@ function parseCanonicalCheckpoint(
for (const [locationIndex, location] of (
(finding[field] as JsonObject[] | undefined) ?? []
).entries()) {
- safeRelativePath(
- location.path as string,
- `findings[${index}].${field}[${locationIndex}].path`,
- );
+ if (normalizedScanRelativePath(location.path as string) === undefined)
+ throw new Error(
+ `findings[${index}].${field}[${locationIndex}].path: expected a safe scan-relative POSIX path.`,
+ );
}
}
}
diff --git a/plugins/codex-security/mcp-app/src/contract-values.ts b/plugins/codex-security/mcp-app/src/contract-values.ts
index 1c69895a4..1ead65ffe 100644
--- a/plugins/codex-security/mcp-app/src/contract-values.ts
+++ b/plugins/codex-security/mcp-app/src/contract-values.ts
@@ -20,36 +20,41 @@ export function hasRequiredFindingText(value: string): boolean {
return !/^[\p{White_Space}\u001c-\u001f]*$/u.test(value);
}
-export function safeRelativePath(
+/** Return canonical spelling for a valid scan-relative POSIX path. */
+export function normalizedScanRelativePath(
value: string,
- context: string,
allowDot = false,
-): string {
- const parts = value.split("/");
+): string | undefined {
if (
value.length === 0 ||
Buffer.from(value, "utf8").toString("utf8") !== value ||
- (!allowDot && value === ".") ||
value.startsWith("/") ||
/^[A-Za-z]:/.test(value) ||
- parts.includes("..") ||
+ value.split("/").includes("..") ||
value.includes("\\") ||
/[\u0000-\u001f]/u.test(value)
- ) {
- throw new SealedContractError(
- `${context}: expected a safe scan-relative POSIX path.`,
- );
- }
+ )
+ return;
const normalized = posix.normalize(value).replace(/\/+$/, "");
if (
(!allowDot && normalized === ".") ||
normalized.startsWith("../") ||
isAbsolute(normalized)
- ) {
+ )
+ return;
+ return normalized;
+}
+
+export function safeRelativePath(
+ value: string,
+ context: string,
+ allowDot = false,
+): string {
+ const normalized = normalizedScanRelativePath(value, allowDot);
+ if (normalized === undefined)
throw new SealedContractError(
`${context}: expected a safe scan-relative POSIX path.`,
);
- }
return normalized;
}
diff --git a/sdk/typescript/src/contract-values.ts b/sdk/typescript/src/contract-values.ts
index c8fd25b85..0e7ae700a 100644
--- a/sdk/typescript/src/contract-values.ts
+++ b/sdk/typescript/src/contract-values.ts
@@ -21,36 +21,41 @@ export function hasRequiredFindingText(value: string): boolean {
return !/^[\p{White_Space}\u001c-\u001f]*$/u.test(value);
}
-export function safeRelativePath(
+/** Return canonical spelling for a valid scan-relative POSIX path. */
+export function normalizedScanRelativePath(
value: string,
- context: string,
allowDot = false,
-): string {
- const parts = value.split("/");
+): string | undefined {
if (
value.length === 0 ||
Buffer.from(value, "utf8").toString("utf8") !== value ||
- (!allowDot && value === ".") ||
value.startsWith("/") ||
/^[A-Za-z]:/.test(value) ||
- parts.includes("..") ||
+ value.split("/").includes("..") ||
value.includes("\\") ||
/[\u0000-\u001f]/u.test(value)
- ) {
- throw new SealedContractError(
- `${context}: expected a safe scan-relative POSIX path.`,
- );
- }
+ )
+ return;
const normalized = posix.normalize(value).replace(/\/+$/, "");
if (
(!allowDot && normalized === ".") ||
normalized.startsWith("../") ||
isAbsolute(normalized)
- ) {
+ )
+ return;
+ return normalized;
+}
+
+export function safeRelativePath(
+ value: string,
+ context: string,
+ allowDot = false,
+): string {
+ const normalized = normalizedScanRelativePath(value, allowDot);
+ if (normalized === undefined)
throw new SealedContractError(
`${context}: expected a safe scan-relative POSIX path.`,
);
- }
return normalized;
}