Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion scripts/scan_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@
RE_EXEC_EVAL = re.compile(r"\b(exec|eval)\s*\(")

# Network APIs (excludes urllib.parse which is pure string manipulation)
# ``httpx2`` is the pydantic-maintained successor and a separate import name, so the older
# ``httpx``-only alternative did not see it. openai 3.0.0 requires httpx2 and routes every
# call through it, which made the SDK's own HTTP invisible to each combined check that needs
# a network half (secrets + network, IMDS + network, archive + network).
RE_NETWORK = re.compile(
r"\burllib\.request\b"
r"|\burlopen\s*\("
r"|\brequests\s*\.\s*(get|post|put|patch|delete|head|Session)\b"
r"|\bhttpx\s*\.\s*(get|post|put|patch|delete|Client|AsyncClient)\b"
r"|\b(?:httpx|httpx2)\s*\.\s*(get|post|put|patch|delete|Client|AsyncClient)\b"
r"|\bsocket\s*\.\s*(socket|create_connection)\b"
r"|\bhttp\.client\b"
r"|\bhttp\.server\b",
Expand Down
76 changes: 36 additions & 40 deletions scripts/scan_packages_baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -330,38 +330,6 @@
"evidence": "L274: while True: sha256:90a38e5c1e26893c7c273354143612640e9a9c0f079d3e2b60612d79f24e80a6",
"evidence_hash": "1022e8e8649436ec64a98a9d9141d085452c49549fd2157b0278fc369a83ac66"
},
{
"package": "openai",
"file": "openai/_client.py",
"check": "Harvests environment variables/secrets AND makes network calls",
"severity": "CRITICAL",
"evidence": "Env: L209: api_key = os.environ.get(\"OPENAI_API_KEY\") | L219: admin_api_key = os.environ.get(\"OPENAI_ADMIN_KEY\") | L243: webhook_secret = os.environ.get(\"OPENAI_WEBHOOK_SECRET\") | L805: api_key = os.environ.get(\"OPENAI_API_KEY\") | L815: admin_api_key = os.environ.get(\"OPENAI_ADMIN_KEY\") | L839: webhook_secret = os.environ.get(\"OPENAI_WEBHOOK_SECRET\")\nNetwork: L144: http_client: httpx.Client | None = None, | L586: http_client: httpx.Client | None = None, | L740: http_client: httpx.AsyncClient | None = None, | L1193: http_client: httpx.AsyncClient | None = None,",
"evidence_hash": "d806c1e5eedb1eba7e2d9e6f31f3cc59b1882c8e843dfa3f5eac1fe7abdf296d"
},
{
"package": "openai",
"file": "openai/auth/_workload.py",
"check": "Accesses cloud metadata/IMDS AND makes network calls",
"severity": "CRITICAL",
"evidence": "IMDS: L97: url = \"http://169.254.169.254/metadata/identity/oauth2/token\" | L150: url = \"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity\"\nNetwork: L78: http_client: httpx.Client | None = None, | L109: with httpx.Client() as client: | L134: http_client: httpx.Client | None = None, | L156: with httpx.Client() as client: | L251: exchange_client = DefaultHttpx2Client(follow_redirects=False) if self._use_httpx2 else httpx.Client()",
"evidence_hash": "9717e51cb961dc14c458955d91a1e48e3753997346ecea0106bded3a8d64bfe0"
},
{
"package": "openai",
"file": "openai/lib/azure.py",
"check": "Harvests environment variables/secrets AND makes network calls",
"severity": "CRITICAL",
"evidence": "Env: L214: api_key = os.environ.get(\"AZURE_OPENAI_API_KEY\") | L217: azure_ad_token = os.environ.get(\"AZURE_OPENAI_AD_TOKEN\") | L538: api_key = os.environ.get(\"AZURE_OPENAI_API_KEY\") | L541: azure_ad_token = os.environ.get(\"AZURE_OPENAI_AD_TOKEN\")\nNetwork: L37: _HttpxClientT = TypeVar(\"_HttpxClientT\", bound=Union[httpx.Client, httpx.AsyncClient]) | L100: class AzureOpenAI(BaseAzureClient[httpx.Client, Stream[Any]], OpenAI): | L119: http_client: httpx.Client | None = None, | L141: http_client: httpx.Client | None = None, | L163: http_client: httpx.Client | None = None, | L189: http_client: httpx.Client | None = None, | L297: http_client: httpx.Client | None = None, | L421: class AsyncAzureOpenAI(BaseAzureClient[httpx.AsyncClient, AsyncStream[Any]], AsyncOpenAI): | L441: http_client: httpx.AsyncClient | None = None, | L464: http_client: httpx.AsyncClient | None = None, | L487: http_client: httpx.AsyncClient | None = None, | L513: http_client: httpx.AsyncClient | None = None, | L621: http_client: httpx.AsyncClient | None = None,",
"evidence_hash": "a81d958bdcc6c2e98290a6592a9d52f8fc44e6ce4ac983301840136464779923"
},
{
"package": "openai",
"file": "openai/lib/bedrock.py",
"check": "Harvests environment variables/secrets AND makes network calls",
"severity": "CRITICAL",
"evidence": "Env: L105: token = os.environ.get(\"AWS_BEARER_TOKEN_BEDROCK\") | L150: environment_token = os.environ.get(\"AWS_BEARER_TOKEN_BEDROCK\")\nNetwork: L415: http_client: httpx.Client | None = None, | L531: http_client: httpx.Client | None = None, | L649: http_client: httpx.AsyncClient | None = None, | L767: http_client: httpx.AsyncClient | None = None,",
"evidence_hash": "92dbec8ccd79c1e0bc41e93cdd0bdbb091220616c6a1352873196e9dda6bd85c"
},
{
"package": "openai",
"file": "openai/resources/beta/responses/responses.py",
Expand Down Expand Up @@ -1763,14 +1731,6 @@
"evidence": "L281: while True: sha256:fbefef3c441ac4aad2220118629caaf5947f6e5ce919b5d22696559b78cefe34",
"evidence_hash": "6db10c4435d07c75201311256c6e35d807a271250d9d1520ac00b3622e8daebb"
},
{
"package": "openai",
"file": "openai/auth/_workload.py",
"check": "Accesses cloud metadata / IMDS endpoints",
"severity": "HIGH",
"evidence": "L97: url = \"http://169.254.169.254/metadata/identity/oauth2/token\" | L150: url = \"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity\"",
"evidence_hash": "b74e53f2e9dcf692737e69af8ca6b77448b65a6f4b6b96218d483c22124048af"
},
{
"package": "botocore",
"file": "botocore/httpsession.py",
Expand All @@ -1794,6 +1754,42 @@
"severity": "HIGH",
"evidence": "Obfusc: L530: module = __import__(mod_path, fromlist=[\"compute_module_sizes\"])\nExec: L1366: model.eval()",
"evidence_hash": "d11a00fda9ab43a1390c5a10cf09f760efcbacd26c89fc18b359f063ee167135"
},
{
"package": "openai",
"file": "openai/_client.py",
"check": "Harvests environment variables/secrets AND makes network calls",
"severity": "CRITICAL",
"evidence": "Env: L210: api_key = os.environ.get(\"OPENAI_API_KEY\") | L220: admin_api_key = os.environ.get(\"OPENAI_ADMIN_KEY\") | L244: webhook_secret = os.environ.get(\"OPENAI_WEBHOOK_SECRET\") | L816: api_key = os.environ.get(\"OPENAI_API_KEY\") | L826: admin_api_key = os.environ.get(\"OPENAI_ADMIN_KEY\") | L850: webhook_secret = os.environ.get(\"OPENAI_WEBHOOK_SECRET\")\nNetwork: L147: http_client: httpx2.Client | None = None, | L599: http_client: httpx2.Client | None = None, | L753: http_client: httpx2.AsyncClient | None = None, | L1216: http_client: httpx2.AsyncClient | None = None,",
"evidence_hash": "ee8e7ed79d2bfe99f85f53b1bff3d0f59cfc0de3393ab31a2955ef4365005e3c",
"file_sha256": "389ea48de3b3c46a9048d6636ca19e43533bda98e4dccde2d7f96037d0c82817"
},
{
"package": "openai",
"file": "openai/auth/_workload.py",
"check": "Accesses cloud metadata/IMDS AND makes network calls",
"severity": "CRITICAL",
"evidence": "IMDS: L97: url = \"http://169.254.169.254/metadata/identity/oauth2/token\" | L150: url = \"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity\"\nNetwork: L78: http_client: httpx2.Client | None = None, | L109: with httpx2.Client() as client: | L134: http_client: httpx2.Client | None = None, | L156: with httpx2.Client() as client:",
"evidence_hash": "a4b0fb17867ab99605026f7d5d753ba2d3cd0a79f5fab6d77521ec251c256a4c",
"file_sha256": "6a01858ee5a351df969067995bde0a97d4762455468f24c116f818db3aaec95d"
},
{
"package": "openai",
"file": "openai/lib/azure.py",
"check": "Harvests environment variables/secrets AND makes network calls",
"severity": "CRITICAL",
"evidence": "Env: L215: api_key = os.environ.get(\"AZURE_OPENAI_API_KEY\") | L218: azure_ad_token = os.environ.get(\"AZURE_OPENAI_AD_TOKEN\") | L539: api_key = os.environ.get(\"AZURE_OPENAI_API_KEY\") | L542: azure_ad_token = os.environ.get(\"AZURE_OPENAI_AD_TOKEN\")\nNetwork: L38: _HttpxClientT = TypeVar(\"_HttpxClientT\", bound=Union[httpx2.Client, httpx2.AsyncClient]) | L101: class AzureOpenAI(BaseAzureClient[httpx2.Client, Stream[Any]], OpenAI): | L120: http_client: httpx2.Client | None = None, | L142: http_client: httpx2.Client | None = None, | L164: http_client: httpx2.Client | None = None, | L190: http_client: httpx2.Client | None = None, | L298: http_client: httpx2.Client | None = None, | L422: class AsyncAzureOpenAI(BaseAzureClient[httpx2.AsyncClient, AsyncStream[Any]], AsyncOpenAI): | L442: http_client: httpx2.AsyncClient | None = None, | L465: http_client: httpx2.AsyncClient | None = None, | L488: http_client: httpx2.AsyncClient | None = None, | L514: http_client: httpx2.AsyncClient | None = None, | L622: http_client: httpx2.AsyncClient | None = None,",
"evidence_hash": "a2048fafc6303825095270dc766eb7630cdf9b0fb2e6eb0272695dbe9cd185c1",
"file_sha256": "b031fc8ab1d1d7865cd7f09e07a8cb08ea15b9269c710df62b1bb505d0920ed2"
},
{
"package": "openai",
"file": "openai/lib/bedrock.py",
"check": "Harvests environment variables/secrets AND makes network calls",
"severity": "CRITICAL",
"evidence": "Env: L105: token = os.environ.get(\"AWS_BEARER_TOKEN_BEDROCK\") | L150: environment_token = os.environ.get(\"AWS_BEARER_TOKEN_BEDROCK\")\nNetwork: L415: http_client: httpx2.Client | None = None, | L531: http_client: httpx2.Client | None = None, | L649: http_client: httpx2.AsyncClient | None = None, | L767: http_client: httpx2.AsyncClient | None = None,",
"evidence_hash": "ed7177e905325f49fc00b6329c7b4c18436af60fa52a559251a3a54618715873",
"file_sha256": "9d38c8b3d410100ea7d63d17eac2e92d71a2916305586c244a06111e99da7704"
}
]
}
58 changes: 58 additions & 0 deletions tests/security/test_scan_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,64 @@ def test_baseline_key_line_shift_stable_but_code_specific():
assert sp._finding_key(base) != sp._finding_key(malicious)


