aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pyproject.toml1
-rw-r--r--tests/test_views.py152
-rw-r--r--uv.lock105
-rw-r--r--webmentions_ssg/__init__.py13
-rw-r--r--webmentions_ssg/config.py7
-rw-r--r--webmentions_ssg/views.py53
6 files changed, 302 insertions, 29 deletions
diff --git a/pyproject.toml b/pyproject.toml
index 58e3be4..f42752a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -19,6 +19,7 @@ dependencies = [
"coloredlogs>=15.0.1",
"dnspython>=2.8.0",
"flask>=3.1.3",
+ "flask-limiter>=4.1.1",
"flask-login>=0.6.3",
"flask-mail>=0.10.0",
"flask-migrate>=4.1.0",
diff --git a/tests/test_views.py b/tests/test_views.py
index c7f86d2..54098db 100644
--- a/tests/test_views.py
+++ b/tests/test_views.py
@@ -3,9 +3,9 @@
# SPDX-License-Identifier: BSD-3-Clause
import uuid
-from datetime import UTC, datetime
+from datetime import UTC, datetime, timedelta
from types import ModuleType
-from unittest.mock import ANY, Mock, call
+from unittest.mock import ANY, Mock
import pytest
import sqlalchemy as sa
@@ -190,10 +190,7 @@ def test_endpoint_is_idempotent(
identifier = webmention.uuid
- assert verify_webmention.call_args_list == [
- call(identifier, send_notification=True),
- call(identifier, send_notification=False),
- ]
+ verify_webmention.assert_called_once_with(identifier, send_notification=True)
def test_resending_resets_failure_state(
@@ -213,6 +210,7 @@ def test_resending_resets_failure_state(
target=TARGET_URL,
status="failed",
failure_reason="Previous failure",
+ updated_at=datetime.now(UTC) - timedelta(minutes=11),
)
db.session.add(existing)
@@ -797,10 +795,140 @@ def test_endpoint_recovers_from_concurrent_insert(
assert execute.call_count == 2
add.assert_called_once_with(ANY)
rollback.assert_called_once_with()
- assert commit.call_count == 2
+ assert commit.call_count == 1
- assert competing_webmention.status == "received"
- assert competing_webmention.failure_reason is None
- verify_webmention.assert_called_once_with(
- competing_identifier, send_notification=False
- )
+ assert competing_webmention.status == "failed"
+ assert competing_webmention.failure_reason == "Competing request failed"
+ verify_webmention.assert_not_called()
+
+
+def test_endpoint_rate_limits_requests_per_ip(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ app.config["ENDPOINT_RATE_LIMIT"] = "2/minute"
+
+ form = mock_endpoint_form(views_module, monkeypatch, valid=True)
+ verify = Mock()
+ monkeypatch.setattr(views_module, "verify_webmention", verify)
+
+ responses = []
+
+ for index in range(3):
+ form.source.data = f"https://source.example/post-{index}"
+ responses.append(
+ client.post("/endpoint", environ_overrides={"REMOTE_ADDR": "192.0.2.10"})
+ )
+
+ assert [r.status_code for r in responses] == [201, 201, 429]
+
+ assert verify.call_count == 2
+
+ with app.app_context():
+ count = db.session.scalar(
+ sa.select(sa.func.count()).select_from(ReceivedWebmention)
+ )
+ assert count == 2
+
+
+def test_endpoint_global_rate_limit(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ app.config["ENDPOINT_GLOBAL_RATE_LIMIT"] = "2/minute"
+
+ form = mock_endpoint_form(views_module, monkeypatch, valid=True)
+ verify = Mock()
+ monkeypatch.setattr(views_module, "verify_webmention", verify)
+
+ responses = []
+
+ for index in range(3):
+ form.source.data = f"https://source.example/post-{index}"
+
+ responses.append(
+ client.post(
+ "/endpoint", environ_overrides={"REMOTE_ADDR": f"192.0.2.{index + 1}"}
+ )
+ )
+
+ assert [r.status_code for r in responses] == [201, 201, 429]
+ assert verify.call_count == 2
+
+
+def test_endpoint_recent_duplicate_does_not_reverify(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ mock_endpoint_form(views_module, monkeypatch, valid=True)
+
+ verify = Mock()
+ monkeypatch.setattr(views_module, "verify_webmention", verify)
+
+ with app.app_context():
+ webmention = ReceivedWebmention(
+ uuid=uuid.uuid7(),
+ source=SOURCE_URL,
+ target=TARGET_URL,
+ status="failed",
+ failure_reason="Previous failure",
+ )
+ db.session.add(webmention)
+ db.session.commit()
+ identifier = webmention.uuid
+
+ response = client.post("/endpoint")
+
+ assert response.status_code == 201
+ assert response.headers["Location"].endswith(str(identifier))
+
+ with app.app_context():
+ stored = db.session.get(ReceivedWebmention, identifier)
+ assert stored is not None
+ assert stored.status == "failed"
+ assert stored.failure_reason == "Previous failure"
+
+ verify.assert_not_called()
+
+
+def test_endpoint_reverifies_after_cooldown(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ mock_endpoint_form(views_module, monkeypatch, valid=True)
+
+ verify = Mock()
+ monkeypatch.setattr(views_module, "verify_webmention", verify)
+
+ with app.app_context():
+ webmention = ReceivedWebmention(
+ uuid=uuid.uuid7(),
+ source=SOURCE_URL,
+ target=TARGET_URL,
+ status="failed",
+ failure_reason="Previous failure",
+ updated_at=datetime.now(UTC) - timedelta(minutes=11),
+ )
+ db.session.add(webmention)
+ db.session.commit()
+ identifier = webmention.uuid
+
+ response = client.post("/endpoint")
+
+ assert response.status_code == 201
+
+ with app.app_context():
+ stored = db.session.get(ReceivedWebmention, identifier)
+ assert stored is not None
+ assert stored.status == "received"
+ assert stored.failure_reason is None
+
+ verify.assert_called_once_with(identifier, send_notification=False)
diff --git a/uv.lock b/uv.lock
index 9f0c8ba..1ddffdc 100644
--- a/uv.lock
+++ b/uv.lock
@@ -275,6 +275,18 @@ wheels = [
]
[[package]]
+name = "deprecated"
+version = "3.0.0"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "wrapt" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/f7/9c/16649913bf14c73e0a9453782e148362ff2657067deff6aa9c7ebcddcc31/deprecated-3.0.0.tar.gz", hash = "sha256:16850204d3a1e6bb0acd06bff48d96e8b0a0d25d1c52f71705405a0f4894192d", size = 166912, upload-time = "2026-09-26T13:58:10.675Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/83/ae/676feae8e4644a6d7169951a97f61c56f416c73f67bf1761f2461d75cc81/deprecated-3.0.0-py3-none-any.whl", hash = "sha256:58204cf4a7f6270d547af5c278ee7a6bb56045a4b3d8441a1cd11660f41b7939", size = 21912, upload-time = "2026-09-26T13:58:09.458Z" },
+]
+
+[[package]]
name = "dnspython"
version = "2.9.0"
source = { registry = "https://pypi.org/simple" }
@@ -301,6 +313,21 @@ wheels = [
]
[[package]]
+name = "flask-limiter"
+version = "4.1.1"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "flask" },
+ { name = "limits" },
+ { name = "ordered-set" },
+ { name = "typing-extensions" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/4e/98/71780be5d1afb941219c4b48d241d4e246e3062b017caa4e79c4dc71314c/flask_limiter-4.1.1.tar.gz", hash = "sha256:ca11608fc7eec43dcea606964ca07c3bd4ec1ae89043a0f67f717899a4f48106", size = 403198, upload-time = "2025-12-06T17:39:00.575Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/75/7c/9fe9ffc83be199011bb0c6deb82cdcbc5a355601e380581de9dbc30490dd/flask_limiter-4.1.1-py3-none-any.whl", hash = "sha256:e1ae13e06e6b3e39a4902e7d240b901586b25932c2add7bd5f5eeb4bdc11111b", size = 30554, upload-time = "2025-12-06T17:38:59.162Z" },
+]
+
+[[package]]
name = "flask-login"
version = "0.6.3"
source = { registry = "https://pypi.org/simple" }
@@ -478,6 +505,20 @@ wheels = [
]
[[package]]
+name = "limits"
+version = "5.8.0"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "deprecated" },
+ { name = "packaging" },
+ { name = "typing-extensions" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/71/69/826a5d1f45426c68d8f6539f8d275c0e4fcaa57f0c017ec3100986558a41/limits-5.8.0.tar.gz", hash = "sha256:c9e0d74aed837e8f6f50d1fcebcf5fd8130957287206bc3799adaee5092655da", size = 226104, upload-time = "2026-02-05T07:17:35.859Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/b9/98/cb5ca20618d205a09d5bec7591fbc4130369c7e6308d9a676a28ff3ab22c/limits-5.8.0-py3-none-any.whl", hash = "sha256:ae1b008a43eb43073c3c579398bd4eb4c795de60952532dc24720ab45e1ac6b8", size = 60954, upload-time = "2026-02-05T07:17:34.425Z" },
+]
+
+[[package]]
name = "mako"
version = "1.4.3"
source = { registry = "https://pypi.org/simple" }
@@ -591,6 +632,15 @@ wheels = [
]
[[package]]
+name = "ordered-set"
+version = "4.1.0"
+source = { registry = "https://pypi.org/simple" }
+sdist = { url = "https://files.pythonhosted.org/packages/4c/ca/bfac8bc689799bcca4157e0e0ced07e70ce125193fc2e166d2e685b7e2fe/ordered-set-4.1.0.tar.gz", hash = "sha256:694a8e44c87657c59292ede72891eb91d34131f6531463aab3009191c77364a8", size = 12826, upload-time = "2022-01-26T14:38:56.6Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/33/55/af02708f230eb77084a299d7b08175cff006dea4f2721074b92cdb0296c0/ordered_set-4.1.0-py3-none-any.whl", hash = "sha256:046e1132c71fcf3330438a539928932caf51ddbc582496833e23de611de14562", size = 7634, upload-time = "2022-01-26T14:38:48.677Z" },
+]
+
+[[package]]
name = "packaging"
version = "26.3"
source = { registry = "https://pypi.org/simple" }
@@ -846,6 +896,7 @@ dependencies = [
{ name = "coloredlogs" },
{ name = "dnspython" },
{ name = "flask" },
+ { name = "flask-limiter" },
{ name = "flask-login" },
{ name = "flask-mail" },
{ name = "flask-migrate" },
@@ -877,6 +928,7 @@ requires-dist = [
{ name = "coloredlogs", specifier = ">=15.0.1" },
{ name = "dnspython", specifier = ">=2.8.0" },
{ name = "flask", specifier = ">=3.1.3" },
+ { name = "flask-limiter", specifier = ">=4.1.1" },
{ name = "flask-login", specifier = ">=0.6.3" },
{ name = "flask-mail", specifier = ">=0.10.0" },
{ name = "flask-migrate", specifier = ">=4.1.0" },
@@ -914,6 +966,59 @@ wheels = [
]
[[package]]
+name = "wrapt"
+version = "2.5.1"
+source = { registry = "https://pypi.org/simple" }
+sdist = { url = "https://files.pythonhosted.org/packages/04/22/89e2f3bdae5cb34e0cab0cd86d7172dbf418de4b46c9b17b9c7a560dfa44/wrapt-2.5.1.tar.gz", hash = "sha256:f595bb0185aab3e9dc31950c95d914f56ea8278810c3b928f3426e12ed6d27bc", size = 184455, upload-time = "2026-10-14T00:39:39.24Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/25/3d/ec1937283863bbe0d90528e09f2b27cfc0dd7e608fc2b65c804967dee369/wrapt-2.5.1-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:cf63fffcdcd8c60f223d3967bb92cc4fc2e8b46f09e75b67a6a75e6f47c0fc43", size = 106439, upload-time = "2026-10-14T00:37:53.853Z" },
+ { url = "https://files.pythonhosted.org/packages/93/39/cca8afb80dbb9fce6103e59db507a9415291c4dbe97ea055875ff62901fb/wrapt-2.5.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:9f0750cbc2e29e4f3c9529d3587d4e7ed8f60638ceafb80b87a95833b0c5acd9", size = 106378, upload-time = "2026-10-14T00:37:55.386Z" },
+ { url = "https://files.pythonhosted.org/packages/aa/a0/e784d7a9fd277a2ee395490ec4df96608b7fe218bb1ef7dced2a1caea490/wrapt-2.5.1-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:3cf273b7e8d2038abb7f0a8c6550aff4f617b9d486a9965c8e8acc96a3a04de9", size = 251505, upload-time = "2026-10-14T00:37:57.022Z" },
+ { url = "https://files.pythonhosted.org/packages/81/56/01ebc86b88056f5782b9d50f962fb398c6b82aa11efc98f50c64896d94e3/wrapt-2.5.1-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:380f72610181883f66b41442cfc7c0f7552b42169efb2113def26e6380013d37", size = 250744, upload-time = "2026-10-14T00:37:58.652Z" },
+ { url = "https://files.pythonhosted.org/packages/5f/5c/0e8eaaf31e2d6e7bf13c6eae2fd5b85eaa24e21e06466e6e7a0f35532689/wrapt-2.5.1-cp314-cp314-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:cef2a8f006410b6134a0d273ec037fea8cc7a6a914f1bd7555ad9788ad788c6e", size = 231598, upload-time = "2026-10-14T00:38:00.505Z" },
+ { url = "https://files.pythonhosted.org/packages/6a/6b/6a3e257e65de6cc0027e78b423942697c74451520ba3797fd86455ec4df8/wrapt-2.5.1-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:9bad4dbb4e61624fcce5f301e37f9e743ecae4f1259a3777b3207eb7eba3dccd", size = 247998, upload-time = "2026-10-14T00:38:02.63Z" },
+ { url = "https://files.pythonhosted.org/packages/22/38/b2b8f3ee22b05f33f5aefb052844a36a0d7edd1eaff2ff4249f97792bea8/wrapt-2.5.1-cp314-cp314-musllinux_1_2_riscv64.whl", hash = "sha256:9a34640eb6295f33ca23462977de275fe8f3a50ab339b8918b96d69a7451e2e1", size = 228795, upload-time = "2026-10-14T00:38:04.317Z" },
+ { url = "https://files.pythonhosted.org/packages/29/39/e6c86552286ac27b855042fb9c229c580ed2d4c3ced5d0a1dad5f5ee8c11/wrapt-2.5.1-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:26313f38d18d40a9975123a4ebff9da125ec63ab9ece4f05320a3d8d37d2c1fe", size = 239090, upload-time = "2026-10-14T00:38:06.119Z" },
+ { url = "https://files.pythonhosted.org/packages/e7/7a/aea209f64e894573935b17de26ecfa0efe139b63f170a60be9f13734e0f5/wrapt-2.5.1-cp314-cp314-win32.whl", hash = "sha256:0591e6eace0d186c9ef1ecd1244be5a04e98041424cfca425b684ffe4f0d8030", size = 100919, upload-time = "2026-10-14T00:38:07.874Z" },
+ { url = "https://files.pythonhosted.org/packages/57/24/847096aa49d42990137ed3b940743c8a6da806d39f6c455317114e8ebfde/wrapt-2.5.1-cp314-cp314-win_amd64.whl", hash = "sha256:25ed8b1b39234140d5b5c6a273130c7595e0abece417c3ca3cb378fcea5cd0fe", size = 106243, upload-time = "2026-10-14T00:38:09.49Z" },
+ { url = "https://files.pythonhosted.org/packages/9c/ff/1cdc742133b9fb8558cdf42b2a6c2699bd7c72f7d0606286ec2f9142e20a/wrapt-2.5.1-cp314-cp314-win_arm64.whl", hash = "sha256:6201c7e122f40060a9b50696d80deec8f93b1a235ec0443f51d7a8a42f7044a6", size = 103974, upload-time = "2026-10-14T00:38:11.354Z" },
+ { url = "https://files.pythonhosted.org/packages/fc/6f/c32dc64900f1970a7f991ff5d06788cd636ca2f3ee2f99709d82577ca198/wrapt-2.5.1-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:da847332447db5505162759a4cd5ac374eb8b74841fe97a98ef3de14edd2586d", size = 109552, upload-time = "2026-10-14T00:38:12.965Z" },
+ { url = "https://files.pythonhosted.org/packages/e9/73/a9c8cc82b166e3de42f5fbd88089d2ef9b72e87aac7d6cdddb070335c20b/wrapt-2.5.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:9f437dd704abc4ee1bd03bb2d796d362d0e75915e8f3113a7900b3b7ec5f8b47", size = 109853, upload-time = "2026-10-14T00:38:14.565Z" },
+ { url = "https://files.pythonhosted.org/packages/f6/48/f341d82e69ae47df2755847af1c744ff0732543482dbc2373e5e57676621/wrapt-2.5.1-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:03aa7d2256309b57ddbf317bff2cae5f47e50ea9ae8d582780ebe0b554347b42", size = 284103, upload-time = "2026-10-14T00:38:16.23Z" },
+ { url = "https://files.pythonhosted.org/packages/fd/50/b87c6374377b08ee0783b6c5c31cd41a5e103bc54e7e857e800a0a965550/wrapt-2.5.1-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:fcccaa1484f7dd1091602970988ab741491f9f974013c844f70e45ac1196b80d", size = 293883, upload-time = "2026-10-14T00:38:17.986Z" },
+ { url = "https://files.pythonhosted.org/packages/38/e0/6d0810ae73f7a5180ec366577588ab3ad55fc1bc7e3623e82d5f8528dd2a/wrapt-2.5.1-cp314-cp314t-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:8078186f719a92693199f1e06c4ec72e1e6d374c2e459da18ed5c39d6966d727", size = 263097, upload-time = "2026-10-14T00:38:20.107Z" },
+ { url = "https://files.pythonhosted.org/packages/d6/d3/c890a46f4d395a7935e5a7436f374ceefa362eb612fdd39376dd775e0283/wrapt-2.5.1-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:1425fcf0e70b27053bd610d57bae975856e7897e3f6ba1456d2b80b9d7fd15d1", size = 289045, upload-time = "2026-10-14T00:38:21.913Z" },
+ { url = "https://files.pythonhosted.org/packages/e5/c6/042e30e0d851ca6ea743e6978902527f9166da42d736f074245d1cc54c8f/wrapt-2.5.1-cp314-cp314t-musllinux_1_2_riscv64.whl", hash = "sha256:b238e955ba34ef2b8897f358b7b868b41b9a02ffd338014b62985fa91898cc4a", size = 260378, upload-time = "2026-10-14T00:38:23.662Z" },
+ { url = "https://files.pythonhosted.org/packages/31/a4/5e65f90bf414c2f1c7eefc3d26c33af01d87ba33c4b879db4e1d4ba7fc3b/wrapt-2.5.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:25eb4d928a9abeaf70ca786a35861b46d1ab37cc4ce49ea70a070dacdead4dfe", size = 278746, upload-time = "2026-10-14T00:38:25.523Z" },
+ { url = "https://files.pythonhosted.org/packages/4b/86/17a85475e218e05225a4f6d65237b12b280596e04520df0e3d8841c4eae3/wrapt-2.5.1-cp314-cp314t-win32.whl", hash = "sha256:df6e3a36170cda0d313be50fe5065948e7f12f3a181b38cbc262e9f2ee4824e1", size = 103415, upload-time = "2026-10-14T00:38:27.409Z" },
+ { url = "https://files.pythonhosted.org/packages/27/1c/495b3aebbbe5aebf52ae5f9e8ddd0e072a412b9f5a9bc68e5eba43fa26ba/wrapt-2.5.1-cp314-cp314t-win_amd64.whl", hash = "sha256:bc5c0203d383403043fb86c964bd0bab4fcbfb26004ff4bb9c6d02ebc1d608ae", size = 109940, upload-time = "2026-10-14T00:38:29.145Z" },
+ { url = "https://files.pythonhosted.org/packages/28/4d/030ecd98da4d052c264290c4fb9f984706c9a19026154c833580e8624a05/wrapt-2.5.1-cp314-cp314t-win_arm64.whl", hash = "sha256:a424e8a9776c06aef6313af1d0e3fe6e0838af4241d0c09eb0a3b46f2c9a5ff3", size = 106214, upload-time = "2026-10-14T00:38:30.795Z" },
+ { url = "https://files.pythonhosted.org/packages/90/2b/eec5745baaad284fa19232f47796914134e1ea2dd21e289b012aa7981323/wrapt-2.5.1-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:a18e63910252eb75d8806b4baefbc3a03612502f63eab042e3741b00b719f043", size = 106441, upload-time = "2026-10-14T00:38:32.504Z" },
+ { url = "https://files.pythonhosted.org/packages/29/f3/976b0f014a08654289358d41a799c2d24642151b091cfe18a8b91766ee28/wrapt-2.5.1-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:183bf0bb893f783c9d22f953cb01fababb9f618e098763f8e66337b575b0647a", size = 106411, upload-time = "2026-10-14T00:38:34.338Z" },
+ { url = "https://files.pythonhosted.org/packages/c7/f4/4b94583d9bec0ff0573a5f10fab295675e3b9a64b701c4609b1a1982c390/wrapt-2.5.1-cp315-cp315-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:a1e823aecb3746b8f9e0aee2e1413887871ee2f5c502a3e0ef8d466dbd4adde1", size = 252017, upload-time = "2026-10-14T00:38:36.142Z" },
+ { url = "https://files.pythonhosted.org/packages/5e/3c/4f9ba033343b2935a453188f97866f0bd4f7748748ab307aaefb18be3a0a/wrapt-2.5.1-cp315-cp315-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:bde5d1b37101b1e9dd3da1f35072e2e7028e9c5e3511f7d76d3fdd4d071b7663", size = 251030, upload-time = "2026-10-14T00:38:38.003Z" },
+ { url = "https://files.pythonhosted.org/packages/69/a1/704c761913be404ed893d05702eeda5ff96c5d8448271c28b80101202bcb/wrapt-2.5.1-cp315-cp315-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:12d3d2b9d6553df6e2421ab99e1cc5413509076788f57fcb3169f5ce100a19d1", size = 232875, upload-time = "2026-10-14T00:38:40.124Z" },
+ { url = "https://files.pythonhosted.org/packages/f0/3a/779ca20fb8c70238069efd0a2b60ea3da450e57c4f103748db0247f52f3a/wrapt-2.5.1-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:521bd5ef2a33171fac08a0a302d51a983c19c3519406c1ee8da7ce29285488da", size = 248302, upload-time = "2026-10-14T00:38:42.138Z" },
+ { url = "https://files.pythonhosted.org/packages/03/02/80e13786204ce8e1002edb66d06a3194906c0dbbc038bce7bbda426d0f2b/wrapt-2.5.1-cp315-cp315-musllinux_1_2_riscv64.whl", hash = "sha256:129cab3c7b21e68e693c2819a95c47f3b1c41a834b931154688c83b6aef6bdab", size = 229839, upload-time = "2026-10-14T00:38:44.039Z" },
+ { url = "https://files.pythonhosted.org/packages/9d/d1/14c0d041375ae5d0a12445b5c5bc61b109df954cd5bfb852736cd4281cbd/wrapt-2.5.1-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:8a7c078323e6e1534968cb85488c5eb7ee2b9bbd0f8a291095213a763da40dab", size = 239634, upload-time = "2026-10-14T00:38:45.896Z" },
+ { url = "https://files.pythonhosted.org/packages/97/6e/dacc92526fbed1013eb903406ce961da6ff2a1e66b7349a253f439b979aa/wrapt-2.5.1-cp315-cp315-win32.whl", hash = "sha256:736c1de0230c6d24327b14684794214167b2c5ebb6332e28a10f504641b600df", size = 100931, upload-time = "2026-10-14T00:38:47.718Z" },
+ { url = "https://files.pythonhosted.org/packages/e8/e4/84bbd88554052958ecbbb481a75559b1e40753aba52ec86e3e1efb50ccf0/wrapt-2.5.1-cp315-cp315-win_amd64.whl", hash = "sha256:69fd0fbb3daf7c8c6f5e062847a0061f880f347374d74cf1daba57220fb64cd0", size = 106252, upload-time = "2026-10-14T00:38:49.511Z" },
+ { url = "https://files.pythonhosted.org/packages/b7/98/98d4c4524e8af70ccf35b66864be29ea9d232e5a918efc1dbcf5c87a039d/wrapt-2.5.1-cp315-cp315-win_arm64.whl", hash = "sha256:051220e5071fdfb1a6678707c8abb7bbf4824d40f99758394b2b4d64855fb284", size = 103980, upload-time = "2026-10-14T00:38:51.384Z" },
+ { url = "https://files.pythonhosted.org/packages/75/d9/4b242519d6d29eabb73cb9e50e645e014eb2c13f022601151953b3e81946/wrapt-2.5.1-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:711e73da3d7983547fc9dd208973b6b0c52640822f5d477910ba24622df6ba64", size = 109538, upload-time = "2026-10-14T00:38:53.124Z" },
+ { url = "https://files.pythonhosted.org/packages/58/05/e434f56fcceaafb251cc56c03107ae278e99158466b49f4146f7b33a2532/wrapt-2.5.1-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:5be9816d9de88f02fce23cf55f392403411d9bd9c7ae57fdc965a43b22e2de5e", size = 109874, upload-time = "2026-10-14T00:38:54.941Z" },
+ { url = "https://files.pythonhosted.org/packages/23/09/d2c0b34d02804018225279a157307b873c8d0f8452790efdd7f0004387a8/wrapt-2.5.1-cp315-cp315t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:4b3f410c416752e1dba53d361e2e6562f22c2c3ec855740dfa5836e061b22571", size = 284866, upload-time = "2026-10-14T00:38:57.081Z" },
+ { url = "https://files.pythonhosted.org/packages/71/6f/2b56319c0565d9a6b63eeba2f11f3e699dc324aaa460f1aae079bd4507b0/wrapt-2.5.1-cp315-cp315t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:094b847491b813b6e6c1775e03770930d75078c0821adf929ac712830951ef25", size = 295154, upload-time = "2026-10-14T00:38:59.007Z" },
+ { url = "https://files.pythonhosted.org/packages/3c/1b/9ac4238a1a839457d6b687b9e9c35d57ba1b2050a42d6bf8c93176bc1bdf/wrapt-2.5.1-cp315-cp315t-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:26d8ea2ec6818aeb656bd8a9e745a6f1fb0edfcd8f54291ccd94f62eb5f5e3bd", size = 264914, upload-time = "2026-10-14T00:39:01.154Z" },
+ { url = "https://files.pythonhosted.org/packages/4f/95/9faed8e5f6e5431edd36b2cfb4f305df520c197ba3639e1c78d11c70a068/wrapt-2.5.1-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:0a526227efe17dd94bd16b123d170f879bce42c15f10eb92495a745f54caa943", size = 290238, upload-time = "2026-10-14T00:39:03.052Z" },
+ { url = "https://files.pythonhosted.org/packages/b5/52/cae26590ef8ee46b55aa8b211c507f6e5ec0fbd241a6730bf7da024b5dab/wrapt-2.5.1-cp315-cp315t-musllinux_1_2_riscv64.whl", hash = "sha256:36d7d0ad593c4f1a651e4032de834db59aee1a929ee396cd483895b673328e51", size = 262208, upload-time = "2026-10-14T00:39:05.008Z" },
+ { url = "https://files.pythonhosted.org/packages/00/f3/34e5008307be4169592e99de52946cd8800b98097e2a145da11484c7b3e4/wrapt-2.5.1-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:89d9a8607b7028054bb6fd01d437f205534a5d59d53c3665d15949a99a2fce0d", size = 280005, upload-time = "2026-10-14T00:39:07.048Z" },
+ { url = "https://files.pythonhosted.org/packages/f3/f9/64e000aa84a88c52a481c7c8b011c80a8ae60bd5f68598e567a918a624a0/wrapt-2.5.1-cp315-cp315t-win32.whl", hash = "sha256:ad81bf81b0a0b6c6ec74169638202851962843e86749570c463eecc55072f93b", size = 103378, upload-time = "2026-10-14T00:39:09.336Z" },
+ { url = "https://files.pythonhosted.org/packages/2f/e4/69efa7c6e8535c5188e041ac278079949fb2daaa97e6f08beb91cf31b3d1/wrapt-2.5.1-cp315-cp315t-win_amd64.whl", hash = "sha256:d5b665a43fe0d3b390cbdd3c003d61c92fa07bd5e3fb1ed3f47920c2d03cd9fd", size = 109928, upload-time = "2026-10-14T00:39:11.156Z" },
+ { url = "https://files.pythonhosted.org/packages/13/77/6e414b3388b9f1ecb76107ef4a2aae501f1bdfcab85c8e34ef78f7db22db/wrapt-2.5.1-cp315-cp315t-win_arm64.whl", hash = "sha256:6405ff2160af9d59132ebb076eda0304db44d9d09809582932412ef7c0788a36", size = 106226, upload-time = "2026-10-14T00:39:13.061Z" },
+ { url = "https://files.pythonhosted.org/packages/bc/0c/7da7513ddcc8f1d831ec4bfbedc9f7f174ecb91042bc16916fc1e0d06b22/wrapt-2.5.1-py3-none-any.whl", hash = "sha256:c6e6c226b1ca5402d7ae5fb34a0d21f1b49124fe4200e5884d1e19e53c47ac1d", size = 81849, upload-time = "2026-10-14T00:39:37.441Z" },
+]
+
+[[package]]
name = "wtforms"
version = "3.3.0"
source = { registry = "https://pypi.org/simple" }
diff --git a/webmentions_ssg/__init__.py b/webmentions_ssg/__init__.py
index 61add02..c4ad4f6 100644
--- a/webmentions_ssg/__init__.py
+++ b/webmentions_ssg/__init__.py
@@ -12,6 +12,8 @@ from pathlib import Path
import coloredlogs
from flask import Flask
from flask_bootstrap import Bootstrap5
+from flask_limiter import Limiter
+from flask_limiter.util import get_remote_address
from flask_mail import Mail
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
@@ -37,6 +39,7 @@ DATABASE = SQLAlchemy(model_class=Base)
MIGRATE = Migrate()
HUEY = Huey()
MAIL = Mail()
+LIMITER = Limiter(key_func=get_remote_address)
def create_app(config_class: type[DefaultConfig] | None = None) -> Flask:
@@ -73,6 +76,15 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask:
app.config.from_prefixed_env(prefix="FLASK")
app.config.from_prefixed_env(prefix="WEBMENTIONS_SSG")
+ if (
+ not app.testing
+ and not app.debug
+ and app.config["RATELIMIT_STORAGE_URI"] == "memory://"
+ ):
+ raise RuntimeError(
+ "Configure RATELIMIT_STORAGE_URI with shared storage before running in production."
+ )
+
default_logging_formatter = logging.Formatter(app.config["LOG_FORMAT"])
log_file_handler = logging.handlers.RotatingFileHandler(
@@ -110,6 +122,7 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask:
command="migrate",
)
CSRF.init_app(app)
+ LIMITER.init_app(app)
MAIL.init_app(app)
from .auth import AUTH
diff --git a/webmentions_ssg/config.py b/webmentions_ssg/config.py
index 6cadbab..aea201b 100644
--- a/webmentions_ssg/config.py
+++ b/webmentions_ssg/config.py
@@ -34,6 +34,13 @@ class DefaultConfig:
MAIL_PASSWORD = None
MAIL_DEFAULT_SENDER = None
+ RATELIMIT_STORAGE_URI = "memory://"
+ RATELIMIT_HEADERS_ENABLED = True
+
+ ENDPOINT_RATE_LIMIT = "10/minute;100/hour"
+ ENDPOINT_GLOBAL_RATE_LIMIT = "1000/hour"
+ ENDPOINT_REVERIFY_COOLDOWN_SECONDS = 600
+
class DevelopmentConfig(DefaultConfig):
DEBUG = True
diff --git a/webmentions_ssg/views.py b/webmentions_ssg/views.py
index cef81fc..bb1f38a 100644
--- a/webmentions_ssg/views.py
+++ b/webmentions_ssg/views.py
@@ -5,7 +5,8 @@
"""Provide routes for authentication and Webmention administration."""
import uuid
-from typing import cast
+from datetime import UTC, datetime, timedelta
+from typing import Any, cast
import sqlalchemy as sa
import sqlalchemy.exc as sa_exc
@@ -13,6 +14,7 @@ from flask import (
Blueprint,
Response,
abort,
+ current_app,
flash,
redirect,
render_template,
@@ -21,9 +23,10 @@ from flask import (
)
from flask.typing import ResponseReturnValue
from flask_login import current_user, login_required, login_user, logout_user
+from sqlalchemy.engine import CursorResult
from sqlalchemy.orm import selectinload
-from . import CSRF, forms
+from . import CSRF, LIMITER, forms
from . import DATABASE as db
from .models import ReceivedWebmention, SentWebmention, Source, User
from .tasks.receiver import verify_webmention
@@ -292,6 +295,11 @@ def resend_sent_webmention(
@root_page.post("/endpoint")
+@LIMITER.limit(
+ lambda: current_app.config["ENDPOINT_GLOBAL_RATE_LIMIT"],
+ key_func=lambda: "all-clients",
+)
+@LIMITER.limit(lambda: current_app.config["ENDPOINT_RATE_LIMIT"])
@CSRF.exempt
def endpoint() -> ResponseReturnValue:
"""
@@ -318,8 +326,8 @@ def endpoint() -> ResponseReturnValue:
)
).scalar_one_or_none()
- # Only notify about new webmentions, not resubmissions of existing ones.
- send_notification = webmention is None
+ should_verify = False
+ send_notification = False
if webmention is None:
identifier = uuid.uuid7()
@@ -330,9 +338,10 @@ def endpoint() -> ResponseReturnValue:
try:
db.session.commit()
+ should_verify = True
+ send_notification = True
except sa_exc.IntegrityError:
db.session.rollback()
- send_notification = False
# Another request may have inserted the same source/target pair
# after our SELECT but before our COMMIT.
@@ -343,22 +352,32 @@ def endpoint() -> ResponseReturnValue:
)
).scalar_one()
- identifier = webmention.uuid
- webmention.status = "received"
- webmention.failure_reason = None
-
- db.session.commit()
-
else:
- identifier = webmention.uuid
-
- # Re-sent webmention: re-verify the existing row.
- webmention.status = "received"
- webmention.failure_reason = None
+ 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(status="received", failure_reason=None, updated_at=now)
+ .execution_options(synchronize_session=False)
+ ),
+ )
+ should_verify = result.rowcount == 1
db.session.commit()
- verify_webmention(webmention.uuid, send_notification=send_notification)
+ identifier = webmention.uuid
+
+ if should_verify:
+ verify_webmention(identifier, send_notification=send_notification)
status_url = url_for("root.status", identifier=str(identifier), _external=True)