Advanced

The defence stack, in order

Defences are not equal, and they are not interchangeable. They sit in a fixed order, cheapest and least dependent first, so that the expensive and fragile ones only ever see what the cheap ones missed.

What you will see

One. The answer is inside the closed set, so the stack released it.

About 1 min of reading

Try It

Run one input through three layers

1

Layer one, escape

printf 'x <<>> y\n' | sed 's/<< /tmp/l1.txt; cat /tmp/l1.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
The marker broken. Layer one changed the text.
2

Layer two, fence

printf '<<>> %s <<>> ' "$(cat /tmp/l1.txt)" > /tmp/l2.txt; grep -c 'END_USER_TEXT' /tmp/l2.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One. Exactly one closer means the fence is intact.
3

Layer three, shape

printf 'safe\n' > /tmp/l3.txt; grep -c -x -f /tmp/l3.txt <(printf 'safe\nunsafe\nunsure\n')

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One. The answer is inside the closed set, so the stack released it.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Input through a fixed stack
1 Input is escaped
↓
2 Escaped input is fenced
↓
3 The model answers inside a shape
↓
4 The output is detected and judged before release
Why this works
Why the cheap layers come first

Cheap layers run on every request. An expensive layer that runs on every request costs the request twice over.

The stack has one exit

One exit means one place to log. Two exits means two places, and one of them will forget the case.

Examples

The order that matters

Escape, then fence, then shape, then detect, then judge. Each layer sees only what the previous layer let through.

Why the model is never a layer

Asking the model to refuse is not a layer. The model is the thing being defended, and it cannot defend itself.

Where a layer is allowed to fail

A layer may fail only if the next one can see the failure. Silent pass through is the bug that makes a stack useless.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Auditing which inputs reach the model
Previous Lesson
Auditing which inputs reach the model
Next Lesson
Tool calls are the real risk
Tool calls are the real risk →