-
Notifications
You must be signed in to change notification settings - Fork 43
Dev #84
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
Merged
Dev #84
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b16e43
fix(http): block SSRF via attacker-controlled OpenAPI servers[0].url …
h3xxit de2c3a8
utcp-http 1.1.2
h3xxit e356ea7
fix(openapi): block remote specs from declaring loopback servers (#83)
h3xxit 3a50250
utcp-http 1.1.3
h3xxit f873ed6
docs(openapi): correct converter comment about runtime SSRF coverage
h3xxit 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
112 changes: 112 additions & 0 deletions
112
plugins/communication_protocols/http/src/utcp_http/_security.py
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,112 @@ | ||
| """URL validation shared by every HTTP-based communication protocol. | ||
|
|
||
| Centralised so all three HTTP protocols (http, streamable_http, sse) enforce | ||
| the same trust boundary at every network edge — manual discovery AND tool | ||
| invocation. Issue #83 (CVE-class SSRF) was caused by the runtime invocation | ||
| path forgetting the discovery-time check, so this module also provides an | ||
| explicit ``ensure_secure_url`` to call before every aiohttp request. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from ipaddress import ip_address | ||
| from typing import Optional | ||
| from urllib.parse import urlparse | ||
|
|
||
| # Hostnames considered safe to talk to over plain HTTP. | ||
| _LOOPBACK_HOSTNAMES = frozenset({"localhost", "127.0.0.1", "::1", "[::1]"}) | ||
|
|
||
|
|
||
| def is_secure_url(url: str) -> bool: | ||
| """Return True if ``url`` is safe to fetch from a UTCP HTTP protocol. | ||
|
|
||
| Allowed: | ||
| - Any ``https://`` URL. | ||
| - ``http://`` URLs whose host is exactly ``localhost``, ``127.0.0.1``, | ||
| or ``::1``. | ||
|
|
||
| Disallowed: | ||
| - Plain ``http://`` to any other host (MITM exposure). | ||
| - URLs whose hostname *starts* with ``localhost`` / ``127.0.0.1`` but | ||
| isn't actually loopback (e.g. ``http://localhost.evil.com``, | ||
| ``http://127.0.0.1.attacker.example``). The earlier ``startswith`` | ||
| check let these through. | ||
| - Anything without a scheme/host (file://, gopher://, javascript:, ...). | ||
| """ | ||
| if not isinstance(url, str) or not url: | ||
| return False | ||
|
|
||
| try: | ||
| parsed = urlparse(url) | ||
| except ValueError: | ||
| return False | ||
|
|
||
| scheme = (parsed.scheme or "").lower() | ||
| if scheme not in {"http", "https"}: | ||
| return False | ||
|
|
||
| host = (parsed.hostname or "").lower() | ||
| if not host: | ||
| return False | ||
|
|
||
| if scheme == "https": | ||
| return True | ||
|
|
||
| # http:// is only allowed for loopback. | ||
| if host in _LOOPBACK_HOSTNAMES: | ||
| return True | ||
|
|
||
| # Catch any other literal loopback IP that urlparse normalised | ||
| # (e.g. ``http://127.000.000.001``). | ||
| try: | ||
| return ip_address(host).is_loopback | ||
| except ValueError: | ||
| return False | ||
|
|
||
|
|
||
| def is_loopback_url(url: str) -> bool: | ||
| """Return True if ``url``'s host is a literal loopback address. | ||
|
|
||
| Used by the OpenAPI converter to detect the SSRF case where a remote spec | ||
| declares ``servers: [{ url: "http://127.0.0.1:..." }]`` to redirect tool | ||
| invocation at the host running the agent. Hostname-based — not a string | ||
| prefix — so ``http://localhost.evil.com`` returns False. | ||
| """ | ||
| if not isinstance(url, str) or not url: | ||
| return False | ||
|
|
||
| try: | ||
| parsed = urlparse(url) | ||
| except ValueError: | ||
| return False | ||
|
|
||
| host = (parsed.hostname or "").lower() | ||
| if not host: | ||
| return False | ||
|
|
||
| if host in _LOOPBACK_HOSTNAMES: | ||
| return True | ||
|
|
||
| try: | ||
| return ip_address(host).is_loopback | ||
| except ValueError: | ||
| return False | ||
|
|
||
|
|
||
| def ensure_secure_url(url: str, *, context: Optional[str] = None) -> None: | ||
| """Raise ``ValueError`` if ``url`` is not safe to fetch. | ||
|
|
||
| ``context`` is a short label (``"manual discovery"``, ``"tool invocation"``, | ||
| etc.) included in the error so log readers can tell which trust boundary | ||
| was breached. | ||
| """ | ||
| if is_secure_url(url): | ||
| return | ||
|
|
||
| where = f" during {context}" if context else "" | ||
| raise ValueError( | ||
| f"Security error{where}: URL must use HTTPS or be a literal loopback " | ||
| f"address (localhost / 127.0.0.1 / ::1). Got: {url!r}. " | ||
| "Plain HTTP to any other host is rejected to prevent MITM attacks " | ||
| "and SSRF into internal services." | ||
| ) |
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
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
Oops, something went wrong.
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.
P1: The SSRF check allows loopback addresses (
http://127.0.0.1:*), which contradicts the threat model documented in its own comment. An attacker-controlled OpenAPI spec fetched over a legitimate HTTPS endpoint can setservers[0].urltohttp://127.0.0.1:9200(or any other local service), andensure_secure_urlwill pass it through because127.0.0.1is in the loopback allowlist.Consider using a stricter variant for the tool-invocation context that only allows HTTPS (no loopback HTTP), or at least remove the misleading comment about blocking
http://127.0.0.1:9200.Prompt for AI agents