diff options
| author | Dennis Fink | 2026-10-31 19:22:11 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-31 19:22:11 +0100 |
| commit | 40bdba0f2aa132f47326d2591e83039249ae2121 (patch) | |
| tree | db45887ea64777ecd1e5e970333c87730bc7b531 | |
| parent | 3d6ee99142fbcc7fabaeab770a9c9e9d090ddf0f (diff) | |
| download | webmentions-ssg-40bdba0f2aa132f47326d2591e83039249ae2121.tar.gz webmentions-ssg-40bdba0f2aa132f47326d2591e83039249ae2121.zip | |
fix(sender): treat malformed requests as terminal failures
Reject invalid stored targets and local URL or protocol errors as
permanent failures so unrecoverable requests do not consume the sender
retry budget.
Preserve the recorded attempt state while preventing duplicate retries
for the same source revision.
Diffstat (limited to '')
| -rw-r--r-- | tests/tasks/test_sender.py | 101 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/sender.py | 42 |
2 files changed, 129 insertions, 14 deletions
diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 38bae11..7ebe083 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -1052,6 +1052,107 @@ def test_sender_request_error_uses_same_retry_budget( assert webmention.next_retry_at is None +@pytest.mark.parametrize( + ("failure", "reason"), + [ + pytest.param( + httpx.InvalidURL("Invalid IDNA hostname"), + "Invalid IDNA hostname", + id="invalid-url", + ), + pytest.param( + httpx.UnsupportedProtocol("Unsupported URL scheme"), + "Unsupported URL scheme", + id="unsupported-protocol", + ), + pytest.param( + httpx.LocalProtocolError("Invalid HTTP Host header"), + "Invalid HTTP Host header", + id="local-protocol-error", + ), + pytest.param(ValueError("Invalid port"), "Invalid port", id="value-error"), + pytest.param( + UnicodeError("Invalid URL encoding"), + "Invalid URL encoding", + id="unicode-error", + ), + pytest.param(httpx.InvalidURL(""), "InvalidURL", id="no-message"), + ], +) +@pytest.mark.parametrize("stage", ["discovery", "post"]) +def test_sender_malformed_request_is_terminal( + app: Flask, + sender_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + failure: Exception, + reason: str, + stage: str, +) -> None: + app.config["SEND_MAX_ATTEMPTS"] = 3 + identifier = create_sent_webmention(app) + httpx_client, _, discover, post = mock_sender_requests(sender_module, monkeypatch) + if stage == "discovery": + discover.side_effect = failure + else: + post.side_effect = failure + + sender_module.send_webmention.call_local(identifier) + sender_module.send_webmention.call_local(identifier) + + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert webmention.status == SentWebmentionStatus.FAILED + assert webmention.failure_reason == reason + assert webmention.processed_revision == 1 + assert webmention.attempt_count == 1 + assert webmention.next_retry_at is None + assert webmention.endpoint == (ENDPOINT_URL if stage == "post" else None) + assert webmention.response_status is None + assert webmention.last_attempted_at is not None + + assert httpx_client.call_count == 1 + assert discover.call_count == 1 + assert post.call_count == (1 if stage == "post" else 0) + + +@pytest.mark.parametrize( + ("target", "reason"), + [ + pytest.param( + "https://ex☃mple.test/", "Invalid IDNA hostname", id="invalid-idna" + ), + pytest.param("https://example.com:abc/post", "Invalid port", id="invalid-port"), + pytest.param( + "ftp://example.com/post", "Invalid target URL", id="non-http-scheme" + ), + pytest.param( + "http://[invalid/post", "Invalid target URL", id="invalid-ip-literal" + ), + ], +) +def test_sender_rejects_malformed_stored_target_without_retry( + app: Flask, sender_module: ModuleType, target: str, reason: str +) -> None: + identifier = create_sent_webmention(app) + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + webmention.target = target + db.session.commit() + + sender_module.send_webmention.call_local(identifier) + + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert webmention.status == SentWebmentionStatus.FAILED + assert reason in (webmention.failure_reason or "") + assert webmention.processed_revision == 1 + assert webmention.attempt_count == 1 + assert webmention.next_retry_at is None + + def test_send_webmention_skips_duplicate_while_locked( app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: diff --git a/webmentions_ssg/tasks/sender.py b/webmentions_ssg/tasks/sender.py index 4da1a5c..9330247 100644 --- a/webmentions_ssg/tasks/sender.py +++ b/webmentions_ssg/tasks/sender.py @@ -422,6 +422,9 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None: response_status: int | None = None try: + if not is_http_url(target): + raise PermanentSenderError(f"Invalid target URL: {target!r}") + with httpx.Client( headers={"User-Agent": (f"{APP_NAME}/{VERSION} SentWebmention")}, timeout=httpx.Timeout(current_app.config.get("REQUEST_TIMEOUT", 5.0)), @@ -460,6 +463,31 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None: raise PermanentSenderError(message) + except ( + PermanentSenderError, + UnsafeAddressError, + httpx.InvalidURL, + httpx.UnsupportedProtocol, + httpx.LocalProtocolError, + ValueError, + UnicodeError, + ) as exc: + # Malformed URLs and local request/protocol errors cannot be repaired + # by retrying this source revision. InvalidURL is not a RequestError; + # protocol errors are, so handle them before the retryable branch. + if not attempt_is_current(webmention, revision): + return + + webmention.processed_revision = revision + webmention.next_retry_at = None + webmention.status = SentWebmentionStatus.FAILED + webmention.failure_reason = str(exc) or type(exc).__name__ + webmention.endpoint = endpoint + webmention.response_status = response_status + + db.session.commit() + return + except (TemporarySenderError, AddressResolutionError, httpx.RequestError) as exc: if not attempt_is_current(webmention, revision): return @@ -483,20 +511,6 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None: # No Huey retries: only the scanner may requeue after next_retry_at. return - except (PermanentSenderError, UnsafeAddressError) as exc: - if not attempt_is_current(webmention, revision): - return - - webmention.processed_revision = revision - webmention.next_retry_at = None - webmention.status = SentWebmentionStatus.FAILED - webmention.failure_reason = str(exc) - webmention.endpoint = endpoint - webmention.response_status = response_status - - db.session.commit() - return - if not attempt_is_current(webmention, revision): return |
