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(targets): share DiffTarget state validation (#1667)

Michael D'Angeloa4a98a799137
diff --git a/sdk/typescript/src/targets.ts b/sdk/typescript/src/targets.ts
index 18bf98cd1..3da90aa14 100644
--- a/sdk/typescript/src/targets.ts
+++ b/sdk/typescript/src/targets.ts
@@ -64,24 +64,7 @@ export class DiffTarget {
this.kind = options.kind;
this.base = options.base;
this.head = options.head;
- if (this.kind !== "refs" && this.kind !== "working_tree") {
- throw new InvalidTargetError(
- `Unsupported diff target kind: ${String(this.kind)}`,
- );
- }
- if (!isNonEmptyString(this.base)) {
- throw new InvalidTargetError("The diff base ref must be non-empty.");
- }
- if (this.kind === "refs" && !isNonEmptyString(this.head)) {
- throw new InvalidTargetError(
- "Git diff refs must include a non-empty head ref.",
- );
- }
- if (this.kind === "working_tree" && this.head !== undefined) {
- throw new InvalidTargetError(
- "Working-tree targets cannot specify a head ref.",
- );
- }
+ requireValidDiffTarget(this);
Object.freeze(this);
}
@@ -102,6 +85,30 @@ export class DiffTarget {
}
}
+function requireValidDiffTarget(target: DiffTargetOptions): void {
+ if (target.kind !== "refs" && target.kind !== "working_tree") {
+ throw new InvalidTargetError(
+ `Unsupported diff target kind: ${String(target.kind)}`,
+ );
+ }
+ if (typeof target.base !== "string" || target.base.length === 0) {
+ throw new InvalidTargetError("The diff base ref must be non-empty.");
+ }
+ if (
+ target.kind === "refs" &&
+ (typeof target.head !== "string" || target.head.length === 0)
+ ) {
+ throw new InvalidTargetError(
+ "Git diff refs must include a non-empty head ref.",
+ );
+ }
+ if (target.kind === "working_tree" && target.head !== undefined) {
+ throw new InvalidTargetError(
+ "Working-tree targets cannot specify a head ref.",
+ );
+ }
+}
+
export type ScanTarget = "repository" | DiffTarget | readonly string[];
export type NormalizedTargetKind =
"repository" | "paths" | "refs" | "working_tree";
@@ -515,27 +522,7 @@ export async function normalizeTarget(
}
if (target instanceof DiffTarget) {
- if (target.kind !== "refs" && target.kind !== "working_tree") {
- throw new InvalidTargetError(
- `Unsupported diff target kind: ${String(target.kind)}`,
- );
- }
- if (typeof target.base !== "string" || target.base.length === 0) {
- throw new InvalidTargetError("The diff base ref must be non-empty.");
- }
- if (
- target.kind === "refs" &&
- (typeof target.head !== "string" || target.head.length === 0)
- ) {
- throw new InvalidTargetError(
- "Git diff refs must include a non-empty head ref.",
- );
- }
- if (target.kind === "working_tree" && target.head !== undefined) {
- throw new InvalidTargetError(
- "Working-tree targets cannot specify a head ref.",
- );
- }
+ requireValidDiffTarget(target);
await requireGitRepository(root, signal);
const base = await resolveGitRef(root, target.base, signal);
const kind = target.kind === "refs" ? "refs" : "working_tree";
diff --git a/sdk/typescript/tests-ts/targets.test.ts b/sdk/typescript/tests-ts/targets.test.ts
index 5d18923c8..6a506394a 100644
--- a/sdk/typescript/tests-ts/targets.test.ts
+++ b/sdk/typescript/tests-ts/targets.test.ts
@@ -658,6 +658,24 @@ describe("scan target normalization", () => {
).rejects.toThrow("cannot specify a head");
});
+ test("revalidates a forged diff head after resolving the base", async () => {
+ const repo = await repository();
+ let headReads = 0;
+ const forged: DiffTarget = Object.assign(
+ Object.create(DiffTarget.prototype),
+ { kind: "refs", base: "HEAD" },
+ );
+ Object.defineProperty(forged, "head", {
+ get: () => (++headReads <= 2 ? "HEAD" : undefined),
+ });
+
+ const normalized = normalizeTarget(repo, forged);
+ await expect(normalized).rejects.toThrow(InvalidTargetError);
+ await expect(normalized).rejects.toThrow(
+ "Git diff refs must include a non-empty head ref.",
+ );
+ });
+
test("honors cancellation before repository and Git validation", async () => {
const repo = await repository();
const controller = new AbortController();