All writing
SecurityAIAgentsSystems

I Build as if Every Token Will Leak

My agents touch my brain, my apps and my keys all day. So every part of the system assumes a token will leak, and makes a leaked one narrow, loud and quick to kill.

Max KellyNovember 8, 20266 min read
I Build as if Every Token Will Leak

Here is what a leaked token can do in my setup.

It can do what its grant allows, and nothing more. It cannot read a single API key. If it reaches for a key it does not normally read, I get paged. The record of its calls cannot be quietly edited. And once I revoke it, its next call fails.

I run a lot of agents. Some live on my own machines and some run in the cloud. All of them reach my brain, my connected apps and, one step removed, my keys. Every one of them holds a credential, and an agent setup has a lot of places for a credential to end up.

So I stopped asking whether a token will leak. I design for the day it does.

I rebuilt how my systems handle keys around that idea. These are the rules, and what each one looks like in what I run.

A token gets one job

My AI tools and agents reach my brain, MxMind, through a front door, MxGateway. Each one gets its own grant: one agent, in one place, acting for me. I wrote about the permission model in Your Company Brain Is a Permissions Problem.

The grant's profile decides what it can do. A work grant gets the brain's general tools and nothing else. It cannot read a key, not even as a sealed file. Every grant has an expiry.

The brain stores only a hash of each token, so a copy of its database holds no usable token. The token itself is shown once, when it is minted.

When one job needed two kinds of access, it got two tokens. The agent that files notes into the brain and the one that answers questions used to share a token. Now each has its own, with access to exactly the keys its job uses. Key access is denied by default.

The same limits hold when the agent itself goes wrong. An agent steered by something it read is held to its grant's profile, its own keys and the same pages as a stolen token.

No token can read a key

This is the rule I care about most.

MxMind holds every API key I use, and it cannot read any of them. Each key is sealed with age to the public keys of the machines allowed to use it, plus a recovery key that lives offline. The private keys stay in each machine's own keystore. The server stores ciphertext it has no way to open.

So no grant can read a key's value. An operator grant, the widest I issue, gets the sealed file at most. To use a key, an agent has to be on one of my machines, where the local vault client opens it. Opening a key takes two things: a token allowed that key, and a machine it was sealed to.

Writes follow the same rule. A key is sealed and signed on my own device before it leaves, and the server refuses a plain value. Every machine also pins the list of machines it seals to. A recipient slipped in on the server side reaches nobody, and the change pages me.

The old vault encrypted keys under a master key held by the server, which meant the server could read everything. Once every key was resealed to my machines, I deleted the master key.

The dangerous moves are loud

Every key read is recorded with who asked and from where. Some reads page me on the spot:

  • a key that caller does not normally read
  • a read from an address that caller does not normally use
  • more keys in an hour than any job needs
  • any refused request

Some entries in the vault are decoys, under the names an intruder would go for first. Reading one pages me, whoever asks. Nothing I run reads them, so a decoy read is never a false alarm. Nothing in the vault listing tells an agent which names they are.

The break-glass token sits in the vault, not with any agent, and every use of it pages me. So does every new grant, rotation and revocation.

The record has to be hard to fake

Every call the brain decides, allowed or refused, gets a row. The row has the grant, the tool and the outcome. It never has the arguments, the results or a token. That keeps the log from becoming a second copy of the brain. It shows which tools a leaked token called, not what it read.

Each row is signed with a key that is not in the database, and chained to the row before it. Someone with database access alone cannot edit, reorder or insert a row without breaking the chain. The head of the chain is posted outside the database every day, and every Sunday a full check walks it back to the start. The latest full check found no breaks.

Rotation is a design property

A grant's token rotates in place. With an overlap window, the old token keeps working for a set number of hours so the client can switch without a gap. A token that leaked gets revoked instead, which takes effect on its next call.

A token nobody uses gets deleted, not rotated.

A lost machine means rotating every key that machine could open. Removing it from the vault stops new keys reaching it, but it cannot take back what that machine could already open.

New tools start with nothing

I connect a lot of AI tools to my brain, and I want to try new ones.

Apps sign in to MxGateway with OAuth, using my passkey. There is no password. A sign-in alone gets nothing. The app has to be bound to a grant first, and revoking that grant ends the app's sessions on its next call.

A second agent tries to break it first

I do not trust my own review of my own security code.

Changes to the vault and the gateway go to a second agent whose only job is to break them. It writes a failing test for each attack it tries, and its verdict, SHIP or FIX, names an exact commit. A FIX goes back for changes and comes back for another verdict.

It catches failures I would not have thought to test. One was in the alerts themselves. If the brain waits for an alert to post before it starts serving, an outage at the messaging service keeps the brain down too. Now the brain records the event first and sends the alert after, and the alerts that matter most, like a decoy read, go to the front of the queue.

What it adds up to

None of this makes a leak impossible. It makes a leak small.

That is the bar I hold any system to before I let it near my keys.