From 53fa361245ffcef92d6415223b7fb88818844cf7 Mon Sep 17 00:00:00 2001 From: traoreera <68350805+traoreera@users.noreply.github.com> Date: Tue, 6 Oct 2026 14:51:15 +0000 Subject: [PATCH] security(kernel): forbid function reflection attributes in ASTScanner Enhance FORBIDDEN_ATTRIBUTES in xcore/kernel/security/section.py to block function reflection/introspection attributes (__closure__, __defaults__, __kwdefaults__, __func__, __self__, __module__) during AST scanning to prevent sandbox bypasses. --- tests/unit/security/test_sentinel_fixes.py | 28 ++++++++++++++++++++++ xcore/kernel/security/section.py | 6 +++++ 2 files changed, 34 insertions(+) diff --git a/tests/unit/security/test_sentinel_fixes.py b/tests/unit/security/test_sentinel_fixes.py index 50b79fd6..cdd879ba 100644 --- a/tests/unit/security/test_sentinel_fixes.py +++ b/tests/unit/security/test_sentinel_fixes.py @@ -52,3 +52,31 @@ def test_ast_scanner_blocks_new_forbidden_modules(tmp_path): assert "import interdit — 'termios'" in error_msgs assert "import interdit — 'tty'" in error_msgs assert "import interdit — 'fcntl'" in error_msgs + + +def test_ast_scanner_blocks_reflection_forbidden_attributes(tmp_path): + src_dir = tmp_path / "src" + src_dir.mkdir() + + code = """ +def check_func(f): + c = f.__closure__ + d = f.__defaults__ + kd = f.__kwdefaults__ + fn = f.__func__ + s = f.__self__ + m = f.__module__ +""" + (src_dir / "main.py").write_text(code) + + scanner = ASTScanner() + result = scanner.scan(tmp_path) + + assert not result.passed + error_msgs = "\n".join(result.errors) + assert "attribut interdit : '__closure__'" in error_msgs + assert "attribut interdit : '__defaults__'" in error_msgs + assert "attribut interdit : '__kwdefaults__'" in error_msgs + assert "attribut interdit : '__func__'" in error_msgs + assert "attribut interdit : '__self__'" in error_msgs + assert "attribut interdit : '__module__'" in error_msgs diff --git a/xcore/kernel/security/section.py b/xcore/kernel/security/section.py index 0c08a123..4eacf414 100644 --- a/xcore/kernel/security/section.py +++ b/xcore/kernel/security/section.py @@ -28,6 +28,12 @@ "__globals__", "__subclasses__", "__code__", + "__closure__", + "__defaults__", + "__kwdefaults__", + "__func__", + "__self__", + "__module__", "__mro__", "__builtins__", "__dict__",