Skip to content

Commit 03ab8e2

Browse files
[SECURITY] Hardening documentation urls more (- WIP PR #373 -)
* this work is related to GHI #213 Changes in file docs/utils.py: * added max length of 2048 validation for urls * related work
1 parent e5c341c commit 03ab8e2

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

docs/utils.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,65 @@
3030
# URL allowed scheme list
3131
# Enforces:
3232
# - URLs Must start with https
33-
URL_ALLOWED_SCHEMES = {"https"}
33+
URL_ALLOWED_SCHEMES = frozenset({"https"})
3434

3535

3636
# URL allowed domain list
3737
# Enforces:
3838
# - URLs Must belong to one of these domains
39-
URL_ALLOWED_NETLOCS = {"github.com", "readthedocs.com", "docs.python.org"}
39+
URL_ALLOWED_NETLOCS = frozenset({"github.com", "readthedocs.com", "docs.python.org"})
40+
41+
42+
# Maximum allowed URL length
43+
MAX_URL_LENGTH = 2048 # Common browser limit
44+
"""Maximum allowed length for URL validation.
45+
46+
Should be large enough for most URLs but no larger than common browser limits.
47+
48+
Unit-Testing:
49+
50+
First set up test fixtures by importing utils.
51+
52+
>>> import docs.utils as _utils
53+
>>>
54+
55+
>>> _utils.MAX_URL_LENGTH is not None
56+
True
57+
>>> type(_utils.MAX_URL_LENGTH) is type(int())
58+
True
59+
>>> _utils.MAX_URL_LENGTH > 0
60+
True
61+
>>> _utils.MAX_URL_LENGTH >= 256
62+
True
63+
>>> _utils.MAX_URL_LENGTH <= 2048
64+
True
65+
>>>
66+
67+
"""
4068

4169

4270
# Error messages for URL validation
71+
INVALID_LENGTH_ERROR = f"URL exceeds maximum length of {MAX_URL_LENGTH} characters."
72+
"""Length error message for URL validation.
73+
74+
Unit-Testing:
75+
76+
First set up test fixtures by importing utils.
77+
78+
>>> import docs.utils as _utils
79+
>>>
80+
81+
>>> _utils.INVALID_LENGTH_ERROR is not None
82+
True
83+
>>> type(_utils.INVALID_LENGTH_ERROR) is type(str())
84+
True
85+
>>> len(_utils.INVALID_LENGTH_ERROR) > 0
86+
True
87+
>>>
88+
89+
"""
90+
91+
4392
INVALID_SCHEME_ERROR = "Invalid URL scheme. Only 'https' is allowed."
4493
"""Scheme error message for URL validation.
4594
@@ -216,6 +265,9 @@ def sanitize_url(url: str) -> str:
216265
>>>
217266
218267
"""
268+
# Validate length
269+
if len(url) > MAX_URL_LENGTH:
270+
raise ValueError(INVALID_LENGTH_ERROR)
219271
parsed_url = urlparse(url)
220272
# Validate scheme
221273
if parsed_url.scheme not in URL_ALLOWED_SCHEMES:

0 commit comments

Comments
 (0)