← Back to all products

Prompt-Injection Defense Kit

$39

Layered, tested defenses against prompt injection for LLM apps and agents: input isolation, a heuristic injection detector, an action gate, and 16+ attack tests proving what each layer stops.

📁 8 files🏷 v1.0.0 (updated 2026-08-02)
TypeScriptMarkdownJSONLLM

📄 Product Preview

Try the interactive reader and demo tools below, or get the full product with all content unlocked.

📖 Interactive Reader (Free Preview) 📦 Download Free Sample

📁 File Structure 8 files

prompt-injection-defense-kit/ ├── ATTACK-CATALOG.md ├── README.md ├── package.json ├── src/ │ ├── detect.ts │ ├── gate.ts │ └── isolate.ts └── test/ └── attacks.test.ts

📖 Documentation Preview README excerpt

Prompt-Injection Defense Kit

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.

The layers (defense in depth)

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).

What's inside

  • 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 caught

or neutralized (Vitest).

  • ATTACK-CATALOG.md — the injection techniques, what each layer does about them.

What this stops (and what it doesn't)

  • Stops / reduces: naive "ignore instructions" overrides, fake system prompts,

data-exfil-via-output when output is schema-constrained, unauthorized tool calls

(gated).

  • Does NOT fully stop: a determined adversary + a capable model can still be

steered. That's why layer 5 (least privilege) is non-negotiable — assume some

injections succeed and cap the blast radius.

Requirements

Node 18+, TypeScript 5+.

License

MIT.

📄 Code Sample .ts preview

src/detect.ts/** Heuristic prompt-injection detector. Not a guarantee — a signal. Use it to * flag/score/route suspicious input (e.g. require review, lower trust, log). */ const PATTERNS: Array<{ re: RegExp; weight: number; label: string }> = [ { re: /ignore (all |the )?(previous|prior|above) (instructions|prompts?)/i, weight: 5, label: 'override' }, { re: /disregard (everything|all|the above)/i, weight: 5, label: 'override' }, { re: /you are now\b|from now on,? you (are|will)/i, weight: 4, label: 'role-hijack' }, { re: /new (instructions|task|system prompt)\s*:/i, weight: 4, label: 'reinstruction' }, { re: /<\/?(system|assistant|user)\s*>/i, weight: 4, label: 'fake-role-tag' }, { re: /\[?(system|admin)\]?\s*:/i, weight: 2, label: 'role-prefix' }, { re: /reveal (your |the )?(system prompt|instructions|secrets?)/i, weight: 5, label: 'exfil' }, { re: /print (your |the )?(prompt|instructions|api key|token)/i, weight: 5, label: 'exfil' }, { re: /do not (tell|inform|alert) (the )?(user|owner)/i, weight: 3, label: 'stealth' }, { re: /base64|rot13|reverse the following/i, weight: 2, label: 'obfuscation' }, { re: /(pretend|roleplay|act as).{0,20}(no (rules|restrictions)|jailbreak|DAN)/i, weight: 4, label: 'jailbreak' }, ]; export interface Detection { score: number; suspicious: boolean; labels: string[]; } /** Score text for injection signals. suspicious=true above a tunable threshold. */ export function detectInjection(text: string, threshold = 4): Detection { let score = 0; const labels = new Set<string>(); for (const p of PATTERNS) { if (p.re.test(text)) { score += p.weight; labels.add(p.label); } } return { score, suspicious: score >= threshold, labels: [...labels] }; }
Buy Now — $39 Back to Products