rsnarsna
feat(sandbox): implement sandboxed safe import resolver and internal submodule allowlist (DEC-041)
023beca Download tests/test_library_internal_modules.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 4.46 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/tests/test_library_internal_modules.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/tests/test_library_internal_modules.py
-
curl -L -o test_library_internal_modules.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/tests/test_library_internal_modules.py
4.46 kB
| """ | |
| Test Suite for ALLI Library & Internal Submodule Usability. | |
| Verifies safe imports (e.g. urllib.parse, collections, datetime, itertools) | |
| and enforces strict rejection of restricted host modules (os, subprocess, urllib.request). | |
| """ | |
| import sys | |
| import unittest | |
| from pathlib import Path | |
| backend_dir = Path(__file__).resolve().parent.parent | |
| if str(backend_dir) not in sys.path: | |
| sys.path.insert(0, str(backend_dir)) | |
| from language_core.executor.custom_script_executor import ( | |
| WorkflowExecutionError, | |
| execute_custom_script_node, | |
| ) | |
| from language_core.executor.safe_import_resolver import is_module_safe, safe_import | |
| class TestLibraryInternalModules(unittest.TestCase): | |
| def test_standard_math_import(self): | |
| code = "import math\nresult = math.sqrt(64)" | |
| out = execute_custom_script_node({}, {"code": code}, "node_math") | |
| self.assertEqual(out["result"], 8.0) | |
| def test_collections_submodule_import(self): | |
| code = """ | |
| from collections import defaultdict, Counter | |
| d = defaultdict(int) | |
| d['alpha'] += 5 | |
| c = Counter(['x', 'y', 'x']) | |
| result = {'count': d['alpha'], 'x_freq': c['x']} | |
| """ | |
| out = execute_custom_script_node({}, {"code": code}, "node_collections") | |
| self.assertEqual(out["count"], 5) | |
| self.assertEqual(out["x_freq"], 2) | |
| def test_urllib_parse_submodule_import(self): | |
| code = """ | |
| import urllib.parse | |
| raw_query = "search=ai visual programming&lang=en" | |
| parsed = urllib.parse.parse_qs(raw_query) | |
| result = {"search": parsed["search"][0], "lang": parsed["lang"][0]} | |
| """ | |
| out = execute_custom_script_node({}, {"code": code}, "node_urllib_parse") | |
| self.assertEqual(out["search"], "ai visual programming") | |
| self.assertEqual(out["lang"], "en") | |
| def test_datetime_timezone_and_delta(self): | |
| code = """ | |
| from datetime import datetime, timezone, timedelta | |
| t0 = datetime(2026, 1, 1, 12, 0, tzinfo=timezone.utc) | |
| t1 = t0 + timedelta(days=5) | |
| result = t1.day | |
| """ | |
| out = execute_custom_script_node({}, {"code": code}, "node_datetime") | |
| self.assertEqual(out["result"], 6) | |
| def test_itertools_and_functools(self): | |
| code = """ | |
| import itertools | |
| import functools | |
| combos = list(itertools.combinations([1, 2, 3], 2)) | |
| product = functools.reduce(lambda acc, x: acc * x, [2, 3, 4], 1) | |
| result = {"combos_len": len(combos), "product": product} | |
| """ | |
| out = execute_custom_script_node({}, {"code": code}, "node_iter_functools") | |
| self.assertEqual(out["combos_len"], 3) | |
| self.assertEqual(out["product"], 24) | |
| def test_dangerous_os_import_blocked(self): | |
| code = "import os\nresult = os.listdir('.')" | |
| with self.assertRaises(WorkflowExecutionError) as ctx: | |
| execute_custom_script_node({}, {"code": code}, "node_os") | |
| self.assertIn("restricted", str(ctx.exception).lower()) | |
| def test_dangerous_subprocess_import_blocked(self): | |
| code = "from subprocess import Popen\nresult = Popen(['ls'])" | |
| with self.assertRaises(WorkflowExecutionError) as ctx: | |
| execute_custom_script_node({}, {"code": code}, "node_subp") | |
| self.assertIn("restricted", str(ctx.exception).lower()) | |
| def test_urllib_request_network_escape_blocked(self): | |
| code = "import urllib.request\nresult = urllib.request.urlopen('http://evil.com')" | |
| with self.assertRaises(WorkflowExecutionError) as ctx: | |
| execute_custom_script_node({}, {"code": code}, "node_urllib_req") | |
| self.assertIn("restricted", str(ctx.exception).lower()) | |
| def test_socket_network_blocked(self): | |
| code = "import socket\ns = socket.socket()" | |
| with self.assertRaises(WorkflowExecutionError) as ctx: | |
| execute_custom_script_node({}, {"code": code}, "node_socket") | |
| self.assertIn("restricted", str(ctx.exception).lower()) | |
| def test_relative_import_blocked(self): | |
| with self.assertRaises(ImportError) as ctx: | |
| safe_import("..secret", level=1) | |
| self.assertIn("relative imports", str(ctx.exception).lower()) | |
| def test_is_module_safe_helper(self): | |
| self.assertTrue(is_module_safe("math")[0]) | |
| self.assertTrue(is_module_safe("urllib.parse")[0]) | |
| self.assertTrue(is_module_safe("collections.abc")[0]) | |
| self.assertFalse(is_module_safe("os")[0]) | |
| self.assertFalse(is_module_safe("urllib.request")[0]) | |
| self.assertFalse(is_module_safe("subprocess")[0]) | |
| if __name__ == "__main__": | |
| unittest.main() | |