aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/models.py
diff options
context:
space:
mode:
authorDennis Fink2026-10-31 17:42:00 +0100
committerDennis Fink2026-10-31 17:42:00 +0100
commit6924ad7e52033365c55d5afacdd916c502cc80cb (patch)
tree8a52b8e8fbae7a6bcc85d250cac2436a2c42e748 /webmentions_ssg/models.py
parent3494eab1208dc18478485e02c61e292556502fa6 (diff)
downloadwebmentions-ssg-6924ad7e52033365c55d5afacdd916c502cc80cb.tar.gz
webmentions-ssg-6924ad7e52033365c55d5afacdd916c502cc80cb.zip
feat(receiver): add resource limits and retention
Bound incoming request and URL sizes so untrusted Webmention submissions cannot consume unbounded application or database resources. Purge old failed and deleted received Webmentions on a schedule, and use portable SHA-256 URL fingerprints to preserve uniqueness for long URLs.
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/models.py22
1 files changed, 19 insertions, 3 deletions
diff --git a/webmentions_ssg/models.py b/webmentions_ssg/models.py
index 7cab879..0dfbb78 100644
--- a/webmentions_ssg/models.py
+++ b/webmentions_ssg/models.py
@@ -25,10 +25,11 @@ from sqlalchemy import (
UniqueConstraint,
Uuid,
)
-from sqlalchemy.orm import Mapped, mapped_column, relationship
+from sqlalchemy.orm import Mapped, mapped_column, relationship, validates
from werkzeug.security import check_password_hash, generate_password_hash
from . import Base
+from .url_digest import received_url_digest, sent_target_digest
class SentWebmentionStatus(StrEnum):
@@ -109,6 +110,15 @@ class ReceivedWebmention(Base):
source: Mapped[str] = mapped_column(Text(), nullable=False)
target: Mapped[str] = mapped_column(Text(), nullable=False)
+ url_digest: Mapped[str] = mapped_column(String(64), nullable=False)
+
+ @validates("source", "target")
+ def _update_url_digest(self, key: str, value: str) -> str:
+ source = value if key == "source" else self.__dict__.get("source")
+ target = value if key == "target" else self.__dict__.get("target")
+ if source is not None and target is not None:
+ self.url_digest = received_url_digest(source, target)
+ return value
status: Mapped[str] = mapped_column(Text(), nullable=False, default="received")
failure_reason: Mapped[str | None] = mapped_column(
@@ -127,7 +137,7 @@ class ReceivedWebmention(Base):
)
__table_args__ = (
- UniqueConstraint("source", "target", name="uq_webmention_source_target"),
+ UniqueConstraint("url_digest", name="uq_webmention_url_digest"),
Index("ix_received_webmentions_status_updated_at", "status", "updated_at"),
)
@@ -226,6 +236,12 @@ class SentWebmention(Base):
)
target: Mapped[str] = mapped_column(Text(), nullable=False)
+ target_digest: Mapped[str] = mapped_column(String(64), nullable=False)
+
+ @validates("target")
+ def _update_target_digest(self, key: str, value: str) -> str:
+ self.target_digest = sent_target_digest(value)
+ return value
active: Mapped[bool] = mapped_column(Boolean(), nullable=False, default=True)
@@ -275,7 +291,7 @@ class SentWebmention(Base):
__table_args__ = (
UniqueConstraint(
- "source_id", "target", name="uq_sent_webmention_source_target"
+ "source_id", "target_digest", name="uq_sent_webmention_source_target_digest"
),
)