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(snapshot): create new SQLite destinations with private permissions (#1747)
Michael D'Angeloeaa7c59857ca
diff --git a/plugins/codex-security/scripts/snapshot_sqlite.py b/plugins/codex-security/scripts/snapshot_sqlite.pyindex 2f39f0509..e91d81a75 100644--- a/plugins/codex-security/scripts/snapshot_sqlite.py+++ b/plugins/codex-security/scripts/snapshot_sqlite.py@@ -22,6 +22,13 @@ def main() -> None: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:+ creation_destination = destination.resolve()+ try:+ creation_destination.touch(mode=0o600, exist_ok=False)+ except FileExistsError:+ pass+ else:+ creation_destination.chmod(0o600)with sqlite3.connect(destination) as destination_connection:source_connection.backup(destination_connection)destination.chmod(0o600)diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.pyindex 20e22612f..64736fc12 100644--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py@@ -325,6 +325,118 @@ def test_sqlite_snapshot_includes_uncheckpointed_wal_rows(tmp_path: Path) -> Nonassert connection.execute("SELECT value FROM records").fetchone() == ("sealed",)[email protected](os.name == "nt", reason="requires POSIX file permission bits")[email protected]("destination_kind", ["regular", "symlink", "symlink-chain"])[email protected]("umask", [0o022, 0o777])[email protected]("malformed_source", [False, True])+def test_sqlite_snapshot_new_destination_is_private_during_backup(+ tmp_path: Path, destination_kind: str, umask: int, malformed_source: bool+) -> None:+ source = tmp_path / "source.sqlite3"+ snapshot = tmp_path / "snapshot.sqlite3"+ links = []+ if destination_kind != "regular":+ target = tmp_path / "target.sqlite3"+ if destination_kind == "symlink-chain":+ intermediate = tmp_path / "intermediate.sqlite3"+ intermediate.symlink_to(target.name)+ links.append(intermediate)+ target = intermediate+ snapshot.symlink_to(target.name)+ links.append(snapshot)+ link_inodes = {link: link.lstat().st_ino for link in links}+ if malformed_source:+ source.write_text("not a SQLite database")+ else:+ with sqlite3.connect(source) as connection:+ connection.execute("CREATE TABLE records (value TEXT NOT NULL)")+ connection.execute("INSERT INTO records VALUES ('sealed')")+ namespace = runpy.run_path(str(SNAPSHOT_SCRIPT))+ real_connect = sqlite3.connect+ modes = []++ def observe_connect(database, *args, **kwargs):+ if database == snapshot:+ modes.append(snapshot.stat().st_mode & 0o777 if snapshot.exists() else None)+ return real_connect(database, *args, **kwargs)++ previous_umask = os.umask(umask)+ try:+ with (+ mock.patch.object(sys, "argv", [str(SNAPSHOT_SCRIPT), str(source), str(snapshot)]),+ mock.patch.object(sqlite3, "connect", side_effect=observe_connect),+ ):+ if malformed_source:+ with pytest.raises(sqlite3.DatabaseError, match="not a database"):+ namespace["main"]()+ else:+ namespace["main"]()+ finally:+ os.umask(previous_umask)+ assert modes == [0o600]+ assert snapshot.stat().st_mode & 0o777 == 0o600+ for link, inode in link_inodes.items():+ assert link.is_symlink()+ assert link.lstat().st_ino == inode+ if not malformed_source:+ with sqlite3.connect(snapshot) as connection:+ assert connection.execute("SELECT value FROM records").fetchone() == ("sealed",)++[email protected](os.name == "nt", reason="requires POSIX file permission bits")[email protected]("destination_kind", ["regular", "symlink", "hardlink"])[email protected]("malformed_source", [False, True])+def test_sqlite_snapshot_preserves_existing_destination(+ tmp_path: Path, destination_kind: str, malformed_source: bool+) -> None:+ source = tmp_path / "source.sqlite3"+ existing = tmp_path / "existing.sqlite3"+ snapshot = tmp_path / "snapshot.sqlite3"+ if malformed_source:+ source.write_text("not a SQLite database")+ else:+ with sqlite3.connect(source) as connection:+ connection.execute("CREATE TABLE records (value TEXT NOT NULL)")+ connection.execute("INSERT INTO records VALUES ('sealed')")+ with sqlite3.connect(existing) as connection:+ connection.execute("CREATE TABLE records (value TEXT NOT NULL)")+ connection.execute("INSERT INTO records VALUES ('original')")+ existing.chmod(0o644)+ if destination_kind == "symlink":+ snapshot.symlink_to(existing)+ elif destination_kind == "hardlink":+ snapshot.hardlink_to(existing)+ else:+ snapshot = existing+ original_inode = existing.stat().st_ino+ namespace = runpy.run_path(str(SNAPSHOT_SCRIPT))+ real_connect = sqlite3.connect+ modes = []++ def observe_connect(database, *args, **kwargs):+ connection = real_connect(database, *args, **kwargs)+ if database == snapshot:+ modes.append(snapshot.stat().st_mode & 0o777)+ return connection++ with (+ mock.patch.object(sys, "argv", [str(SNAPSHOT_SCRIPT), str(source), str(snapshot)]),+ mock.patch.object(sqlite3, "connect", side_effect=observe_connect),+ ):+ if malformed_source:+ with pytest.raises(sqlite3.DatabaseError, match="not a database"):+ namespace["main"]()+ else:+ namespace["main"]()+ assert modes == [0o644]+ assert snapshot.stat().st_mode & 0o777 == (0o644 if malformed_source else 0o600)+ assert snapshot.stat().st_ino == existing.stat().st_ino == original_inode+ assert snapshot.is_symlink() == (destination_kind == "symlink")+ with sqlite3.connect(existing) as connection:+ expected = "original" if malformed_source else "sealed"+ assert connection.execute("SELECT value FROM records").fetchone() == (expected,)++@pytest.mark.native_windows@pytest.mark.skipif(os.name != "nt", reason="requires Win32 extended-length paths")def test_sqlite_snapshot_reads_windows_extended_length_source(tmp_path: Path) -> None: