Skip to content

Commit d54812b

Browse files
Reuse shared URL component decode limits in URL builders
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 7bb133b commit d54812b

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

hyperbrowser/client/base.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from urllib.parse import unquote, urlparse
2+
from urllib.parse import urlparse
33
from typing import Mapping, Optional, Type, Union
44

55
from hyperbrowser.exceptions import HyperbrowserError
@@ -101,14 +101,9 @@ def _build_url(self, path: str) -> str:
101101
normalized_query_suffix = (
102102
f"?{normalized_parts.query}" if normalized_parts.query else ""
103103
)
104-
decoded_path = normalized_path_only
105-
for _ in range(10):
106-
next_decoded_path = unquote(decoded_path)
107-
if next_decoded_path == decoded_path:
108-
break
109-
decoded_path = next_decoded_path
110-
else:
111-
raise HyperbrowserError("path contains excessively nested URL encoding")
104+
decoded_path = ClientConfig._decode_url_component_with_limit(
105+
normalized_path_only, component_label="path"
106+
)
112107
if "\\" in decoded_path:
113108
raise HyperbrowserError("path must not contain backslashes")
114109
if "\n" in decoded_path or "\r" in decoded_path:

hyperbrowser/config.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ def __post_init__(self) -> None:
3434
mapping_error_message="headers must be a mapping of string pairs",
3535
)
3636

37+
@staticmethod
38+
def _decode_url_component_with_limit(value: str, *, component_label: str) -> str:
39+
decoded_value = value
40+
for _ in range(10):
41+
next_decoded_value = unquote(decoded_value)
42+
if next_decoded_value == decoded_value:
43+
return decoded_value
44+
decoded_value = next_decoded_value
45+
raise HyperbrowserError(
46+
f"{component_label} contains excessively nested URL encoding"
47+
)
48+
3749
@staticmethod
3850
def normalize_base_url(base_url: str) -> str:
3951
if not isinstance(base_url, str):
@@ -78,16 +90,9 @@ def normalize_base_url(base_url: str) -> str:
7890
"base_url must contain a valid port number"
7991
) from exc
8092

81-
decoded_base_path = parsed_base_url.path
82-
for _ in range(10):
83-
next_decoded_base_path = unquote(decoded_base_path)
84-
if next_decoded_base_path == decoded_base_path:
85-
break
86-
decoded_base_path = next_decoded_base_path
87-
else:
88-
raise HyperbrowserError(
89-
"base_url path contains excessively nested URL encoding"
90-
)
93+
decoded_base_path = ClientConfig._decode_url_component_with_limit(
94+
parsed_base_url.path, component_label="base_url path"
95+
)
9196
if "\\" in decoded_base_path:
9297
raise HyperbrowserError("base_url must not contain backslashes")
9398
if any(character.isspace() for character in decoded_base_path):

0 commit comments

Comments
 (0)