Static analysis (SAST) check · Python

Llm tool shell execution (Python)

A language-model agent is given a tool that runs shell commands or arbitrary Python.

Why it matters

Whatever the model is persuaded to write, including by instructions in a document or web page it reads, runs on this host.

How to fix it

Give the agent narrow tools that do one fixed action with validated arguments. If code execution is required, run it in a disposable sandbox with no credentials or network, and require human approval.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (Python)
import subprocess

from langchain_core.tools import tool


@tool
def run_shell(command: str) -> str:
    """Run a shell command and return its output."""
    return subprocess.run(command, shell=True, capture_output=True, text=True).stdout
Fixed: not reported (Python)
from langchain_core.tools import tool

ORDERS = {"1001": "shipped"}


@tool
def order_status(order_id: str) -> str:
    """Look up the status of an order by its ID."""
    return ORDERS.get(order_id, "unknown")

What TrustIvan reads

Runs on repository scans of Python source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.