fix: escape strings in sandbox (#2156)

Co-authored-by: Sarah Wooders <sarahwooders@gmail.com>
This commit is contained in:
Kevin Lin
2024-12-03 19:37:17 -08:00
committed by GitHub
parent d255b58661
commit 9ac7e9b5d2
2 changed files with 13 additions and 3 deletions

View File

@@ -287,13 +287,12 @@ class ToolExecutionSandbox:
f"{self.LOCAL_SANDBOX_RESULT_VAR_NAME} = base64.b64encode(pickle.dumps({self.LOCAL_SANDBOX_RESULT_VAR_NAME})).decode('utf-8')\n"
)
code += f"{self.LOCAL_SANDBOX_RESULT_VAR_NAME}\n"
return code
def _convert_param_to_value(self, param_type: str, raw_value: str) -> str:
if param_type == "string":
value = '"' + raw_value + '"'
value = "pickle.loads(" + str(pickle.dumps(raw_value)) + ")"
elif param_type == "integer" or param_type == "boolean" or param_type == "number":
value = raw_value
@@ -306,7 +305,6 @@ class ToolExecutionSandbox:
else:
raise TypeError(f"Unsupported type: {param_type}, raw_value={raw_value}")
return str(value)
def initialize_param(self, name: str, raw_value: str) -> str:

View File

@@ -388,6 +388,18 @@ def test_e2b_sandbox_core_memory_replace(check_e2b_key_is_set, core_memory_repla
assert result.func_return is None
@pytest.mark.e2b_sandbox
def test_e2b_sandbox_escape_strings_in_args(check_e2b_key_is_set, core_memory_replace_tool, test_user, agent_state):
new_name = "Matt"
args = {"label": "human", "old_content": "Chad", "new_content": new_name + "\n"}
sandbox = ToolExecutionSandbox(core_memory_replace_tool.name, args, user_id=test_user.id)
# run the sandbox
result = sandbox.run(agent_state=agent_state)
assert new_name in result.agent_state.memory.get_block("human").value
assert result.func_return is None
@pytest.mark.e2b_sandbox
def test_e2b_sandbox_core_memory_replace_errors(check_e2b_key_is_set, core_memory_replace_tool, test_user, agent_state):
nonexistent_name = "Alexander Wang"