fix(interpreter security): functions from the builtins module must be explicitely added so as to prevent the dangerous ones from being indirectly available (compile, exec, eval, breakpoint, __import__, open, ...) (#299)

This commit is contained in:
tandiapa 2025-01-22 12:28:18 +01:00 committed by GitHub
parent 398c932250
commit 83ecd572fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 2 deletions

View File

@ -17,6 +17,7 @@
import ast
import builtins
import difflib
import inspect
import math
import re
from collections.abc import Mapping
@ -643,8 +644,14 @@ def evaluate_call(
# cap the number of lines
return None
else: # Assume it's a callable object
if (func in [eval, compile, exec]) and (func not in static_tools.values()):
raise InterpreterError(f"Invoking eval, compile or exec is not allowed ({func_name}).")
if (
(inspect.getmodule(func) == builtins)
and inspect.isbuiltin(func)
and (func not in static_tools.values())
):
raise InterpreterError(
f"Invoking a builtin function that has not been explicitly added as a tool is not allowed ({func_name})."
)
return func(*args, **kwargs)