From f801d8acef788a7ce676adc099ebcb5618db94b2 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 13 Nov 2026 17:14:29 +0100 Subject: 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. --- webmentions_ssg/views/endpoint.py | 113 ++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 53 deletions(-) (limited to 'webmentions_ssg/views') 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, ): - return None + raise UrlDigestCollisionError - 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() - - # 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 - else: - now = datetime.now(UTC) - cutoff = now - timedelta( - seconds=current_app.config["ENDPOINT_REVERIFY_COOLDOWN_SECONDS"] - ) +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) - # 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 + try: db.session.commit() + except sa_exc.IntegrityError: + db.session.rollback() + return None + + return webmention + - return webmention.uuid, should_verify +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"] + ) + + 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 + + +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 + + # 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") -- cgit v1.3.1