-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtest_validators.py
More file actions
175 lines (148 loc) · 5.8 KB
/
Copy pathtest_validators.py
File metadata and controls
175 lines (148 loc) · 5.8 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from __future__ import annotations
import pytest
from shared.validators import (
validate_alias,
validate_blocked_url,
validate_url,
validate_url_password,
)
# ---------------------------------------------------------------------------
# shared.validators — validate_url
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
"url, expected",
[
("https://example.com", True),
("https://example.com/foo/bar?q=1", True),
("https://spoo.me/abc", False),
("https://SPOO.ME/abc", False), # case-insensitive block
("https://www.spoo.me/abc", False), # subdomain of a blocked host
("not-a-url", False),
("http://192.168.1.1/path", False), # IPv4 skipped by validator
("https://[::1", False), # urlparse raises on this, must not escape
# Host-scoped: a foreign destination that merely mentions the blocked
# name in its path, query or fragment is not a redirect loop. The
# dashboard hit this shortening a PostHog analytics URL filtered on
# spoo.me, which the old substring check rejected.
("https://eu.posthog.com/project/1?filter=spoo.me", True),
("https://example.com/spoo.me/guide", True),
("https://example.com/#spoo.me", True),
("https://notspoo.me/abc", True), # suffix must be a label boundary
# userinfo can't smuggle either direction — hostname wins.
("https://spoo.me@example.com/", True),
("https://example.com@spoo.me/", False),
],
ids=[
"valid",
"valid_with_path",
"self_ref",
"self_ref_uppercase",
"self_ref_subdomain",
"plain_text",
"ipv4",
"malformed_ipv6_authority",
"foreign_host_mentions_in_query",
"foreign_host_mentions_in_path",
"foreign_host_mentions_in_fragment",
"lookalike_host_not_blocked",
"userinfo_lookalike_allowed",
"userinfo_cannot_mask_self_host",
],
)
def test_validate_url(url, expected):
assert validate_url(url) is expected
def test_validate_url_custom_blocked_domain():
assert validate_url("https://evil.com", blocked_self_domains=("evil.com",)) is False
def test_validate_url_empty_blocked_list_allows_spoo():
assert validate_url("https://spoo.me/x", blocked_self_domains=()) is True
@pytest.mark.parametrize(
"url",
[
"ftp://example.com",
"file:///etc/passwd",
"data:text/html,foo",
"javascript:alert(1)",
"//example.com/path",
"example.com",
],
ids=["ftp", "file", "data", "javascript", "scheme_relative", "no_scheme"],
)
def test_validate_url_rejects_non_http_schemes(url):
assert validate_url(url) is False
def test_validate_url_blocks_self_link_across_schemes():
# Bare hostname catches both http:// and https:// variants — guards
# against the wiring bug where the full app_url only matched one scheme.
assert validate_url("http://spoo.me/abc") is False
assert validate_url("https://spoo.me/abc") is False
assert validate_url("https://docs.spoo.me/x") is False
# ---------------------------------------------------------------------------
# shared.validators — validate_url_password
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
"password, valid",
[
("Hello123.", True),
("Hello123@", True),
("Hi1.", False), # too short
("12345678.", False), # no letter
("HelloWorld.", False), # no digit
("Hello1234", False), # no special char
("Hello12..", False), # consecutive specials (..)
("Hello12@.", False), # consecutive specials (@.)
],
ids=[
"dot_ok",
"at_ok",
"too_short",
"no_letter",
"no_digit",
"no_special",
"consecutive_dots",
"consecutive_at_dot",
],
)
def test_validate_url_password(password, valid):
assert validate_url_password(password) is valid
# ---------------------------------------------------------------------------
# shared.validators — validate_alias
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
"alias, expected",
[
("MyAlias123", True),
("my_alias", True),
("my-alias", True),
("", True), # zero chars matches *
("my alias", False), # space
("alias!", False), # special char
],
ids=["alphanumeric", "underscore", "hyphen", "empty", "space", "exclamation"],
)
def test_validate_alias(alias, expected):
assert validate_alias(alias) is expected
# ---------------------------------------------------------------------------
# shared.validators — validate_blocked_url
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
"url, patterns, expected",
[
("https://evil.com", [], True), # no patterns → always allow
("https://phishing.example.com", [r"phishing"], False), # match → block
("https://good.example.com", [r"phishing"], True), # no match → allow
("https://spam.ru/page", [r"\.ru/"], False), # regex applied
("https://evil.com", [r"safe", r"evil"], False), # second pattern matches
],
ids=[
"no_patterns",
"match_blocks",
"no_match_allows",
"regex_match",
"second_pattern",
],
)
def test_validate_blocked_url(url, patterns, expected):
assert validate_blocked_url(url, patterns) is expected
def test_validate_blocked_url_timeout_fails_open(mocker):
"""Timed-out patterns must fail open (URL stays allowed)."""
mocker.patch("shared.validators.regex.search", side_effect=TimeoutError)
assert validate_blocked_url("https://example.com", [r"any_pattern"]) is True