diff options
Diffstat (limited to 'webmentions_ssg/tasks/receiver.py')
| -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, ): |
