Memory Poisoning (OWASP ASI06) and Indirect Injection Defenses
Explore how persistent memory can be poisoned by indirect prompt injection and how provenance-based controls prevent untrusted writes. You’ll build a write gate, quarantine suspicious updates, scan privileged content for hidden instructions and secrets, block unsafe automatic writes, and test defenses against reworded attacks.
We'll cover the following...
In lesson 5.5, we gave the on-call assistant a memory that lasts across sessions. When an engineer states a fact or a rule, reconcile() decides whether to add it, update it, delete it, or skip it, and writes the result to facts.json or rules.json. The next session loads those files into its system prompt, so the assistant starts with what earlier sessions learned.
Every statement that reached reconcile() so far came from an engineer typing in the chat. But the assistant also reads text no engineer wrote, and nothing in our memory code asks where a statement came from.
In this lesson, we’ll see what happens when a retrieved document contains a sentence shaped like a memory correction. We’ll watch it rewrite a stored fact, try a keyword filter and see it fail, and then build a provenance gate: a check that records where every write came from, lets only trusted sources write to memory, holds suspicious writes for review, and blocks writes the agent makes after reading untrusted text.
A note with a hidden instruction
The knowledge base imports pages from outside the team, such as forum threads about the tools we use. Here’s one of them, stored as note-10. Most of it is ordinary tuning advice, but one sentence is shaped exactly like a memory correction, and it targets the first step of our checkout runbook.
In the code above:
Lines 5–8: We define
FORUM_NOTE. Its third sentence asks to change the stored first step for checkout latency to a Redis restart.Line 9: We add it to the note store from chapter 4.
Lines 10–14: We define
memory_sentences(), the extraction step of a simple pipeline. Any sentence of the form “update stored fact X to Y” becomes a subject and a value to save.Lines 18–20: We search for a Redis question with BM25 and extract the memory-shaped sentences from
note-10.
The note looks like a normal search result, and it ranks first:
top 3: [('note-10', 2.33), ('note-9', 1.23), ('note-4', 1.19)]memory-shaped sentences in note-10: [('checkout first step', 'restart the Redis cache')]
This is an indirect injection. The attacker never talks to the assistant. They place text where the assistant will read it later, written to look like ordinary content, and wait for some part of the pipeline to treat it as an instruction.
Watch the stored fact change
Let’s seed memory the way earlier sessions did, plus one fact an engineer stated about the checkout runbook. Then we pass the extracted sentence to reconcile(), as a pipeline would.
Here’s what the code does:
Lines 7–8: We define
USER_FACT, a statement an engineer made: the first step for checkout latency is to turn off the newest feature flag. This matches the current runbook,note-1.Lines 11–14: We define
seed_memory(). It clears memory and replays the first three statements from lesson 5.5 plusUSER_FACTthroughreconcile().Line 19: We print the stored first step.
Lines 20–21: We pass each sentence extracted from
note-10toreconcile(). ...