aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/tasks/sender.py
diff options
context:
space:
mode:
authorDennis Fink2026-10-30 20:31:43 +0100
committerDennis Fink2026-10-30 20:31:43 +0100
commit13a9d9ddce09230017699bc34ac3bf08ed2f84ed (patch)
tree3715bd13439a61a8cc6896581811a30b62ac1b16 /webmentions_ssg/tasks/sender.py
parent8c561e702ed15022b17eae81e4185448c021e1cb (diff)
downloadwebmentions-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 'webmentions_ssg/tasks/sender.py')
-rw-r--r--webmentions_ssg/tasks/sender.py28
1 files changed, 8 insertions, 20 deletions
diff --git a/webmentions_ssg/tasks/sender.py b/webmentions_ssg/tasks/sender.py
index 725b5c8..7121f6a 100644
--- a/webmentions_ssg/tasks/sender.py
+++ b/webmentions_ssg/tasks/sender.py
@@ -19,7 +19,8 @@ from .. import APP_NAME, VERSION
from .. import DATABASE as db
from .. import HUEY as huey
from ..models import SentWebmention, SentWebmentionStatus
-from ..url_security import AddressResolutionError, is_http_url, is_public_url
+from ..public_transport import PublicHTTPTransport
+from ..url_security import AddressResolutionError, UnsafeAddressError, is_http_url
LINK_SPLIT = re.compile(r",\s*(?=<)")
@@ -48,21 +49,6 @@ def temporary_http_status(status: int) -> bool:
return status in {408, 425, 429} or 500 <= status <= 599
-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 PermanentSenderError: If the request resolves to a non-public address.
- :raises TemporarySenderError: If the request hostname cannot be resolved.
- """
- try:
- if not is_public_url(str(request.url)):
- raise PermanentSenderError("Request resolves to a non-public address")
- except AddressResolutionError as exc:
- raise TemporarySenderError("Request hostname could not be resolved") from exc
-
-
def resolve_endpoint(response: httpx.Response, href: str) -> str:
"""
Resolve and validate a discovered Webmention endpoint.
@@ -224,7 +210,7 @@ def discover_webmention_endpoint(client: httpx.Client, target: str) -> str | Non
"""
try:
head_response = client.head(target, headers=DISCOVERY_HEADERS)
- except httpx.RequestError, TemporarySenderError:
+ except (httpx.RequestError, TemporarySenderError):
# HEAD is only an optimization. Servers and intermediaries may reject
# or drop it even when fetching the document with GET would succeed.
pass
@@ -442,7 +428,9 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None:
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:
endpoint = discover_webmention_endpoint(client, target)
@@ -472,7 +460,7 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None:
raise PermanentSenderError(message)
- except (TemporarySenderError, httpx.RequestError) as exc:
+ except (TemporarySenderError, AddressResolutionError, httpx.RequestError) as exc:
if not attempt_is_current(webmention, revision):
return
@@ -495,7 +483,7 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None:
# No Huey retries: only the scanner may requeue after next_retry_at.
return
- except PermanentSenderError as exc:
+ except (PermanentSenderError, UnsafeAddressError) as exc:
if not attempt_is_current(webmention, revision):
return