Day 6 of 1260 minCipher CadetHands-on lab
Day 6: ASI05 Unexpected Code Execution
Taught by Mr Elite Updated
By the end of today you can
- Explain ASI05 Unexpected Code Execution and why it matters in agentic applications
- Describe command injection from the boundary between data and executable instructions
- Trace how model-generated text can flow through an agent tool into an operating-system command
- Distinguish unsafe shell construction from safer argument-based process execution
- Recommend input validation, least privilege, sandboxing and execution controls
Warm-up
Answer from memory first, then tap to check. Recalling yesterday is what makes it stick.
What is the key danger when an application places untrusted text into a shell command?
The shell may interpret parts of that text as command syntax rather than as ordinary data.
Does validating a user's text alone make shell command construction safe?
Not necessarily. Prefer APIs that avoid the shell, pass arguments separately and enforce a narrow allowlist of permitted operations.
An agent can generate text, call a tool and ask the operating system to perform work. The security boundary becomes critical when text produced by a model is inserted into a command. If an application builds a shell command by joining strings, data that was meant to be an argument may instead be interpreted as command syntax.
This is the core idea behind command injection and the risk addressed by ASI05 Unexpected Code Execution. The issue is not that a model can write text. The issue is that an application gives that text more authority than it should have by passing it into an execution context without a safe boundary.
Day 5 introduced code execution risk and the impact of broad permissions. Today builds on that foundation: unexpected execution becomes more damaging when an agent can reach sensitive files, credentials, networks or privileged tools. The defensive goal is to keep model output as data, use safe process APIs, restrict what can execute and contain any failure.
ASI05 Unexpected Code Execution explained
ASI05 Unexpected Code Execution concerns cases where an agentic application causes code or commands to run in ways the application designer did not intend. The execution may be triggered by model output, user input, retrieved content, tool arguments or data passed between components.
A common path looks like this:
- A user asks the agent to perform a task
- The model generates text that is passed to a tool
- The tool constructs an operating-system command
- The command is executed by a shell or process API
- The resulting process performs actions using the application's permissions
The model's output is not inherently safe simply because it was generated by an AI system. It may contain unexpected characters, malformed values, content influenced by an untrusted document, or text that violates the tool's intended input format. If the application concatenates that text into a command string, the execution layer may interpret it in an unintended way.
Unexpected execution can lead to file access, data modification, process execution, network activity or privilege misuse. The impact depends on the execution context: the operating-system account, filesystem permissions, available binaries, network access, secrets in the environment and the presence of isolation controls.
- Command injection
- Untrusted input changes the intended meaning of a command
- Shell
- A command interpreter that parses shell syntax and launches programs
- Argument
- A value passed to a program as data
- Allowlist
- An explicit set of permitted values or operations
- Least privilege
- Giving a component only the permissions needed for its task
- Sandbox
- An isolated execution environment that limits possible impact
How model text becomes a system command
The vulnerable design usually appears at the boundary between an agent tool and the operating system. A tool might accept a filename, hostname, search term or other value from the model. The application then creates a command string from a fixed command plus that value.
The risk arises when the application asks a shell to parse the combined string. Shells support operators, expansions, substitutions, quoting rules and other syntax. If untrusted text is inserted into the command string, characters that appear to be ordinary input may be treated as control syntax.
The safer design is to avoid constructing shell command strings. Use a process API that accepts an executable and a separate argument list, with shell interpretation disabled. The program then receives the intended argument as a value rather than having it reparsed as shell syntax. This is an important improvement, but it does not remove the need for validation: the invoked program may have its own option syntax, file path behaviour or dangerous features.
A secure agent tool should:
- Expose a small, fixed set of permitted operations rather than a general-purpose command interface
- Validate each argument against the expected type, format, length and business rule
- Use a fixed executable path and a structured argument array
- Avoid enabling shell interpretation for model-controlled values
- Reject unexpected options, paths and values rather than trying to repair arbitrary input
- Apply timeouts, resource limits and controlled working directories
Quick check
Which design best preserves the boundary between data and command syntax?
Structured argument passing avoids asking a shell to reinterpret untrusted text as command syntax.
Safe execution patterns and containment
Preventing unexpected code execution requires controls at several layers. Start by narrowing the tool's purpose. If the agent only needs to check a service status, it should not receive a generic “run any command” capability. The tool should map a small set of approved user intents to fixed operations.
Input validation should be specific to the argument. Validate types, allowed characters, maximum length, permitted paths and expected values. Prefer canonical identifiers over arbitrary paths or command fragments. Reject invalid input; do not rely on a blacklist of suspicious characters because command languages and platform behaviours differ.
Use process execution APIs that pass the executable and arguments separately. Keep shell interpretation disabled. Set a known working directory, a minimal environment and a fixed executable path. Do not pass secrets in command-line arguments or expose sensitive environment variables unnecessarily.
Sandboxing and least privilege reduce impact if an execution flaw remains:
- Run under a dedicated low-privilege operating-system identity
- Restrict filesystem access to the minimum required workspace
- Limit network egress and access to internal services
- Apply CPU, memory, process-count and execution-time limits
- Use read-only mounts where possible and discard temporary environments after use
- Log the requesting task, validated operation, executable, outcome and policy decision
For agentic systems, tool authorisation must be independent of model reasoning. The model may propose an operation, but the application must verify that the operation is allowed for the user, task and current context. Retrieved documents or tool outputs must not be allowed to grant new execution privileges.
Your turn, lab 1 of 2
Command Injection
This lab introduces command injection in a controlled training scenario. It demonstrates why combining input with a command string is unsafe and how the application should constrain the operation. Use only the lab-provided target and inputs.
What to try
What to notice
- Keep all testing inside the supplied simulator; do not run commands on external hosts
- Do not substitute real infrastructure, addresses or unrelated command strings
- Distinguish normal program output from evidence of unintended command interpretation
- Stop when the lab's success condition is reached and follow its reset or cleanup instructions
Your turn, lab 2 of 2
AI Tool Argument Injection
This exercise focuses on an agent tool that passes model-provided text into a command-execution boundary. The objective is to recognise unsafe argument handling and identify the safer implementation pattern. Use only the designated lab environment and synthetic values.
What to try
What to notice
- Do not use the exercise to execute arbitrary commands or access files outside the simulator
- A model-generated argument should be treated as untrusted input
- A safe tool exposes a narrow operation and does not let the model choose an unrestricted command
- Stop at the stated success condition and use the provided reset or cleanup process
If it did not work
- Assuming model-generated text is safe because it came from the assistant
- Concatenating untrusted values into a shell command string
- Believing that removing a few suspicious characters is a complete defence
- Treating shell escaping as portable across platforms and execution contexts
- Allowing the model to select arbitrary executables or command options
- Validating only the input format while ignoring the invoked program's own argument semantics
Recap
- ASI05 Unexpected Code Execution occurs when an agentic workflow causes unintended code or commands to run
- Command injection happens when untrusted data is interpreted as command syntax
- Model output must be treated as untrusted input at the tool boundary
- Prefer fixed executables and separately supplied validated arguments with shell interpretation disabled
- Expose narrow, purpose-built tools instead of general-purpose command execution
- Least privilege, sandboxing, resource limits and egress restrictions reduce impact
- Day 5's lesson about broad permissions applies directly: execution risk grows with the authority available to the process
Cheat sheet
Threat- Untrusted model or user text reaches an execution context
Unsafe pattern- Build a shell command by concatenating strings
Safer pattern- Fixed executable plus validated argument array; shell disabled
Validation- Check type, format, length, allowed values and application-specific rules
Tool design- Provide narrow operations, not unrestricted command execution
Permissions- Dedicated low-privilege identity and minimal environment
Sandbox- Restrict filesystem, network, CPU, memory and runtime
Monitoring- Log task context, approved operation, execution result and policy decision
Core rule- Treat model-generated text as data, never as authority to define a command
Before the next day
Mission: Redesign an unsafe agent command boundary
Using the concepts from today's lesson and evidence from the designated training labs, write a defensive design for an agent tool that performs one narrowly defined system task without allowing model text to become shell syntax.
About 30 min
You are done when: A one-page secure tool design containing its purpose, input contract, execution model, validation rules, permissions, sandbox boundaries and audit events. Do not include arbitrary command payloads or real-system test instructions.
Want more? Add a threat model showing how the design contains a malicious or malformed model-generated value at the tool boundary and limits impact if another control fails.
Quiz
4 questions. Your score is saved with your progress.
1. What is the central cause of command injection?
Injection occurs when data crosses into a command interpreter without a safe separation.
2. Which approach is generally safer for launching a process?
Structured argument passing avoids shell parsing of model-controlled text.
3. Why is input validation still needed when the application avoids the shell?
Avoiding shell interpretation reduces one class of risk, but the called program and business logic still need constrained inputs.
4. How does least privilege reduce the impact of unexpected code execution?
Least privilege is a containment control. It reduces the potential impact even when a vulnerability is present.
Finish the day
Mark the day complete to save your progress.
Next: Next, continue the agentic application security course by examining how excessive agency and permissions can turn a limited tool weakness into a broader compromise. You will connect execution boundaries to tool authorisation, privilege scope and the principle of least privilege. Coming soon.
Your notes
Private to this device. Write what you want to remember from today.