-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Allow builtin functions when "*" in authorized imports #1832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| func = custom_tools[func_name] | ||
| elif func_name in ERRORS: | ||
| func = ERRORS[func_name] | ||
| elif "*" in authorized_imports and hasattr(builtins, func_name): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly, open will work while other builtin functions like exec will get caught at
| if (inspect.getmodule(func) == builtins) and inspect.isbuiltin(func) and (func not in static_tools.values()): |
With
smolagents.local_python_executor.InterpreterError: Code execution failed at line 'exec(code_string)' due to: InterpreterError: Invoking a builtin function that has not been explicitly added as a tool is not allowed (exec).
Because, open belongs to the io module
>>> import inspect
>>> inspect.getmodule(open)
<module '_io' (built-in)>
>>> inspect.getmodule(exec)
<module 'builtins' (built-in)>
I'm not a python security expert. Should we allow eval/exec if the user says it is ok (By passing in *)?
|
cc: @albertvillanova / @aymeric-roucher Please take a look when free |
| return None | ||
| else: # Assume it's a callable object | ||
| if (inspect.getmodule(func) == builtins) and inspect.isbuiltin(func) and (func not in static_tools.values()): | ||
| if ("*" not in authorized_imports) and inspect.isbuiltin(func) and (func not in static_tools.values()): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new addition allows eval etc to work when '*' is passed
|
Added a unit test |
Fix for #1830
Even if the user passes "*" in the authorized imports (Unrestricted mode), builtin functions aren't allowed
I think it is fair for the user to assume that builtin functions will be available when they use
*Repro example
Code to reproduce the issue
Error
After change
Use case from GH Issue
additional_authorized_imports=["*"].Side note: The LLM retries with
pandas.openand that passes