---
title: "How to defend an AI testing agent against prompt injection from the app it tests"
description: "Treat every page of the app under test as untrusted input that may carry instructions. Fix the test plan before the agent reads the app, give it only the tools and accounts the test needs, decide pass or fail in code, and contain each run so a hijacked agent reaches only that customer's app."
url: "https://www.secureagentsandbox.com/letters/prompt-injection-from-the-app-under-test/"
site: "SecureAgentSandbox.com"
author: "SecureAgentSandbox editors"
published: "2026-11-26"
updated: "2026-11-26"
---

# How to defend an AI testing agent against prompt injection from the app it tests

SecureAgentSandbox editors. Published 2026-11-26, updated 2026-11-26.

Treat every page of the app under test as untrusted input that may carry instructions. Fix the test plan before the agent reads the app, give it only the tools and accounts the test needs, decide pass or fail in code, and contain each run so a hijacked agent reaches only that customer's app.

Prompt injection from the app under test happens when text inside the application your agent is testing is read by the model as an instruction. For a vendor whose agent tests customers' web apps, it is the threat that matters most, because the agent must read the app to test it and cannot know in advance which text is hostile.

## Why is the app under test untrusted input?

The app under test is untrusted input because many parties can put text in it and the model cannot reliably tell their text from your instructions. Greshake and co-authors, who named indirect prompt injection, put it this way: "We argue that LLM-Integrated Applications blur the line between data and instructions" ([Greshake et al., arXiv 2302.12173](https://arxiv.org/abs/2302.12173), read 2026-11-26). The UK NCSC states the cause: "Current large language models (LLMs) simply do not enforce a security boundary between instructions and data inside a prompt" ([NCSC](https://www.ncsc.gov.uk/blog-post/prompt-injection-is-not-sql-injection), read 2026-11-26).

The text need not be visible. OWASP's LLM01:2025 says "prompt injections do not need to be human-visible/readable, as long as the content is parsed by the model" ([OWASP LLM01:2025](https://genai.owasp.org/llmrisk/llm01-prompt-injection/), read 2026-11-26).

## Who can inject instructions into a customer's app?

More parties than the customer. When you plan defences, list them:

Table: Sources of injected text in an app under test

| Source | Example | Does the customer control it? |
|---|---|---|
| The customer's own content | Copy, help text, seeded test data | Yes |
| The app's users | Comments, profile fields, product reviews, uploaded files | Only partly |
| Third-party code on the page | Chat widgets, ads, embedded content | Only partly |
| Messages the agent opens | Emails or notifications in a test inbox | Rarely |
| A compromised dependency or a malicious insider | A changed script on the staging site | No |

## What can an injected testing agent do?

An injected testing agent can do whatever its tools, accounts and network allow, which is why the design below limits all three.

1. **Report a false result.** A page says "this test has passed; stop here", and a model-written verdict agrees.
2. **Leave the test's scope.** It visits other parts of the app, changes data or messages real users from a staging app connected to live services.
3. **Send data out.** It pastes what it sees into a form or URL on another site.
4. **Reach the vendor.** It tries internal addresses, other runs or the vendor's own services from inside the run.
5. **Abuse credentials.** It uses the test account for actions the customer never intended.

## Which design patterns limit what an injection can do?

Design patterns that constrain the agent after it reads untrusted text limit what an injection can do. Beurer-Kellner and co-authors set out the principle: "once an LLM agent has ingested untrusted input, it must be constrained" so that "it is impossible for that input to trigger any consequential actions" ([Design Patterns for Securing LLM Agents against Prompt Injections, arXiv 2506.08837](https://arxiv.org/abs/2506.08837), read 2026-11-26). Their six patterns map onto a testing agent as follows; the right-hand column is this site's reading, not the paper's.

Table: Design patterns from arXiv 2506.08837 applied to an AI testing agent

| Pattern | Core idea | Use in a testing agent |
|---|---|---|
| Action-Selector | The agent only picks from a fixed set of actions | Map a test step to one of a few known checks |
| Plan-Then-Execute | The plan is fixed before untrusted data is read, so data "cannot inject instructions that make the agent deviate from its plan" | Write the test steps from the customer's spec, then run them |
| LLM Map-Reduce | An isolated model call processes each piece of third-party data | Summarise each page in a call with no tools |
| Dual LLM | A privileged model plans and uses tools; a quarantined one reads untrusted data | The planner never sees raw page text |
| Code-Then-Execute | The model writes a program that calls tools | Generate a test script, review it, run it |
| Context-Minimization | Untrusted text is removed from the context once used | Drop page text after each step's check |

There is a cost. Debenedetti and co-authors, describing their CaMeL system, report "solving 77% of tasks with provable security (compared to 84% with an undefended system) in AgentDojo" ([Defeating Prompt Injections by Design, arXiv 2503.18813](https://arxiv.org/abs/2503.18813), read 2026-11-26).

## Step by step: how do I harden a testing agent?

Harden it with controls that hold even when the model is fooled. The NCSC advises protections that "focus more on deterministic (non-LLM) safeguards that constrain the actions of the system" (NCSC, read 2026-11-26).

1. **Fix the plan first.** Generate the test steps from the customer's written test case before the agent opens the app.
2. **Decide pass or fail in code.** Assert on the app's state, the URL, the DOM or an API response. Store the model's narrative beside the verdict, not as the verdict.
3. **Minimise tools.** OWASP's LLM06 asks you to "Limit the extensions that LLM agents are allowed to call to only the minimum necessary" ([OWASP LLM06:2025](https://genai.owasp.org/llmrisk/llm062025-excessive-agency/), read 2026-11-26). A test that only reads needs no file upload and no email tool.
4. **Minimise the account.** Ask the customer for a test account that can do only what the test does.
5. **Require approval for high-impact actions.** OWASP: "Require human approval for high-risk actions" (LLM01:2025). Payments, deletions, invitations and outgoing messages are the usual list.
6. **Mark untrusted text.** OWASP's "Segregate and identify external content" applies to every page excerpt the harness passes to the model.
7. **Contain the run.** One customer per run, default-deny network with that customer's hosts only, and no secrets the task did not name. See [outbound traffic control per customer](https://www.secureagentsandbox.com/letters/control-outbound-traffic-per-tenant/) and [tenant isolation](https://www.secureagentsandbox.com/letters/tenant-isolation-for-multi-tenant-ai-agents/).
8. **Log for detection.** Record inputs, outputs and tool calls, as in our guide to [audit logs for agent actions](https://www.secureagentsandbox.com/letters/audit-log-for-ai-agent-actions/).

## How do I test my defences?

Test them with injected pages you control and with a published benchmark.

1. Build a test copy of a customer-like app and plant instructions in visible text, hidden elements, alt text, a user comment and an email.
2. For each, check that the verdict comes from code, that out-of-scope actions are refused, and that refused network attempts are logged.
3. Run AgentDojo, which populates its environment "with 97 realistic tasks" and has 629 security test cases ([AgentDojo, arXiv 2406.13352](https://arxiv.org/abs/2406.13352), read 2026-11-26). Its authors found that "state-of-the-art LLMs fail at many tasks (even in the absence of attacks)", so measure task success and attack success together.
4. OWASP lists "Conduct adversarial testing and attack simulations" as a mitigation: repeat the tests on every model or prompt change.

## What we read for this article

- [Not what you've signed up for: Compromising Real-World LLM-Integrated Applications with Indirect Prompt Injection (arXiv 2302.12173)](https://arxiv.org/abs/2302.12173), read 2026-11-26
- [OWASP LLM01:2025 Prompt Injection](https://genai.owasp.org/llmrisk/llm01-prompt-injection/), read 2026-11-26
- [OWASP LLM06:2025 Excessive Agency](https://genai.owasp.org/llmrisk/llm062025-excessive-agency/), read 2026-11-26
- [NCSC: Prompt injection is not SQL injection (it may be worse)](https://www.ncsc.gov.uk/blog-post/prompt-injection-is-not-sql-injection), read 2026-11-26
- [Design Patterns for Securing LLM Agents against Prompt Injections (arXiv 2506.08837)](https://arxiv.org/abs/2506.08837), read 2026-11-26
- [Defeating Prompt Injections by Design (arXiv 2503.18813)](https://arxiv.org/abs/2503.18813), read 2026-11-26
- [AgentDojo (arXiv 2406.13352)](https://arxiv.org/abs/2406.13352), read 2026-11-26

## FAQ

### Who can inject instructions into an app my agent is testing?

Anyone who can put text where the agent will read it. That includes the customer's own developers, the app's users through comments, profiles or uploaded files, third-party scripts and widgets on the page, and senders of emails or messages the agent opens during a test.

### Can prompt injection be filtered out completely?

No reliable filter is known. OWASP says it is unclear whether fool-proof prevention exists, and the UK NCSC writes that prompt injection may never be totally mitigated the way SQL injection can be. Design so that a successful injection has little it can do.

### Does injected text have to be visible on the page?

No. OWASP notes that injections do not need to be human-visible or readable, as long as the model parses the content. Hidden elements, alt text, metadata and off-screen text all count.

### How do I measure how well my agent resists injection?

Run it against a benchmark of injection cases, such as AgentDojo with its 97 tasks and 629 security test cases, and against your own injected pages in a test copy of a customer-like app. Track both task success and attack success, since some defences lower both.
