Repository navigation
fix(http tools): stop reading an upstream body at the tool's byte cap - #95
Merged
Merged
Conversation
The response was fetched whole and only then compared with max_response_bytes, so a hostile or broken upstream's huge body sat in ToolGate's memory first (toolgate-1 F3). The body is now streamed and the read stops as soon as it passes the cap. Two tests faked client.request and now fake client.stream.
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.
Hunt finding toolgate-1 F3 (re-verified on main, dispatch.py:113):
client.request(...)read the whole body and only then comparedlen(response.content)withmax_response_bytes(at most 1 MiB). A hostile or broken upstream could make ToolGate hold any amount in memory first.The body is now read with
client.streamanditer_bytes, and reading stops as soon as it passes the cap: RESPONSE_TOO_LARGE, which stays OUTCOME_UNKNOWN because the request was sent. Parsing usesjson.loadson the bytes; ValueError still covers bad JSON and bad encoding.Tests:
toolgate/tests/test_http_response_cap.py. A 25 MiB body is cut off after at most 6 x 64 KiB chunks; this fails on main, which pulls all 400 chunks. A small chunked body is parsed. Two existing fakes (test_tool_secret_refs.py,test_tool_settings.py) moved fromrequesttostream. Every test file using HTTP tools orpublic_client: 278 passed. Ruff and the type baseline are ok.