fix(security): harden _get() against SSRF via redirects and unbounded responses#34
Open
DennisTraub wants to merge 1 commit intostrands-agents:mainfrom
Open
fix(security): harden _get() against SSRF via redirects and unbounded responses#34DennisTraub wants to merge 1 commit intostrands-agents:mainfrom
DennisTraub wants to merge 1 commit intostrands-agents:mainfrom
Conversation
…d response size cap - Add _validate_fetch_url(): HTTPS-only, reject userinfo URLs, require hostname - Add _SameHostRedirectHandler: block cross-host and scheme-downgrade redirects - Validate redirect targets with same checks as initial requests - Add response size cap via bounded read (max_response_bytes + 1) - Add max_response_bytes config (2 MB default) - Add 13 tests covering redirect policy, size cap, and URL validation
61bd4a6 to
976aa8b
Compare
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.
Summary
_get()follows redirects without restriction and reads responses without a size limit. A malicious or compromised URL in anllms.txtfile could exploit this to redirect requests to unintended hosts or stream unbounded data into memory.This PR hardens
_get()with three measures:read()is bounded tomax_response_bytes + 1(default 2 MB), preventing memory exhaustion from oversized responsesAll changes use stdlib only. No new dependencies.
Changes
config.py— Addmax_response_bytesfield (default 2 MB)doc_fetcher.py— Add_validate_fetch_url(),_SameHostRedirectHandler, bounded read in_get()test_doc_fetcher.py— 13 unit tests covering redirect policy, size cap, and URL validationTest plan
pytest tests/test_doc_fetcher.py— 13/13 passingpytest tests_integ/— 9/9 integration tests passing against live strandsagents.comManual test protocol
search_docs(query="how to create an agent", k=3)fetch_doc(uri=<url from test 1>)- TOC modefetch_doc(uri=<large doc>, section="1")- Section modefetch_doc(uri="")- Empty URIfetch_doc(uri="http://169.254.169.254/latest/meta-data/")- SSRFfetch_doc(uri="https://strandsagents.com/docs/user-guide/concepts/agents/hooks/index.md")- Legitimate fetch