diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/tasks/test_sender.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 6a9afc1..ecfc27a 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -1150,3 +1150,66 @@ def test_post_webmention( assert form["source"] == SOURCE_URL assert form["target"] == TARGET_URL + + +@pytest.mark.parametrize( + ("code", "location", "expected"), + [ + (201, "/webmention/status/123", "https://example.com/webmention/status/123"), + (201, "https://status.example.org/123", "https://status.example.org/123"), + (201, "status/123", "https://example.com/status/123"), + (201, "javascript:alert(1)", None), + (201, "data:text/html,evil", None), + (201, "ftp://example.com/status", None), + (201, "http://[invalid", None), + (202, "https://example.com/status", None), + ], +) +def test_post_webmention_validates_location( + sender_module: ModuleType, code: int, location: str, expected: str | None +) -> None: + def handler(_request: httpx.Request) -> httpx.Response: + return httpx.Response(code, headers={"Location": location}) + + with httpx.Client(transport=httpx.MockTransport(handler)) as client: + result = sender_module.post_webmention( + client, endpoint=ENDPOINT_URL, source=SOURCE_URL, target=TARGET_URL + ) + + assert result == (code, expected) + + +@pytest.mark.parametrize( + ("status_url", "expected"), + [ + ("javascript:alert(1)", None), + ("data:text/html,evil", None), + ("ftp://example.com/status", None), + ("https://example.com/status", "https://example.com/status"), + ], +) +def test_send_does_not_persist_unsafe_status_url( + app: Flask, + sender_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + status_url: str, + expected: str | None, +) -> None: + identifier = create_sent_webmention(app) + client_factory = MagicMock() + monkeypatch.setattr(sender_module.httpx, "Client", client_factory) + monkeypatch.setattr( + sender_module, "discover_webmention_endpoint", Mock(return_value=ENDPOINT_URL) + ) + monkeypatch.setattr( + sender_module, "post_webmention", Mock(return_value=(201, status_url)) + ) + + sender_module.send_webmention.call_local(identifier) + + with app.app_context(): + mention = db.session.get(SentWebmention, identifier) + assert mention is not None + assert mention.status == SentWebmentionStatus.SENT + assert mention.response_status == 201 + assert mention.status_url == expected |
