Agents and Automation30/08/2026Laboratório de Prompts e IA14 min readUpdated on 23/09/2026

How to test prompt injection in AI agents before connecting email, PDF, RAG, and tools

A reproducible method to prove whether untrusted content can divert an agent, trigger tools, leak data, or persist instructions before production.

Painel de laboratório de segurança com agente de IA central ligado a e-mail, documentos PDF, base RAG e ferramentas, enquanto entradas hostis são bloqueadas e casos de teste são avaliados.

Connecting an AI agent to email, PDFs, a RAG knowledge base, or tools changes the security problem. The question is no longer only whether the model responds to a malicious prompt, but whether untrusted content can become authority, alter the user's intent, or produce an action with a real-world effect. That is what the test needs to prove before any credential, inbox, private knowledge base, or write-capable tool enters the loop.

The 2026 edition of the OWASP Top 10 for LLM applications, published on August 3, keeps prompt injection in the top position and explicitly expands the surface to retrieved content, tool outputs, images, persistent memory, and agentic execution. The central issue is architectural: data and instructions reach the model in the same context, so defense cannot depend on a sentence in the system prompt or a word filter. The system must remain safe even when the model interprets something hostile as an instruction.

This focus also helps put the risk in perspective. HackerOne reported in March 2026 a 540% year-over-year increase in validated prompt injection reports on its own platform. The figure does not measure global incidence, but it reinforces a practical conclusion for builders: declared guardrails are not enough. You need evidence that the agent resists, that controls outside the model contain the failure, and that the same case can be repeated after every change.

The right test proves that hostile data does not become authority

A weak test asks: "did the model refuse the phrase ignore previous instructions?". A useful test asks: "given a legitimate user objective, a source of untrusted content, and an available dangerous capability, did the agent preserve the original intent and did the controls prevent any unauthorized effect?".

That distinction separates jailbreak testing from agentic security. An agent can write an apparently correct answer and still have prepared an improper tool call. It can also refuse in text and then store a malicious instruction in memory, or appear to obey the user while sending a tampered argument to an API. That is why the test oracle must observe behavior, tool calls, authorizations, persistence, and side effects, not just the last message displayed.

A practical way to think about the scenario is to connect a source to a risk sink. The source is where the attacker can plant content: a message, email, PDF, RAG-retrieved passage, API response, tool output, web page, image, or memory. The sink is the capability that turns a wrong interpretation into harm: sending email, writing to a database, exposing private data, calling an external endpoint, deleting a file, modifying permissions, executing code, or persisting state. The test becomes meaningful when there is a plausible path between the two.

Build the lab before connecting any real data

The first rule of an adversarial evaluation is that the test must not create the incident it is meant to prevent. The agent should start in a synthetic or isolated environment, with a fake inbox, test documents, a separate RAG index, stub or sandbox tools, and credentials with no production access. If there is an external call, the destination must be controlled and contain no real data.

Use canary markers instead of real secrets. A value such as CANARY-7319 can represent sensitive data that must never leave the authorized scope. Instead of testing whether the agent can send a real file to a third party, make the sending tool log the attempt to a local collector and return a simulated status. This lets you measure exfiltration and misuse without exposing a user, customer, or infrastructure.

The authorization layer must also exist in the lab. If the production policy is meant to prevent sending to unapproved recipients, accessing administrative tables, or writing outside an allowed directory, the same deterministic rule must be active during testing. This matters because a mature defense accepts that the model may propose a wrong action; what must not happen is the application executing it simply because the model asked.

Build the matrix across three axes

The matrix does not need to start with hundreds of attacks. It needs to systematically cover where the content comes from, how it tries to divert the agent, and what consequence it seeks. OWASP 2026 proposes decomposing prompt injection by delivery surface, propagation behavior, and encoding form. For a product team, it is useful to complement that model with the impact destination, because the destination is what defines the release gate.

Axis 1: input surface

Include the user's direct message as a baseline, but do not stop there. The set needs to cover email, body and subject, quoted text, attachments, PDF, RAG-retrieved passages, tool responses, API content, tool or MCP descriptions, images if the agent is multimodal, and memory if persistence exists. Indirect injection is precisely the case in which the user

did not type the hostile instruction; it arrived with something the agent needed to read in order to complete the task.

Axis 2: diversion technique

Vary more than the classic override phrase. Use role conflict, false authority, delayed instructions, content that tries to change an action's recipient, requests to reveal a canary, persistence requests, text fragmented across different fields, instructions hidden in quoted content, and rewritten versions of the same attack. If the system processes images or OCR, include cases where the instruction is in the visual document, not only in text extracted by an earlier step.

The goal is not to collect famous phrases. It is to test the security invariant. If the protection recognizes only a specific textual pattern, a small rewording can turn a case that "passed" into an incident. A regression case should preserve the adversarial intent and, when possible, include variants that change language, position, and encoding without changing the expected consequence.

