DEV Community

Cover image for The API key can cancel bookings
Hoegun You
Hoegun You

Posted on Originally published at blog.hoegun.xyz

The API key can cancel bookings

When I went through a supplier's API documentation to separate the reads from the writes, the HTTP method was no help.

Every booking endpoint was a POST. Looking up bookings was a POST. Confirming one was a POST. Cancelling one, which also cancels the payment, was a POST.

This is part 3 of a series on Prism, the gateway I built so my teammates' AI agents can read company data safely. Part 2 was about the database, where I could lean on the database's own permissions. A supplier's API gives me nothing like that. The key I hold can do everything, and suppliers don't issue a read-only one.

You can't sort by method

Here's the shape of the problem, with made-up paths:

POST /bookings            look up bookings by date or number
POST /booking/confirm     confirm a booking
GET  /listings/{id}       read a listing
PUT  /listings/{id}       change a listing
Enter fullscreen mode Exit fullscreen mode

Three things in those four lines broke the shortcuts I had in mind:

  • The method doesn't tell you. A search that takes a body is a POST, and so is a confirmation.
  • The same path can be both. One method reads it and another writes it.
  • A lookup and a write can be one character apart. /bookings and /booking/confirm. Any rule based on prefixes or wildcards lets a write through sooner or later.

So the rule in Prism is the least clever one available: a request is relayed only if its method and its path both exactly match an entry on a list. Everything else is refused. The list never infers anything.

A list made by reading

Building the list meant going through every endpoint in the supplier's spec, a few dozen of them, and putting each into one of three piles:

  • Lookups, allowed. About a third of the total.
  • Lookups, blocked anyway. A couple of endpoints return text that customers typed, such as inquiries. Free text can contain anything, so they stay closed until someone needs them.
  • Writes, blocked. Everything that creates, changes, confirms, cancels or refunds.

One allowed lookup returns customer details, because bookings have customers. That one needs a separate permission, which most people won't have.

For each allowed POST, I went back to the spec and read its request fields one by one, to confirm they were all search criteria and none of them set a value. One field had the same name as a field in a write endpoint. Here it was a filter. The endpoint that sets it lives at a different path.

A list like this is only as good as the document it was built from. Reading a spec can't tell you about behaviour the spec doesn't mention. That's a limit of the method, and it's why the list starts as small as the first project allows.

Refuse, don't repair

What happens to a supplier call: the agent sends a method, path, query and body with no headers or host; ambiguous paths are refused; only exact matches on the allowlist continue, so a write never reaches the supplier; one rate-limit response pauses all agent calls on that key; the gateway adds the real key, follows no redirects and retries nothing; secrets are scrubbed before the response and the log entry leave

An exact-match list has a classic weakness: the string you check isn't always the string the other server acts on. /bookings/../booking/confirm might match one pattern here and resolve to something else there.

The usual fix is to normalize the path first. I went the other way. If a path is ambiguous in any way, Prism refuses it and sends nothing. That covers dot segments, doubled slashes, encoded slashes and backslashes, double encoding, malformed escapes, control characters, and a ? or # inside the path. The query string has its own field, because the list only looks at the path and anything mixed into it would slip past the check.

The result is a property I can state in one line: the path that was checked is the path that is sent.

The agent can't aim the request

The tool the agent calls takes a method, a path, a query and a body. That's all.

  • No headers. The agent has no way to set one, so nothing from its side can reach the supplier as a credential, a cookie or a forwarding header.
  • No host. Each supplier's address is fixed in Prism's configuration.
  • No account code. Part of the supplier's path identifies our account. The agent writes a placeholder there, and Prism fills in the real value after the match.

Prism adds the authentication itself. Two more rules apply on the way out. It doesn't follow redirects, because a redirect could carry the key to a different host. And it doesn't retry, because a gateway that retries multiplies load exactly when the supplier is struggling.

Someone else is using this key

The key Prism uses is the same key our production systems use to take real orders. Suppliers count requests per key. If an agent burns through the quota exploring a catalogue, a customer's booking fails.

I think this is the biggest risk in the whole feature, bigger than a leaked token, because it needs no attacker. It only needs a curious agent in a loop.

So the first design goal for quotas was: Prism gives way to production.

  • Each person has a per-minute limit, and all agents together share a conservative overall limit.
  • If the supplier answers "too many requests" even once, Prism stops every agent call on that key for a while. It doesn't matter who caused it.
  • After several failures in a row, Prism stops calling and probes again later with a single request.
  • The pause is stored in the database, so restarting Prism doesn't quietly lift it.
  • The refusal tells the agent when it may try again, so it doesn't sit in a loop asking.

Looking for my own key

The agent never has the real key, so why scan for it? Because a person might. Someone pastes a key into a conversation to "help", and the agent passes it along.

Prism checks the path, the query and the body of every request for the real secrets it holds. If it finds one, it refuses the request and raises an alert. It also removes its own secrets from responses, in case a supplier echoes one back.

A test found a bug here that I wouldn't have found by reading. When a request containing a real key was refused, the refusal worked, and the key was written into the log entry describing the refusal. The safeguard had created a second copy of the secret. The fix was a final scrub over the whole log entry just before it's written, whatever path the request took to get there.

How I know a write never leaves

The promise is that a write is never sent, and a promise like that needs a test, not a code review. The tests run against a fake supplier that counts the calls it receives. For write endpoints, and for the path tricks above, the expected count is zero.

What it costs

  • Someone has to read every spec. Each new supplier means classifying every endpoint by hand. A new endpoint is one line of configuration, plus the reading.
  • The list can go stale. If a supplier changes what an endpoint does, the list doesn't know.
  • Agents hit walls. A refusal is a normal part of using it, so the refusal has to explain itself. That turned out to matter more than I expected, and it's the next post.

What I'd tell someone building one

  • Don't trust the method. POST doesn't mean write, and GET doesn't promise read.
  • Match exactly, deny by default. A list that needs wildcards will eventually match something you didn't mean.
  • Refuse ambiguous input instead of fixing it. Then what you checked is what you send.
  • Give the caller as few fields as possible. Every field the agent can't set is an attack you don't have to think about.
  • If you share a key with production, yield. One rate-limit response should stop the agents, not the customers.
  • Scrub the log last. Your safeguards write logs too.

The series

  1. A read-only account is not a safe account
  2. I didn't write a SQL parser
  3. The API key can cancel bookings (this post)
  4. I designed it for curl. Agents needed a tool
  5. Who asked, and what did they get?

Next: the first version of this was a transparent HTTP proxy, and it was my own idea. Then agents started using it.

Which of the API keys your team shares could do more than the people holding them realise?

Top comments (0)