def test_annotation_only_network_entries_are_digest_pinned():
"""A baselined finding whose network evidence is only type annotations must pin the file.

RE_NETWORK matches ``httpx2.Client`` where it appears in a signature, but not a call
through an instance, so ``client.post(..., data=api_key)`` appended to one of these
files contributes no evidence: the evidence hash is unchanged and the entry would go
on suppressing it. Pinning the file digest is what makes any edit reopen the finding,
which is the property _load_baseline documents for exactly this shape.
"""
import json
import pathlib

baseline = json.loads(
(pathlib.Path(__file__).resolve().parents[2] / "scripts" / "scan_packages_baseline.json")
.read_text(encoding = "utf-8")
)
credential_adjacent = {
"openai/_client.py",
"openai/lib/azure.py",
"openai/lib/bedrock.py",
"openai/auth/_workload.py",
}
seen = set()
for entry in baseline["entries"]:
if entry.get("package") == "openai" and entry.get("file") in credential_adjacent:
seen.add(entry["file"])
assert entry.get("file_sha256"), (
f"{entry['file']} is baselined on evidence that a later payload can leave "
f"unchanged; it has to pin the reviewed file digest"
)
assert seen == credential_adjacent, f"missing entries for {credential_adjacent - seen}"


def test_network_check_sees_httpx2():
"""httpx2 is a separate import name, not a submodule of httpx.

openai 3.0.0 requires httpx2 and routes every call through it. While the network
check matched only ``httpx.``, the SDK's own HTTP was invisible to each combined
check that needs a network half, so reading OPENAI_API_KEY next to an httpx2 call
did not register as secrets-plus-network at all.
"""
for call in ("httpx2.get(u)", "httpx2.post(u)", "httpx2.Client()", "httpx2.AsyncClient()"):
assert sp.RE_NETWORK.search(call), call
# the original spelling still matches
for call in ("httpx.get(u)", "httpx.Client()"):
assert sp.RE_NETWORK.search(call), call
# and the widening stays anchored: no bare prefix or unrelated attribute
for miss in ("myhttpx.get(u)", "httpx23.get(u)", "httpx2.Timeout(5)"):
assert not sp.RE_NETWORK.search(miss), miss


def test_httpx2_secrets_plus_network_is_one_finding():
"""The combined check has to fire on a file that reads a secret and calls httpx2."""
src = 'import os, httpx2\nk = os.environ.get("OPENAI_API_KEY")\nhttpx2.Client().get(u)\n'
assert sp.RE_NETWORK.search(src)
assert sp.RE_ENV_HARVEST.search(src)


def test_extract_evidence_records_all_matches():
# The whole point of P1: a match appended after the first few must show up
# in the evidence, so it changes the key instead of riding the earlier ones.
Expand Down
Loading