# An invalid TOTP secret key, checked only where it was read

> A TOTP secret only base32 can't spell got stored and showed as a row of dashes forever. The fix: validate where the value is written, not read.

Source: https://mosaicbrowser.com/blog/totp-key-checked-at-write

## An invalid TOTP secret key, checked only where it was read

A TOTP secret only base32 can't spell got stored and showed as a row of dashes forever. The fix: validate where the value is written, not read.

![--- ---: A bad setup key, saved as a row of dashes](https://mosaicbrowser.com/covers/totp-key-checked-at-write.webp)

An invalid TOTP secret key is usually one of two things: a character base32 does not have, or a key that was cut short when it was copied. The first can be refused the moment it is typed. The second is quieter, because a truncated key can decode perfectly and then make six wrong digits for ever. Mosaic's authenticator accepted both kinds for a while, because it checked the key only when it was asked for a code. Since 29 September 2026 it checks when the key is saved.

## What makes a TOTP secret key invalid

The key a site shows beside its QR code is base32. [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648) defines the alphabet as the letters A to Z and the digits 2 to 7, with `=` only as padding. A 0, 1, 8 or 9 cannot appear, which is why keys typed from a screen so often fail: an O read as a zero, an I read as a one.

The code itself comes from [RFC 6238](https://www.rfc-editor.org/rfc/rfc6238): an HMAC over the current 30-second step, truncated to six digits. The key is the only secret in that sum, so a key that is wrong by one character gives codes that are wrong every time, with nothing to say why. (The [TOTP glossary entry](https://mosaicbrowser.com/glossary/totp) has the rest of the mechanism.)

Mosaic is a desktop app

Email yourself the link and open it on your computer.

[How it works: Logins and 2FA codes](https://mosaicbrowser.com/features/logins-and-2fa)

## How ours went wrong

Building the Auth panel in the companion, the extension inside each profile, we pasted `not a key!!` into it to see what happened. The panel said "Saved to this profile." The row then showed `--- ---` for ever.

The decoder did refuse the key. It threw `"!" is not valid base32`, but only when something asked for a code, and the code route is not one anyone presses twice. The key had already been accepted, encrypted and stored by then. From outside, a stored bad key and a stored good key looked the same until a login failed.

That is a rule we now write down in one line: a value checked only where it is read is not checked. The check goes where the value is written.

## Where the check went

The fix is `assertUsableTotpSecret` in `store/credentials.ts`, which runs when a login is created and when its key is changed. It lives in the store because the window, the REST API and MCP all save logins through that one method. A check in the window alone would have left the other two accepting the same bad key.

It first tidies what people actually paste. Spaces and hyphens are removed, letters are upper-cased, and an `otpauth://` link has its `secret` parameter read out. Then it decodes, and refuses with a `400` that names the field:

```
totpSecret is empty — paste the setup key the site shows beside its QR code.
totpSecret is not a valid setup key: "0" is not valid base32 — check the secret.
totpSecret is too short to be a setup key — 12 characters decode to 7 bytes,
  and the shortest key any site issues is 13.

```

## Why the floor is 64 bits

The last message is the one that catches a paste that stopped halfway. The floor is 8 bytes, 64 bits, which is 13 base32 characters.

That is deliberately below the standard. [RFC 4226](https://www.rfc-editor.org/rfc/rfc4226), which TOTP builds on, says the shared secret must be at least 128 bits. Some sites still issue 13- and 14-character keys, and refusing a key a real site gave you is worse than accepting a short one that works. Anything under 64 bits is not a short key. It is a fragment of one, and a fragment decodes fine and then produces wrong codes with no symptom at the moment the mistake is made.

## How the check is held

`check:totp` runs in every build. It starts from one known-good key, `JBSWY3DPEHPK3PXP`, and breaks it one way at a time: a `1` swapped in for one letter, punctuation, an empty string, nothing but separators, half the key, a twelve-character fragment, a single character, and an `otpauth://` link whose secret is not base32. All eight have to be refused with a `400` that names `totpSecret`. Five pasted shapes of the same good key have to be accepted: with spaces as Google prints it, with hyphens, in lower case, inside an `otpauth://` link, and with padding. Writing the bad cases by breaking a good one means a new rule cannot leave an old case out of date.

If your own authenticator rejects a key, the fix is nearly always to copy the full key again from the site's two-factor setup page, not to retype it. If it accepts the key and the codes are still wrong, suspect a partial copy first and the computer's clock second.

How the Auth panel reads keys from QR codes, and the three other faults found while building it, are in [an authenticator inside the profile](https://mosaicbrowser.com/blog/authenticator-in-the-profile). The per-profile vault itself is on [logins and 2FA](https://mosaicbrowser.com/features/logins-and-2fa).

## Sources

1. [RFC 4648: The Base16, Base32, and Base64 Data Encodings](https://www.rfc-editor.org/rfc/rfc4648), read November 2, 2026
2. [RFC 4226: HOTP, an HMAC-based one-time password algorithm](https://www.rfc-editor.org/rfc/rfc4226), read November 2, 2026
3. [RFC 6238: TOTP, a time-based one-time password algorithm](https://www.rfc-editor.org/rfc/rfc6238), read November 2, 2026

## Questions

### What makes a TOTP secret key invalid?

A setup key is base32: the letters A to Z and the digits 2 to 7. A 0, 1, 8 or 9, or any punctuation, makes it invalid. So does a key cut short when it was copied, which can still decode and then gives wrong codes.

### Why does my authenticator show wrong codes for a valid-looking key?

If the key decodes but the codes never work, the most likely cause is a partial paste. Copy the full key again from the site's two-factor setup page. Also check the computer's clock, because TOTP codes depend on the time.

### Does Mosaic accept spaces, lower case or an otpauth link?

Yes. Spaces and hyphens are removed, letters are upper-cased, and an otpauth:// link has its secret parameter read out. What is left must then decode as base32 to at least 8 bytes.

### What is the shortest TOTP key Mosaic accepts?

64 bits, which is 13 base32 characters. RFC 4226 asks for at least 128 bits, but some sites issue shorter keys, and refusing a key a real site gave you is worse than accepting a short one that works.

## Related

[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.

[An authenticator inside the profile, and four faults](https://mosaicbrowser.com/blog/authenticator-in-the-profile): The companion extension in each Mosaic profile shows its own TOTP codes, read from a QR code or a pasted key. Four faults we found by driving it 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.

[How to manage 2FA codes for multiple accounts](https://mosaicbrowser.com/guides/manage-2fa-codes-for-multiple-accounts): Keep each account's 2FA secret next to its login, back the secrets up somewhere you control, and save recovery codes before you need them.









[The companion held a full-power API key](https://mosaicbrowser.com/blog/companion-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.

[Why our MCP server speaks HTTP and still ships a stdio bridge](https://mosaicbrowser.com/blog/mcp-streamable-http-and-stdio): stdio means the client starts the server. Mosaic is already running, so its MCP server is HTTP inside the app, plus a small stdio bridge for clients.

## 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.