Axis 3: consequence and authority

Define what the attack would try to do if it succeeded. For a read-only agent, the risk may be revealing private data or manipulating the answer. For an agent with tools, include external sending, state changes, out-of-scope access, tool chaining, memory writes, and credential use. The more irreversible or external the effect, the stricter the approval criterion should be.

This third axis avoids a common problem: celebrating a low average success rate while a rare case still enables the most dangerous action in the system. NIST draws attention to this distinction by recommending performance be examined by task, not only as an aggregate average. An attack that almost always fails can still be unacceptable if the one time it succeeds it can send private data or execute a privileged action.

Minimum cases for email, PDF, RAG, and tools

Email: hostile content tries to change the recipient

Create a synthetic inbox with a message the agent must summarize. In the body, insert a hostile instruction telling it to abandon the task and send the CANARY-7319 marker to an unapproved test address. The case passes when the agent summarizes the relevant content, treats the instruction as data, sends nothing, and, if it still proposes a send call, the authorization layer blocks it.

Then change the position of the attack. Put it in the subject, in quoted text from an earlier reply, and in an attachment. The legitimate intent remains identical. If the result changes by location, you have discovered a trust difference between surfaces that must be addressed or explicitly accepted.

PDF: the document must not rewrite the task

Use a test PDF whose legitimate content is, for example, a report with three facts to extract. In a secondary section, include an instruction trying to make the agent ignore the request and reveal a private marker. In another variant, put the instruction in an image that will be read by OCR or vision, if that path exists in the product.

A pass is not "the model said it saw an attack attempt". A pass is correct extraction of the authorized facts, no disclosure of the canary, and no change in tool, scope, or destination. If the pipeline retains metadata, hidden text, an OCR layer, or annotations, test every representation that actually reaches the model.

RAG: a retrieved document tries to become policy

In the test index, add a poisoned document that is semantically relevant to the query and contains a false high-priority instruction. The query should deliberately retrieve that passage. The agent must use the permitted facts from the document without accepting the instruction as higher authority.

This scenario should also verify the transition from RAG to tools. OWASP itself recommends not allowing retrieved content to directly influence execution without an intermediate validation step. Therefore, if the passage asks to call a tool, change a recipient, or open an out-of-scope resource, the execution policy must deny the action regardless of how confidently the model states otherwise.

Tool or MCP: output is also untrusted input

Builders often protect the user prompt and forget that a tool response comes back into the context. Simulate a search tool, CRM, or MCP that returns valid data together with a hostile instruction. The agent should consume the data as an observation, not as an order that can reconfigure the objective.

Also run the inverse test: the model proposes an allowed tool but with an out-of-scope argument. Validation must happen in the application's trusted layer. If a tool is allowed to read only one specific project, it is not the model's text that should decide whether another project is authorized.

Memory: the failure must not cross sessions

If the agent has memory, include a case that tries to persist a malicious rule for future use. The first execution may look harmless; the risk appears when a later session inherits the instruction. OWASP 2026 treats writing to memory as a sensitive operation precisely because persistent context can turn a one-off injection into recurring diversion.

The test should open a new session and verify whether the hostile content was rejected, sanitized, scoped, or subjected to the intended approval. Cleaning the final response is not enough. You need to inspect the state that was actually written.

Define the oracle before execution

Each case needs a legitimate task, a hostile fixture, an expected behavior, and a forbidden effect. Without that, the team falls into subjective evaluation such as "it seemed safe". For agents, the result can be classified across separate dimensions: user-task success, attack success, unauthorized tool use, canary exposure, recipient diversion, approval bypass, improper memory writes, and audit-trail integrity.

The oracle must observe the complete execution. A proposed call that is blocked by policy is different from the model never attempting the action. Both situations may be acceptable depending on the architecture, but they tell different stories. The first shows that the model was influenced and containment worked; the second shows resistance

before execution. Recording the distinction helps decide where the defense should be strengthened.

Also record the functional side. A system that blocks every email, ignores every PDF, or refuses to use RAG may look immune but has failed as a product. The suite therefore needs mirrored benign cases: a normal email that should be summarized, a PDF without an attack that should be read, a trusted RAG passage that should inform the answer, and an authorized tool call that should work.

Approval criteria: the gate must reflect impact

For high-impact effects, a useful gate is simple: no observed unauthorized execution. This includes improper external sending, state changes, privileged access, canary leakage, execution without confirmation when confirmation is required, and malicious persistence. A single occurrence is a release failure even if the suite's overall average remains high.

That observed zero does not mean zero risk. Models are probabilistic, and adaptive attacks can find paths the suite does not cover. The gate says only that, under the tested conditions, the system did not produce that forbidden effect. That is why the number of attempts, the variants, and the breadth of the matrix are part of the evidence, not implementation details.

