Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions plugins/codex-security/scripts/workbench_publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from validate_scan_contract import validate_contract
from workbench_constants import PRIMARY_FINDING_LOCATION_SQL

# SQLite's default Win32 VFS has MAX_PATH * 4 bytes; the pager reserves eight for "-journal".
_WIN32_SQLITE_FILENAME_BYTES = 260 * 4 - 8


def destination_key(destination: Any) -> str:
"""Encode exact provider-specific destination IDs into a stable key for matching issue receipts."""
Expand Down Expand Up @@ -211,10 +214,15 @@ def finding_issues(db: Any, payload: Any) -> dict[str, Any]:
"storeExists": True,
"receipts": [],
}
path = db.database_path()
if payload["action"] == "inspect" or require_history:
path = db.database_path()
stat_path = path
if os.name == "nt":
from windows_scan_local_files import _extended_path

stat_path = Path(_extended_path(path))
try:
path.stat()
stat_path.stat()
except FileNotFoundError:
if require_history:
raise SystemExit(
Expand All @@ -226,8 +234,15 @@ 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":
sqlite_path = stat_path
if os.name == "nt" and (
len(str(path).encode("utf-8"))
<= _WIN32_SQLITE_FILENAME_BYTES
< len(str(stat_path).encode("utf-8"))
):
sqlite_path = path
try:
connection = sqlite3.connect(readonly_sqlite_uri(path), uri=True, timeout=5)
connection = sqlite3.connect(readonly_sqlite_uri(sqlite_path), uri=True, timeout=5)
except sqlite3.OperationalError as exc:
if str(exc) == "unable to open database file":
exc._codex_security_state_unavailable = True
Expand Down
113 changes: 112 additions & 1 deletion plugins/codex-security/tests/test_finding_issue_receipts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
import sys
import uuid
from contextlib import closing
from pathlib import Path
from pathlib import Path, PureWindowsPath
from types import SimpleNamespace
from unittest.mock import Mock
from urllib.parse import quote

import pytest
from workbench_test_support import load_script, run_workbench, write_completed_contract
Expand All @@ -26,6 +29,114 @@
JIRA = {"type": "jira", "cloudId": "site-example", "projectId": "10001"}


@pytest.mark.cross_platform
@pytest.mark.parametrize("root", ["C:\\", "\\\\server\\share\\"])
@pytest.mark.parametrize("near_byte_limit", [False, True])
def test_inspection_windows_filename_spelling(
tmp_path: Path, monkeypatch, root, near_byte_limit
) -> None:
publication = load_script("workbench_publication")
scan_dir, validated = source(tmp_path)
payload = {"action": "inspect", "scanDirectory": str(scan_dir), "destination": LINEAR}
directory = PureWindowsPath(root) / ("s" * (240 - len(root))) / "state #% é"
if near_byte_limit:
directory /= "界" * 200
remaining = 1030 - len(str(directory / "workbench.sqlite3").encode("utf-8")) - 1
directory /= "s" * remaining
ordinary = directory / "workbench.sqlite3"
if near_byte_limit:
assert len(str(ordinary).encode("utf-8")) == 1030
else:
assert len(str(directory)) < 260 <= len(str(ordinary))
expected = (
("\\\\?\\UNC\\" + str(ordinary)[2:])
if root.startswith("\\\\")
else "\\\\?\\" + str(ordinary)
)
stat = Mock()

class Filename(PureWindowsPath):
def stat(self):
return stat(str(self))

path = Filename(ordinary)
connect = Mock(return_value=sqlite3.connect(":memory:"))
monkeypatch.setattr(publication, "os", SimpleNamespace(name="nt"))
monkeypatch.setattr(publication, "validate_contract", lambda _: validated)
monkeypatch.setattr(publication, "Path", Filename)
monkeypatch.setattr(publication.sqlite3, "connect", connect)

result = publication.finding_issues(SimpleNamespace(database_path=lambda: path), payload)

assert result["storeExists"] is True
assert {"stat": stat.call_args.args[0], "uri": connect.call_args.args[0]} == {
"stat": expected,
"uri": f"file:{quote(str(ordinary) if near_byte_limit else expected, safe='')}?mode=ro",
}
assert connect.call_args.kwargs == {"uri": True, "timeout": 5}


@pytest.mark.native_windows
@pytest.mark.skipif(os.name != "nt", reason="requires native Windows extended-length paths")
@pytest.mark.parametrize("extended", [False, True])
def test_inspection_native_windows_filename_boundary(tmp_path: Path, extended: bool) -> None:
scan_dir, validated = source(tmp_path)
windows_files = load_script("windows_scan_local_files")
directory = tmp_path / ("s" * (244 - len(str(tmp_path))) + " #% é")
path = directory / "workbench.sqlite3"
assert len(str(path)) >= 260
native_path = Path(windows_files._extended_path(path))
legacy_database(native_path, validated, LINEAR)
original = native_path.read_bytes()

result = issues(native_path.parent if extended else directory, scan_dir, "inspect")

assert result["storeExists"] is True
assert result["receipts"][0]["issueIdentifier"] == "SEC-101"
assert native_path.read_bytes() == original
assert list(native_path.parent.iterdir()) == [native_path]


@pytest.mark.native_windows
@pytest.mark.skipif(os.name != "nt", reason="requires native Windows SQLite")
def test_inspection_native_windows_sqlite_byte_limit(tmp_path: Path) -> None:
scan_dir, validated = source(tmp_path)
seed = tmp_path / "seed" / "workbench.sqlite3"
legacy_database(seed, validated, LINEAR)
original = seed.read_bytes()

# Use multiple components so only the complete SQLite filename reaches the byte limit.
directory = tmp_path
while 1030 - len(str(directory / seed.name).encode("utf-8")) > 180:
directory /= "界" * 40
padding = 1030 - len(str(directory / seed.name).encode("utf-8")) - 1
path = directory / ("s" * padding) / seed.name
assert len(str(path).encode("utf-8")) == 1030
native_path = Path(load_script("windows_scan_local_files")._extended_path(path))
native_path.parent.mkdir(parents=True)
native_path.write_bytes(original)

# The compatibility case requires SQLite to support the ordinary name on this host.
try:
connection = sqlite3.connect(
load_script("snapshot_sqlite").readonly_sqlite_uri(path), uri=True
)
except sqlite3.OperationalError as exc:
if str(exc) == "unable to open database file":
pytest.skip("native SQLite cannot open the ordinary 1,030-byte control filename")
raise
with closing(connection):
assert connection.execute("SELECT external_id FROM finding_publications").fetchone() == (
"SEC-101",
)

result = issues(path.parent, scan_dir, "inspect")
assert result["storeExists"] is True
assert result["receipts"][0]["issueIdentifier"] == "SEC-101"
assert native_path.read_bytes() == original
assert list(native_path.parent.iterdir()) == [native_path]


def source(tmp_path: Path) -> tuple[Path, dict]:
scan_dir = tmp_path / "scan"
shutil.copytree(PLUGIN_ROOT / "examples" / "completed-scan", scan_dir)
Expand Down
3 changes: 2 additions & 1 deletion sdk/typescript/tests-ts/inventory-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,8 @@ for (const command of ["generate-in-scope-files", "make-repo-scope-input"]) {
});
const shell = join(f.root, "operator's shell");
mkdirSync(shell);
const envAlias = join(f.root, "launch-env");
// Multicall implementations select the utility by its executable name.
const envAlias = join(f.root, "env");
symlinkSync("/usr/bin/env", envAlias);
symlinkSync("/bin/sh", join(shell, "final-sh"));
writeFileSync(
Expand Down
Loading