Skip to content

Commit a18c20c

Browse files
Harden fetch operation-name builder with normalized fallback
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 1c07ded commit a18c20c

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

hyperbrowser/client/polling.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,14 @@ def build_operation_name(prefix: object, identifier: object) -> str:
116116
return operation_name
117117

118118

119-
def build_fetch_operation_name(operation_name: str) -> str:
120-
prefixed_operation_name = f"{_FETCH_OPERATION_NAME_PREFIX}{operation_name}"
119+
def build_fetch_operation_name(operation_name: object) -> str:
120+
normalized_operation_name = build_operation_name("", operation_name)
121+
prefixed_operation_name = (
122+
f"{_FETCH_OPERATION_NAME_PREFIX}{normalized_operation_name}"
123+
)
121124
if len(prefixed_operation_name) <= _MAX_OPERATION_NAME_LENGTH:
122125
return prefixed_operation_name
123-
return operation_name
126+
return normalized_operation_name
124127

125128

126129
def _ensure_boolean_terminal_result(result: object, *, operation_name: str) -> bool:

tests/test_polling.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ def test_build_fetch_operation_name_falls_back_for_max_length_inputs():
5252
assert build_fetch_operation_name(operation_name) == operation_name
5353

5454

55+
def test_build_fetch_operation_name_sanitizes_non_string_and_control_input():
56+
operation_name = build_fetch_operation_name(" \nabc\tdef ")
57+
assert operation_name == "Fetching abc?def"
58+
59+
numeric_operation_name = build_fetch_operation_name(123)
60+
assert numeric_operation_name == "Fetching 123"
61+
62+
63+
def test_build_fetch_operation_name_handles_unstringifiable_input():
64+
class _BadOperationName:
65+
def __str__(self) -> str:
66+
raise RuntimeError("cannot stringify")
67+
68+
operation_name = build_fetch_operation_name(_BadOperationName())
69+
assert operation_name == "Fetching unknown"
70+
71+
5572
def test_build_operation_name_keeps_short_names_unchanged():
5673
assert build_operation_name("crawl job ", "123") == "crawl job 123"
5774

0 commit comments

Comments
 (0)