Skip to content

Commit 0889737

Browse files
Sanitize control characters in transport fallback errors
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent c9318d8 commit 0889737

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

hyperbrowser/transport/error_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ def _safe_to_string(value: Any) -> str:
5050
normalized_value = str(value)
5151
except Exception:
5252
return f"<unstringifiable {type(value).__name__}>"
53-
if normalized_value.strip():
54-
return normalized_value
53+
sanitized_value = "".join(
54+
"?" if ord(character) < 32 or ord(character) == 127 else character
55+
for character in normalized_value
56+
)
57+
if sanitized_value.strip():
58+
return sanitized_value
5559
return f"<{type(value).__name__}>"
5660

5761

tests/test_transport_error_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ def __str__(self) -> str:
158158
return " "
159159

160160

161+
class _ControlFallbackError(Exception):
162+
def __str__(self) -> str:
163+
return "bad\tfallback\ntext"
164+
165+
161166
class _BrokenFallbackResponse:
162167
@property
163168
def text(self) -> str:
@@ -742,6 +747,14 @@ def test_extract_error_message_uses_placeholder_for_blank_fallback_error_text():
742747
assert message == "<_BlankFallbackError>"
743748

744749

750+
def test_extract_error_message_sanitizes_control_characters_in_fallback_error_text():
751+
message = extract_error_message(
752+
_DummyResponse(" ", text=" "), _ControlFallbackError()
753+
)
754+
755+
assert message == "bad?fallback?text"
756+
757+
745758
def test_extract_error_message_extracts_errors_list_messages():
746759
message = extract_error_message(
747760
_DummyResponse({"errors": [{"msg": "first issue"}, {"msg": "second issue"}]}),

0 commit comments

Comments
 (0)