aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/tasks
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/tasks/receiver.py34
-rw-r--r--webmentions_ssg/tasks/sender.py28
2 files changed, 22 insertions, 40 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,
):
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