fix: guard json.loads() against non-JSON HTTP responses in security_audit - #31819
Open
annguyenNous wants to merge 1 commit into
Open
fix: guard json.loads() against non-JSON HTTP responses in security_audit#31819annguyenNous wants to merge 1 commit into
annguyenNous wants to merge 1 commit into
Conversation
…udit When the OSV API returns non-JSON (HTML error pages, 502/503 gateway errors), json.loads() crashes with JSONDecodeError. The callers only catch network errors (URLError, TimeoutError, ConnectionError), so the JSON parse error propagates as an unhandled exception. Fix: wrap json.loads() in _http_post_json() and _http_get_json() with try/except that converts JSONDecodeError/UnicodeDecodeError into a RuntimeError with context (status code + response preview). Update callers to also catch RuntimeError.
Collaborator
Contributor
|
Thanks for isolating the OSV response-decoding failure path. The premise remains present on current main: Problems
Suggested changes
The member note linking #30138, #29696, and #29019 is useful overlap context. This is an automated hermes-sweeper review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the OSV API returns non-JSON (HTML error pages, 502/503 gateway errors),
json.loads()crashes withJSONDecodeError. The callers only catch network errors (URLError,TimeoutError,ConnectionError), so the JSON parse error propagates as an unhandled exception.Fix
Wrap
json.loads()in_http_post_json()and_http_get_json()withtry/exceptthat convertsJSONDecodeError/UnicodeDecodeErrorinto aRuntimeErrorwith context (status code + response preview).Update callers to also catch
RuntimeError:_osv_query_batch()line 330_fetch_one()line 405Before vs After
JSONDecodeErrorcrashRuntimeError("OSV API returned non-JSON response (502): ...")JSONDecodeErrorcrashRuntimeError("OSV API returned non-JSON response (200): b''")Tests
py_compilevalidation