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(sdk): preserve historical publication content (#1756)
Michael D'Angelob10d249f30ca
diff --git a/sdk/typescript/src/publication.ts b/sdk/typescript/src/publication.tsindex 56a6b4626..b217e94eb 100644--- a/sdk/typescript/src/publication.ts+++ b/sdk/typescript/src/publication.ts@@ -1,4 +1,5 @@import { findingEntry } from "./value.js";+import { isRecord } from "./record.js";import {loadContractWithScanDirectory,type LoadedContract,@@ -195,11 +196,15 @@ function renderFindingDescription(lines.push("", "## Reproduction summary", "", finding.attackPath.summary);}- const rootCause = finding.rootCause;+ const rootCause = publicationRootCause(finding);if (typeof rootCause === "string") {lines.push("", "## Root cause", "", rootCause);- } else if (rootCause !== undefined) {- lines.push("", "## Root cause", "", rootCause.summary);+ } else if (+ rootCause !== undefined &&+ (rootCause.summary !== undefined || rootCause.code !== undefined)+ ) {+ lines.push("", "## Root cause");+ if (rootCause.summary !== undefined) lines.push("", rootCause.summary);if (rootCause.code !== undefined) {lines.push("", fencedCode(rootCause.code, rootCause.language));}@@ -222,10 +227,11 @@ function renderFindingDescription(lines.push(...limits.map((limit) => `- ${limit}`));}- if (finding.codeEvidence !== undefined && finding.codeEvidence.length > 0) {+ const codeEvidence = publicationCodeEvidence(finding);+ if (codeEvidence.length > 0) {lines.push("", "## Source-code evidence");- for (const evidence of finding.codeEvidence) {- lines.push("", ...renderCodeEvidence(scan.target, evidence));+ for (const [index, evidence] of codeEvidence.entries()) {+ lines.push("", ...renderCodeEvidence(scan.target, evidence, index));}}@@ -307,22 +313,138 @@ function renderLocation(: `-${location.endLine}`}`;const sourceUrl = immutableSourceUrl(target, location);- return `- **${role}:** ${sourceUrl === undefined ? `\`${label}\`` : `[\`${label}\`](${sourceUrl})`}`;+ const formattedLabel = inlineCode(label);+ return `- **${role}:** ${sourceUrl === undefined ? formattedLabel : `[${formattedLabel}](${sourceUrl})`}`;}function renderCodeEvidence(target: ScanTargetRecord,- evidence: FindingCodeEvidence,+ evidence: PublicationCodeEvidence,+ index: number,): string[] {- return [- `### ${evidence.label}`,- "",- renderLocation(target, evidence),- "",- fencedCode(evidence.code, evidence.language),- "",- evidence.explanation,- ];+ const lines = [`### ${evidence.label ?? `Code evidence ${index + 1}`}`, ""];+ if (typeof evidence["location"] === "string") {+ lines.push(+ `- **${evidence.role === undefined ? "Location" : humanizeRole(evidence.role)}:** ${inlineCode(evidence["location"])}`,+ "",+ );+ } else if (evidence.path !== undefined) {+ lines.push(+ evidence.startLine === undefined+ ? `- **${evidence.role === undefined ? "Location" : humanizeRole(evidence.role)}:** ${inlineCode(evidence.path)}`+ : renderLocation(target, {+ ...evidence,+ path: evidence.path,+ startLine: evidence.startLine,+ }),+ "",+ );+ }+ lines.push(fencedCode(evidence.code, evidence.language));+ if (evidence.explanation !== undefined) lines.push("", evidence.explanation);+ return lines;+}++function publicationRootCause(finding: Finding) {+ const canonical = finding.rootCause;+ const legacy = finding.root_cause;+ if (canonical === undefined) return legacy ?? undefined;+ if (legacy === undefined || legacy === null) return canonical;+ const details: Array<{ summary?: string; code?: string; language?: string }> =+ [canonical, legacy].map((detail) =>+ typeof detail === "string" ? { summary: detail } : detail,+ );+ const summary = details.find((detail) => detail.summary?.trim())?.summary;+ const code = details.find((detail) => detail.code?.trim())?.code;+ const language = details.find(+ (detail) => detail.code === code && detail.language?.trim(),+ )?.language;+ return summary === undefined && code === undefined+ ? undefined+ : { summary, code, language };+}++type PublicationCodeEvidence = Partial<FindingCodeEvidence> &+ Pick<FindingCodeEvidence, "code">;++function publicationCodeEvidence(finding: Finding): PublicationCodeEvidence[] {+ const evidence: PublicationCodeEvidence[] = (finding.codeEvidence ?? []).map(+ (item) =>+ typeof item["location"] === "string" || isRecord(item["location"])+ ? publicationEvidenceEntry(item, item.code)+ : item,+ );+ const ids = new Set(+ evidence.filter((item) => item.code.trim()).map((item) => item.id),+ );+ for (const item of finding.code_evidence ?? []) {+ if (ids.has(item.id)) continue;+ if (item.code.trim()) ids.add(item.id);+ evidence.push(publicationEvidenceEntry(item, item.code));+ }+ const snippets = new Set(+ evidence.map((item) => JSON.stringify([item.id ?? "", item.code])),+ );+ const attackPath = finding.attackPath;+ for (const section of [+ finding.rootCause,+ finding.root_cause,+ finding.validation,+ attackPath,+ attackPath?.dataFlow,+ attackPath?.["dataflow"],+ attackPath?.data_flow,+ attackPath?.reachability,+ ]) {+ if (!isRecord(section)) continue;+ for (const key of ["codeEvidence", "code_evidence"]) {+ const embedded = section[key];+ if (!Array.isArray(embedded)) continue;+ for (const item of embedded) {+ if (!isRecord(item)) continue;+ const code = item["code"];+ if (typeof code !== "string" || !code.trim()) continue;+ const identity = JSON.stringify([item["id"] ?? "", code]);+ if (snippets.has(identity)) continue;+ snippets.add(identity);+ evidence.push(publicationEvidenceEntry(item, code));+ }+ }+ }+ return evidence;+}++function publicationEvidenceEntry(+ item: Record<string, unknown>,+ code: string,+): PublicationCodeEvidence {+ const entry: PublicationCodeEvidence = { code };+ for (const key of [+ "id",+ "label",+ "language",+ "role",+ "explanation",+ ] as const) {+ const value = item[key];+ if (typeof value === "string" && value.length > 0) entry[key] = value;+ }+ if (typeof item["location"] === "string") {+ const label = item["location"].trim().replace(/\s+/g, " ");+ if (label) entry["location"] = label;+ return entry;+ }+ const location = isRecord(item["location"]) ? item["location"] : item;+ const path = location["path"];+ if (typeof path === "string" && path.length > 0) entry.path = path;+ const start = location["startLine"];+ if (typeof start === "number" && Number.isInteger(start) && start > 0) {+ entry.startLine = start;+ const end = location["endLine"];+ if (typeof end === "number" && Number.isInteger(end) && end >= start)+ entry.endLine = end;+ }+ return entry;}function humanizeRole(role: string): string {@@ -330,6 +452,16 @@ function humanizeRole(role: string): string {return `${words.slice(0, 1).toUpperCase()}${words.slice(1)}`;}+function inlineCode(label: string): string {+ let fenceLength = 1;+ for (const match of label.matchAll(/`+/g)) {+ fenceLength = Math.max(fenceLength, match[0].length + 1);+ }+ const fence = "`".repeat(fenceLength);+ const padding = label.startsWith("`") || label.endsWith("`") ? " " : "";+ return `${fence}${padding}${label}${padding}${fence}`;+}+function fencedCode(code: string, language?: string): string {let fenceLength = 3;for (const match of code.matchAll(/`+/g)) {diff --git a/sdk/typescript/tests-ts/publication.test.ts b/sdk/typescript/tests-ts/publication.test.tsindex 18fca3599..1b5366b09 100644--- a/sdk/typescript/tests-ts/publication.test.ts+++ b/sdk/typescript/tests-ts/publication.test.ts@@ -366,6 +366,474 @@ describe("scan publication preparation", () => {expect(description).not.toContain("Snapshot digest");});+ test.each([+ ["string", undefined, "Legacy root summary.", ["Legacy root summary."], []],+ [+ "code only",+ undefined,+ { code: "legacy_control()", language: "python" },+ ["```python\nlegacy_control()\n```"],+ ["undefined"],+ ],+ [+ "complementary",+ { summary: "Canonical root summary.", language: "javascript" },+ {+ summary: "Shadow summary.",+ code: "legacy_control()",+ language: "python",+ },+ ["Canonical root summary.", "```python\nlegacy_control()\n```"],+ ["Shadow summary.", "```javascript"],+ ],+ [+ "conflicting",+ {+ summary: "Canonical root summary.",+ code: "canonical_control()",+ language: "python",+ },+ {+ summary: "Shadow summary.",+ code: "legacy_control()",+ language: "javascript",+ },+ ["Canonical root summary.", "```python\ncanonical_control()\n```"],+ ["Shadow summary.", "legacy_control()"],+ ],+ [+ "different code without canonical language",+ { summary: "Canonical root summary.", code: "canonical_control()" },+ { code: "legacy_control()", language: "python" },+ ["```\ncanonical_control()\n```"],+ ["```python", "legacy_control()"],+ ],+ [+ "string with complementary code",+ "Canonical root summary.",+ { code: "legacy_control()", language: "python" },+ ["Canonical root summary.", "```python\nlegacy_control()\n```"],+ [],+ ],+ [+ "same code",+ { summary: "Canonical root summary.", code: "same_control()" },+ { code: "same_control()", language: "python" },+ ["```python\nsame_control()\n```"],+ [],+ ],+ [+ "blank canonical",+ { summary: " ", code: "\t", language: "javascript" },+ {+ summary: "Legacy root summary.",+ code: "legacy_control()",+ language: "python",+ },+ ["Legacy root summary.", "```python\nlegacy_control()\n```"],+ ["```javascript"],+ ],+ ["null", undefined, null, [], ["## Root cause"]],+ ["empty object", undefined, {}, [], ["## Root cause"]],+ ] as const)(+ "preserves %s historical root details",+ async (_name, canonical, legacy, present, absent) => {+ const scanDirectory = await copyExample();+ const findingsPath = join(scanDirectory, "findings.json");+ const findings = await readJson<FindingsDocument>(findingsPath);+ findings.findings[0]!.rootCause = canonical;+ findings.findings[0]!.root_cause = legacy;+ await writeJson(findingsPath, findings);+ await reseal(scanDirectory);+ const saved = await readFile(findingsPath, "utf8");+ const { description } = (+ await prepareScanPublication(+ scanDirectory,+ publicationOptions(scanDirectory),+ )+ ).issues[0]!;+ for (const text of present) expect(description).toContain(text);+ for (const text of absent) expect(description).not.toContain(text);+ expect(await readFile(findingsPath, "utf8")).toBe(saved);+ },+ );++ test("preserves historical evidence with missing or invalid optional metadata", async () => {+ const scanDirectory = await copyExample();+ const findingsPath = join(scanDirectory, "findings.json");+ const findings = await readJson<FindingsDocument>(findingsPath);+ const finding = findings.findings[0]!;+ const canonical = {+ id: "shared",+ label: "Canonical source",+ path: "src/canonical.py",+ startLine: 12,+ code: "canonical_source()",+ explanation: "Canonical explanation.",+ };+ finding.codeEvidence = [+ canonical,+ { ...canonical, code: "second_canonical_source()" },+ { ...canonical, id: "canonical-whitespace", code: " \t " },+ ];+ finding.code_evidence = [+ { id: "shared", code: "shadow_source()" },+ {+ id: "minimal",+ code: "````\nlegacy_minimal()",+ language: "text\n## injected-language",+ },+ { id: "minimal", code: "duplicate_legacy_source()" },+ {+ id: "invalid",+ code: "legacy_invalid_metadata()",+ label: 42,+ path: false,+ role: {},+ language: [],+ explanation: true,+ },+ { id: "whitespace", code: " \t " },+ { id: "whitespace", code: "legacy_whitespace_evidence()" },+ { id: "canonical-whitespace", code: "canonical_whitespace_evidence()" },+ {+ id: "path-only",+ code: "legacy_path_only()",+ path: "src/path-only.py",+ startLine: true,+ endLine: 42,+ },+ {+ id: "range",+ code: "legacy_range()",+ path: "src/range.py",+ startLine: 12,+ endLine: 8,+ label: "Legacy range",+ explanation: "Legacy explanation.",+ role: "source",+ language: "python",+ },+ ];+ await writeJson(findingsPath, findings);+ const manifestPath = join(scanDirectory, "scan-manifest.json");+ const manifest = await readJson<ScanManifest>(manifestPath);+ manifest.scan.target.kind = "git_revision";+ manifest.scan.target.revision = "0123456789abcdef0123456789abcdef01234567";+ delete manifest.scan.target.snapshotDigest;+ await writeJson(manifestPath, manifest);+ await reseal(scanDirectory);+ const saved = await readFile(findingsPath, "utf8");+ const { description } = (+ await prepareScanPublication(+ scanDirectory,+ publicationOptions(scanDirectory),+ )+ ).issues[0]!;+ for (const snippet of [+ "canonical_source()",+ "second_canonical_source()",+ "legacy_minimal()",+ "legacy_invalid_metadata()",+ "legacy_path_only()",+ "legacy_range()",+ "legacy_whitespace_evidence()",+ "canonical_whitespace_evidence()",+ ])+ expect(description).toContain(snippet);+ expect(description.indexOf("canonical_source()")).toBeLessThan(+ description.indexOf("second_canonical_source()"),+ );+ expect(description.indexOf("second_canonical_source()")).toBeLessThan(+ description.indexOf("legacy_minimal()"),+ );+ expect(description.indexOf("legacy_minimal()")).toBeLessThan(+ description.indexOf("legacy_invalid_metadata()"),+ );+ for (const omitted of [+ "shadow_source()",+ "duplicate_legacy_source()",+ "injected-language",+ "undefined",+ "[object Object]",+ ])+ expect(description).not.toContain(omitted);+ expect(description).toContain("`````\n````\nlegacy_minimal()\n`````");+ expect(description.split("```\n \t \n```")).toHaveLength(3);+ expect(description).toContain("`src/path-only.py`");+ expect(description).not.toContain("[`src/path-only.py");+ expect(description).toContain("src/range.py#L12)");+ expect(description).not.toContain("#L12-L8");+ expect(description).toContain("Legacy explanation.");+ expect(await readFile(findingsPath, "utf8")).toBe(saved);+ });++ test("preserves embedded root-cause evidence across historical aliases", async () => {+ const scanDirectory = await copyExample();+ const findingsPath = join(scanDirectory, "findings.json");+ const findings = await readJson<FindingsDocument>(findingsPath);+ const finding = findings.findings[0]!;+ const canonical = {+ id: "shared",+ label: "Canonical source",+ path: "src/canonical.py",+ startLine: 12,+ code: "canonical_source()",+ explanation: "Canonical explanation.",+ };+ finding.codeEvidence = [canonical, canonical];+ finding.code_evidence = [{ id: "top-level", code: "top_level_legacy()" }];+ finding.rootCause = {+ summary: "Canonical root summary.",+ codeEvidence: [+ { id: "shared", code: "embedded_canonical()" },+ { code: "anonymous_first()" },+ { code: "anonymous_second()" },+ { code: "anonymous_first()" },+ null,+ "not an evidence object",+ {},+ { code: false },+ { code: " \t " },+ ],+ code_evidence: [+ { id: "canonical-alias", code: "embedded_canonical_alias()" },+ ],+ };+ finding.root_cause = {+ codeEvidence: [+ { id: "shared", code: "embedded_canonical()" },+ { id: "shared", code: "embedded_legacy()" },+ ],+ code_evidence: [{ id: "legacy-alias", code: "embedded_legacy_alias()" }],+ };+ await writeJson(findingsPath, findings);+ await reseal(scanDirectory);+ const saved = await readFile(findingsPath, "utf8");++ const { description } = (+ await prepareScanPublication(+ scanDirectory,+ publicationOptions(scanDirectory),+ )+ ).issues[0]!;++ const snippets = [+ "canonical_source()",+ "top_level_legacy()",+ "embedded_canonical()",+ "anonymous_first()",+ "anonymous_second()",+ "embedded_canonical_alias()",+ "embedded_legacy()",+ "embedded_legacy_alias()",+ ];+ for (const snippet of snippets) expect(description).toContain(snippet);+ for (let index = 1; index < snippets.length; index++)+ expect(description.lastIndexOf(snippets[index - 1]!)).toBeLessThan(+ description.indexOf(snippets[index]!),+ );+ expect(description.split("canonical_source()")).toHaveLength(3);+ expect(description.split("embedded_canonical()")).toHaveLength(2);+ expect(description.split("anonymous_first()")).toHaveLength(2);+ expect(await readFile(findingsPath, "utf8")).toBe(saved);+ });++ test("preserves nested historical evidence coordinates and source-link rules", async () => {+ const scanDirectory = await copyExample();+ const findingsPath = join(scanDirectory, "findings.json");+ const findings = await readJson<FindingsDocument>(findingsPath);+ const finding = findings.findings[0]!;+ finding.codeEvidence = [+ {+ id: "canonical-nested",+ label: "Canonical nested location",+ path: "src/shadow-canonical.py",+ startLine: 1,+ code: "canonical_nested_location()",+ explanation: "Canonical explanation.",+ location: {+ path: "src/canonical-nested.py",+ startLine: 20,+ endLine: 24,+ },+ },+ {+ id: "canonical-backticks",+ label: "Canonical backtick path",+ path: "app/api/`id`/route.ts",+ startLine: 7,+ code: "canonical_backtick_path()",+ explanation: "Canonical backtick explanation.",+ },+ ];+ finding.code_evidence = [+ {+ id: "nested-range",+ code: "nested_range()",+ path: "src/shadow.py",+ startLine: 1,+ endLine: 2,+ location: { path: "src/nested.py", startLine: 12, endLine: 15 },+ },+ {+ id: "reversed-range",+ code: "reversed_range()",+ location: { path: "src/reversed.py", startLine: 48, endLine: 47 },+ },+ {+ id: "path-only",+ code: "nested_path_only()",+ location: { path: "src/path-only.py", startLine: true, endLine: 42 },+ },+ {+ id: "backtick-path-only",+ code: "backtick_path_only()",+ location: { path: "`route`.ts" },+ },+ {+ id: "unsafe-path",+ code: "unsafe_path()",+ location: { path: "../outside.py", startLine: 10 },+ },+ ];+ await writeJson(findingsPath, findings);+ const manifestPath = join(scanDirectory, "scan-manifest.json");+ const manifest = await readJson<ScanManifest>(manifestPath);+ manifest.scan.target.kind = "git_revision";+ manifest.scan.target.revision = "0123456789abcdef0123456789abcdef01234567";+ delete manifest.scan.target.snapshotDigest;+ await writeJson(manifestPath, manifest);+ await reseal(scanDirectory);+ const saved = await readFile(findingsPath, "utf8");++ const { description } = (+ await prepareScanPublication(+ scanDirectory,+ publicationOptions(scanDirectory),+ )+ ).issues[0]!;++ expect(description).toContain("src/canonical-nested.py#L20-L24)");+ expect(description).toContain("[``app/api/`id`/route.ts:7``]");+ expect(description).toContain("app/api/%60id%60/route.ts#L7)");+ expect(description).toContain("`` `route`.ts ``");+ expect(description).not.toContain("src/shadow-canonical.py");+ expect(description).toContain("src/nested.py:12-15");+ expect(description).toContain("src/nested.py#L12-L15)");+ expect(description).not.toContain("src/shadow.py");+ expect(description).toContain("src/reversed.py#L48)");+ expect(description).not.toContain("#L48-L47");+ expect(description).toContain("`src/path-only.py`");+ expect(description).not.toContain("[`src/path-only.py");+ expect(description).toContain("`../outside.py:10`");+ expect(description).not.toContain("/../outside.py#");+ expect(await readFile(findingsPath, "utf8")).toBe(saved);+ });++ test("preserves embedded validation and attack-path evidence catalogs", async () => {+ const scanDirectory = await copyExample();+ const findingsPath = join(scanDirectory, "findings.json");+ const findings = await readJson<FindingsDocument>(findingsPath);+ const finding = findings.findings[0]!;+ const embedded = (name: string) => ({+ codeEvidence: [{ code: `${name}_canonical()` }],+ code_evidence: [{ code: `${name}_legacy()` }],+ });+ finding.validation = embedded("validation");+ finding.attackPath = {+ ...embedded("attack"),+ dataFlow: embedded("data_flow_camel"),+ dataflow: embedded("data_flow_lower"),+ data_flow: embedded("data_flow_snake"),+ reachability: embedded("reachability"),+ };+ await writeJson(findingsPath, findings);+ await reseal(scanDirectory);+ const saved = await readFile(findingsPath, "utf8");++ const { description } = (+ await prepareScanPublication(+ scanDirectory,+ publicationOptions(scanDirectory),+ )+ ).issues[0]!;++ let previous = -1;+ for (const section of [+ "validation",+ "attack",+ "data_flow_camel",+ "data_flow_lower",+ "data_flow_snake",+ "reachability",+ ]) {+ for (const alias of ["canonical", "legacy"]) {+ const snippet = `${section}_${alias}()`;+ expect(description).toContain(snippet);+ expect(description.indexOf(snippet)).toBeGreaterThan(previous);+ previous = description.indexOf(snippet);+ }+ }+ expect(await readFile(findingsPath, "utf8")).toBe(saved);+ });++ test("preserves string evidence locations as literal labels in both catalogs", async () => {+ const scanDirectory = await copyExample();+ const findingsPath = join(scanDirectory, "findings.json");+ const findings = await readJson<FindingsDocument>(findingsPath);+ const finding = findings.findings[0]!;+ finding.codeEvidence = [+ {+ id: "canonical-string",+ label: "Canonical string location",+ path: "src/shadow-canonical.py",+ startLine: 1,+ code: "canonical_string_location()",+ explanation: "Canonical explanation.",+ location: " src/string.py:12 \n additional context ",+ },+ ];+ finding.code_evidence = [+ {+ id: "legacy-string",+ code: "legacy_string_location()",+ path: "src/shadow-legacy.py",+ startLine: 2,+ location: " `src/quoted.py:12` \n ",+ },+ {+ id: "legacy-backticks",+ code: "legacy_backtick_location()",+ location: "src/with``ticks.py:12",+ },+ ];+ await writeJson(findingsPath, findings);+ const manifestPath = join(scanDirectory, "scan-manifest.json");+ const manifest = await readJson<ScanManifest>(manifestPath);+ manifest.scan.target.kind = "git_revision";+ manifest.scan.target.revision = "0123456789abcdef0123456789abcdef01234567";+ delete manifest.scan.target.snapshotDigest;+ await writeJson(manifestPath, manifest);+ await reseal(scanDirectory);+ const saved = await readFile(findingsPath, "utf8");++ const { description } = (+ await prepareScanPublication(+ scanDirectory,+ publicationOptions(scanDirectory),+ )+ ).issues[0]!;++ expect(description).toContain("`src/string.py:12 additional context`");+ expect(description).toContain("`` `src/quoted.py:12` ``");+ expect(description).toContain("```src/with``ticks.py:12```");+ expect(description).not.toContain("src/shadow-");+ for (const path of ["src/string.py", "src/quoted.py", "src/with``ticks.py"])+ expect(description).not.toContain(`${path}#L`);+ expect(await readFile(findingsPath, "utf8")).toBe(saved);+ });+test("does not turn non-HTTPS repository remotes into source links", async () => {const scanDirectory = await copyExample();const manifestPath = join(scanDirectory, "scan-manifest.json");@@ -447,6 +915,12 @@ describe("scan publication preparation", () => {explanation:"Preserve canonical evidence even without a safe source link.",}));+ findings.findings[0]!.code_evidence = paths.map((path, index) => ({+ id: `legacy-path-${index}`,+ path,+ startLine: 41,+ code: `legacy_preserved_snippet_${index}()`,+ }));await writeJson(findingsPath, findings);await reseal(scanDirectory);@@ -463,6 +937,7 @@ describe("scan publication preparation", () => {for (const [index, path] of paths.entries()) {expect(description).toContain(`**Source:** \`${path}:41\``);expect(description).toContain(`preserved_snippet_${index}()`);+ expect(description).toContain(`legacy_preserved_snippet_${index}()`);expect(description).not.toContain(`[\`${path}:41\`](`);}});