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(contract): reject remote targets the SDK reader refuses to load (#1569)
Rabari Mihir7b003d5f1674
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.pyindex ffbfb6069..e659441d8 100644--- a/plugins/codex-security/scripts/finalize_scan_contract.py+++ b/plugins/codex-security/scripts/finalize_scan_contract.py@@ -9,6 +9,7 @@ import csvimport errnoimport hashlibimport io+import ipaddressimport jsonimport mathimport os@@ -67,6 +68,7 @@ RFC3339_RE = re.compile(re.ASCII,)REMOTE_CONTROL_RE = re.compile(r"[\x00-\x1f\x7f-\x9f\u2028\u2029]")+REMOTE_UNSAFE_HOST_RE = re.compile(r"[\s<>^|]")GITHUB_HASH_BLOCK_SIZE = 100GITHUB_HASH_MOD = 37GITHUB_HASH_MASK = (1 << 64) - 1@@ -895,13 +897,35 @@ def _write_scan_local_json(scan_dir: Path, relative_path: str, payload: Any) ->def _validate_remote(remote: str, context: str) -> None:if REMOTE_CONTROL_RE.search(remote):raise ContractError(f"{context}: expected a sanitized canonical absolute URL")- parsed = urlsplit(remote)+ try:+ parsed = urlsplit(remote)+ except ValueError as exc:+ # urlsplit rejects authorities with unbalanced brackets.+ raise ContractError(f"{context}: expected a sanitized canonical absolute URL") from excif "\\" in remote or not parsed.scheme or not parsed.netloc:raise ContractError(f"{context}: expected a sanitized canonical absolute URL")- if parsed.username or parsed.password or parsed.query or parsed.fragment:+ if "@" in parsed.netloc or parsed.query or parsed.fragment:raise ContractError(f"{context}: remote URL must not contain credentials, query, or fragment")+ # Mirrors the authority rules of the WHATWG URL parser the SDK reader applies.+ authority = parsed.netloc+ if authority.startswith("["):+ try:+ ipaddress.IPv6Address(authority.partition("]")[0][1:])+ except ValueError as exc:+ raise ContractError(f"{context}: expected a sanitized canonical absolute URL") from exc+ authority = authority.partition("]")[2]+ if authority and not authority.startswith(":"):+ raise ContractError(f"{context}: expected a sanitized canonical absolute URL")+ port = authority.partition(":")[2]+ hostname = parsed.hostname+ if (+ not hostname+ or REMOTE_UNSAFE_HOST_RE.search(hostname)+ or (port and (not port.isascii() or not port.isdigit() or int(port) > 65535))+ ):+ raise ContractError(f"{context}: expected a sanitized canonical absolute URL")def parse_timestamp(value: str) -> datetime:diff --git a/plugins/codex-security/tests/test_finalize_scan_contract.py b/plugins/codex-security/tests/test_finalize_scan_contract.pyindex ed86530fc..b2931cae6 100644--- a/plugins/codex-security/tests/test_finalize_scan_contract.py+++ b/plugins/codex-security/tests/test_finalize_scan_contract.py@@ -2341,6 +2341,55 @@ The extraction root is not enforced.):FINALIZER.finalize_scan(self.scan_dir)+ def test_rejects_remote_unsupported_port(self) -> None:+ for remote in ("https://example.com:bad/repo", "https://example.com:99999/repo"):+ with self.subTest(remote=remote):+ self.manifest["scan"]["target"]["remote"] = remote+ self.write_scan()+ with self.assertRaisesRegex(+ FINALIZER.ContractError, "expected a sanitized canonical absolute URL"+ ):+ FINALIZER.finalize_scan(self.scan_dir)++ def test_rejects_remote_unsafe_authority(self) -> None:+ for remote in (+ "https://exa mple.com/repo",+ "https://example.com>/repo",+ "https://exa^mple.com/repo",+ "https://exa|mple.com/repo",+ "https://:8022/repo",+ "https://a:b:80/repo",+ "https://[::1/repo",+ "https://[example]/repo",+ ):+ with self.subTest(remote=remote):+ self.manifest["scan"]["target"]["remote"] = remote+ self.write_scan()+ with self.assertRaisesRegex(+ FINALIZER.ContractError, "expected a sanitized canonical absolute URL"+ ):+ FINALIZER.finalize_scan(self.scan_dir)++ def test_rejects_remote_bare_userinfo_separator(self) -> None:+ self.manifest["scan"]["target"]["remote"] = "https://@example.com/repo"+ self.write_scan()+ with self.assertRaisesRegex(FINALIZER.ContractError, "must not contain credentials"):+ FINALIZER.finalize_scan(self.scan_dir)++ def test_seals_remote_port_and_ipv6_authority(self) -> None:+ for remote in (+ "https://example.com:8022/repo",+ "https://example.com:/repo",+ "https://[::1]:8022/repo",+ "https://[::ffff:1.2.3.4]/repo",+ ):+ with self.subTest(remote=remote):+ self.manifest["scan"]["target"]["remote"] = remote+ self.write_scan()+ FINALIZER.finalize_scan(self.scan_dir)+ manifest = self.read_json("scan-manifest.json")+ self.assertEqual(manifest["scan"]["target"]["remote"], remote)+def test_rejects_repository_root_finding_location(self) -> None:self.findings["findings"][0]["locations"][0]["path"] = "."self.write_scan()