# Browser extension API key security: scope it to one job

> Our in-browser extension used a full API key kept in a file. Now each launch gives it a token in memory that reaches its own profile and nothing else.

Source: https://mosaicbrowser.com/blog/companion-api-key

## The companion held a full-power API key

Our in-browser extension used a full API key kept in a file. Now each launch gives it a token in memory that reaches its own profile and nothing else.

![From a full API key to one profile's token](https://mosaicbrowser.com/covers/companion-api-key.webp)

Browser extension API key security mostly comes down to scope. A key an extension holds is a credential kept in a file, presented on every request, and usually far more powerful than the extension needs. Mosaic's companion, the extension inside every profile, held exactly that kind of key. We replaced it with a token issued per launch, kept in memory, that reaches one profile and nothing else.

## How it was reported

The report came with a screenshot of the API page. Among the keys a person had made sat a row called **In-browser companion**, `msc_ff27…67bb`, with a Revoke button beside it. The request was plain: stop using a normal API key for the extension, because the logic around it is fragile.

![The companion inside a profile's browser, showing that profile's two-factor codes](https://mosaicbrowser.com/shots/companion.png)
*Every call the popup makes is about its own profile*

## Three faults that compound

`companionSecret()` called `createApiKey('In-browser companion')`, kept the plain text in a file under Mosaic's data folder, and handed it to the extension at every launch.

**It appeared in the key list.** Revoking a key you do not recognise is the sensible thing to do. Here it broke the popup until the next launch made the key again. The honest label for that Revoke button was "put this back in a moment", and a control whose only effect is to break something until it silently repairs itself is not a control.

**It was a full-power credential.** That key could create profiles, delete them, read the whole library and drive every other profile on the machine. We read the popup's own source to see what it uses: every request is a `GET`, `PATCH` or `POST` on `/profiles/<its own id>` or a route under it. The widest authority in the product had gone to its narrowest client.

**It was a cached credential, and those go stale.** The file outlives the key through a revoke, a restored backup or a moved data folder. Keeping the two in step needed a whole mechanism, which existed only to cover the first mistake.

Mosaic is a desktop app

Email yourself the link and open it on your computer.

[How it works: REST API](https://mosaicbrowser.com/features/rest-api)

## Browser extension API key security: what replaced it

`src/main/api/companion-session.ts` makes a capability for the browser the launcher has just started. It is per launch, per profile, held in memory and never written to disk. It is not an API key. The server checks it before it checks API keys, so the two kinds of caller cannot be confused, and it never appears in the key list because it was never put there.

Why a token at all, rather than trusting where the request comes from? The [local REST API](https://mosaicbrowser.com/features/rest-api) listens on `127.0.0.1`, so every program on the computer can open a socket to it. The extension's `Origin` header proves which extension sent a request. It proves nothing about everything else running as you. A secret the caller has to hold is the one thing that separates "the companion we built into this profile" from any other local process, and this one is as small as that job allows.

## What the token can reach, measured

One profile started, and the token read out of the extension the launcher had just written:

```
GET    /profiles/<own>           200
PATCH  /profiles/<own>           200
GET    /profiles/<own>/cookies   200
GET    /profiles/<other>         403
GET    /profiles  (the library)  403
GET    /overview                 403
DELETE /profiles/<other>         403
unrevoked keys in the list       [ 'Arche' ]   (the companion is not one)
after the profile stops          401           (the session is gone)

```

The middle four are `403`, not `401`, on purpose. The companion is authenticated; it is being told the request is not its business. The popup still does everything it did before: notes, the [cookie jar](https://mosaicbrowser.com/features/cookie-jars), saved logins and codes, the audit and the leak test, all for its own profile.

## Old installs shed the key themselves

`retireCompanionKey` runs once at startup. It deletes the file nothing reads any more and revokes any unrevoked key named "In-browser companion". After the first run it finds neither and says nothing. On our machine, the first launch after the change logged:

```
[mosaic] Retired the companion's old API key — the popup now uses a
per-launch session instead (file removed: true, keys revoked: 1).

```

A full-power credential nobody uses is worse than one somebody does.

## The check, and the trap it fell into

`check:companion` runs in every build. It no longer tests a cache, because there is none. It tests three things: a relaunch makes a new token and the old one stops working, two profiles never share a token, and a companion for one profile cannot reach another, the library, `/overview` or a bulk route. We proved each negative by putting the fault back and watching those exact lines fail.

One last assertion says the module never writes a secret or makes a key. Its first version failed against a module that does neither, because the module's header explains what it replaced and the pattern matched the prose. Comments are stripped before it searches now.

For an extension you write yourself, the rule that transfers is to start from the calls it makes and grant exactly those, for as long as the session that needs them lasts. The same popup carries each profile's [saved logins and 2FA codes](https://mosaicbrowser.com/features/logins-and-2fa), read through this token. The API keys people do create are covered in [the local API guide](https://mosaicbrowser.com/guides/anti-detect-browser-local-api).

## Questions

### Should a browser extension store an API key?

Only if nothing narrower will do. A key kept by an extension sits in a file indefinitely and usually carries more authority than the extension uses. A token issued per session, scoped to what the extension calls, leaves much less to steal or go stale.

### What can the Mosaic companion reach through the API?

Only routes about its own profile: reading and editing it, its cookies, its notes, the audit and the leak test. A request for another profile, the whole library or the overview is refused with a 403.

### Does the companion show up in Mosaic's API key list?

No. Its token is made when the profile starts, kept in memory only, and is not an API key. There is nothing in the list to revoke, and the token stops working when the profile stops.

### Why does the companion need a token at all?

The API listens on 127.0.0.1, so every program on the computer can reach it. An Origin header proves which extension sent a request, not that nothing else did. A secret the caller must hold is what tells the companion apart.

## Related

[Anti-detect browser REST API: profiles from your scripts](https://mosaicbrowser.com/features/rest-api): Mosaic's anti-detect browser REST API runs on 127.0.0.1 with API keys. Create and start profiles, then attach Puppeteer or Playwright over CDP.

[2FA codes per browser profile, with a login vault each](https://mosaicbrowser.com/features/logins-and-2fa): Mosaic keeps 2FA codes per browser profile: each profile has its own login vault with TOTP codes, shown with a countdown and typed with real keystrokes.

[Anti-detect browser local API: what it is and what to check](https://mosaicbrowser.com/guides/anti-detect-browser-local-api): An anti-detect browser local API is an HTTP server on 127.0.0.1 that creates, starts and stops profiles and hands your script a DevTools endpoint.

[TOTP: how a six-digit code is made from a secret and the clock](https://mosaicbrowser.com/glossary/totp): TOTP is the time-based one-time password behind authenticator apps: an HMAC of a shared secret and the 30-second step, cut to six digits. RFC 6238.

[Cookie jars: import and export cookies per browser profile](https://mosaicbrowser.com/features/cookie-jars): Import and export cookies per browser profile in Mosaic: paste Cookie-Editor JSON, a Netscape cookies.txt or a Cookie header, and export the jar as JSON.







[Google's reCAPTCHA demo loads two libraries that race](https://mosaicbrowser.com/blog/grecaptcha-execute-not-a-function): grecaptcha.execute is not a function when the library is not ready, or when Enterprise owns window.grecaptcha. Google's own v3 demo loads both scripts.

## Mosaic, on your own computer

The browser this blog is about: its own Chromium, fingerprints from real machines and a proxy IP per profile. Free covers 3 profiles with no card, and Pro is $25 a month for any number of them.

Mosaic is a desktop app

Email yourself the link and open it on your computer.
