From 40bdba0f2aa132f47326d2591e83039249ae2121 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 31 Oct 2026 19:22:11 +0100 Subject: 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. --- tests/tasks/test_sender.py | 101 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) (limited to 'tests') 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: -- cgit v1.3.1