The server can't read your chats any more. The AI still has to.
Putting an AI chat back together in the browser after end-to-end encrypted storage landed, and being honest about what it does not protect.
I pointed our frontend at the new backend and typed “hello”. The answer streamed in normally, word by word. Then, the moment it finished, the whole answer turned into a line of strange text on the screen: {"v":1,"encDek":"....
That was my first test of end-to-end encrypted chat storage in our AI chat assistant. The backend now locked every saved message so that only the user’s own browsers could open it. The server could still save chats, but it could never read them back.
This article is about what broke when that happened, how to put the product back together in the browser one small phase at a time, and what this kind of encryption does not protect.
What end-to-end means here
The system uses a key pair. The public key can lock data, and anyone can have it; only the private key can unlock that data. In our design, the server knows the user’s public key. The private key lives only in the user’s browsers, stored in IndexedDB, which is a small database built into the browser.
On an AI product, “end-to-end” means something narrower than it does in a messaging app. The AI model has to read your message to answer it. So the flow looks like this:
- The browser sends the message to the server as plain text, over a normal secure connection (TLS).
- The server passes it to the model and streams the answer back, also as plain text.
- When it saves the conversation, the server locks the question and the answer with the user’s public key. It keeps no copy it can open.
- Later, when the user opens the chat, the server returns the locked data. Only a browser with the private key can open it.
So the promise is about stored chats. Someone who gets the database or the backups sees only locked data. But at the moment of answering, the server and the model provider do see the text. A hosted model can’t answer something it can’t read. Profile fields were not locked in this round either. And a share link is a deliberate exception: the user gives the server a readable copy of that one chat.
That narrower promise is still worth having, and it is what created all the work below.
The starting point
The cryptography code was written by a frontend teammate earlier in the year. A backend teammate built the locking on save, and new request fields so the browser could send context to the server.
What was left was everything between “the backend now returns locked data” and “the product works again”. Much of the implementation went through Claude Code. Each phase had tests first, then a scripted check in a real browser, then a manual test.
The problem
The broken “hello” showed me the real size of the job. After every answer, the chat page loads the conversation again from the server to match up message IDs. That fresh copy was now locked, so opening locked data had to come first.
Display was only the start. Many features had depended on the server reading old messages:
- The AI used the stored messages as its memory of the conversation. Now it forgot everything.
- The AI used to see the text of files uploaded earlier. Now it forgot them too.
- Share links failed, because the server could not read the chat it was asked to publish.
- Chats saved under the old system showed up as long strings of random-looking characters.
Opening everything in one place
Locked values came back in the same fields that used to hold text. There were eight different chat requests: the chat list, a single chat, search, favourites and others. Each one shaped its data a little differently.
Instead of fixing eight screens, I added one step in the service layer that walks through each response and opens every locked value. The screens never know that encryption exists.
Here is the idea, simplified. Two details matter: the key is loaded once per batch, and a value that can’t be opened shows a short label, never raw locked text.
export function makeOpener(privateKey: string | null) {
const keyPromise = privateKey
? importKey(privateKey).catch(() => null)
: Promise.resolve(null);
const cache = new Map<string, Promise<string>>();
return (value: unknown) => {
if (!looksLocked(value)) return value; // older plain text passes through
if (!cache.has(value)) {
cache.set(value, keyPromise.then(key =>
key ? openValue(value, key) : LOCKED_LABEL
).catch(() => LOCKED_LABEL));
}
return cache.get(value);
};
}
The cache matters because the chat loads again after every message. Without it, the browser would unlock the whole history each time, and long chats would get slower and slower.
For the tests, I locked sample values exactly the way the backend does, using Node’s crypto module. Then the browser code had to open them.
A logout that did nothing
One early check found a bug that had nothing to do with chats. Logout never removed the keys from any browser.
The user ID in the login token was a number at runtime, even though our TypeScript type said it was a string. The keys were saved under the string version of the ID. IndexedDB treats 3 and "3" as two different keys, so the cleanup always looked in the wrong place and found nothing.
The fix was one word: String(id). I wrote a test that failed first, then made it pass.
Giving the AI its memory back
The server used to build the AI’s context from the last saved messages. Now it couldn’t read them. So the browser started sending the recent turns with every new message, using the same limits the server used before.
My test was simple. In a new chat I wrote “my favourite colour is teal”, and then asked “what is my favourite colour?” It answered teal. After a reload, it still did.
My first rule for which messages to include was that a user message counts once it has an ID from the server. It failed sometimes: in a brand-new chat, the first message’s server ID could end up in the wrong place in the browser’s state, so the rule dropped that message and the AI forgot the very first thing the user said.
The rule that worked does not trust IDs at all. A user message counts when a finished answer follows it. The message being sent right now has no finished answer yet, so it stays out, which is what the server expects.
Files the AI could no longer see
Uploaded files had the same problem. The server used to add the text of recent files to the prompt by itself. Under encryption, it only knows about earlier files if the browser sends them.
So I copied the server’s old rule into the browser: a file stays in context for its own question and a number of questions after it, with limits on how much text is sent. Chats without files make no extra request, and a failed request never blocks the message.
To prove it worked, I ran a control test. I uploaded a PDF invoice with a code in it that the model could not guess. With the file context removed from the request, the model said no invoice had been mentioned. With it, the model quoted the code.
The answer that disappeared
Sometimes an account had no public key on the server yet, for example when the first setup failed. In that case, the backend still answered, but it saved the message as blank and flagged it. I added a warning for each such message: “This message wasn’t saved”, with a button to set up encryption again.
Testing this found a worse bug: in a new chat on such an account, the answer streamed in and then disappeared. In 1 run out of 3, the question disappeared too. A new chat reloads its saved copy right after the answer finishes. That copy was blank, so it replaced the text the user had just read.
The fix keeps the text on screen when the server’s copy of that message is blank. After the fix, the answer stayed in 3 of 3 timed runs.
Share links and old chats
The server can’t read the chat it is asked to share. So now the browser opens the whole chat and sends the readable title and messages, within the server’s limits. One rule was important to me: if this browser can’t open every message, it does not share the chat. Otherwise the public page would show lock labels instead of text. The dialog explains why.
Chats from before this change came back as raw encoded text. I gave them a clear label, “Saved before end-to-end encryption”, and an “Upgrade older chats” card that asks the server to lock them again with the user’s key.
This phase also added one rule, checked in three places: a lock label is never sent as AI history, as file context, or in a share link.
Where it stands
All six phases were committed in one day, each with tests.
Two things are still not great. When the server can’t lock a message, it saves a blank row, so after a reload an unsaved chat shows empty messages. Fixing that on the server would help every client, including the mobile app. Also, the browser detects old-format chats by their shape. That works, but it is a guess, and every client has to repeat it.
What I learned
Make a list of everything that used to read the database. AI memory, file context, sharing and search all depended on the server reading old messages. Once it couldn’t, each one became a job for the browser, or it broke.
Don’t trust IDs while the browser is still matching its state with the server. “Was this message answered?” was a more reliable signal than “does it have an ID?”
Test encryption against the other side’s real code. Locking test data with the backend’s own method checked the format in a way my own round trip never could.
Run a control. Removing the new field and watching the model fail is what proved each phase actually did something.
Product names and some details are changed or left out. The engineering is real.