Article
Hardening the AI Assistant on My Own Site Against Prompt Injection
The assistant on this site can call tools and save visitor details. Here is what I changed to make it harder to misuse, and what those changes do not guarantee.
2 min read
The chat endpoint on this site used to accept messages with role: "system". The browser never sent one, but anyone calling the API directly could append instructions with the same authority as mine. For someone who says he works on AI security, that was a good place to start.
The attack surface#
The assistant on this site answers questions about my work. It has three read-only tools (list projects, read a case study, list writing) and one write tool, captureLead, which saves a visitor's name and contact details so I can follow up. Every message goes to a hosted model.
Small as it is, it has the same shape as larger LLM systems: untrusted text in, tool calls out, a database behind it. That gives three things worth protecting: my instructions, the data the write tool stores, and my model bill.
Why the obvious defence isn't enough#
The tempting fix for prompt injection is a stronger system prompt. But a prompt is guidance, not enforcement: a determined attacker can often talk a model past it. So the prompt is only one layer, and the layers that actually hold are the ones in code.
The layers#
1. Clients can't add instructions#
The endpoint accepts only user and assistant messages. Each is cut to 2,000 characters and only the last 20 are kept:
// "system" is deliberately absent: a client must never be able
// to add instructions of its own.
function isChatRole(role: unknown): role is ChatRole {
return role === "user" || role === "assistant";
}2. Visitor text is data#
The system prompt says that everything a visitor writes, and everything a tool returns, is data, never instructions. Requests to ignore the rules, adopt a persona or reveal the prompt are declined, and the conversation carries on.
This is the weakest layer. It handles casual attempts. It is not the security boundary.
3. What the model writes is untrusted input#
The model fills captureLead from visitor text, so its output is untrusted input to my database. Every field is trimmed and capped at 300 characters, and a value in the email field that doesn't look like an email is dropped rather than stored.
4. Cost is bounded across instances#
Rate limits used to live in each serverless instance's memory, so a script spread across instances could get far more than the limit. They now live in Postgres and are shared: 12 messages a minute and 150 a day per client. Client identifiers are hashed with a salt before they are stored. If the database is unreachable, the limiter falls back to per-instance memory rather than failing open.
Reality check#
- The model can still be persuaded to say things I wouldn't. The prompt rules reduce that; they don't prevent it.
- Tool access is the real boundary. The assistant can read only published content and write to one table. That's deliberate, and it's why the remaining risk is mostly embarrassment rather than data loss.
- No systematic red-team evaluation yet. Everything above is design review and code, not measured attack success rates.
Takeaways#
- Put the boundary in code: which roles are accepted, which tools exist, what they may write. Treat the prompt as a filter for the casual cases.
- An LLM's tool arguments are user input that has passed through a model. Validate them like any other user input.