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(publish): simplify evidence grouping and parsing (#1368)

mldangelo-oai2fe3ccdee429
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index f6f718d03..a9e64f2f6 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import { listenForAbort } from "./cli-signals.js";
-import { isNonEmptyString } from "./value.js";
+import { isNonEmptyString, parseJson } from "./value.js";
import {
execFile as execFileCallback,
@@ -7683,12 +7683,7 @@ export async function readSkillCommandOutput(
for await (const line of createInterface({ input: Readable.from(stream) })) {
if (line.trim().length === 0) continue;
- let event: unknown;
- try {
- event = JSON.parse(line);
- } catch {
- continue;
- }
+ const event = parseJson(() => line);
if (typeof event !== "object" || event === null) {
continue;
}
@@ -7952,12 +7947,8 @@ function incurErrorMessage(output: string): string {
.find((line) => line.startsWith("message: "))
?.slice("message: ".length);
if (message === undefined) return output.trim();
- try {
- const parsed: unknown = JSON.parse(message);
- return typeof parsed === "string" ? parsed : message;
- } catch {
- return message;
- }
+ const parsed = parseJson(() => message);
+ return typeof parsed === "string" ? parsed : message;
}
async function runExport(
@@ -9541,7 +9532,6 @@ export class Progress {
#timerLineActive = false;
#cursorHidden = false;
#observingStreamErrors = false;
- #streamErrorsActive = false;
#streamErrorGeneration = 0;
readonly #onStreamError = (): void => {};
@@ -9609,14 +9599,12 @@ export class Progress {
}
} finally {
if (this.#observingStreamErrors) {
- this.#streamErrorsActive = false;
const generation = this.#streamErrorGeneration;
try {
this.#stream.write("", () => {
queueMicrotask(() => {
if (
generation === this.#streamErrorGeneration &&
- !this.#streamErrorsActive &&
this.#observingStreamErrors
) {
this.#stream.off?.("error", this.#onStreamError);
@@ -9652,7 +9640,6 @@ export class Progress {
}
#observeStreamErrors(): void {
- this.#streamErrorsActive = true;
this.#streamErrorGeneration += 1;
if (!this.#observingStreamErrors && this.#stream.on !== undefined) {
this.#stream.on("error", this.#onStreamError);
diff --git a/sdk/typescript/src/contract.ts b/sdk/typescript/src/contract.ts
index 77efad052..5aaba2bbc 100644
--- a/sdk/typescript/src/contract.ts
+++ b/sdk/typescript/src/contract.ts
@@ -219,17 +219,12 @@ export function normalizePersistedFindings(payload: unknown): unknown {
if (!isRecord(finding)) continue;
const legacyEvidence = finding["code_evidence"];
if (Array.isArray(legacyEvidence)) {
- const compatibleEvidence: JsonRecord[] = [];
- for (const evidence of legacyEvidence) {
- if (!isRecord(evidence)) continue;
- const id = evidence["id"];
- const code = evidence["code"];
- if (!isNonEmptyString(id) || !isNonEmptyString(code)) {
- continue;
- }
- compatibleEvidence.push(evidence);
- }
- finding["code_evidence"] = compatibleEvidence;
+ finding["code_evidence"] = legacyEvidence.filter(
+ (evidence) =>
+ isRecord(evidence) &&
+ isNonEmptyString(evidence["id"]) &&
+ isNonEmptyString(evidence["code"]),
+ );
} else if ("code_evidence" in finding && legacyEvidence !== null) {
delete finding["code_evidence"];
}
@@ -283,10 +278,7 @@ export function normalizePersistedFindings(payload: unknown): unknown {
const validation = finding["validation"];
if (isRecord(validation)) {
- if (
- typeof validation["evidence"] !== "string" ||
- validation["evidence"].length === 0
- ) {
+ if (!isNonEmptyString(validation["evidence"])) {
normalizeLegacyStringLists(validation, ["evidence"]);
}
removeUnsupportedLegacyStrings(validation, ["method", "summary"]);
@@ -303,16 +295,8 @@ export function normalizePersistedFindings(payload: unknown): unknown {
removeUnsupportedLegacyStrings(attackPath, ["summary"]);
for (const field of ["dataFlow", "data_flow", "dataflow", "reachability"]) {
const detail = attackPath[field];
- if (detail === null) {
- delete attackPath[field];
- continue;
- }
- if (typeof detail === "string") {
- if (detail.length === 0) delete attackPath[field];
- continue;
- }
if (!isRecord(detail)) {
- if (field in attackPath) delete attackPath[field];
+ if (!isNonEmptyString(detail)) delete attackPath[field];
continue;
}
removeUnsupportedLegacyStrings(detail, [
@@ -367,10 +351,7 @@ function removeUnsupportedLegacyStrings(
fields: string[],
): void {
for (const field of fields) {
- if (
- field in section &&
- (typeof section[field] !== "string" || section[field].length === 0)
- ) {
+ if (field in section && !isNonEmptyString(section[field])) {
delete section[field];
}
}
diff --git a/sdk/typescript/src/linear.ts b/sdk/typescript/src/linear.ts
index 0fba72be9..a121c35cd 100644
--- a/sdk/typescript/src/linear.ts
+++ b/sdk/typescript/src/linear.ts
@@ -1,3 +1,4 @@
+import { parseJson } from "./value.js";
import {
AuthenticationLinearError,
ForbiddenLinearError,
@@ -151,12 +152,7 @@ export async function importLinearIssues(options: {
function linearIssueFilter(input: string | undefined): JsonObject {
if (input === undefined) return {};
- let filter: unknown;
- try {
- filter = JSON.parse(input);
- } catch {
- filter = null;
- }
+ const filter = parseJson(() => input);
if (typeof filter === "object" && filter !== null && !Array.isArray(filter)) {
return filter as JsonObject;
}
diff --git a/sdk/typescript/src/publication-events.ts b/sdk/typescript/src/publication-events.ts
index faa73b5ce..fd39130ac 100644
--- a/sdk/typescript/src/publication-events.ts
+++ b/sdk/typescript/src/publication-events.ts
@@ -1,3 +1,4 @@
+import { parseJson } from "./value.js";
import { isLinearIssueIdentifier, linearIssueReference } from "./linear.js";
import { isRecord } from "./record.js";
import {
@@ -55,12 +56,7 @@ export function collectPublicationEvents(
for (const rawLine of output.split(/\r?\n/)) {
if (rawLine.trim().length === 0) continue;
- let event: unknown;
- try {
- event = JSON.parse(rawLine) as unknown;
- } catch {
- continue;
- }
+ const event = parseJson(() => rawLine);
if (!isRecord(event) || event["type"] !== "item.completed") continue;
const item = event["item"];
if (!isLinearCreateCall(item)) continue;
@@ -221,11 +217,8 @@ function collectPublicationClaims(
if (!isRecord(content) || typeof content["text"] !== "string") {
continue;
}
- try {
- nested.push(JSON.parse(content["text"]) as unknown);
- } catch {
- continue;
- }
+ const parsed = parseJson(() => content["text"] as string);
+ if (isRecord(parsed)) nested.push(parsed);
}
}
for (const child of nested.reverse()) {
diff --git a/sdk/typescript/src/publication.ts b/sdk/typescript/src/publication.ts
index d6c7d3e44..9895c5ba4 100644
--- a/sdk/typescript/src/publication.ts
+++ b/sdk/typescript/src/publication.ts
@@ -330,12 +330,7 @@ function renderCodeEvidence(
return [
`### ${evidence.label}`,
"",
- renderLocation(target, {
- path: evidence.path,
- startLine: evidence.startLine,
- ...(evidence.endLine === undefined ? {} : { endLine: evidence.endLine }),
- ...(evidence.role === undefined ? {} : { role: evidence.role }),
- }),
+ renderLocation(target, evidence),
"",
fencedCode(evidence.code, evidence.language),
"",
diff --git a/sdk/typescript/src/publish.ts b/sdk/typescript/src/publish.ts
index c230cb5f4..c26948ba8 100644
--- a/sdk/typescript/src/publish.ts
+++ b/sdk/typescript/src/publish.ts
@@ -217,18 +217,6 @@ type PublicationHandoffEvidence = {
type PublicationEvidence =
PublicationEventEvidence | PublicationHandoffEvidence;
-interface IndexedPublicationEvidence {
- byOwner: Map<string, PublicationEvidence[]>;
- unowned: PublicationEvidence[];
- claimLedger: Map<
- string,
- {
- kinds: Set<PublicationClaim["kind"]>;
- owners: Set<string | undefined>;
- }
- >;
-}
-
interface ReconciledPublication {
created: PublishedScanIssue[];
failed: FailedScanPublication[];
@@ -931,148 +919,136 @@ async function collectPublicationHandoffEvidence(
return [];
}
- const evidence: PublicationHandoffEvidence[] = [];
const expectedIssues = new Map(publication.issues.map(findingEntry));
- for (const rawLine of content.split(/\r?\n/)) {
- if (rawLine.trim().length === 0) continue;
- let record: unknown;
- try {
- record = JSON.parse(rawLine) as unknown;
- } catch {
- evidence.push({
- source: "handoff",
- status: "invalid",
- resolution: resolveClaims([]),
- error: "Codex wrote an invalid Linear publication handoff.",
- });
- continue;
- }
+ return content
+ .split(/\r?\n/)
+ .flatMap((rawLine): PublicationHandoffEvidence | [] => {
+ if (rawLine.trim().length === 0) return [];
+ let record: unknown;
+ try {
+ record = JSON.parse(rawLine) as unknown;
+ } catch {
+ return {
+ source: "handoff",
+ status: "invalid",
+ resolution: resolveClaims([]),
+ error: "Codex wrote an invalid Linear publication handoff.",
+ };
+ }
- const resolution = resolvePublicationClaims(record);
- const possibleMutation =
- isRecord(record) && record["possibleMutation"] === true
- ? { possibleMutation: true as const }
- : {};
- if (!isRecord(record) || typeof record["findingId"] !== "string") {
- evidence.push({
- source: "handoff",
- status: "invalid",
- resolution,
- ...possibleMutation,
- error: "Codex wrote an unexpected Linear publication handoff.",
- });
- continue;
- }
- const issue = expectedIssues.get(record["findingId"]);
- if (issue === undefined) {
- evidence.push({
- source: "handoff",
- status: "invalid",
- resolution,
- ...possibleMutation,
- error: "Codex wrote a Linear publication for an unknown finding.",
- });
- continue;
- }
+ const resolution = resolvePublicationClaims(record);
+ const possibleMutation =
+ isRecord(record) && record["possibleMutation"] === true
+ ? { possibleMutation: true as const }
+ : {};
+ if (!isRecord(record) || typeof record["findingId"] !== "string") {
+ return {
+ source: "handoff",
+ status: "invalid",
+ resolution,
+ ...possibleMutation,
+ error: "Codex wrote an unexpected Linear publication handoff.",
+ };
+ }
+ const issue = expectedIssues.get(record["findingId"]);
+ if (issue === undefined) {
+ return {
+ source: "handoff",
+ status: "invalid",
+ resolution,
+ ...possibleMutation,
+ error: "Codex wrote a Linear publication for an unknown finding.",
+ };
+ }
- const argumentsValid = hasExpectedPublicationArguments(
- publication,
- issue,
- record["arguments"],
- );
- const mutationPossible =
- record["possibleMutation"] === true ||
- (!Object.hasOwn(record, "error") && argumentsValid);
- const base = {
- source: "handoff" as const,
- ownerFindingId: issue.findingId,
- resolution,
- ...(mutationPossible ? { possibleMutation: true as const } : {}),
- };
- if (
- record["scanId"] !== publication.scanId ||
- record["occurrenceId"] !== issue.occurrenceId
- ) {
- evidence.push({
+ const argumentsValid = hasExpectedPublicationArguments(
+ publication,
+ issue,
+ record["arguments"],
+ );
+ const mutationPossible =
+ record["possibleMutation"] === true ||
+ (!Object.hasOwn(record, "error") && argumentsValid);
+ const base = {
+ source: "handoff" as const,
+ ownerFindingId: issue.findingId,
+ resolution,
+ ...(mutationPossible ? { possibleMutation: true as const } : {}),
+ };
+ const invalid = (
+ error: string,
+ recoverableWithEvent?: true,
+ ): PublicationHandoffEvidence => ({
...base,
status: "invalid",
- error:
- "Codex wrote a Linear publication with an unexpected scan or finding occurrence.",
+ ...(recoverableWithEvent === undefined ? {} : { recoverableWithEvent }),
+ error,
});
- continue;
- }
-
- const identityNames = ["issueIdentifier", "identifier", "key", "id"].filter(
- (name) => Object.hasOwn(record, name),
- );
- if (Object.hasOwn(record, "error")) {
- const error = record["error"];
- const hasInvalidPossibleMutation =
- Object.hasOwn(record, "possibleMutation") &&
- record["possibleMutation"] !== true;
if (
- identityNames.length === 0 &&
- !Object.hasOwn(record, "url") &&
- resolution.claims.length === 0 &&
- !hasInvalidPossibleMutation &&
- typeof error === "string" &&
- error.trim().length > 0
+ record["scanId"] !== publication.scanId ||
+ record["occurrenceId"] !== issue.occurrenceId
) {
- evidence.push({ ...base, status: "failure", error });
- } else {
- evidence.push({
- ...base,
- status: "invalid",
- error: "Codex wrote an invalid Linear publication failure.",
- });
+ return invalid(
+ "Codex wrote a Linear publication with an unexpected scan or finding occurrence.",
+ );
}
- continue;
- }
- if (resolution.state === "conflicting") {
- evidence.push({
- ...base,
- status: "invalid",
- error:
+ const identityNames = [
+ "issueIdentifier",
+ "identifier",
+ "key",
+ "id",
+ ].filter((name) => Object.hasOwn(record, name));
+ if (Object.hasOwn(record, "error")) {
+ const error = record["error"];
+ const hasInvalidPossibleMutation =
+ Object.hasOwn(record, "possibleMutation") &&
+ record["possibleMutation"] !== true;
+ if (
+ identityNames.length === 0 &&
+ !Object.hasOwn(record, "url") &&
+ resolution.claims.length === 0 &&
+ !hasInvalidPossibleMutation &&
+ typeof error === "string" &&
+ error.trim().length > 0
+ ) {
+ return { ...base, status: "failure", error };
+ } else {
+ return invalid("Codex wrote an invalid Linear publication failure.");
+ }
+ }
+
+ if (resolution.state === "conflicting") {
+ return invalid(
"Codex wrote conflicting Linear publication issue identifiers or URLs.",
+ );
+ }
+ const topLevelResolution = resolveTopLevelPublicationClaims(record);
+ const hasInvalidIdentityField = identityNames.some((name) => {
+ const value = record[name];
+ return typeof value !== "string" || value.trim().length === 0;
});
- continue;
- }
- const topLevelResolution = resolveTopLevelPublicationClaims(record);
- const hasInvalidIdentityField = identityNames.some((name) => {
- const value = record[name];
- return typeof value !== "string" || value.trim().length === 0;
- });
- const topLevelUrl = record["url"];
- if (
- resolution.state !== "resolved" ||
- topLevelResolution.state !== "resolved" ||
- topLevelResolution.issueIdentifier !== resolution.issueIdentifier ||
- hasInvalidIdentityField ||
- (topLevelUrl !== undefined &&
- (typeof topLevelUrl !== "string" || topLevelUrl.trim().length === 0))
- ) {
- evidence.push({
- ...base,
- status: "invalid",
- error:
+ const topLevelUrl = record["url"];
+ if (
+ resolution.state !== "resolved" ||
+ topLevelResolution.state !== "resolved" ||
+ topLevelResolution.issueIdentifier !== resolution.issueIdentifier ||
+ hasInvalidIdentityField ||
+ (topLevelUrl !== undefined &&
+ (typeof topLevelUrl !== "string" || topLevelUrl.trim().length === 0))
+ ) {
+ return invalid(
"Codex wrote a Linear publication without a valid created issue identifier.",
- });
- continue;
- }
- if (!argumentsValid) {
- evidence.push({
- ...base,
- status: "invalid",
- recoverableWithEvent: true,
- error:
+ );
+ }
+ if (!argumentsValid) {
+ return invalid(
"Codex wrote a Linear publication with unexpected arguments or destination.",
- });
- continue;
- }
- evidence.push({ ...base, status: "success" });
- }
- return evidence;
+ true,
+ );
+ }
+ return { ...base, status: "success" };
+ });
}
function resolveTopLevelPublicationClaims(
@@ -1092,14 +1068,37 @@ function reconcilePublicationEvidence(
evidence: readonly PublicationEvidence[],
failureMessage: string,
): ReconciledPublication {
- const indexed = indexPublicationEvidence(evidence);
+ const byOwner = Map.groupBy(evidence, (item) => item.ownerFindingId);
+ const claimLedger = new Map<
+ string,
+ {
+ kinds: Set<PublicationClaim["kind"]>;
+ owners: Set<string | undefined>;
+ }
+ >();
+
+ for (const item of evidence) {
+ for (const claim of item.resolution.claims) {
+ for (const alias of publicationClaimAliases(claim)) {
+ const key = alias.value;
+ const reservation = claimLedger.get(key) ?? {
+ kinds: new Set<PublicationClaim["kind"]>(),
+ owners: new Set<string | undefined>(),
+ };
+ reservation.kinds.add(alias.kind);
+ reservation.owners.add(item.ownerFindingId);
+ claimLedger.set(key, reservation);
+ }
+ }
+ }
+
const outcomes = publication.issues.map((issue) =>
- reconcileFindingEvidence(issue, indexed.byOwner.get(issue.findingId)),
+ reconcileFindingEvidence(issue, byOwner.get(issue.findingId)),
);
let indeterminate = outcomes.some((outcome) => outcome.indeterminate);
const collidingOwners = new Set<string>();
- for (const reservation of indexed.claimLedger.values()) {
+ for (const reservation of claimLedger.values()) {
if (reservation.kinds.size > 1 || reservation.owners.size > 1) {
for (const owner of reservation.owners) {
if (owner !== undefined) collidingOwners.add(owner);
@@ -1114,7 +1113,7 @@ function reconcilePublicationEvidence(
indeterminate = true;
}
- const unowned = indexed.unowned;
+ const unowned = byOwner.get(undefined) ?? [];
if (
unowned.some(
(item) =>
@@ -1159,39 +1158,6 @@ function reconcilePublicationEvidence(
};
}
-function indexPublicationEvidence(
- evidence: readonly PublicationEvidence[],
-): IndexedPublicationEvidence {
- const byOwner = new Map<string, PublicationEvidence[]>();
- const unowned: PublicationEvidence[] = [];
- const claimLedger: IndexedPublicationEvidence["claimLedger"] = new Map();
-
- for (const item of evidence) {
- for (const claim of item.resolution.claims) {
- for (const alias of publicationClaimAliases(claim)) {
- const key = alias.value;
- const reservation = claimLedger.get(key) ?? {
- kinds: new Set<PublicationClaim["kind"]>(),
- owners: new Set<string | undefined>(),
- };
- reservation.kinds.add(alias.kind);
- reservation.owners.add(item.ownerFindingId);
- claimLedger.set(key, reservation);
- }
- }
-
- if (item.ownerFindingId === undefined) {
- unowned.push(item);
- continue;
- }
- const bucket = byOwner.get(item.ownerFindingId) ?? [];
- bucket.push(item);
- byOwner.set(item.ownerFindingId, bucket);
- }
-
- return { byOwner, unowned, claimLedger };
-}
-
function reconcileFindingEvidence(
issue: PreparedPublicationIssue,
bucket: PublicationEvidence[] = [],