diff options
| author | Dennis Fink | 2026-11-13 17:14:29 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-13 17:14:29 +0100 |
| commit | f801d8acef788a7ce676adc099ebcb5618db94b2 (patch) | |
| tree | 2ed7c85328a47a7abf0f06ff6ce26d19091628ba | |
| parent | 6d9cfbabafd373aaca7b9f9ed607f9aa4b39f3b9 (diff) | |
| download | webmentions-ssg-f801d8acef788a7ce676adc099ebcb5618db94b2.tar.gz webmentions-ssg-f801d8acef788a7ce676adc099ebcb5618db94b2.zip | |
fix(receiver): reopen raced submissions after cooldown
Recover a concurrently inserted Webmention through the same cooldown
path as an existing submission so stale failures can be queued for
verification.
Extract digest lookup, insertion, and reopening helpers while preserving
explicit SHA-256 collision handling.
| -rw-r--r-- | tests/views/test_endpoint.py | 54 | ||||
| -rw-r--r-- | webmentions_ssg/views/endpoint.py | 109 |
2 files changed, 105 insertions, 58 deletions
diff --git a/tests/views/test_endpoint.py b/tests/views/test_endpoint.py index 72efa47..4b81d70 100644 --- a/tests/views/test_endpoint.py +++ b/tests/views/test_endpoint.py @@ -173,8 +173,13 @@ def test_status_displays_webmention(app: Flask, client: FlaskClient) -> None: assert "Verification failed" in html +@pytest.mark.parametrize(("age_minutes", "reopened"), [(0, False), (11, True)]) def test_endpoint_recovers_from_concurrent_insert( - client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + age_minutes: int, + reopened: bool, ) -> None: mock_endpoint_form(views_module, monkeypatch, valid=True) @@ -185,6 +190,7 @@ def test_endpoint_recovers_from_concurrent_insert( target=TARGET_URL, status="failed", failure_reason="Competing request failed", + updated_at=datetime.now(UTC) - timedelta(minutes=age_minutes), ) initial_result = Mock() @@ -192,7 +198,8 @@ def test_endpoint_recovers_from_concurrent_insert( competing_result = Mock() competing_result.scalar_one_or_none.return_value = competing_webmention - execute = Mock(side_effect=[initial_result, competing_result]) + update_result = Mock(rowcount=int(reopened)) + execute = Mock(side_effect=[initial_result, competing_result, update_result]) add = Mock() rollback = Mock() commit = Mock( @@ -218,14 +225,47 @@ def test_endpoint_recovers_from_concurrent_insert( assert response.status_code == 201 assert response.headers["Location"].endswith(f"/status/{competing_identifier}") - assert execute.call_count == 2 + assert execute.call_count == 3 add.assert_called_once_with(ANY) rollback.assert_called_once_with() - assert commit.call_count == 1 + assert commit.call_count == 2 - assert competing_webmention.status == "failed" - assert competing_webmention.failure_reason == "Competing request failed" - verify_webmention.assert_not_called() + if reopened: + verify_webmention.assert_called_once_with(competing_identifier) + else: + verify_webmention.assert_not_called() + + +def test_endpoint_rejects_digest_collision( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + form = mock_endpoint_form(views_module, monkeypatch, valid=True) + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + with app.app_context(): + existing = ReceivedWebmention( + uuid=uuid.uuid7(), source=SOURCE_URL, target=TARGET_URL + ) + db.session.add(existing) + db.session.commit() + digest = existing.url_digest + + form.source.data = "https://source.example/different" + monkeypatch.setattr( + views_module, "received_url_digest", lambda source, target: digest + ) + + response = client.post("/endpoint") + + assert response.status_code == 409 + assert response.get_json() == {"error": "Webmention URL fingerprint conflict"} + verify.assert_not_called() + with app.app_context(): + assert db.session.scalar(sa.select(sa.func.count(ReceivedWebmention.uuid))) == 1 def test_endpoint_rate_limits_requests_per_ip( diff --git a/webmentions_ssg/views/endpoint.py b/webmentions_ssg/views/endpoint.py index 95e8731..a94310e 100644 --- a/webmentions_ssg/views/endpoint.py +++ b/webmentions_ssg/views/endpoint.py @@ -21,73 +21,80 @@ from ..tasks.receiver import verify_webmention from .blueprint import root_page -def submit_received(source: str, target: str) -> tuple[uuid.UUID, bool] | None: - """Upsert a submission atomically and return (identifier, enqueue flag). +class UrlDigestCollisionError(Exception): + """The URL digest already belongs to a different source/target pair.""" - ``None`` means that the URL fingerprint matches a different source/target. - No background task is enqueued until the transaction is committed. - """ - url_digest = received_url_digest(source, target) + +def find_webmention_by_digest(source: str, target: str) -> ReceivedWebmention | None: + """Return the matching Webmention, or raise on a SHA-256 collision.""" webmention = db.session.execute( - sa.select(ReceivedWebmention).where(ReceivedWebmention.url_digest == url_digest) + sa.select(ReceivedWebmention).where( + ReceivedWebmention.url_digest == received_url_digest(source, target) + ) ).scalar_one_or_none() - # A SHA-256 collision must never cause an unrelated Webmention to be overwritten. if webmention is not None and (webmention.source, webmention.target) != ( source, target, ): + raise UrlDigestCollisionError + + return webmention + + +def insert_webmention(source: str, target: str) -> ReceivedWebmention | None: + """Insert and return a Webmention, or None if another request won the race.""" + webmention = ReceivedWebmention(uuid=uuid.uuid7(), source=source, target=target) + db.session.add(webmention) + + try: + db.session.commit() + except sa_exc.IntegrityError: + db.session.rollback() return None - should_verify = False + return webmention - if webmention is None: - identifier = uuid.uuid7() - webmention = ReceivedWebmention(uuid=identifier, source=source, target=target) - db.session.add(webmention) - try: - db.session.commit() - should_verify = True - except sa_exc.IntegrityError: - db.session.rollback() +def reopen_if_cooled_down(webmention: ReceivedWebmention) -> bool: + """Atomically reopen an old Webmention and return whether it was reopened.""" + now = datetime.now(UTC) + cutoff = now - timedelta( + seconds=current_app.config["ENDPOINT_REVERIFY_COOLDOWN_SECONDS"] + ) - # Another request may have inserted the same source/target pair - # after our SELECT but before our COMMIT. - webmention = db.session.execute( - sa.select(ReceivedWebmention).where( - ReceivedWebmention.url_digest == url_digest - ) - ).scalar_one_or_none() - if webmention is None or (webmention.source, webmention.target) != ( - source, - target, - ): - return None + result = cast( + CursorResult[Any], + db.session.execute( + sa.update(ReceivedWebmention) + .where( + ReceivedWebmention.uuid == webmention.uuid, + ReceivedWebmention.updated_at <= cutoff, + ) + .values(ReceivedWebmention.verification_reset_values(at=now)) + .execution_options(synchronize_session=False) + ), + ) + db.session.commit() + return result.rowcount == 1 - else: - now = datetime.now(UTC) - cutoff = now - timedelta( - seconds=current_app.config["ENDPOINT_REVERIFY_COOLDOWN_SECONDS"] - ) - # Atomic conditional update: only one concurrent request can reopen an expired verification. - result = cast( - CursorResult[Any], - db.session.execute( - sa.update(ReceivedWebmention) - .where( - ReceivedWebmention.uuid == webmention.uuid, - ReceivedWebmention.updated_at <= cutoff, - ) - .values(ReceivedWebmention.verification_reset_values(at=now)) - .execution_options(synchronize_session=False) - ), - ) - should_verify = result.rowcount == 1 - db.session.commit() +def submit_received(source: str, target: str) -> tuple[uuid.UUID, bool] | None: + """Return (identifier, enqueue flag), or None on a digest conflict. + + A new submission is committed before it can be queued for verification. + """ + try: + if (webmention := find_webmention_by_digest(source, target)) is None: + if (webmention := insert_webmention(source, target)) is not None: + return webmention.uuid, True - return webmention.uuid, should_verify + # A concurrent insert won: look up its row after rolling back. + if (webmention := find_webmention_by_digest(source, target)) is None: + return None + return webmention.uuid, reopen_if_cooled_down(webmention) + except UrlDigestCollisionError: + return None @root_page.post("/endpoint") |
