Skip to content

Commit 56d7fc8

Browse files
committed
Restrict certain modules in code interpreter
1 parent e640330 commit 56d7fc8

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

llmstack/common/runner/server.py

+14
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,27 @@ def custom_pyplot_show():
297297
buf.close()
298298

299299
def custom_import(name, globals=None, locals=None, fromlist=(), level=0):
300+
unsafe_modules = [
301+
"os",
302+
"sys",
303+
"subprocess",
304+
"shutil",
305+
"importlib",
306+
"imp",
307+
"importlib",
308+
"importlib.util",
309+
"socket",
310+
]
300311
module = __import__(name, globals, locals, fromlist, level)
301312
if module.__name__ == "matplotlib":
302313
pyplot_attr = getattr(module, "pyplot")
303314
if pyplot_attr and hasattr(pyplot_attr, "show"):
304315
# Override pyplot.show() to route to our custom show function
305316
pyplot_attr.show = custom_pyplot_show
306317

318+
elif module.__name__ in unsafe_modules:
319+
raise Exception("Module {} is not allowed".format(module.__name__))
320+
307321
if fromlist:
308322
safe_attrs = {attr: getattr(module, attr) for attr in fromlist}
309323
if len(safe_attrs):

0 commit comments

Comments
 (0)