diff options
| author | Dennis Fink | 2026-10-30 20:31:43 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-30 20:31:43 +0100 |
| commit | 13a9d9ddce09230017699bc34ac3bf08ed2f84ed (patch) | |
| tree | 3715bd13439a61a8cc6896581811a30b62ac1b16 /webmentions_ssg/tasks/receiver.py | |
| parent | 8c561e702ed15022b17eae81e4185448c021e1cb (diff) | |
| download | webmentions-ssg-13a9d9ddce09230017699bc34ac3bf08ed2f84ed.tar.gz webmentions-ssg-13a9d9ddce09230017699bc34ac3bf08ed2f84ed.zip | |
fix(core): prevent DNS rebinding in HTTP requests
Pin outbound connections to validated public IP addresses so DNS changes
cannot redirect receiver or sender requests onto private or link-local
networks.
Apply the same protection across redirects, add an overall request
deadline, and keep DNS resolution out of synchronous endpoint form
validation.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/tasks/receiver.py | 34 |
1 files changed, 14 insertions, 20 deletions
diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py index 4e8df74..f999d1b 100644 --- a/webmentions_ssg/tasks/receiver.py +++ b/webmentions_ssg/tasks/receiver.py @@ -18,7 +18,8 @@ from .. import APP_NAME, VERSION from .. import DATABASE as db from .. import HUEY as huey from ..models import ReceivedWebmention -from ..url_security import AddressResolutionError, is_public_url +from ..public_transport import PublicHTTPTransport +from ..url_security import AddressResolutionError, UnsafeAddressError class VerificationError(Exception): @@ -124,24 +125,6 @@ def text_mentions_target(body: str, target_url: str) -> bool: return False -def ensure_public_request(request: httpx.Request) -> None: - """ - Ensure that an HTTP request targets a public network address. - - :param request: HTTP request to validate. - :raises VerificationError: If the URL has no hostname or resolves to a - non-public address. - :raises TemporaryFetchError: If the hostname cannot be resolved. - """ - try: - if not is_public_url(str(request.url)): - raise VerificationError("Source resolves to a non-public address") - except ValueError as exc: - raise VerificationError("Source URL has no hostname") from exc - except AddressResolutionError as exc: - raise TemporaryFetchError("Source hostname could not be resolved") from exc - - def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: """ Fetch a webmention source document. @@ -157,6 +140,15 @@ def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: status or exceeds the configured maximum response size. """ + try: + return _fetch_source(source_url) + except UnsafeAddressError as exc: + raise VerificationError("Source resolves to a non-public address") from exc + except AddressResolutionError as exc: + raise TemporaryFetchError("Source hostname could not be resolved") from exc + + +def _fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: with ( httpx.Client( headers={ @@ -167,7 +159,9 @@ def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: follow_redirects=True, max_redirects=current_app.config.get("MAX_REDIRECTS", 20), trust_env=False, - event_hooks={"request": [ensure_public_request]}, + transport=PublicHTTPTransport( + current_app.config.get("REQUEST_TOTAL_TIMEOUT", 30.0) + ), ) as client, client.stream("GET", source_url) as response, ): |
