Skip to content

Commit 15984e8

Browse files
fix: URL fragment check (#305)
- Fix erroneously forbidden characters in fragment check - Narrow allowed characters for fragment based on standard
1 parent b313f15 commit 15984e8

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/validators/url.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ def _validate_optionals(path: str, query: str, fragment: str, strict_query: bool
111111
if query and parse_qs(query, strict_parsing=strict_query):
112112
optional_segments &= True
113113
if fragment:
114-
fragment = fragment.lstrip("/") if fragment.startswith("/") else fragment
115-
optional_segments &= all(char_to_avoid not in fragment for char_to_avoid in ("?",))
114+
# See RFC3986 Section 3.5 Fragment for allowed characters
115+
optional_segments &= bool(re.fullmatch(r"[0-9a-zA-Z?/:@\-._~!$&'()*+,;=]*", fragment))
116116
return optional_segments
117117

118118

tests/test_url.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
"http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
8686
"https://exchange.jetswap.finance/#/swap",
8787
"https://www.foo.com/bar#/baz/test",
88+
"https://matrix.to/#/!BSqRHgvCtIsGittkBG:talk.puri.sm/$1551464398"
89+
+ "853539kMJNP:matrix.org?via=talk.puri.sm&via=matrix.org&via=disroot.org",
8890
# when simple_host=True
8991
# "http://localhost",
9092
# "http://localhost:8000",

0 commit comments

Comments
 (0)