File tree 2 files changed +29
-10
lines changed
2 files changed +29
-10
lines changed Original file line number Diff line number Diff line change @@ -49,11 +49,8 @@ def execute_oc_tool_calls(
49
49
logger .error (tool_output )
50
50
else :
51
51
try :
52
- # inject token into tool args and immediately remove it
53
- # to avoid leaking
54
- tool_args ["token" ] = token
55
- tool_output = tool .invoke (tool_args )
56
- del tool_args ["token" ]
52
+ # create a new dict with the tool args and the token
53
+ tool_output = tool .invoke ({** tool_args , "token" : token })
57
54
except ValidationError :
58
55
tool_output = (
59
56
f"Error executing { tool_name } : tool arguments are in wrong format"
@@ -64,11 +61,6 @@ def execute_oc_tool_calls(
64
61
except Exception as e :
65
62
tool_output = f"Error executing { tool_name } : { e } "
66
63
logger .exception (tool_output )
67
- finally :
68
- # remove token from tool args if it was not removed
69
- # in the try block
70
- if "token" in tool_args :
71
- del tool_args ["token" ]
72
64
73
65
logger .debug (
74
66
"Tool: %s | Args: %s | Output: %s" , tool_name , tool_args , tool_output
Original file line number Diff line number Diff line change @@ -69,3 +69,30 @@ def tool1(some_arg: str):
69
69
in caplog .text
70
70
)
71
71
assert "fake-token" not in caplog .text
72
+
73
+
74
+ def test_execute_oc_tool_calls_not_leaks_token_into_output (caplog ):
75
+ """Test execute_oc_tool_calls does not leak token into output."""
76
+ caplog .set_level (10 ) # set debug level
77
+
78
+ @tool
79
+ def tool1 (some_args : list ):
80
+ """Tool 1."""
81
+ return "bla"
82
+
83
+ tools_map = {"tool1" : tool1 }
84
+
85
+ # missing args
86
+ tool_calls = [{"id" : 1 , "name" : "tool1" }]
87
+ tool_messages = execute_oc_tool_calls (tools_map , tool_calls , "fake-token" )
88
+ assert len (tool_messages ) == 1
89
+ assert "fake-token" not in tool_messages [0 ].content
90
+
91
+ # unknown args
92
+ tool_calls = [{"id" : 1 , "name" : "tool1" , "args" : {"unknown_args" : "blo" }}]
93
+ tool_messages = execute_oc_tool_calls (tools_map , tool_calls , "fake-token" )
94
+ assert len (tool_messages ) == 1
95
+ assert "fake-token" not in tool_messages [0 ].content
96
+
97
+ # ensure the token is also not in the logs
98
+ assert "fake-token" not in caplog .text
You can’t perform that action at this time.
0 commit comments