← All blog posts
Engineering · 5 min read

Locking Myself Out of My Database on Purpose

Supporting cloud AI apps meant holding keys on my server. Here is how I made my database useless between requests.

If you run NotiBuddy from your own machine, you don’t have to care much about how my server works. Your computer holds the keys, encrypts every prompt before a byte leaves your network, and verifies your iPhone’s signed reply locally. My relay just shovels ciphertext back and forth.

Cloud AI apps like ChatGPT and Claude break that model. They run on someone else’s servers, send plain JSON tool calls over HTTPS, and have nowhere on the client side to hold a key or run my cryptography. Supporting them at all meant hosting an MCP server of my own to hold keys and talk to your phone on their behalf.

If I couldn’t avoid touching live requests on the hosted path, I at least wanted to minimize how long your data stays hot. As soon as a request finishes, my server shouldn’t be holding keys it can open on its own.

Why deleting a row doesn’t delete a key

When I shipped the first hosted build in late November, I reached for the naive, straightforward setup almost everyone starts with: generate fresh keys for each connection, encrypt them with a secret in my Worker environment, and run DELETE when you disconnect.

The catch with that setup is backups. Managed cloud databases keep point-in-time recovery history, thirty days of it in my case, so DELETE clears the live table right away and leaves weeks of snapshots untouched. As long as my Worker configuration held the only wrapping key, last week’s snapshot plus my own environment secret was enough to reopen a connection you thought you had removed.

I run this project alone, which makes sitting on a master key like that unappealing for the same reason I wrote about in AI and the Era of the Maximum Viable Product. I would much rather build storage I cannot unlock on my own than promise to guard another secret forever.

Wrapping the vault with a token I don’t keep

Every MCP call from ChatGPT or Claude already arrives with a secret my server has no reason to save: the OAuth token in the Authorization header. The first build checked that token and then unlocked your keys with a static server secret. Now I mix the raw token into the encryption key itself and store only a hash, so I can recognize the token next time. Once the request finishes and the token leaves memory, NotiBuddy alone cannot unlock the stored keys.

OAuth expires access tokens and swaps them out using a separate refresh token, so I lock each connection’s credentials once under a random vault key and keep two small wrappers around that key. Rotating tokens never has to rewrite the vault itself:

// Saved in the database (raw tokens and vaultKey are never stored):
row.tokenHash  = sha256(accessToken)
row.byAccess   = encrypt(key: hmac(serverSecret, accessToken),  data: vaultKey)
row.byRefresh  = encrypt(key: hmac(serverSecret, refreshToken), data: vaultKey)
row.vault      = encrypt(key: hmac(serverSecret, vaultKey),     data: senderKeys)

// Rotating tokens only re-wraps the 32-byte vaultKey; row.vault stays put.

If you are reading this in a browser, the simulator right above runs those exact WebCrypto steps live, so you can try rotating tokens, disconnecting, and attempting to crack a thirty-day backup with the server secret.

Dropped refreshes and locking myself out of cleanup

Tying database keys to rotating tokens gave me two ways to shoot myself in the foot. The first is a dropped connection mid-refresh. If my server rotates your refresh token and your network drops before the response arrives, your AI app will retry a second later with the token it just spent. Had I thrown the old wrapper away immediately, one flaky Wi-Fi handoff could have bricked the vault for good. So I keep the newly minted token pair sealed under the outgoing refresh token for sixty seconds. A retry inside that window gets the same answer back, and a replay after it revokes the connection. Those transitions were easy enough to get subtly wrong that I ran the rotation rules through the same formal policy models I use elsewhere in the codebase.

The second problem showed up in background cleanup. Each connection registers a public key on my relay so your iPhone knows who is allowed to reach it, and removing that registration normally requires a signature from the sender’s private key. When an abandoned connection times out after ninety days of silence, nobody is on the line with a token to unlock that key. Deleting my database rows alone would leave a dead connection sitting on your phone.

To get out of that corner, pairing now pre-signs a single-purpose self-destruct message in the brief moment before the new sender key is sealed away. The relay accepts that signature for one action only: permanently removing that sender. Cleanup can wipe the database and clear your phone without ever opening the vault.

Where hosted trust starts and stops

None of this turns a hosted server into end-to-end encryption. While a call is in flight, my Worker is in the loop: it issues the OAuth tokens, unwraps the connection’s keys in memory for a few milliseconds, drops outgoing prompt text as soon as the relay accepts it, and checks your phone’s reply signature on the AI app’s behalf.

It stores no message content in plaintext at rest, though memory is messier. Dropping a reference in JavaScript only hands the string to the garbage collector, and I can’t promise those bytes are zeroed the instant a call ends. If you are sending something you don’t want passing through my Worker’s memory at all, that is what the local client is for.

When you run locally, my server’s code doesn’t matter, because everything is encrypted before it leaves your machine. The hosted bridge asks more of you: you are taking my word that the running Worker throws those tokens away when a request ends. Even so, making cold rows and thirty-day backups useless to me on their own was worth building, because the fewer live secrets sitting around in my infrastructure, the less there is to go wrong.