1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause
import pytest
from flask import Flask
from werkzeug.datastructures import MultiDict
from wtforms import Form, StringField, ValidationError
from webmentions_ssg.forms.validators import (
AllowedTargetHostname,
MaxURLBytes,
RejectNonPublicIPLiteral,
)
class URLForm(Form):
url = StringField("URL")
def test_allowed_hostname_accepts_configured_hostname(app: Flask) -> None:
app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"]
form = URLForm(MultiDict({"url": "https://dennisfink.me/blog/example/"}))
with app.app_context():
AllowedTargetHostname()(form, form.url)
def test_allowed_hostname_rejects_unconfigured_hostname(app: Flask) -> None:
app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"]
form = URLForm(MultiDict({"url": "https://example.com/post"}))
with (
app.app_context(),
pytest.raises(
ValidationError, match="Target hostname is not in ALLOWED_TARGET_HOSTNAMES"
),
):
AllowedTargetHostname()(form, form.url)
def test_allowed_hostname_uses_custom_message(app: Flask) -> None:
app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"]
form = URLForm(MultiDict({"url": "https://example.com/post"}))
with (
app.app_context(),
pytest.raises(ValidationError, match="Hostname is not allowed"),
):
AllowedTargetHostname("Hostname is not allowed")(form, form.url)
@pytest.mark.parametrize(
"address",
["64:ff9b::a9fe:a9fe", "::10.0.0.1", "2002:a9fe:a9fe::1", "::ffff:169.254.169.254"],
)
def test_public_ip_literal_rejects_ipv6_transition_addresses(address: str) -> None:
form = URLForm(MultiDict({"url": f"https://[{address}]/"}))
with pytest.raises(ValidationError, match="URL must use a public address"):
RejectNonPublicIPLiteral()(form, form.url)
def test_public_ip_literal_accepts_public_ipv6_address() -> None:
form = URLForm(MultiDict({"url": "https://[2606:2800:220:1:248:1893:25c8:1946]/"}))
RejectNonPublicIPLiteral()(form, form.url)
def test_allowed_hostname_handles_invalid_ipv6(app: Flask) -> None:
form = URLForm(MultiDict({"url": "http://[::1"}))
with app.app_context(), pytest.raises(ValidationError, match="Invalid input"):
AllowedTargetHostname()(form, form.url)
def test_url_max_bytes_respects_utf8_byte_length_and_current_config(app: Flask) -> None:
validator = MaxURLBytes()
form = URLForm(MultiDict({"url": "é" * 3}))
with app.app_context():
app.config["MAX_WEBMENTION_URL_BYTES"] = 6
validator(form, form.url)
app.config["MAX_WEBMENTION_URL_BYTES"] = 5
with pytest.raises(ValidationError, match="5 bytes"):
validator(form, form.url)
def test_url_max_bytes_custom_message(app: Flask) -> None:
form = URLForm(MultiDict({"url": "too-long"}))
with app.app_context():
app.config["MAX_WEBMENTION_URL_BYTES"] = 2
with pytest.raises(ValidationError, match="URL too long"):
MaxURLBytes(message="URL too long")(form, form.url)
def test_url_max_bytes_rejects_malformed_unicode(app: Flask) -> None:
form = URLForm(MultiDict({"url": "\ud800"}))
with app.app_context(), pytest.raises(ValidationError, match="valid UTF-8"):
MaxURLBytes()(form, form.url)
def test_max_url_bytes_skips_empty_field_data(app: Flask) -> None:
form = URLForm()
form.url.data = None
with app.app_context():
MaxURLBytes()(form, form.url)
def test_strict_http_url_rejects_non_string_field_data() -> None:
from wtforms.validators import StopValidation
from webmentions_ssg.forms.validators import StrictHTTPURL
form = URLForm()
form.url.data = None
with pytest.raises(StopValidation, match="Invalid URL"):
StrictHTTPURL()(form, form.url)
def test_nonpublic_ip_validator_reports_malformed_host() -> None:
form = URLForm(MultiDict({"url": "http://[invalid"}))
with pytest.raises(ValidationError, match="Invalid input"):
RejectNonPublicIPLiteral()(form, form.url)
|