aboutsummaryrefslogtreecommitdiff
path: root/tests/test_url_security.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_url_security.py')
-rw-r--r--tests/test_url_security.py193
1 files changed, 0 insertions, 193 deletions
diff --git a/tests/test_url_security.py b/tests/test_url_security.py
deleted file mode 100644
index 95c2bd9..0000000
--- a/tests/test_url_security.py
+++ /dev/null
@@ -1,193 +0,0 @@
-# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
-#
-# SPDX-License-Identifier: BSD-3-Clause
-
-import ipaddress
-from unittest.mock import Mock, PropertyMock, patch
-
-import dns.resolver
-import pytest
-
-from webmentions_ssg.url_security import (
- AddressResolutionError,
- UnsafeAddressError,
- is_http_url,
- is_public_address,
- is_public_url,
- is_safe_next_url,
- public_addresses,
-)
-
-
-@pytest.mark.parametrize(
- ("url", "expected"),
- [
- pytest.param("http://example.com/", True, id="http"),
- pytest.param("https://example.com/path", True, id="https"),
- pytest.param("HTTP://EXAMPLE.COM/", True, id="scheme-case-insensitive"),
- pytest.param("https://example.com:8443/path", True, id="port"),
- pytest.param("http://[2001:db8::1]/", True, id="ipv6"),
- pytest.param("ftp://example.com/", False, id="ftp"),
- pytest.param("mailto:example@example.com", False, id="mailto"),
- pytest.param("/relative/url", False, id="relative"),
- pytest.param("//example.com/path", False, id="scheme-relative"),
- pytest.param("https:///missing-host", False, id="missing-host"),
- pytest.param("", False, id="empty"),
- pytest.param("http://[::1", False, id="malformed"),
- ],
-)
-def test_is_http_url(url: str, expected: bool) -> None:
- assert is_http_url(url) is expected
-
-
-@pytest.mark.parametrize(
- ("next_url", "expected"),
- [
- pytest.param("/", True, id="root"),
- pytest.param("/received", True, id="local-path"),
- pytest.param("/received?page=2", True, id="query-string"),
- pytest.param("/received#details", True, id="fragment"),
- pytest.param(
- "/sent?next=https://evil.example", True, id="external-url-in-query"
- ),
- pytest.param("/foo//bar", True, id="double-slash-within-path"),
- pytest.param("", False, id="empty"),
- pytest.param("received", False, id="relative-path"),
- pytest.param(" /received", False, id="leading-space"),
- pytest.param("https://evil.example/phishing", False, id="absolute-https"),
- pytest.param("http://evil.example", False, id="absolute-http"),
- pytest.param("javascript:alert(1)", False, id="non-http-scheme"),
- pytest.param("//evil.example/phishing", False, id="scheme-relative"),
- pytest.param("///evil.example/phishing", False, id="triple-slash"),
- pytest.param("////evil.example", False, id="four-slashes"),
- pytest.param("//", False, id="two-slashes"),
- pytest.param(r"/\evil.example", False, id="slash-backslash-host"),
- pytest.param(r"/received\evil.example", False, id="backslash-in-path"),
- pytest.param(r"\/evil.example", False, id="backslash-slash-host"),
- ],
-)
-def test_is_safe_next_url(next_url: str, expected: bool) -> None:
- assert is_safe_next_url(next_url) is expected
-
-
-@patch("webmentions_ssg.url_security.dns.resolver.resolve_name")
-@pytest.mark.parametrize(
- ("url", "resolved_addresses"),
- [
- ("http://127.0.0.1/", ["127.0.0.1"]),
- ("http://127.0.0.1:8080/test", ["127.0.0.1"]),
- ("http://[::1]/", ["::1"]),
- ("http://10.0.0.1/", ["10.0.0.1"]),
- ("http://172.16.0.1/", ["172.16.0.1"]),
- ("http://192.168.1.1/", ["192.168.1.1"]),
- ("http://169.254.169.254/", ["169.254.169.254"]),
- ("http://localhost/", ["127.0.0.1", "::1"]),
- ("http://internal.example/", ["192.168.1.10"]),
- ("https://example.com/", ["93.184.216.34", "192.168.1.10"]),
- ],
-)
-def test_is_public_url_returns_false_for_non_public_addresses(
- resolve_name: Mock, url: str, resolved_addresses: list[str]
-) -> None:
- resolve_name.return_value.addresses.return_value = resolved_addresses
- assert not is_public_url(url)
-
-
-@patch("webmentions_ssg.url_security.dns.resolver.resolve_name")
-@pytest.mark.parametrize(
- ("url", "resolved_addresses"),
- [
- ("https://example.com/", ["93.184.216.34"]),
- (
- "https://example.com/",
- ["93.184.216.34", "2606:2800:220:1:248:1893:25c8:1946"],
- ),
- ],
-)
-def test_is_public_url_returns_true_for_public_addresses(
- resolve_name: Mock, url: str, resolved_addresses: list[str]
-) -> None:
- resolve_name.return_value.addresses.return_value = resolved_addresses
- assert is_public_url(url)
-
-
-@patch(
- "webmentions_ssg.url_security.dns.resolver.resolve_name",
- side_effect=dns.resolver.NXDOMAIN(),
-)
-def test_is_public_url_raises_for_resolution_failure(resolve_name: Mock) -> None:
- with pytest.raises(AddressResolutionError, match="Could not resolve hostname"):
- is_public_url("https://nonexistent.example/")
- resolve_name.assert_called_once_with("nonexistent.example", lifetime=None)
-
-
-@patch("webmentions_ssg.url_security.dns.resolver.resolve_name")
-def test_is_public_url_raises_when_url_has_no_hostname(resolve_name: Mock) -> None:
- with pytest.raises(ValueError, match="No hostname was specified"):
- is_public_url("/relative/url")
- resolve_name.assert_not_called()
-
-
-def test_ip_literal_does_not_use_dns() -> None:
- with patch("webmentions_ssg.url_security.dns.resolver.resolve_name") as dns_resolve:
- with pytest.raises(UnsafeAddressError):
- public_addresses("169.254.169.254")
- with pytest.raises(UnsafeAddressError):
- public_addresses("::1")
- assert public_addresses("8.8.8.8") == ("8.8.8.8",)
- dns_resolve.assert_not_called()
-
-
-@pytest.mark.parametrize(
- "address",
- [
- pytest.param("64:ff9b::a9fe:a9fe", id="nat64-metadata"),
- pytest.param("64:ff9b::808:808", id="nat64-public-ipv4"),
- pytest.param("::10.0.0.1", id="ipv4-compatible-private"),
- pytest.param("::8.8.8.8", id="ipv4-compatible-public"),
- pytest.param("2002:a9fe:a9fe::1", id="6to4-metadata"),
- pytest.param("2002:0a00:0001::1", id="6to4-private"),
- pytest.param("::ffff:10.0.0.1", id="mapped-private"),
- pytest.param("::ffff:169.254.169.254", id="mapped-metadata"),
- ],
-)
-def test_public_addresses_rejects_ipv6_transition_literals(address: str) -> None:
- with patch("webmentions_ssg.url_security.dns.resolver.resolve_name") as resolve:
- with pytest.raises(UnsafeAddressError, match="Non-public"):
- public_addresses(address)
- resolve.assert_not_called()
-
-
-@pytest.mark.parametrize(
- "address", ["64:ff9b::a9fe:a9fe", "::10.0.0.1", "2002:a9fe:a9fe::1"]
-)
-def test_public_addresses_rejects_transition_addresses_in_dns(address: str) -> None:
- with patch("webmentions_ssg.url_security.dns.resolver.resolve_name") as resolve:
- resolve.return_value.addresses.return_value = ["93.184.216.34", address]
- with pytest.raises(UnsafeAddressError, match="Non-public"):
- public_addresses("transition.example")
-
-
-@pytest.mark.parametrize("address", ["2002:0a00:0001::1", "::ffff:10.0.0.1"])
-def test_private_embedded_ipv4_rejected_even_if_ipv6_is_global(address: str) -> None:
- # Simulate a Python version treating the outer transition address as global.
- with patch.object(
- ipaddress.IPv6Address, "is_global", new_callable=PropertyMock
- ) as global_:
- global_.return_value = True
- assert not is_public_address(ipaddress.ip_address(address))
-
-
-def test_public_addresses_rejects_mixed_dns_results() -> None:
- with patch("webmentions_ssg.url_security.dns.resolver.resolve_name") as resolve:
- resolve.return_value.addresses.return_value = ["93.184.216.34", "127.0.0.1"]
- with pytest.raises(UnsafeAddressError, match="Non-public"):
- public_addresses("rebinding.example", lifetime=1.5)
- resolve.assert_called_once_with("rebinding.example", lifetime=1.5)
-
-
-def test_public_addresses_rejects_empty_dns_result() -> None:
- with patch("webmentions_ssg.url_security.dns.resolver.resolve_name") as resolve:
- resolve.return_value.addresses.return_value = []
- with pytest.raises(AddressResolutionError):
- public_addresses("empty.example")