Sub topic 13
Prompt injection and approval integrity
This chapter assumes you can read short Python programs, know how an agent calls tools, and have built the approval gate from earlier chapters. It covers how attackers try to get a harmful action approved, and how to keep the approval step trustworthy.
Why approval is a target
An approval gate is a security control. Attackers who cannot call a dangerous tool directly will try to make the agent request it, then make the request look harmless to the reviewer.
Prompt injection is text planted in content the agent reads, such as a web page, an email, or a document, that tries to give the agent new instructions. The agent cannot reliably tell data from commands, so it may act on the planted text.
Treat the gate as the defense, not the model
Do not rely on the model to refuse injected instructions. The gate must work even when the model is fully fooled. That leads to three rules:
- Classify risk in code, from the tool name, never from text the model wrote.
- Show the reviewer the real arguments, not the model's description of them.
- Execute only the action the reviewer saw.
The unknown-tool default from the risk lookup table already follows rule 1. A tool the attacker invents, or a renamed one, lands in the high-risk path.
Common integrity failures
| Failure | How it happens | Defense |
|---|---|---|
| Misleading summary | The model writes "Send weekly report" while the recipient is an outside address | Render the raw arguments from the call object |
| Swapped action | Arguments change after approval | Fingerprint check at execution |
| Replay | An old approval is reused for a new call | One-time approval IDs that are removed when resolved |
| Stale approval | A decision arrives days later, after the context changed | Expiry time on pending requests |
| Self-approval | The agent, or a tool it controls, submits the decision | Accept decisions only from an authenticated reviewer channel |
Show the facts, not the story
Build the review text from the call itself. Highlight values that matter for injection, such as external recipients or URLs.
from dataclasses import dataclass
@dataclass(frozen=True)
class ToolCall:
name: str
args: dict
COMPANY_DOMAIN = "example.com"
def review_text(call: ToolCall) -> str:
lines = [f"Tool: {call.name}"]
for key, value in sorted(call.args.items()):
lines.append(f" {key} = {value!r}")
to = str(call.args.get("to", ""))
if to and not to.endswith("@" + COMPANY_DOMAIN):
lines.append("WARNING: recipient is outside the company domain")
return "\n".join(lines)
call = ToolCall("send_email", {"to": "[email protected]", "body": "Q3 numbers"})
print(review_text(call))The warning comes from a rule in code. The model's own explanation can be shown as a separate, clearly labeled note, but the reviewer should never need it to decide.
Bind and verify at execution time
The fingerprint from the earlier chapter must be checked at the last moment before the tool runs. Check inside the same function that executes, so no code path skips it.
import hashlib
import hmac
import json
def fingerprint(call) -> str:
payload = json.dumps({"name": call.name, "args": call.args}, sort_keys=True)
return hashlib.sha256(payload.encode()).hexdigest()
def execute_if_approved(call, approval, tools):
if not hmac.compare_digest(approval["fingerprint"], fingerprint(call)):
raise PermissionError("Approval does not match this action")
return tools[call.name](**call.args)hmac.compare_digest compares in constant time. Here it is a good habit more than a strict need, since the fingerprint is not a secret.
A fingerprint stops argument changes, but it does not stop a bad action that was approved on purpose. If an injected instruction produces a call that looks plausible, a tired reviewer may still approve it. Pair the gate with narrow tool permissions, so a tool such as send_email can only reach approved domains even after approval.
Practical checklist
- Keep untrusted content apart from instructions where the framework allows it, and label it as data.
- Give each tool the least access it needs.
- Log the fingerprint with each decision so audits can match approvals to executions.
- Test with injected documents and confirm the gate holds.
Exercise
A reviewer approves a send_email call to a colleague. Before execution, injected text changes the recipient to an outside address. Which mechanism stops the send, and what does it compare?
Show answer
The fingerprint check in executeifapproved stops it. It compares the SHA-256 hash stored with the approval against a fresh hash of the tool name and arguments about to run. The recipient changed, so the hashes differ and a PermissionError is raised.
Exercise
Why should the "outside company domain" warning be computed in code instead of asked of the model?
Show answer
The model may be the thing that was fooled by injection, so its judgment cannot be trusted as a control. A code rule gives the same result every time and cannot be talked out of it by planted text.