Skip to content

Commit 7bb133b

Browse files
Cap request method length in error context normalization
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent f95ae6a commit 7bb133b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

hyperbrowser/transport/error_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
_HTTP_METHOD_TOKEN_PATTERN = re.compile(r"^[!#$%&'*+\-.^_`|~0-9A-Z]+$")
88
_MAX_ERROR_MESSAGE_LENGTH = 2000
99
_MAX_REQUEST_URL_DISPLAY_LENGTH = 1000
10+
_MAX_REQUEST_METHOD_LENGTH = 50
1011

1112

1213
def _normalize_request_method(method: Any) -> str:
1314
if not isinstance(method, str) or not method.strip():
1415
return "UNKNOWN"
1516
normalized_method = method.strip().upper()
17+
if len(normalized_method) > _MAX_REQUEST_METHOD_LENGTH:
18+
return "UNKNOWN"
1619
if not _HTTP_METHOD_TOKEN_PATTERN.fullmatch(normalized_method):
1720
return "UNKNOWN"
1821
return normalized_method

tests/test_transport_error_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class _LowercaseMethodRequest:
3232
url = "https://example.com/lowercase"
3333

3434

35+
class _TooLongMethodRequest:
36+
method = "A" * 51
37+
url = "https://example.com/too-long-method"
38+
39+
3540
class _InvalidMethodTokenRequest:
3641
method = "GET /invalid"
3742
url = "https://example.com/invalid-method"
@@ -72,6 +77,12 @@ def request(self): # type: ignore[override]
7277
return _LowercaseMethodRequest()
7378

7479

80+
class _RequestErrorWithTooLongMethod(httpx.RequestError):
81+
@property
82+
def request(self): # type: ignore[override]
83+
return _TooLongMethodRequest()
84+
85+
7586
class _RequestErrorWithInvalidMethodToken(httpx.RequestError):
7687
@property
7788
def request(self): # type: ignore[override]
@@ -145,6 +156,15 @@ def test_extract_request_error_context_normalizes_method_to_uppercase():
145156
assert url == "https://example.com/lowercase"
146157

147158

159+
def test_extract_request_error_context_rejects_overlong_methods():
160+
method, url = extract_request_error_context(
161+
_RequestErrorWithTooLongMethod("network down")
162+
)
163+
164+
assert method == "UNKNOWN"
165+
assert url == "https://example.com/too-long-method"
166+
167+
148168
def test_extract_request_error_context_rejects_invalid_method_tokens():
149169
method, url = extract_request_error_context(
150170
_RequestErrorWithInvalidMethodToken("network down")
@@ -204,6 +224,16 @@ def test_format_request_failure_message_normalizes_lowercase_fallback_method():
204224
assert message == "Request POST https://example.com/fallback failed"
205225

206226

227+
def test_format_request_failure_message_rejects_overlong_fallback_methods():
228+
message = format_request_failure_message(
229+
httpx.RequestError("network down"),
230+
fallback_method="A" * 51,
231+
fallback_url="https://example.com/fallback",
232+
)
233+
234+
assert message == "Request UNKNOWN https://example.com/fallback failed"
235+
236+
207237
def test_format_request_failure_message_truncates_very_long_fallback_urls():
208238
very_long_url = "https://example.com/" + ("a" * 1200)
209239
message = format_request_failure_message(

0 commit comments

Comments
 (0)