-
Notifications
You must be signed in to change notification settings - Fork 44
Security updates #87
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
h3xxit
wants to merge
1
commit into
main
Choose a base branch
from
dev
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
Security updates #87
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
219 changes: 219 additions & 0 deletions
219
plugins/communication_protocols/gql/src/utcp_gql/_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,219 @@ | ||
| """URL validation for the GraphQL communication protocol. | ||
|
|
||
| Mirror of ``utcp_http._security`` -- intentionally duplicated rather | ||
| than cross-plugin-imported so ``utcp-gql`` does not gain a runtime | ||
| dependency on ``utcp-http``. Keep the two files in sync when changing | ||
| the validator behavior. Backs GHSA-ppx3-28rw-8fpf (the original CVE | ||
| fix did not reach this plugin) and GHSA-9qhg-99ww-9mqc (redirect | ||
| SSRF on the GraphQL endpoint). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from contextlib import asynccontextmanager | ||
| from ipaddress import ip_address | ||
| from typing import Any, AsyncIterator, Optional | ||
| from urllib.parse import urljoin, 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." | ||
| ) | ||
|
|
||
|
|
||
| # HTTP statuses where the server expects the client to re-issue the request | ||
| # against the URL given in the ``Location`` header. 303 forces a GET; the | ||
| # rest preserve the original method. | ||
| _REDIRECT_STATUSES = frozenset({301, 302, 303, 307, 308}) | ||
|
|
||
|
|
||
| @asynccontextmanager | ||
| async def safe_request_with_redirects( | ||
| session: Any, | ||
| method: str, | ||
| url: str, | ||
| *, | ||
| context: str, | ||
| max_redirects: int = 5, | ||
| **kwargs: Any, | ||
| ) -> AsyncIterator[Any]: | ||
| """Issue an aiohttp request that re-validates every redirect hop. | ||
|
|
||
| Closes the residual SSRF window left by ``ensure_secure_url`` (which | ||
| only inspects the initial URL): aiohttp by default follows 3xx | ||
| redirects without rechecking, so an attacker-controlled server could | ||
| 302 the client into ``http://169.254.169.254/...`` (cloud metadata) | ||
| or any internal HTTP service and the response body would be handed | ||
| back to the caller. Backs GHSA-9qhg-99ww-9mqc. | ||
|
|
||
| Behavior: | ||
| * Calls ``ensure_secure_url(url, context=context)`` on the initial | ||
| URL. | ||
| * Disables aiohttp's auto-follow (``allow_redirects=False``). | ||
| * On a 3xx response with a ``Location`` header, resolves the | ||
| target against the current URL and runs ``ensure_secure_url`` | ||
| on it before issuing the next hop. Rejection raises and the | ||
| redirect chain is aborted with the connection released. | ||
| * Caps the chain at ``max_redirects`` hops. Exceeding that raises | ||
| ``RuntimeError``. | ||
| * Mirrors RFC 7231 method semantics: 303 forces ``GET`` and drops | ||
| any request body; 301/302/307/308 preserve method and body. | ||
|
|
||
| Usage: | ||
| ```python | ||
| async with safe_request_with_redirects( | ||
| session, "GET", url, context="tool invocation", params=... | ||
| ) as response: | ||
| response.raise_for_status() | ||
| ... | ||
| ``` | ||
| """ | ||
| ensure_secure_url(url, context=context) | ||
| # We control redirect behavior ourselves; refuse to let callers override. | ||
| kwargs.pop("allow_redirects", None) | ||
|
|
||
| current_url = url | ||
| current_method = method | ||
| hops = 0 | ||
| final_response = None | ||
|
|
||
| try: | ||
| while True: | ||
| response = await session.request( | ||
| current_method, | ||
| current_url, | ||
| allow_redirects=False, | ||
| **kwargs, | ||
| ) | ||
| if response.status not in _REDIRECT_STATUSES: | ||
| final_response = response | ||
| break | ||
|
|
||
| location = response.headers.get("Location") | ||
| if not location: | ||
| # 3xx with no Location header — nothing to follow. Let | ||
| # the caller handle the unusual response. | ||
| final_response = response | ||
| break | ||
|
|
||
| if hops >= max_redirects: | ||
| response.release() | ||
| raise RuntimeError( | ||
| f"Too many redirects (>{max_redirects}) during {context} " | ||
| f"starting from {url!r}." | ||
| ) | ||
|
|
||
| next_url = urljoin(current_url, location) | ||
| try: | ||
| ensure_secure_url( | ||
| next_url, context=f"{context} (redirect target)" | ||
| ) | ||
| except Exception: | ||
| response.release() | ||
| raise | ||
|
|
||
| response.release() | ||
| if response.status == 303: | ||
| current_method = "GET" | ||
| kwargs.pop("json", None) | ||
| kwargs.pop("data", None) | ||
| current_url = next_url | ||
| hops += 1 | ||
|
|
||
| yield final_response | ||
| finally: | ||
| if final_response is not None: | ||
| final_response.release() |
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: Redirect protection is applied too late: it runs after entering the GqlClient context, but schema fetch can already happen during
__aenter__. This leaves an SSRF/credential-leak path on the initial GraphQL request.Prompt for AI agents