The allowlist said the model could call refund_order, and the schema said order_id must be a string matching ^ord_[a-z0-9]{12}$. Every call in the incident passed validation. Every call also refunded an order belonging to a different customer. Validation was green the whole time because we had asked it to answer a question it can't answer.
This post is about LLM tool call security, specifically the habit of treating a JSON-schema validator as the security boundary. A schema tells you whether a payload is well-formed. It says nothing about whether this caller may do this thing to this object. Below are three failure modes we've seen while building AI integrations and auditing other teams' code, and the server-side check that closes each one.
Why a schema validator is the wrong boundary
The usual setup: you register tools with the model, each with a JSON schema. The model emits a tool call, you validate the arguments against the schema, and you dispatch. It feels like input sanitization, a familiar and respectable thing.
The flaw is in the threat model. In a normal API, the caller is a client you authenticated. In an LLM loop, the "caller" is a text generator whose input includes retrieved documents, emails, web pages, and user messages, any of which can contain instructions. Prompt injection means the arguments are attacker-influenced even when the session belongs to a legitimate user. Schema validation checks shape. The attacker only has to produce the right shape.
The principle we now apply: treat the model's tool call exactly like an unauthenticated request body from the open internet, then authorize it against the session's real identity, not anything the model says.
Failure mode 1: argument smuggling
We had a search_notes tool with a schema like {query: string, filters: object}. The filters object was loosely typed because the backend supported several filter keys. The handler did roughly this:
- Validate against the schema (passes,
filtersis any object). - Spread
filtersstraight into the database query builder.
A document in the retrieval corpus contained text instructing the model to include {"owner_id": "*"} or another user's ID in the filters. The schema had no opinion on that key because additionalProperties defaulted to true. The query builder happily honored it.
The fix
Two layers. First, set additionalProperties: false everywhere and enumerate filter keys. That is hygiene, not the boundary. The actual closure is server-side: the handler never accepts tenant or owner scoping from the model at all. It takes owner_id from the authenticated session and applies it as a mandatory predicate the model can't override:
- Build the query from an explicit allowlist of model-controllable fields.
- Always add
WHERE owner_id = :session_userfrom server state. - Where possible, enforce it again in the database (row-level security), so a handler bug can't undo it.
Failure mode 2: the confused-deputy ID
This is the refund scenario from the opening. The model is a deputy holding the user's authority. It resolves "refund my last order" into an ID, and if it can be talked into emitting a different ID, the system performs the action with the user's privileges against someone else's object. The regex proved the ID looked like an order ID. It never proved the ID was this user's.
Variants we've seen in audits: IDs lifted from a retrieved document, IDs the model "remembered" from earlier in a long context, and sequential IDs the model simply guessed at.
The fix
Object-level authorization on every tool call, in the handler, using the session identity: load the resource, check order.customer_id == session.customer_id, and return the same generic not-found error for both "doesn't exist" and "isn't yours" so the model can't be used as an enumeration oracle. If your REST API already does this for human clients, route tool calls through the same service layer instead of writing a second, thinner path. Most of the bugs we find are in that second path.
For destructive actions, add a step the model cannot satisfy by itself: a confirmation token issued by your UI after the user clicks, bound to the specific tool, arguments, and session, and single-use.
Failure mode 3: validators that pass malformed-but-coercible values
This one is the least glamorous and the most common in code reviews. Many validators coerce by default. A schema says quantity: integer, maximum: 10, and the library cheerfully accepts "5", 5.0, or in some stacks true. Then the handler, in a different language runtime or a different layer, interprets the raw value differently than the validator did.
The failure shape is a time-of-check/time-of-use gap: the validator inspects one representation, and the handler consumes another. We've seen it with numeric strings that overflowed in the database driver, with Unicode lookalike characters that passed a pattern but normalized to something else downstream, and with duplicate JSON keys where the validator saw the first value and the handler the last.
The fix
- Validate once, then use only the validator's output object. Never go back to the raw payload.
- Turn coercion off (strict mode) so type mismatches are rejected rather than repaired.
- Re-check business limits in the handler:
quantityagainst actual stock and the user's actual entitlements, not just the schema maximum. - Reject payloads with duplicate keys at parse time.
What the boundary actually looks like
After these incidents, our baseline for any LLM integration is short. The schema stays, because it gives the model better tool descriptions and cheap early rejection. But the real boundary is a handler that authenticates from the session, authorizes the specific object, derives scope from server state, and logs the call with the identity it was authorized under. We also keep tool permissions narrow per session, so a read-only conversation simply doesn't have write tools registered, rather than relying on the model to behave.
A useful test: for each tool, assume the model's output is hostile and the schema is bypassed entirely. What is the worst the handler would do? If the answer depends on the model being well-behaved, the boundary is in the wrong place.
orithLabs does this kind of work in two directions: building AI features with these constraints from the start, and auditing existing codebases where the tool-call path quietly skipped the authorization the rest of the API enforces. If you're wiring an LLM into a production backend and want a second pair of eyes on that handler layer, we're happy to talk it through with you, engineer to engineer.