diff options
| author | Dennis Fink | 2026-10-29 19:49:44 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-29 19:49:44 +0100 |
| commit | 9a1e863079b64883beadaba7b283621f1b3cf70a (patch) | |
| tree | 3737edbe481460b13dad9e12eaeb3ed5208dda1b /tests/test_views.py | |
| parent | 38f5a47ed79fac6b5cdb525e50168beb67c4a0b3 (diff) | |
| download | webmentions-ssg-9a1e863079b64883beadaba7b283621f1b3cf70a.tar.gz webmentions-ssg-9a1e863079b64883beadaba7b283621f1b3cf70a.zip | |
fix(receiver): rate limit Webmention submissions
Protect the public endpoint from abusive request floods and repeated
duplicate submissions that would otherwise trigger unnecessary
verification work.
Add per-client and global limits, require shared limiter storage in
production, and only reverify existing Webmentions after a configurable
cooldown.
Diffstat (limited to 'tests/test_views.py')
| -rw-r--r-- | tests/test_views.py | 152 |
1 files changed, 140 insertions, 12 deletions
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) |
