The permission you did not know you granted

I added one boolean to an old permissions table and handed several people write access to every customer account in the product. The change was six lines. I caught it before it shipped, but only because I went and read code I had no real reason to read.

We run a small internal server at Mixmax that speaks the Model Context Protocol, or MCP. It exposes tools to AI clients so that employees can ask questions about our own systems in plain language. A handful of those tools had started writing data instead of only reading it. Write tools need a shorter list of people than read tools, so the task was to add a boolean called mcpAdmin to the list of employees allowed to run them.

There was already a list, a long-standing employee capability collection in MongoDB keyed by lowercased email address. Holding a document in that collection is what lets a support engineer open a customer's account and see the product exactly as that customer sees it. Adding one more field to an allowlist that already exists is about as low-risk as a ticket gets.

I wrote the grant script, then read the code that consumes the collection. My one-field upsert was also handing people write access to every customer workspace we have.

Nobody had ever written to this table

The first surprise was archaeological. I went looking for the application code that inserts into this collection, expecting to find an admin endpoint or a settings page whose conventions I could copy.

There isn't one. There never was. I searched the whole service tree and the only inserts were in test fixtures. Every real document in that collection had been created by a person, by hand, in a Mongo shell, over a period of years. My grant script was going to be the first thing in the history of the system to write to it programmatically.

That did two useful things. It killed my first plan, which was to build a proper HTTP endpoint for granting the flag. A new endpoint that writes to a privileged capability collection is its own security review and its own audit-log design, and all of that to serve a few grants a quarter. Instead I used the pattern the codebase already had for this class of work, a one-off privileged script run from the bastion host that fronts production access. The gate there is that you already hold read-write production database credentials. That is a real gate, and somebody already reviewed it.

The second thing it did was make me slow down. When no code has ever written to a table, no code has ever had to be correct about its semantics. Whatever invariants that collection has live in the heads of the people who hand-edited it, and in the code that reads it.

So I read the code that reads it.

Access is not stored, it is inferred

Here is the shape of what I found, simplified and renamed:

// True when this employee may view a customer account as that customer.
async function canImpersonate(email) {
  const doc = await capabilities.findOne({ email: email.toLowerCase() });
  return Boolean(doc);
}

// Employees may act inside that account unless their document says read-only.
function canWrite(doc) {
  return doc.readOnly !== true;
}

Read those two functions together and the rule comes out. The existence of a document is the grant. The absence of a field is the escalation. Nothing in that collection is an explicit statement that a person may do a thing. Privilege is derived from the shape of the record.

Now look at what I had written:

await capabilities.updateOne(
  { email: email.toLowerCase() },
  { $set: { mcpAdmin: true } },
  { upsert: true }
);

For an employee who already had a document, this does exactly what it looks like. One new field, no change in access.

For an employee who did not, the upsert creates a document. That document has an email, an mcpAdmin flag, and no readOnly field, which by the rules above means full impersonation with write. Granted as a side effect of joining an allowlist for an unrelated internal tool, by a script whose commit message says it adds a boolean.

I want to be precise about what the mistake was, because "don't use upsert" is the wrong lesson. I assumed that a write to a collection is only an authorization change when the field being written is an authorization field. In a system where privilege is derived from record shape, every code path that can create a record in that collection is an authorization-granting code path. My script did not hand out write impersonation because of the field it set. It handed it out because of the field it failed to set.

A safe default, and the revoke problem it created

The fix is small enough to read in one breath.

await capabilities.updateOne(
  { email: email.toLowerCase() },
  {
    $set: { mcpAdmin: true },
    $setOnInsert: { readOnly: true, createdByMcpAdminGrant: true },
  },
  { upsert: true }
);

$setOnInsert only applies when the upsert creates the document. Somebody who already has a document keeps whatever access they have and only gains the flag. Somebody who doesn't gets a document that is read-only from the moment it exists. A grant made by this script can never confer write impersonation, in any environment, no matter who runs it.

