Skip to content

Commit

Permalink
fix: bypass the security check with prompt injection (#399) (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
gventuri committed Jul 28, 2023
1 parent b452b3b commit 3aac79b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pandasai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,24 @@ def _is_df_overwrite(self, node: ast.stmt) -> bool:
and re.match(r"df\d{0,2}$", node.targets[0].id)
)

def _is_jailbreak(self, node: ast.stmt) -> bool:
"""
Remove jailbreaks from the code to prevent malicious code execution.
Args:
node (object): ast.stmt
Returns (bool):
"""

DANGEROUS_BUILTINS = ["__subclasses__", "__builtins__", "__import__"]

for child in ast.walk(node):
if isinstance(child, ast.Name) and child.id in DANGEROUS_BUILTINS:
return True

return False

def _clean_code(self, code: str) -> str:
"""
A method to clean the code to prevent malicious code execution
Expand All @@ -608,7 +626,7 @@ def _clean_code(self, code: str) -> str:
if isinstance(node, (ast.Import, ast.ImportFrom)):
self._check_imports(node)
continue
if self._is_df_overwrite(node):
if self._is_df_overwrite(node) or self._is_jailbreak(node):
continue
new_body.append(node)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_pandasai.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ def test_clean_code_remove_builtins(self, pandasai):
assert pandasai.run_code(builtins_code, pd.DataFrame()) == {1, 2, 3}
assert pandasai.last_code_executed == "print(set([1, 2, 3]))"

def test_clean_code_removes_jailbreak_code(self, pandasai):
malicious_code = """
__builtins__['str'].__class__.__mro__[-1].__subclasses__()[140].__init__.__globals__['system']('ls')
print(df)
"""
pandasai._llm._output = malicious_code
pandasai.run_code(malicious_code, pd.DataFrame())
assert pandasai.last_code_executed == "print(df)"

def test_clean_code_remove_environment_defaults(self, pandasai):
pandas_code = """
import pandas as pd
Expand Down

0 comments on commit 3aac79b

Please sign in to comment.