Skip to content

Commit 4c9c2a0

Browse files
committed
Add security tests for indirect module access via pandas
1 parent 401056b commit 4c9c2a0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tests/expressions/test_interpreter_security.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
)
1414

1515

16+
def _has_module(module_name: str) -> bool:
17+
code = f"""
18+
{module_name}.is_available()
19+
"""
20+
parser = ExpressionsParser()
21+
interpreter = ExpressionsInterpreter()
22+
tree = parser.parse(code)
23+
return interpreter.execute(tree)
24+
25+
1626
def test_security_checks():
1727
"""Test comprehensive security checks in the interpreter.
1828
@@ -135,6 +145,30 @@ def test_forbidden_module_access():
135145
interpreter.execute(tree)
136146

137147

148+
@pytest.mark.skipif(not _has_module("pd"), reason="Pandas is not available")
149+
def test_forbidden_module_indirect_access():
150+
"""Test forbidden module access in indirect ways.
151+
152+
Tests:
153+
1. Access to pickle via pandas
154+
2. Access to io via pandas
155+
"""
156+
parser = ExpressionsParser()
157+
interpreter = ExpressionsInterpreter()
158+
159+
# Test pickle access via pandas
160+
with pytest.raises(
161+
SecurityError, match="Access to 'pickle' is not allowed"
162+
):
163+
tree = parser.parse("pd.io.pickle.pickle.codecs")
164+
interpreter.execute(tree)
165+
166+
# Test pickle access via pandas
167+
with pytest.raises(SecurityError, match="Access to 'io' is not allowed"):
168+
tree = parser.parse("pd.io.pickle.pc.io.abc")
169+
interpreter.execute(tree)
170+
171+
138172
def test_forbidden_builtins_access():
139173
"""Test forbidden builtins access in more detail.
140174

0 commit comments

Comments
 (0)