From ff64adf92ec4c1c3ac628690347f77bf49daaff0 Mon Sep 17 00:00:00 2001 From: Eliot Robson Date: Mon, 9 Nov 2026 11:44:06 -0500 Subject: [PATCH 1/2] Make import_blacklist block submodules of blacklisted modules The blacklist only matched the exact name passed to __import__, so `import os.path` got past the default blacklist and bound the whole os module. A blacklisted entry now also covers its submodules, including ones named in a from-import, so "matplotlib.pyplot" blocks `from matplotlib import pyplot` while `import matplotlib` still works. Co-Authored-By: Claude Opus 5.5 --- quick_start.md | 4 ++ .../_student_code_runner.py | 19 +++++++- src/pytest_prairielearn_grader/config.py | 3 +- .../test_import_blacklist_default/__init__.py | 0 .../expected_outcome.json | 43 +++++++++++++++++++ .../test_import_blacklist_default/scenario.py | 18 ++++++++ .../student_code.py | 22 ++++++++++ .../__init__.py | 0 .../expected_outcome.json | 43 +++++++++++++++++++ .../scenario.py | 25 +++++++++++ .../student_code.py | 22 ++++++++++ tests/test_import_blacklist.py | 27 ++++++++++++ 12 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 tests/scenario_root/test_import_blacklist_default/__init__.py create mode 100644 tests/scenario_root/test_import_blacklist_default/expected_outcome.json create mode 100644 tests/scenario_root/test_import_blacklist_default/scenario.py create mode 100644 tests/scenario_root/test_import_blacklist_default/student_code.py create mode 100644 tests/scenario_root/test_import_blacklist_submodules/__init__.py create mode 100644 tests/scenario_root/test_import_blacklist_submodules/expected_outcome.json create mode 100644 tests/scenario_root/test_import_blacklist_submodules/scenario.py create mode 100644 tests/scenario_root/test_import_blacklist_submodules/student_code.py create mode 100644 tests/test_import_blacklist.py diff --git a/quick_start.md b/quick_start.md index 594cbee..0ef21b2 100644 --- a/quick_start.md +++ b/quick_start.md @@ -516,6 +516,10 @@ def my_function(): **Note**: Blacklist is checked first, then whitelist. If a whitelist is specified, only those modules (minus any in blacklist) can be imported. If only a blacklist is specified, those modules are blocked but all others are allowed. +A blacklisted module also blocks its submodules, however they're imported. With `"os"` in the blacklist, `import os.path` is blocked as well as `import os`. With `"matplotlib.pyplot"`, both `import matplotlib.pyplot` and `from matplotlib import pyplot` are blocked, but `import matplotlib` still works. + +The blacklist only checks import statements, so it isn't a security boundary. For example, modules that student code is allowed to import can expose `os` as an attribute. + #### Builtin Function Control Python's builtin functions (like `open()`, `eval()`, `exec()`) are automatically restricted to a safe subset. The autograder provides **only safe builtins by default**, including: diff --git a/src/pytest_prairielearn_grader/_student_code_runner.py b/src/pytest_prairielearn_grader/_student_code_runner.py index 77b34ea..354290b 100644 --- a/src/pytest_prairielearn_grader/_student_code_runner.py +++ b/src/pytest_prairielearn_grader/_student_code_runner.py @@ -161,6 +161,21 @@ def student_function_temp() -> Any: return json.dumps(not_serializable_response) +def _find_blacklisted(name: str, fromlist: Sequence[str], import_blacklist: list[str]) -> str | None: + """ + Returns the module that an import would load in spite of the blacklist, or None. + + A blacklisted entry also covers its submodules, so "os" blocks `import os.path`, and + "matplotlib.pyplot" blocks `from matplotlib import pyplot` but not `import matplotlib`. + """ + names = [name, *(f"{name}.{item}" for item in fromlist if item != "*")] + for candidate in names: + for entry in import_blacklist: + if candidate == entry or candidate.startswith(entry + "."): + return candidate + return None + + def get_custom_importer( import_whitelist: list[str] | None, import_blacklist: list[str] | None, @@ -207,8 +222,8 @@ def custom_import( return original_import(name, globals, locals, fl, level) # Apply blacklist / whitelist to external imports. - if import_blacklist is not None and name in import_blacklist: - raise ImportError(f"Module '{name}' is blacklisted and cannot be imported.") + if import_blacklist is not None and (blocked := _find_blacklisted(name, fl, import_blacklist)) is not None: + raise ImportError(f"Module '{blocked}' is blacklisted and cannot be imported.") elif ( (import_whitelist is not None and name in import_whitelist) or name.startswith("_") # Allow Python internal/private modules (_io, _abc, _collections, etc.) diff --git a/src/pytest_prairielearn_grader/config.py b/src/pytest_prairielearn_grader/config.py index f79ffe6..b15b888 100644 --- a/src/pytest_prairielearn_grader/config.py +++ b/src/pytest_prairielearn_grader/config.py @@ -57,7 +57,8 @@ def test_student_code(sandbox): import_blacklist: list[str] | None = None """List of Python modules that student code is prohibited from importing. - Modules in the blacklist are always blocked, even if in the whitelist. + Modules in the blacklist are always blocked, even if in the whitelist, and so are their + submodules: "os" also blocks `import os.path`. Can be combined with import_whitelist for fine-grained control. Default blocks dangerous system operations: ["os", "sys", "subprocess", "pathlib", "shutil"]. diff --git a/tests/scenario_root/test_import_blacklist_default/__init__.py b/tests/scenario_root/test_import_blacklist_default/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/scenario_root/test_import_blacklist_default/expected_outcome.json b/tests/scenario_root/test_import_blacklist_default/expected_outcome.json new file mode 100644 index 0000000..23a3f3d --- /dev/null +++ b/tests/scenario_root/test_import_blacklist_default/expected_outcome.json @@ -0,0 +1,43 @@ +{ + "expected_data_object": { + "score": 1.0, + "tests": [ + { + "max_points": 1, + "message": "", + "name": "blocked", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_import_blacklist_default.py::test_blocked[student_code-import_os]" + }, + { + "max_points": 1, + "message": "", + "name": "blocked", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_import_blacklist_default.py::test_blocked[student_code-import_os_path]" + }, + { + "max_points": 1, + "message": "", + "name": "blocked", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_import_blacklist_default.py::test_blocked[student_code-from_os_import_path]" + }, + { + "max_points": 1, + "message": "", + "name": "allowed", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_import_blacklist_default.py::test_allowed[student_code]" + } + ] + } +} diff --git a/tests/scenario_root/test_import_blacklist_default/scenario.py b/tests/scenario_root/test_import_blacklist_default/scenario.py new file mode 100644 index 0000000..1d870bb --- /dev/null +++ b/tests/scenario_root/test_import_blacklist_default/scenario.py @@ -0,0 +1,18 @@ +"""The default blacklist also blocks submodules, such as os.path.""" + +import pytest + +from pytest_prairielearn_grader.fixture import StudentFixture + + +@pytest.mark.grading_data(name="blocked", points=1) +@pytest.mark.parametrize("function_name", ["import_os", "import_os_path", "from_os_import_path"]) +def test_blocked(sandbox: StudentFixture, function_name: str) -> None: + response = sandbox.query_function_raw(function_name) + assert response["exception_name"] == "ImportError" + assert "is blacklisted" in str(response["exception_message"]) + + +@pytest.mark.grading_data(name="allowed", points=1) +def test_allowed(sandbox: StudentFixture) -> None: + assert sandbox.query_function("import_json") == "1" diff --git a/tests/scenario_root/test_import_blacklist_default/student_code.py b/tests/scenario_root/test_import_blacklist_default/student_code.py new file mode 100644 index 0000000..f1ce1d5 --- /dev/null +++ b/tests/scenario_root/test_import_blacklist_default/student_code.py @@ -0,0 +1,22 @@ +def import_os(): + import os + + return os.name + + +def import_os_path(): + import os.path + + return os.getcwd() + + +def from_os_import_path(): + from os import path + + return path.sep + + +def import_json(): + import json + + return json.dumps(1) diff --git a/tests/scenario_root/test_import_blacklist_submodules/__init__.py b/tests/scenario_root/test_import_blacklist_submodules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/scenario_root/test_import_blacklist_submodules/expected_outcome.json b/tests/scenario_root/test_import_blacklist_submodules/expected_outcome.json new file mode 100644 index 0000000..6225004 --- /dev/null +++ b/tests/scenario_root/test_import_blacklist_submodules/expected_outcome.json @@ -0,0 +1,43 @@ +{ + "expected_data_object": { + "score": 1.0, + "tests": [ + { + "max_points": 1, + "message": "", + "name": "blocked", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_import_blacklist_submodules.py::test_blocked[student_code-import_pyplot]" + }, + { + "max_points": 1, + "message": "", + "name": "blocked", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_import_blacklist_submodules.py::test_blocked[student_code-from_matplotlib_import_pyplot]" + }, + { + "max_points": 1, + "message": "", + "name": "allowed", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_import_blacklist_submodules.py::test_allowed[student_code-import_matplotlib-matplotlib]" + }, + { + "max_points": 1, + "message": "", + "name": "allowed", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_import_blacklist_submodules.py::test_allowed[student_code-from_matplotlib_import_colors-matplotlib.colors]" + } + ] + } +} diff --git a/tests/scenario_root/test_import_blacklist_submodules/scenario.py b/tests/scenario_root/test_import_blacklist_submodules/scenario.py new file mode 100644 index 0000000..9595f7d --- /dev/null +++ b/tests/scenario_root/test_import_blacklist_submodules/scenario.py @@ -0,0 +1,25 @@ +"""A blacklisted submodule is blocked however it's imported, and its parent package isn't.""" + +import pytest + +from pytest_prairielearn_grader import ConfigObject +from pytest_prairielearn_grader.fixture import StudentFixture + +autograder_config = ConfigObject(import_blacklist=["matplotlib.pyplot"], sandbox_timeout=30) + + +@pytest.mark.grading_data(name="blocked", points=1) +@pytest.mark.parametrize("function_name", ["import_pyplot", "from_matplotlib_import_pyplot"]) +def test_blocked(sandbox: StudentFixture, function_name: str) -> None: + response = sandbox.query_function_raw(function_name, query_timeout=30) + assert response["exception_name"] == "ImportError" + assert "Module 'matplotlib.pyplot' is blacklisted" in str(response["exception_message"]) + + +@pytest.mark.grading_data(name="allowed", points=1) +@pytest.mark.parametrize( + ("function_name", "expected"), + [("import_matplotlib", "matplotlib"), ("from_matplotlib_import_colors", "matplotlib.colors")], +) +def test_allowed(sandbox: StudentFixture, function_name: str, expected: str) -> None: + assert sandbox.query_function(function_name, query_timeout=30) == expected diff --git a/tests/scenario_root/test_import_blacklist_submodules/student_code.py b/tests/scenario_root/test_import_blacklist_submodules/student_code.py new file mode 100644 index 0000000..d367674 --- /dev/null +++ b/tests/scenario_root/test_import_blacklist_submodules/student_code.py @@ -0,0 +1,22 @@ +def import_matplotlib(): + import matplotlib + + return matplotlib.__name__ + + +def import_pyplot(): + import matplotlib.pyplot + + return matplotlib.pyplot.__name__ + + +def from_matplotlib_import_pyplot(): + from matplotlib import pyplot + + return pyplot.__name__ + + +def from_matplotlib_import_colors(): + from matplotlib import colors + + return colors.__name__ diff --git a/tests/test_import_blacklist.py b/tests/test_import_blacklist.py new file mode 100644 index 0000000..2232db7 --- /dev/null +++ b/tests/test_import_blacklist.py @@ -0,0 +1,27 @@ +"""How import_blacklist entries match the module an import would load.""" + +import pytest + +from pytest_prairielearn_grader._student_code_runner import _find_blacklisted + + +@pytest.mark.parametrize( + ("name", "fromlist", "blacklist", "expected"), + [ + ("os", (), ["os"], "os"), + ("os.path", (), ["os"], "os.path"), + ("os", ("path",), ["os"], "os"), + ("os", ("*",), ["os"], "os"), + ("osx_helpers", (), ["os"], None), + ("json", (), ["os"], None), + ("matplotlib", (), ["matplotlib.pyplot"], None), + ("matplotlib.pyplot", (), ["matplotlib.pyplot"], "matplotlib.pyplot"), + ("matplotlib", ("pyplot",), ["matplotlib.pyplot"], "matplotlib.pyplot"), + ("matplotlib", ("colors", "pyplot"), ["matplotlib.pyplot"], "matplotlib.pyplot"), + ("matplotlib", ("colors",), ["matplotlib.pyplot"], None), + ("matplotlib", ("*",), ["matplotlib.pyplot"], None), + ("matplotlib.pyplot.figure", (), ["matplotlib.pyplot"], "matplotlib.pyplot.figure"), + ], +) +def test_find_blacklisted(name: str, fromlist: tuple[str, ...], blacklist: list[str], expected: str | None) -> None: + assert _find_blacklisted(name, fromlist, blacklist) == expected From da0cab1c5699589105cb3fdc64a48de89355faff Mon Sep 17 00:00:00 2001 From: Eliot Robson Date: Mon, 9 Nov 2026 11:54:11 -0500 Subject: [PATCH 2/2] Let libraries import what they need in workspace mode In workspace mode the importer replaces builtins.__import__ for the whole process, so the whitelist and blacklist also applied to imports made inside libraries. Under the default blacklist, numpy, matplotlib and pandas couldn't be imported because their own `import os` was blocked. Imports made by a module file outside the workspace now go through unrestricted, judged by the calling frame because pandas calls __import__ without globals. Student modules, setup code and the entry point are still restricted. Co-Authored-By: Claude Opus 5.5 --- quick_start.md | 6 ++- .../_student_code_runner.py | 14 +++++- .../__init__.py | 0 .../expected_outcome.json | 43 +++++++++++++++++++ .../scenario.py | 24 +++++++++++ .../student/analysis.py | 11 +++++ .../student/uses_os.py | 14 ++++++ .../__init__.py | 0 .../expected_outcome.json | 25 +++++++++++ .../scenario.py | 21 +++++++++ .../student/tables.py | 10 +++++ 11 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 tests/scenario_root/test_workspace_library_imports/__init__.py create mode 100644 tests/scenario_root/test_workspace_library_imports/expected_outcome.json create mode 100644 tests/scenario_root/test_workspace_library_imports/scenario.py create mode 100644 tests/scenario_root/test_workspace_library_imports/student/analysis.py create mode 100644 tests/scenario_root/test_workspace_library_imports/student/uses_os.py create mode 100644 tests/scenario_root/test_workspace_library_imports_whitelist/__init__.py create mode 100644 tests/scenario_root/test_workspace_library_imports_whitelist/expected_outcome.json create mode 100644 tests/scenario_root/test_workspace_library_imports_whitelist/scenario.py create mode 100644 tests/scenario_root/test_workspace_library_imports_whitelist/student/tables.py diff --git a/quick_start.md b/quick_start.md index 0ef21b2..e0d4b44 100644 --- a/quick_start.md +++ b/quick_start.md @@ -1510,7 +1510,11 @@ autograder_config = ConfigObject( - The default `import_blacklist` (`["os", "sys", "subprocess", "pathlib", "shutil"]`) is applied in workspace mode too. If the student's project legitimately uses `pathlib` for - internal file operations, add it to `import_whitelist` explicitly. + internal file operations, set `import_blacklist` to a list without it. Adding it to + `import_whitelist` doesn't help, because the blacklist is checked first. +- `import_whitelist` and `import_blacklist` apply to imports in the student's project, setup code + and the entry point. They don't apply to imports made inside library code, so an allowed library + such as `numpy` or `pandas` can still load `os` and its other dependencies. - `sys.path` inside the subprocess is modified to include `workspace_student_dir`, so all relative imports within the project work naturally. - Each call to `workspace_sandbox.query_function` runs in the same subprocess, so module-level diff --git a/src/pytest_prairielearn_grader/_student_code_runner.py b/src/pytest_prairielearn_grader/_student_code_runner.py index 354290b..67e910b 100644 --- a/src/pytest_prairielearn_grader/_student_code_runner.py +++ b/src/pytest_prairielearn_grader/_student_code_runner.py @@ -188,7 +188,8 @@ def get_custom_importer( When *workspace_dir* is given, any module whose top-level package name resolves to a file or directory inside that directory is unconditionally allowed, so that student modules can import each other without instructors needing to maintain a manual whitelist of their own files. - Relative imports (level > 0) are always permitted for the same reason. + Relative imports (level > 0) are always permitted for the same reason. Imports made by library + modules outside that directory aren't restricted, so an allowed library can load what it needs. """ original_import = _REAL_IMPORT @@ -213,6 +214,17 @@ def custom_import( if level > 0: return original_import(name, globals, locals, fl, level) + # In workspace mode this importer replaces builtins.__import__ for the whole process, so it + # also sees imports made inside libraries, such as numpy's own `import os`. The rules are + # for student code, so let through imports made by a module file outside the workspace. + # Code with no module file, such as setup code and the entry point, is still restricted. + # The caller's frame is used rather than the globals argument, because some libraries call + # __import__ directly without one (pandas does for its dependencies). + if _ws_str is not None: + importer_file = sys._getframe(1).f_globals.get("__file__") + if isinstance(importer_file, str) and not importer_file.startswith(_ws_str + os.sep): + return original_import(name, globals, locals, fl, level) + # Workspace-local modules are always importable regardless of whitelist/blacklist. # A module is considered local if its top-level package maps to a file or directory # directly inside workspace_dir. diff --git a/tests/scenario_root/test_workspace_library_imports/__init__.py b/tests/scenario_root/test_workspace_library_imports/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/scenario_root/test_workspace_library_imports/expected_outcome.json b/tests/scenario_root/test_workspace_library_imports/expected_outcome.json new file mode 100644 index 0000000..fbdb65a --- /dev/null +++ b/tests/scenario_root/test_workspace_library_imports/expected_outcome.json @@ -0,0 +1,43 @@ +{ + "expected_data_object": { + "score": 1.0, + "tests": [ + { + "max_points": 1, + "message": "", + "name": "libraries", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_workspace_library_imports.py::test_libraries" + }, + { + "max_points": 1, + "message": "", + "name": "student imports", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_workspace_library_imports.py::test_student_imports[import_os]" + }, + { + "max_points": 1, + "message": "", + "name": "student imports", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_workspace_library_imports.py::test_student_imports[import_os_path]" + }, + { + "max_points": 1, + "message": "", + "name": "student imports", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_workspace_library_imports.py::test_student_imports[dunder_import_os]" + } + ] + } +} diff --git a/tests/scenario_root/test_workspace_library_imports/scenario.py b/tests/scenario_root/test_workspace_library_imports/scenario.py new file mode 100644 index 0000000..915acb5 --- /dev/null +++ b/tests/scenario_root/test_workspace_library_imports/scenario.py @@ -0,0 +1,24 @@ +"""In workspace mode, libraries can load what they need, and student code is still restricted.""" + +import pytest + +from pytest_prairielearn_grader import ConfigObject +from pytest_prairielearn_grader.fixture import WorkspaceFixture + +# The default blacklist applies, and numpy, matplotlib and statistics all import os or sys. +autograder_config = ConfigObject(workspace_mode=True, sandbox_timeout=60) + + +@pytest.mark.grading_data(name="libraries", points=1) +def test_libraries(workspace_sandbox: WorkspaceFixture) -> None: + assert workspace_sandbox.query("analysis.NORM", query_timeout=60) == 5.0 + assert workspace_sandbox.query("analysis.MEAN") == 2 + assert workspace_sandbox.query("analysis.LINES") == 1 + + +@pytest.mark.grading_data(name="student imports", points=1) +@pytest.mark.parametrize("function_name", ["import_os", "import_os_path", "dunder_import_os"]) +def test_student_imports(workspace_sandbox: WorkspaceFixture, function_name: str) -> None: + response = workspace_sandbox.query_function_raw(f"uses_os.{function_name}") + assert response["exception_name"] == "ImportError" + assert "is blacklisted" in str(response["exception_message"]) diff --git a/tests/scenario_root/test_workspace_library_imports/student/analysis.py b/tests/scenario_root/test_workspace_library_imports/student/analysis.py new file mode 100644 index 0000000..db379a8 --- /dev/null +++ b/tests/scenario_root/test_workspace_library_imports/student/analysis.py @@ -0,0 +1,11 @@ +import statistics + +import matplotlib +import numpy as np + +matplotlib.use("Agg") +import matplotlib.pyplot as plt + +NORM = float(np.linalg.norm([3, 4])) +MEAN = statistics.mean([1, 2, 3]) +LINES = len(plt.plot([1, 2])) diff --git a/tests/scenario_root/test_workspace_library_imports/student/uses_os.py b/tests/scenario_root/test_workspace_library_imports/student/uses_os.py new file mode 100644 index 0000000..c91f0a4 --- /dev/null +++ b/tests/scenario_root/test_workspace_library_imports/student/uses_os.py @@ -0,0 +1,14 @@ +def import_os(): + import os + + return os.name + + +def import_os_path(): + import os.path + + return os.name + + +def dunder_import_os(): + return __import__("os").name diff --git a/tests/scenario_root/test_workspace_library_imports_whitelist/__init__.py b/tests/scenario_root/test_workspace_library_imports_whitelist/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/scenario_root/test_workspace_library_imports_whitelist/expected_outcome.json b/tests/scenario_root/test_workspace_library_imports_whitelist/expected_outcome.json new file mode 100644 index 0000000..12b2bfa --- /dev/null +++ b/tests/scenario_root/test_workspace_library_imports_whitelist/expected_outcome.json @@ -0,0 +1,25 @@ +{ + "expected_data_object": { + "score": 1.0, + "tests": [ + { + "max_points": 1, + "message": "", + "name": "whitelisted libraries", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_workspace_library_imports_whitelist.py::test_whitelisted_libraries" + }, + { + "max_points": 1, + "message": "", + "name": "other imports", + "outcome": "passed", + "points": 1.0, + "points_frac": 1.0, + "test_id": "test_workspace_library_imports_whitelist.py::test_other_imports" + } + ] + } +} diff --git a/tests/scenario_root/test_workspace_library_imports_whitelist/scenario.py b/tests/scenario_root/test_workspace_library_imports_whitelist/scenario.py new file mode 100644 index 0000000..bbeaf83 --- /dev/null +++ b/tests/scenario_root/test_workspace_library_imports_whitelist/scenario.py @@ -0,0 +1,21 @@ +"""With a whitelist, allowed libraries can load their own dependencies, and student code can't.""" + +import pytest + +from pytest_prairielearn_grader import ConfigObject +from pytest_prairielearn_grader.fixture import WorkspaceFixture + +# pandas loads dependencies such as dateutil with direct __import__ calls. +autograder_config = ConfigObject(workspace_mode=True, sandbox_timeout=60, import_whitelist=["numpy", "pandas"]) + + +@pytest.mark.grading_data(name="whitelisted libraries", points=1) +def test_whitelisted_libraries(workspace_sandbox: WorkspaceFixture) -> None: + assert workspace_sandbox.query("tables.TOTAL", query_timeout=60) == 6 + + +@pytest.mark.grading_data(name="other imports", points=1) +def test_other_imports(workspace_sandbox: WorkspaceFixture) -> None: + response = workspace_sandbox.query_function_raw("tables.import_statistics") + assert response["exception_name"] == "ImportError" + assert "Module 'statistics' is not allowed to be imported." in str(response["exception_message"]) diff --git a/tests/scenario_root/test_workspace_library_imports_whitelist/student/tables.py b/tests/scenario_root/test_workspace_library_imports_whitelist/student/tables.py new file mode 100644 index 0000000..b33216c --- /dev/null +++ b/tests/scenario_root/test_workspace_library_imports_whitelist/student/tables.py @@ -0,0 +1,10 @@ +import numpy as np +import pandas as pd + +TOTAL = int(pd.DataFrame({"a": np.array([1, 2, 3])})["a"].sum()) + + +def import_statistics(): + import statistics + + return statistics.mean([1, 2])