Skip to content

Commit 9e098c6

Browse files
Harden polling operation-name component coercion
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 5df9e0b commit 9e098c6

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

hyperbrowser/client/polling.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ def _normalized_exception_text(exc: Exception) -> str:
6161

6262

6363
def _coerce_operation_name_component(value: object, *, fallback: str) -> str:
64-
if isinstance(value, str):
64+
if isinstance(value, str) and type(value) is str:
6565
return value
6666
try:
67-
return str(value)
67+
normalized_value = str(value)
68+
if type(normalized_value) is not str:
69+
raise TypeError("operation name component must normalize to string")
70+
return normalized_value
6871
except Exception:
6972
return fallback
7073

tests/test_polling.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,19 @@ def __str__(self) -> str:
216216
assert operation_name == "crawl job unknown"
217217

218218

219+
def test_build_operation_name_falls_back_for_broken_string_subclass_identifiers():
220+
class _BrokenStringIdentifier(str):
221+
def __str__(self) -> str:
222+
raise RuntimeError("cannot stringify string identifier")
223+
224+
operation_name = build_operation_name(
225+
"crawl job ",
226+
_BrokenStringIdentifier("123"),
227+
)
228+
229+
assert operation_name == "crawl job unknown"
230+
231+
219232
def test_build_operation_name_falls_back_for_unstringifiable_prefixes():
220233
class _BadPrefix:
221234
def __str__(self) -> str:
@@ -229,6 +242,29 @@ def __str__(self) -> str:
229242
assert operation_name == "identifier"
230243

231244

245+
def test_build_operation_name_falls_back_for_broken_string_subclass_prefixes():
246+
class _BrokenStringPrefix(str):
247+
def __str__(self) -> str:
248+
raise RuntimeError("cannot stringify string prefix")
249+
250+
operation_name = build_operation_name(
251+
_BrokenStringPrefix("crawl job "),
252+
"identifier",
253+
)
254+
255+
assert operation_name == "identifier"
256+
257+
258+
def test_build_fetch_operation_name_falls_back_for_broken_string_subclass_input():
259+
class _BrokenOperationName(str):
260+
def __str__(self) -> str:
261+
raise RuntimeError("cannot stringify fetch operation name")
262+
263+
operation_name = build_fetch_operation_name(_BrokenOperationName("crawl job"))
264+
265+
assert operation_name == "Fetching unknown"
266+
267+
232268
def test_build_operation_name_sanitizes_control_characters_in_prefix():
233269
operation_name = build_operation_name(
234270
"crawl\njob\t ",

0 commit comments

Comments
 (0)