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

fix(workbench): preserve sealed CSV artifacts during export (#1047)

mldangelo-oaic828742b3be7
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index e1479238b..d45871973 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -94,6 +94,10 @@ class RecoverableContractError(ContractError):
"""Raised when report projection can safely be retried before publication."""
+class SealedArtifactError(ContractError):
+ """Raised when an export would overwrite a sealed scan artifact."""
+
+
def _reject_non_finite_json(value: str) -> None:
raise ValueError(f"non-finite JSON number {value!r} is not supported")
@@ -2867,7 +2871,7 @@ def write_export_output(scan_dir: Path, output: Path, export_format: str, conten
raise ContractError(f"{relative_output}: unable to inspect export output") from exc
for artifact_path in artifact_paths:
if artifact_path == relative_output:
- raise ContractError(
+ raise SealedArtifactError(
f"{export_format.upper()} output path cannot overwrite a sealed scan artifact"
)
if output_metadata is None:
@@ -2880,7 +2884,7 @@ def write_export_output(scan_dir: Path, output: Path, export_format: str, conten
finally:
os.close(descriptor)
if os.path.samestat(output_metadata, artifact_metadata):
- raise ContractError(
+ raise SealedArtifactError(
f"{export_format.upper()} output path cannot overwrite a sealed scan artifact"
)
write_scan_local_bytes(
diff --git a/plugins/codex-security/scripts/workbench_publication.py b/plugins/codex-security/scripts/workbench_publication.py
index 1816c2ee3..411f3f81a 100644
--- a/plugins/codex-security/scripts/workbench_publication.py
+++ b/plugins/codex-security/scripts/workbench_publication.py
@@ -17,6 +17,7 @@ from urllib.parse import quote
sys.path.insert(0, str(Path(__file__).resolve().parent))
from finalize_scan_contract import (
ContractError,
+ SealedArtifactError,
build_threat_model_export,
csv_cell,
finalize_scan,
@@ -24,7 +25,6 @@ from finalize_scan_contract import (
finding_csv_columns,
write_export_output,
write_sarif_projection,
- write_scan_local_bytes,
)
@@ -447,17 +447,20 @@ def write_csv_export(
row["end_line"],
)
)
+ destination = scan_dir / "exports" / "findings.csv"
try:
- write_scan_local_bytes(
+ write_export_output(
scan_dir,
- "exports/findings.csv",
+ destination,
+ "csv",
output.getvalue().encode("utf-8"),
)
+ except SealedArtifactError as exc:
+ raise SystemExit(str(exc)) from exc
except ContractError as exc:
raise SystemExit(
"exports: expected a regular directory inside the scan directory."
) from exc
- destination = scan_dir / "exports" / "findings.csv"
path = db.available_artifact_path(scan_dir, destination)
if path is None:
raise SystemExit("findings.csv: expected a regular file inside the scan directory.")
diff --git a/plugins/codex-security/tests/test_workbench_db_exports.py b/plugins/codex-security/tests/test_workbench_db_exports.py
index d4a5b3845..b1880ff23 100644
--- a/plugins/codex-security/tests/test_workbench_db_exports.py
+++ b/plugins/codex-security/tests/test_workbench_db_exports.py
@@ -1260,6 +1260,53 @@ def test_primary_location_prefers_root_control_in_bounded_and_csv_results(
assert row["path"] == "root.py"
+def test_csv_export_preserves_a_sealed_export(tmp_path: Path, workbench_api) -> None:
+ state_dir = tmp_path / "state"
+ target = tmp_path / "target"
+ target.mkdir()
+ saved = create_saved_workspace(state_dir, target)
+ started = start_delivered_scan(
+ state_dir,
+ "--workspace-id",
+ str(saved["id"]),
+ "--scan-root",
+ str(tmp_path / "scans"),
+ )["results"]
+ scan_id = str(started["scanId"])
+ scan_dir = Path(str(started["scanDir"]))
+ write_completed_contract(scan_dir, scan_id, target)
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ scan = workbench_api["require_scan"](connection, scan_id)
+ binding = workbench_api["workbench_completion_binding"](scan, workbench_api["now"]())
+ manifest, _, _ = workbench_api["finalize_scan"](scan_dir, completion_binding=binding)
+ csv_path = scan_dir / "exports" / "findings.csv"
+ sealed_csv = b"original,sealed,export\n"
+ csv_path.write_bytes(sealed_csv)
+ manifest["scan"]["artifacts"].append(
+ {
+ "path": "exports/findings.csv",
+ "sha256": hashlib.sha256(sealed_csv).hexdigest(),
+ "mediaType": "text/csv",
+ }
+ )
+ manifest_path = scan_dir / "scan-manifest.json"
+ manifest_path.write_text(json.dumps(manifest, allow_nan=False, indent=2, sort_keys=True) + "\n")
+ run_workbench(state_dir, "complete-scan", "--scan-id", scan_id)
+ sealed_manifest = manifest_path.read_bytes()
+
+ rejected = run_workbench(
+ state_dir, "export-findings", "--scan-id", scan_id, "--format", "csv", check=False
+ )
+
+ assert rejected["returncode"] != 0
+ assert "CSV output path cannot overwrite a sealed scan artifact" in rejected["stderr"]
+ assert csv_path.read_bytes() == sealed_csv
+ assert manifest_path.read_bytes() == sealed_manifest
+ exported = run_workbench(state_dir, "export-findings", "--scan-id", scan_id, "--format", "json")
+ assert exported["export"]["path"] == str(scan_dir / "findings.json")
+
+
def test_csv_export_rejects_symlinked_exports_directory(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
target = tmp_path / "target"
diff --git a/sdk/typescript/scripts/check-markdown.mjs b/sdk/typescript/scripts/check-markdown.mjs
index 1ef2ff028..b06f20fda 100644
--- a/sdk/typescript/scripts/check-markdown.mjs
+++ b/sdk/typescript/scripts/check-markdown.mjs
@@ -4,11 +4,22 @@ import { join } from "node:path";
import { fileURLToPath } from "node:url";
const root = fileURLToPath(new URL("../../../", import.meta.url));
-const files = execFileSync("git", ["ls-files", "-z", "--", "*.md"], {
- cwd: root,
- encoding: "utf8",
- maxBuffer: Infinity,
-})
+const files = execFileSync(
+ "git",
+ [
+ "ls-files",
+ "-z",
+ "--",
+ ":(glob)sdk/typescript/**/*.md",
+ ":(glob)examples/custom-validation/*.md",
+ ":(glob)plugins/codex-security/native/*.md",
+ ],
+ {
+ cwd: root,
+ encoding: "utf8",
+ maxBuffer: Infinity,
+ },
+)
.split("\0")
.filter(
(path) =>
diff --git a/sdk/typescript/tests-ts/check-markdown.test.ts b/sdk/typescript/tests-ts/check-markdown.test.ts
index 71d93f921..16311efa5 100644
--- a/sdk/typescript/tests-ts/check-markdown.test.ts
+++ b/sdk/typescript/tests-ts/check-markdown.test.ts
@@ -16,7 +16,7 @@ test("checks tracked Markdown including leading-dash paths and skips deleted, un
const root = await mkdtemp(join(tmpdir(), "markdown checks "));
const sdk = join(root, "sdk", "typescript");
const script = join(sdk, "scripts", "check-markdown.mjs");
- const guide = join(root, "guide with # spaces.md");
+ const guide = join(sdk, "guide with # spaces.md");
const invalid = "# Title\n\n\n";
try {
await mkdir(join(sdk, "scripts"), { recursive: true });
@@ -30,23 +30,36 @@ test("checks tracked Markdown including leading-dash paths and skips deleted, un
"junction",
);
await writeFile(join(root, ".gitignore"), "node_modules/\n");
- await writeFile(join(root, "README.md"), "# Readme\n");
- await writeFile(join(root, "--plugin=fixture.md"), "# Markdown fixture\n");
+ await writeFile(join(sdk, "README.md"), "# Readme\n");
+ // The previous format command checks these folders, not all repository Markdown.
+ await mkdir(join(root, "github-action"));
+ await writeFile(join(root, "github-action", "README.md"), invalid);
+ const outsideGuides = [
+ join(root, "examples", "custom-validation", "guide.md"),
+ join(root, "plugins", "codex-security", "native", "guide.md"),
+ ];
+ for (const path of outsideGuides) {
+ await mkdir(join(path, ".."), { recursive: true });
+ await writeFile(path, invalid);
+ await mkdir(join(path, "..", "nested"));
+ await writeFile(join(path, "..", "nested", "outside.md"), invalid);
+ }
+ await writeFile(join(sdk, "--plugin=fixture.md"), "# Markdown fixture\n");
// The combined paths exceed the Windows process command-line limit.
for (let index = 0; index < 400; index++) {
await writeFile(
- join(root, `guide-${index}-${"x".repeat(80)}.md`),
+ join(sdk, `guide-${index}-${"x".repeat(80)}.md`),
"# Guide\n",
);
}
await writeFile(guide, invalid);
- await writeFile(join(root, "deleted.md"), invalid);
+ await writeFile(join(sdk, "deleted.md"), invalid);
if (process.platform !== "win32")
- await symlink("untracked.md", join(root, "linked.md"));
+ await symlink("untracked.md", join(sdk, "linked.md"));
gitText(["init", "--quiet"], { cwd: root });
gitText(["add", "--", "."], { cwd: root });
- await rm(join(root, "deleted.md"));
- await writeFile(join(root, "untracked.md"), invalid);
+ await rm(join(sdk, "deleted.md"));
+ await writeFile(join(sdk, "untracked.md"), invalid);
const failing = await runCommand("node", [script], {
cwd: sdk,
@@ -54,8 +67,13 @@ test("checks tracked Markdown including leading-dash paths and skips deleted, un
});
expect(failing.status).toBe(1);
expect(failing.stdout + failing.stderr).toContain("guide with # spaces.md");
+ for (const path of outsideGuides)
+ expect(failing.stdout + failing.stderr).toContain(
+ path.replaceAll("\\", "/").slice(root.length + 1),
+ );
await writeFile(guide, "# Title\n");
+ for (const path of outsideGuides) await writeFile(path, "# Guide\n");
const passing = await runCommand("node", [script], {
cwd: sdk,
timeout: 30_000,