Skip to content

fix: sandbox LLM-generated code execution to prevent RCE (#1893, #1895) - #1899

Open
benaiah-muga wants to merge 1 commit into
sinaptik-ai:mainfrom
benaiah-muga:fix/sandboxed-code-execution
Open

fix: sandbox LLM-generated code execution to prevent RCE (#1893, #1895)#1899
benaiah-muga wants to merge 1 commit into
sinaptik-ai:mainfrom
benaiah-muga:fix/sandboxed-code-execution

Conversation

@benaiah-muga

Copy link
Copy Markdown

Summary

Fixes #1893 (Code Injection in CodeExecutor.execute) and #1895 (Default code executor runs with full builtins, no sandbox).

Both issues share the same root cause: LLM-generated code is executed via exec() with full Python builtins, allowing import os; os.system(...) to run arbitrary OS commands. This PR adds defense in depth at two layers.

Changes

Layer 1: AST-level validation (code_validation.py)

  • Added _SecurityVisitor that detects and blocks:
    • import os, import subprocess, etc. (70+ dangerous modules)
    • from os import system, from subprocess import Popen, etc.
    • __import__("os") calls
    • os.system(...), subprocess.Popen(...) attribute access
  • Uses the previously-unused MaliciousCodeGenerated exception

Layer 2: Restricted builtins (environment.py)

  • Added _safe_import() wrapper that blocks dangerous module imports at runtime
  • Added _get_restricted_builtins() that removes open, exec, eval, compile, getattr, setattr, delattr, etc.
  • Legitimate data analysis builtins (len, range, print, exceptions, etc.) preserved

Tests

  • 44 new unit tests + 216 subtests covering:
    • All PoC payloads from both issues confirmed blocked
    • Safe modules (pandas, numpy, matplotlib) confirmed allowed
    • Legitimate data analysis code confirmed working
  • 609 total tests pass, 0 failures

Verification

PoC from #1893: import os; os.system("whoami") -> BLOCKED (CodeExecutionError)
PoC from #1895: import os; os.popen("whoami").read() -> BLOCKED (CodeExecutionError)
Legitimate code: result = len(pd.DataFrame({"a": [1,2,3]})) -> WORKS

- Add restricted __builtins__ with safe __import__ wrapper in environment.py
  that blocks dangerous builtins (open, exec, eval, compile, getattr, etc.)
  while allowing safe data analysis operations
- Add AST-level _SecurityVisitor in code_validation.py that detects and blocks
  imports of dangerous modules (os, subprocess, sys, etc.), __import__() calls,
  and attribute access on dangerous modules before code reaches exec()
- Defense in depth: AST validation catches malicious code at generation time,
  restricted builtins catch any bypass at execution time
- Fixes sinaptik-ai#1893 (Code Injection in CodeExecutor.execute)
- Fixes sinaptik-ai#1895 (Default code executor runs with full builtins, no sandbox)
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant