From bcb5a2ca6c135e13a0798baf5a05485dd296ab82 Mon Sep 17 00:00:00 2001 From: AlexFucuson9 Date: Sun, 28 Jun 2026 18:48:54 +0700 Subject: [PATCH] fix(relay,osv): guard json.loads against JSONDecodeError gateway/relay/descriptor.py: CapabilityDescriptor.from_json() calls json.loads() without catching JSONDecodeError, and raw.items() crashes with AttributeError if the JSON is not a dict (e.g. a list or string). Now raises ValueError with clear message on invalid input. tools/osv_check.py: _query_osv_batch() calls json.loads() on HTTP response without catching JSONDecodeError. If the OSV API returns a non-JSON response (rate limit HTML, 502 gateway error), the entire vulnerability check crashes. Now logs warning and returns empty list. --- gateway/relay/descriptor.py | 7 ++++++- tools/osv_check.py | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gateway/relay/descriptor.py b/gateway/relay/descriptor.py index 2a8d0fe78f344..c3af70e7e921d 100644 --- a/gateway/relay/descriptor.py +++ b/gateway/relay/descriptor.py @@ -71,7 +71,12 @@ def from_json(cls, data: str) -> "CapabilityDescriptor": fields this gateway does not know yet); missing optional keys fall back to dataclass defaults. """ - raw = json.loads(data) + try: + raw = json.loads(data) + except (json.JSONDecodeError, TypeError) as exc: + raise ValueError(f"Invalid handshake JSON: {exc}") from exc + if not isinstance(raw, dict): + raise ValueError(f"Expected JSON object, got {type(raw).__name__}") known = {f for f in cls.__dataclass_fields__} # type: ignore[attr-defined] filtered = {k: v for k, v in raw.items() if k in known} return cls(**filtered) diff --git a/tools/osv_check.py b/tools/osv_check.py index 1f8a986e11b69..fbef0c86fa263 100644 --- a/tools/osv_check.py +++ b/tools/osv_check.py @@ -162,7 +162,12 @@ def _query_osv( ) with urllib.request.urlopen(req, timeout=_TIMEOUT) as resp: - result = json.loads(resp.read()) + raw = resp.read() + try: + result = json.loads(raw) + except (json.JSONDecodeError, UnicodeDecodeError): + logger.warning("Non-JSON response from OSV API") + return [] vulns = result.get("vulns", []) # Only malware advisories — ignore regular CVEs