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(plugin): preserve native filename bytes in SQLite URIs (#2571)

Michael D'Angelo4fac2dfdf362
diff --git a/plugins/codex-security/scripts/snapshot_sqlite.py b/plugins/codex-security/scripts/snapshot_sqlite.py
index 4d8f7a3dc..d3e5918e2 100644
--- a/plugins/codex-security/scripts/snapshot_sqlite.py
+++ b/plugins/codex-security/scripts/snapshot_sqlite.py
@@ -4,11 +4,18 @@
from __future__ import annotations
import argparse
+import os
import sqlite3
from pathlib import Path
from urllib.parse import quote
+def readonly_sqlite_uri(path: Path) -> str:
+ """Escape the native filename, including separators, without introducing a URI authority."""
+ filename = os.fsencode(path) if os.name == "posix" else str(path)
+ return f"file:{quote(filename, safe='')}?mode=ro"
+
+
def main() -> None:
"""Back up the read-only source database into a separate destination using SQLite’s backup API."""
parser = argparse.ArgumentParser(description=__doc__)
@@ -21,8 +28,7 @@ def main() -> None:
if destination.exists() and destination.samefile(source):
parser.error(f"Destination must not be the same file as source: {destination}")
destination.parent.mkdir(parents=True, exist_ok=True)
- source_uri = f"file:{quote(str(source), safe='')}?mode=ro"
- with sqlite3.connect(source_uri, uri=True) as source_connection:
+ with sqlite3.connect(readonly_sqlite_uri(source), uri=True) as source_connection:
creation_destination = destination.resolve()
try:
creation_destination.touch(mode=0o600, exist_ok=False)
diff --git a/plugins/codex-security/scripts/workbench_publication.py b/plugins/codex-security/scripts/workbench_publication.py
index 42b606ad1..0a1e1dc80 100644
--- a/plugins/codex-security/scripts/workbench_publication.py
+++ b/plugins/codex-security/scripts/workbench_publication.py
@@ -12,7 +12,6 @@ import sys
from contextlib import closing
from pathlib import Path
from typing import Any
-from urllib.parse import quote
# Some plugin hosts launch Python with safe-path isolation enabled.
sys.path.insert(0, str(Path(__file__).resolve().parent))
@@ -27,6 +26,7 @@ from finalize_scan_contract import (
write_export_output,
write_sarif_projection,
)
+from snapshot_sqlite import readonly_sqlite_uri
from validate_scan_contract import validate_contract
from workbench_constants import PRIMARY_FINDING_LOCATION_SQL
@@ -226,9 +226,8 @@ def finding_issues(db: Any, payload: Any) -> dict[str, Any]:
except PermissionError:
pass # Let SQLite report the open failure used by the host's state fallback.
if payload["action"] == "inspect":
- database_uri = f"file:{quote(str(path), safe='')}?mode=ro"
try:
- connection = sqlite3.connect(database_uri, uri=True, timeout=5)
+ connection = sqlite3.connect(readonly_sqlite_uri(path), uri=True, timeout=5)
except sqlite3.OperationalError as exc:
if str(exc) == "unable to open database file":
exc._codex_security_state_unavailable = True
diff --git a/plugins/codex-security/scripts/workbench_scan_usage.py b/plugins/codex-security/scripts/workbench_scan_usage.py
index f06df3788..0972ca691 100644
--- a/plugins/codex-security/scripts/workbench_scan_usage.py
+++ b/plugins/codex-security/scripts/workbench_scan_usage.py
@@ -17,9 +17,9 @@ from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Mapping
-from urllib.parse import quote
sys.path.insert(0, str(Path(__file__).resolve().parent))
+from snapshot_sqlite import readonly_sqlite_uri
from workbench.storage import archive_source_directory, worker_usage_directory
TOKEN_FIELDS = {
@@ -654,7 +654,7 @@ def _discover_rollout_sessions(
completed_at: datetime | None,
) -> tuple[list[RolloutSession], set[str]]:
database = sqlite3.connect(
- f"file:{quote(str(state_database), safe='')}?mode=ro",
+ readonly_sqlite_uri(state_database),
uri=True,
timeout=STATE_DATABASE_TIMEOUT_SECONDS,
)
diff --git a/plugins/codex-security/tests/test_finding_issue_receipts.py b/plugins/codex-security/tests/test_finding_issue_receipts.py
index 81244f719..12767158e 100644
--- a/plugins/codex-security/tests/test_finding_issue_receipts.py
+++ b/plugins/codex-security/tests/test_finding_issue_receipts.py
@@ -6,6 +6,7 @@ import json
import os
import shutil
import sqlite3
+import sys
import uuid
from contextlib import closing
from pathlib import Path
@@ -127,6 +128,18 @@ def test_inaccessible_state_preserves_sqlite_open_error(tmp_path: Path, action:
state.chmod(0o700)
[email protected](sys.platform != "linux", reason="requires Linux filename bytes")
+def test_inspects_recorded_receipt_in_state_path_with_non_utf8_bytes(tmp_path: Path) -> None:
+ state = tmp_path / os.fsdecode(b"state-\xff?#%")
+ scan_dir, validated = source(tmp_path)
+ receipt = receipt_for(validated)
+ issues(state, scan_dir, "record", receipts=[receipt])
+ assert (
+ issues(state, scan_dir, "inspect")["receipts"][0]["issueIdentifier"]
+ == receipt["issueIdentifier"]
+ )
+
+
@pytest.mark.parametrize("project", [None, "project-example"])
def test_legacy_receipts_are_read_without_migration_and_preserved_on_upgrade(
tmp_path: Path, project: str | None
diff --git a/plugins/codex-security/tests/test_workbench_scan_usage.py b/plugins/codex-security/tests/test_workbench_scan_usage.py
index bf85a47d8..c4fae3a14 100644
--- a/plugins/codex-security/tests/test_workbench_scan_usage.py
+++ b/plugins/codex-security/tests/test_workbench_scan_usage.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import hashlib
import json
+import os
import sqlite3
import subprocess
import sys
@@ -1310,27 +1311,25 @@ def test_usage_finds_state_database_in_configured_home(
@pytest.mark.cross_platform
@pytest.mark.parametrize(
- "spelling",
- ["leading-relative", "trailing-absolute"]
- + ([] if sys.platform == "win32" else ["literal-tilde"]),
+ ("spelling", "filename"),
+ [
+ ("leading-relative", " usage.sqlite"),
+ (
+ "trailing-absolute",
+ "usage.sqlite\N{NO-BREAK SPACE}" if sys.platform == "win32" else "usage.sqlite ",
+ ),
+ ]
+ + ([] if sys.platform == "win32" else [("literal-tilde", "~synthetic-usage.sqlite")])
+ + ([("filesystem-bytes", b"usage-\xff?#%.sqlite")] if sys.platform == "linux" else []),
)
def test_completion_reads_the_literal_usage_database(
- tmp_path: Path, spelling: str, monkeypatch: pytest.MonkeyPatch
+ tmp_path: Path, spelling: str, filename: str | bytes, monkeypatch: pytest.MonkeyPatch
) -> None:
fixture = _start_scan(tmp_path)
counted = fixture.started_at + timedelta(microseconds=1)
fixture.write_parent_rollout([_token_event(counted, 18, 2)])
original = Path(fixture.environment["CODEX_SQLITE_HOME"]) / "state_5.sqlite"
- selected = (
- tmp_path
- / {
- "leading-relative": " usage.sqlite",
- "trailing-absolute": "usage.sqlite\N{NO-BREAK SPACE}"
- if sys.platform == "win32"
- else "usage.sqlite ",
- "literal-tilde": "~synthetic-usage.sqlite",
- }[spelling]
- )
+ selected = tmp_path / os.fsdecode(filename)
original.rename(selected)
neighbor = tmp_path / "neighbor"
diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
index f78172ad5..2cdc36c9b 100644
--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
@@ -361,8 +361,23 @@ def assert_new_scan_model_is_independent(state_dir: Path, target: Path, scan_roo
).fetchone() == (None, None, "gpt-5.6-sol", "high")
-def test_sqlite_snapshot_includes_uncheckpointed_wal_rows(tmp_path: Path) -> None:
- source = tmp_path / "source.sqlite3"
+ "source_name",
+ [
+ b"source.sqlite3",
+ pytest.param(
+ b"source-\xff?#%.sqlite3",
+ marks=pytest.mark.skipif(
+ sys.platform != "linux", reason="requires Linux filename bytes"
+ ),
+ id="linux-filename-bytes",
+ ),
+ ],
+)
+def test_sqlite_snapshot_includes_uncheckpointed_wal_rows(
+ tmp_path: Path, source_name: bytes
+) -> None:
+ source = tmp_path / os.fsdecode(source_name)
snapshot = tmp_path / "snapshot.sqlite3"
with sqlite3.connect(source) as connection:
connection.execute("PRAGMA journal_mode = WAL")
diff --git a/sdk/typescript/tests-ts/workbench-compressed-usage.test.ts b/sdk/typescript/tests-ts/workbench-compressed-usage.test.ts
index db2df5903..7e383c171 100644
--- a/sdk/typescript/tests-ts/workbench-compressed-usage.test.ts
+++ b/sdk/typescript/tests-ts/workbench-compressed-usage.test.ts
@@ -42,6 +42,7 @@ async function fixture() {
const plugin = join(root, "plugin");
const paths = [
"scripts/workbench_scan_usage.py",
+ "scripts/snapshot_sqlite.py",
"scripts/windows_scan_local_files.py",
"scripts/read_codex_session.mjs",
"scripts/workbench/__init__.py",