Practical, testable defenses against prompt injection — the #1 security issue
for LLM apps and agents. When your app feeds untrusted text (web pages, documents,
emails, tool outputs) into a model, that text can carry instructions that hijack
your agent. This kit gives you layered, code-level mitigations and a battery of
attack tests to prove they work.
Honest framing: prompt injection is not "solved". No single trick makes a model
immune. Defense is layered risk reduction — this kit implements the layers
that measurably help and shows what each does and does not stop.
1. Input isolation — wrap untrusted content in delimiters and tell the model to
treat it as data, never as instructions. src/isolate.ts.
2. Injection heuristics — flag classic override phrases ("ignore previous
instructions", "you are now…", fake system tags) before/after the model.
src/detect.ts.
3. Output allow-listing — constrain what the model is allowed to *do* with a
strict output schema, so an injected instruction can't invent a new action.
4. Tool/action gating — never let model output directly trigger a
side-effecting tool without a policy check + human/owner approval for the
dangerous ones. src/gate.ts.
5. Least privilege — the agent's tools/keys are scoped so a successful
injection can't do much (the real backstop).
src/isolate.ts — safe wrapping of untrusted content + a hardened system preamble.src/detect.ts — heuristic injection detector (patterns + scoring).src/gate.ts — action gate: policy-check model-proposed actions before executing.test/attacks.test.ts — 20+ known injection payloads asserting they're caughtor neutralized (Vitest).
ATTACK-CATALOG.md — the injection techniques, what each layer does about them.data-exfil-via-output when output is schema-constrained, unauthorized tool calls
(gated).
steered. That's why layer 5 (least privilege) is non-negotiable — assume some
injections succeed and cap the blast radius.
Node 18+, TypeScript 5+.
Get the full Prompt-Injection Defense Kit and unlock everything.
Get the complete guide with every chapter unlocked, including code samples, diagrams, and best practices.
Access all interactive tools with complete data, all workload profiles, and the full scenario library.
Downloadable source code, configuration files, and working examples from every chapter.
Free updates for life. Every new chapter, tool, and improvement included.