forked from open-webui/open-webui
140 lines
2.4 KiB
Python
140 lines
2.4 KiB
Python
ALLOWED_MODULES = {
|
|
"pydantic",
|
|
"math",
|
|
"json",
|
|
"time",
|
|
"datetime",
|
|
"requests",
|
|
} # Add allowed modules here
|
|
|
|
|
|
def custom_import(name, globals=None, locals=None, fromlist=(), level=0):
|
|
if name in ALLOWED_MODULES:
|
|
return __import__(name, globals, locals, fromlist, level)
|
|
raise ImportError(f"Import of module {name} is not allowed")
|
|
|
|
|
|
# Define a restricted set of builtins
|
|
ALLOWED_BUILTINS = {
|
|
"ArithmeticError",
|
|
"AssertionError",
|
|
"AttributeError",
|
|
"BaseException",
|
|
"BufferError",
|
|
"BytesWarning",
|
|
"DeprecationWarning",
|
|
"EOFError",
|
|
"Ellipsis",
|
|
"EnvironmentError",
|
|
"Exception",
|
|
"False",
|
|
"FloatingPointError",
|
|
"FutureWarning",
|
|
"GeneratorExit",
|
|
"IOError",
|
|
"ImportError",
|
|
"ImportWarning",
|
|
"IndentationError",
|
|
"IndexError",
|
|
"KeyError",
|
|
"KeyboardInterrupt",
|
|
"LookupError",
|
|
"MemoryError",
|
|
"NameError",
|
|
"None",
|
|
"NotImplemented",
|
|
"NotImplementedError",
|
|
"OSError",
|
|
"OverflowError",
|
|
"PendingDeprecationWarning",
|
|
"ReferenceError",
|
|
"RuntimeError",
|
|
"RuntimeWarning",
|
|
"StopIteration",
|
|
"SyntaxError",
|
|
"SyntaxWarning",
|
|
"SystemError",
|
|
"SystemExit",
|
|
"TabError",
|
|
"True",
|
|
"TypeError",
|
|
"UnboundLocalError",
|
|
"UnicodeDecodeError",
|
|
"UnicodeEncodeError",
|
|
"UnicodeError",
|
|
"UnicodeTranslateError",
|
|
"UnicodeWarning",
|
|
"UserWarning",
|
|
"ValueError",
|
|
"Warning",
|
|
"ZeroDivisionError",
|
|
"__build_class__",
|
|
"__debug__",
|
|
"__import__",
|
|
"abs",
|
|
"all",
|
|
"any",
|
|
"ascii",
|
|
"bin",
|
|
"bool",
|
|
"bytearray",
|
|
"bytes",
|
|
"callable",
|
|
"chr",
|
|
"classmethod",
|
|
"compile",
|
|
"complex",
|
|
"delattr",
|
|
"dict",
|
|
"dir",
|
|
"divmod",
|
|
"enumerate",
|
|
"eval",
|
|
"exec",
|
|
"filter",
|
|
"float",
|
|
"format",
|
|
"frozenset",
|
|
"getattr",
|
|
"globals",
|
|
"hasattr",
|
|
"hash",
|
|
"hex",
|
|
"id",
|
|
"input",
|
|
"int",
|
|
"isinstance",
|
|
"issubclass",
|
|
"iter",
|
|
"len",
|
|
"list",
|
|
"locals",
|
|
"map",
|
|
"max",
|
|
"memoryview",
|
|
"min",
|
|
"next",
|
|
"object",
|
|
"oct",
|
|
"open",
|
|
"ord",
|
|
"pow",
|
|
"print",
|
|
"property",
|
|
"range",
|
|
"repr",
|
|
"reversed",
|
|
"round",
|
|
"set",
|
|
"setattr",
|
|
"slice",
|
|
"sorted",
|
|
"staticmethod",
|
|
"str",
|
|
"sum",
|
|
"super",
|
|
"tuple",
|
|
"type",
|
|
"vars",
|
|
"zip",
|
|
}
|