aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/views
diff options
context:
space:
mode:
authorDennis Fink2026-11-13 17:14:29 +0100
committerDennis Fink2026-11-13 17:14:29 +0100
commitf801d8acef788a7ce676adc099ebcb5618db94b2 (patch)
tree2ed7c85328a47a7abf0f06ff6ce26d19091628ba /webmentions_ssg/views
parent6d9cfbabafd373aaca7b9f9ed607f9aa4b39f3b9 (diff)
downloadwebmentions-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.
Diffstat (limited to 'webmentions_ssg/views')
-rw-r--r--webmentions_ssg/views/endpoint.py109
1 files changed, 58 insertions, 51 deletions
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")