-
Notifications
You must be signed in to change notification settings - Fork 52.5k
fix(providers): bound the /models catalog response size in fetch_models #42930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
inancweb3
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
inancweb3:fix/fetch-models-size-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """Regression tests for ProviderProfile.fetch_models response-size bounding. | ||
|
|
||
| fetch_models() probes a provider's /models endpoint. The endpoint URL comes | ||
| from operator/config (base_url/models_url, e.g. a self-hosted or community | ||
| relay), so a misconfigured or hostile catalog endpoint must not be able to | ||
| force the process to buffer an unbounded body into memory. A timeout bounds | ||
| wall-clock, not size — the response itself has to be capped. | ||
| """ | ||
|
|
||
| import json | ||
|
|
||
| import providers.base as pbase | ||
| from providers.base import ProviderProfile | ||
|
|
||
|
|
||
| class _FakeResp: | ||
| """Minimal stand-in for an http.client.HTTPResponse context manager.""" | ||
|
|
||
| def __init__(self, body: bytes, content_length=None): | ||
| self._body = body | ||
| self.headers = {} | ||
| if content_length is not None: | ||
| self.headers["Content-Length"] = str(content_length) | ||
|
|
||
| def read(self, n=-1): | ||
| if n is None or n < 0: | ||
| return self._body | ||
| return self._body[:n] | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, *exc): | ||
| return False | ||
|
|
||
|
|
||
| def _patch_urlopen(monkeypatch, resp): | ||
| monkeypatch.setattr( | ||
| "urllib.request.urlopen", lambda req, timeout=None: resp | ||
| ) | ||
|
|
||
|
|
||
| def _profile(): | ||
| return ProviderProfile(name="test", base_url="https://api.example.test/v1") | ||
|
|
||
|
|
||
| def test_small_response_returns_model_ids(monkeypatch): | ||
| body = json.dumps({"data": [{"id": "m1"}, {"id": "m2"}]}).encode() | ||
| _patch_urlopen(monkeypatch, _FakeResp(body)) | ||
| assert _profile().fetch_models() == ["m1", "m2"] | ||
|
|
||
|
|
||
| def test_oversized_body_is_rejected(monkeypatch): | ||
| # Cap to a tiny value so we don't allocate megabytes in the test. | ||
| monkeypatch.setattr(pbase, "_MAX_MODELS_RESPONSE_BYTES", 32) | ||
| body = json.dumps({"data": [{"id": "x" * 200}]}).encode() | ||
| assert len(body) > 32 | ||
| # No Content-Length header → the post-read length guard must catch it. | ||
| _patch_urlopen(monkeypatch, _FakeResp(body)) | ||
| assert _profile().fetch_models() is None | ||
|
|
||
|
|
||
| def test_oversized_content_length_header_is_rejected(monkeypatch): | ||
| monkeypatch.setattr(pbase, "_MAX_MODELS_RESPONSE_BYTES", 32) | ||
| # Small body, but the server advertises a huge Content-Length. | ||
| _patch_urlopen( | ||
| monkeypatch, _FakeResp(b'{"data": []}', content_length=10_000_000) | ||
| ) | ||
| assert _profile().fetch_models() is None |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current main opens this credential-bearing request through
open_credentialed_url, which strips secrets on cross-origin redirects. Please apply the bounded read while retaining that helper rather than switching tourllib.request.urlopen; otherwise this stale patch regresses the redirect-security boundary.