Skip to content

Commit 9362ef7

Browse files
Validate tool parameter key hygiene
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 2f93e65 commit 9362ef7

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

hyperbrowser/tools/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ def _to_param_dict(params: Mapping[str, Any]) -> Dict[str, Any]:
7272
) from exc
7373
for key in param_keys:
7474
if isinstance(key, str):
75+
if not key.strip():
76+
raise HyperbrowserError("tool params keys must not be empty")
77+
if key != key.strip():
78+
raise HyperbrowserError(
79+
"tool params keys must not contain leading or trailing whitespace"
80+
)
81+
if any(ord(character) < 32 or ord(character) == 127 for character in key):
82+
raise HyperbrowserError(
83+
"tool params keys must not contain control characters"
84+
)
7585
continue
7686
raise HyperbrowserError("tool params keys must be strings")
7787
normalized_params: Dict[str, Any] = {}

tests/test_tools_mapping_inputs.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,41 @@ def test_tool_wrappers_reject_non_string_param_keys():
108108
)
109109

110110

111+
def test_tool_wrappers_reject_blank_param_keys():
112+
client = _Client()
113+
114+
with pytest.raises(HyperbrowserError, match="tool params keys must not be empty"):
115+
WebsiteScrapeTool.runnable(
116+
client,
117+
{" ": "https://example.com"},
118+
)
119+
120+
121+
def test_tool_wrappers_reject_param_keys_with_surrounding_whitespace():
122+
client = _Client()
123+
124+
with pytest.raises(
125+
HyperbrowserError,
126+
match="tool params keys must not contain leading or trailing whitespace",
127+
):
128+
WebsiteScrapeTool.runnable(
129+
client,
130+
{" url ": "https://example.com"},
131+
)
132+
133+
134+
def test_tool_wrappers_reject_param_keys_with_control_characters():
135+
client = _Client()
136+
137+
with pytest.raises(
138+
HyperbrowserError, match="tool params keys must not contain control characters"
139+
):
140+
WebsiteScrapeTool.runnable(
141+
client,
142+
{"u\trl": "https://example.com"},
143+
)
144+
145+
111146
def test_tool_wrappers_wrap_param_key_read_failures():
112147
class _BrokenKeyMapping(Mapping[str, object]):
113148
def __iter__(self) -> Iterator[str]:

0 commit comments

Comments
 (0)