The second field, createdByMcpAdminGrant, exists because revoke has the same problem pointing the other way. Revoking the flag from a document the script created should delete the document, since the script created it and it has no other reason to exist. Revoking it from a document that pre-dated the script should only unset the flag, because that document represents somebody's real support access, granted separately and for separate reasons.

Without that field, revoke cannot tell those apart. It either deletes documents it did not create, silently removing access that somebody granted on purpose years ago, or it leaves behind empty documents that still confer impersonation, which is the original bug with extra steps.

const doc = await capabilities.findOne({ email: email.toLowerCase() });
if (!doc) {
  return;
}
if (doc.createdByMcpAdminGrant) {
  await capabilities.deleteOne({ _id: doc._id });
} else {
  await capabilities.updateOne({ _id: doc._id }, { $unset: { mcpAdmin: '' } });
}

There is a leftover risk here, and I wrote it into the script's docblock rather than pretending it away. Read-only impersonation is still a capability, and this script hands it to people who did not previously have it. I decided that was acceptable, because exercising it is not passive. Looking at a customer account takes a deliberate, audited action through the internal console, which is a different risk from a silent standing write grant. That is a judgement call, and the point of putting it in the docblock is that the next person can disagree with it on purpose instead of discovering it by accident.

The same rule, pointed the other way

The same principle, absence of data meaning something, is also the strongest security guarantee in this system.

The internal MCP server is also reachable by external integrations, which authenticate as service accounts. A service account here is just an ordinary document in the users collection that happens to have no email field.

Nobody enforces that as a rule. It is a consequence of how emails get written. A human's email exists on their user document because an OAuth sign-in put it there. A machine never signs in through OAuth, so nothing ever writes the field, so the field does not exist.

And the admin check is a chain that starts by requiring an email:

async function isMcpAdmin(userId) {
  const user = await users.findOne({ _id: userId });
  const email = user?.email;
  if (!email) {
    return false;
  }
  const doc = await capabilities.findOne({ email: email.toLowerCase() });
  return Boolean(doc?.mcpAdmin);
}

A machine token fails at the first link, in every environment. We never wrote a rule blocking machines. There is no email to put on an allowlist, so the promotion is not expressible in the first place, and even a bug in the allowlist itself cannot reach them.

The alternative design was obvious and I am glad we did not build it. A second credential system for machines, service tokens in their own collection, with an isMachine check somewhere in the authorization path. That is more code, a second thing to keep in sync, and an if statement that somebody eventually inverts. Machines being ordinary users that cannot pass the admin check is fewer moving parts, and the safety property falls out of what the record lacks rather than from logic that can be got wrong.

A string that is not a boolean

One more from the same design review, because it is the same mistake wearing different clothes.

The admin flag crosses an AWS API Gateway authorizer boundary on its way to the handler. The authorizer is the small function that runs before your code, decides whether a request is allowed, and attaches context to it. Every value in that context is stringified in transit. A boolean false arrives as the string "false", and the string "false" is truthy in JavaScript.

So the natural code, if (context.isAdmin), promotes every caller to admin, including the ones the authorizer explicitly decided were not admins.

What we shipped is the boring version, with a test that would fail loudly if anyone made it clever:

function parseBooleanClaim(value) {
  return value === 'true';
}

Default deny, and the only string in the universe that passes is the one we meant.

Make absence mean less power

If you have a capability table anywhere in your system whose semantics are "a row exists" and "a field is missing", you have this shape, and you probably inherited it from somebody who is no longer around to explain it.

The rule I took away is not "stop using implicit authorization". Implicit authorization by absence is fine, and the service account case shows it can beat the explicit version, because you cannot forget to apply a property that nothing can express in the first place.

The rule is about direction. Absence of data should resolve toward less power, never toward more. A missing readOnly meaning full write access points the wrong way, and every backwards case of it turns ordinary writes into privilege escalations that no code review will catch, because the diff only ever shows the field you added.

The corollary is procedural. In a system that derives privilege from record shape, adding a writer to that collection is an authorization change, and it deserves the review an authorization change gets. My change was one boolean and six lines, and it should have been reviewed as if it had rewritten the permission model, because in effect it had.