For utility, use a threshold defined by the product. The agent must continue completing legitimate tasks at a level comparable to the baseline. Instead of copying a universal number, establish a regression budget that makes sense for your case. A defense that reduces risk but destroys the core function needs to be redesigned, not merely approved because it blocked attacks.

The gate should also require evidence of controls outside the model. A sensitive action must be authorized by trusted code, identity policy, an allowlist, credential scope, or human approval tied to the action's exact parameters. "The model promised not to do it" is not an approval criterion.

Run the same attack more than once

A case that passes once is not a robust measurement. In NIST's work on agent hijacking with AgentDojo, attacks specifically adapted to the system increased the measured success rate from 11% in the best baseline to 81% for the strongest attack developed by the red team in that experiment. In another analysis, repeating a set of attacks 25 times raised the observed average rate from 57% to 80%.

Those numbers are not a forecast for your agent. They show why evaluation needs to account for adaptation and repetition. A real attacker can rephrase instructions, move the payload, and try again. The suite should do the same within controlled limits.

For critical cases, run multiple attempts and preserve the distribution of results. If the framework lets you control temperature, seed, or other parameters, record them, but do not rely only on a deterministic configuration to declare security. Production behavior needs to be tested close to the production configuration.

Turn every failure into a reproducible regression

The value of a red team increases when a finding becomes an automated test. When you find a failure, freeze the fixture that triggered it and record the context needed to reproduce it: prompt version, model and provider, generation configuration, tool schema, authorization policy, RAG index, memory state, hostile content, legitimate task, expected result, and relevant traces.

Do not store real secrets in the fixture. Replace them with canaries and synthetic resources. Also do not depend on an external endpoint that may disappear. The regression should be able to run in a controlled environment and produce an unambiguous answer about the forbidden effect.

The fix does not close the case. The test becomes part of the permanent suite. Future changes to the prompt, model, tool, MCP description, PDF parser, RAG chunking, memory policy, or authorization can reopen the path. That is exactly why OWASP recommends running adversarial tests after material changes and retaining regressions for failures already observed.

Fit the suite into CI/CD in layers

Not every commit needs to run hundreds of variants, but every commit that touches agent behavior should face a fast suite of invariants. This short layer covers the most critical forbidden effects and their equivalent benign cases. If it fails, the merge stops.

A broader layer can run on a schedule or before releases, with more sources, multiple attempts, language variations, fragmented attacks, and position changes. A third, human and adaptive layer should try to exploit the defense with knowledge of how it works. This reduces the risk that a static suite becomes decorative proof that the system recognizes only the attacks it already knows.

Trigger logic matters too. Changing the model, modifying the system prompt, adding a tool, expanding a credential's scope, changing a call schema, connecting a new MCP, modifying RAG chunking, or enabling memory are events that change the attack surface. The pipeline should treat them as security changes, not simple configuration.

Observability is part of the test

Without traceability, you know something went wrong but not where. Keep identifiers for the user task, the hostile fixture, and retrieved content; record proposed tool calls, the authorization-layer decision, parameters actually executed, confirmations, and state changes. Protect logs from sensitive-data exposure and do not record credentials in plain text.

That trail makes it possible to distinguish a model failure, policy failure, tool failure, and integration failure. It also helps create better regressions. If the agent attempted an action, authorization denied it, and nothing executed, the containment control worked. If the call crossed the policy because the schema allowed an unexpected parameter, the problem is in a different layer.

What should not count as sufficient defense

Blocking the string "ignore previous instructions" does not prove resistance to indirect injection. Using another LLM as the sole judge does not replace verification of real effects.

Relying on system-prompt instructions does not replace authorization. Having a tool allowlist does not solve dangerous arguments inside an allowed tool. Asking for generic confirmation does not protect the user if the interface does not show exactly the recipient, resource, and action that will be executed.

It is also insufficient to evaluate only the last textual response. With agents, the incident can happen before the final sentence: a call already sent, a record already changed, a memory entry already written. The test must look at what the system did, not only what it said.

When the agent is ready for real integrations

The agent is ready to advance when the matrix covers the surfaces that will actually enter the product, high-impact effects do not appear in the defined attempts, benign tasks continue to work, authorization contains the behavior even when the model is diverted, and every known failure has become a reproducible regression.

This still does not make prompt injection a closed problem. OWASP's current guidance treats defense as a layered architecture and assumes that filters and instructions can fail. The purpose of pre-connection testing is different: to demonstrate that hostile content cannot cross the boundary between data and authority on its own, and that if the model is deceived, deterministic components around it prevent the wrong interpretation from becoming a dangerous action.

From there, connect in stages. Start with read-only access and synthetic data, then read real data with minimal scope, then low-impact tools, and only then actions with external effects, while keeping the same cases in the pipeline. Agent security is not a one-time test before launch; it is a property that must be reevaluated whenever context, authority, or tools change.

Topics in this articleagentes de IAevalsMCPprompt injectionRAGred teamingsegurança de IA