Spaces:
Running
Running
Nils Durner
commited on
Commit
·
808df82
1
Parent(s):
2caa575
check actual script imports against list of allowed imports (fixing RCE reported against oai_chat)
Browse files- code_exec.py +7 -1
code_exec.py
CHANGED
@@ -52,7 +52,7 @@ def eval_restricted_script(script):
|
|
52 |
|
53 |
# Define allowed imports
|
54 |
'__allowed_modules__': ['math', 'datetime'],
|
55 |
-
'__import__':
|
56 |
|
57 |
# Basic functions
|
58 |
'len': len,
|
@@ -122,6 +122,12 @@ def _default_write_(obj):
|
|
122 |
|
123 |
return obj
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
"""
|
126 |
Borrowed implementation of _inplacevar_ from the Zope Foundations's AccessControl module
|
127 |
https://github.com/zopefoundation/AccessControl/blob/f9ae58816f0712eb6ea97459b4ccafbf4662d9db/src/AccessControl/ZopeGuards.py#L530
|
|
|
52 |
|
53 |
# Define allowed imports
|
54 |
'__allowed_modules__': ['math', 'datetime'],
|
55 |
+
'__import__': safe_import,
|
56 |
|
57 |
# Basic functions
|
58 |
'len': len,
|
|
|
122 |
|
123 |
return obj
|
124 |
|
125 |
+
def safe_import(name, globals=None, locals=None, fromlist=(), level=0):
|
126 |
+
allowed = ['math', 'datetime']
|
127 |
+
if name not in allowed:
|
128 |
+
raise ImportError(f"Import of module '{name}' is not allowed")
|
129 |
+
return __import__(name, globals, locals, fromlist, level)
|
130 |
+
|
131 |
"""
|
132 |
Borrowed implementation of _inplacevar_ from the Zope Foundations's AccessControl module
|
133 |
https://github.com/zopefoundation/AccessControl/blob/f9ae58816f0712eb6ea97459b4ccafbf4662d9db/src/AccessControl/ZopeGuards.py#L530
|