DEV Community

Cover image for Hardening my post-quantum messenger: the 7 security fixes I almost lost
Mohammad Taha Azizi Ahad
Mohammad Taha Azizi Ahad

Posted on

Hardening my post-quantum messenger: the 7 security fixes I almost lost

Hardening my post-quantum messenger: the 7 security fixes I almost lost

The day before, I had finished the first hardening round of my messenger: 14 security findings fixed, 14 out of 14 tests passing. I felt good about it. Then I started round 2 — and nearly shipped a version that silently undid seven of those fixes.

This is the story of what round 2 changed, how the damage was caught, and the one habit that saved the project.

The project: chat-app — a real-time Persian web messenger. Every message is protected by PQ-MPRBA, an original browser-native protocol: a hybrid ML-KEM-768 + ECDH P-256 ratchet, ML-DSA-65 identity signatures, and Ristretto255-VOPRF blinded authentication. The server never sees plaintext, and never learns who is talking to whom.

The five problems

Round 2 started from a security review of the repo that listed five weak spots:

  1. Web platform limits. Private keys lived in plaintext localStorage, where any cross-site scripting hole could read them. The browser gives you no secure enclave, and the server could serve compromised code.
  2. No independent audit, thin tests. There were unit tests, but no NIST test vectors and no fuzzing.
  3. Centralized dependencies. Login depended entirely on the Bale bot API — if Bale went down, nobody could sign in.
  4. Performance and multi-device. Post-quantum keys are heavy, and multi-device sync was uncharted.
  5. Docs. No threat model, no security policy, no contribution guide.

Some of these are structural — no single round fixes "the web is a hostile platform" or "there is no independent audit." The plan was honest about that: fix what is fixable, mitigate the rest, and write down the limits instead of hiding them.

What round 2 shipped

The headline change is the keystore. Identity and KEM private keys moved out of localStorage into IndexedDB, under a new version marker — v: 4. Anything older is discarded on load and fresh keys are generated. The ECDH P-256 key is a non-extractable WebCrypto key; it can be used but never read out. That is the strongest storage the browser offers, and it closes the easiest key-theft path the review had flagged.

The rest of the round:

  • Strict CSP. The Content Security Policy was tightened: no unsafe-inline, no unpinned CDN. The unpinned Google Fonts link went away, and the admin and error pages now use self-hosted CSS and JS only.
  • NIST Known-Answer Tests. KAT vectors for ML-KEM-768 encapsulation/decapsulation and ML-DSA-65 signing/verification, so the crypto primitives are checked against the official values, not just against themselves.
  • Fuzz tests. Over 490 malformed inputs — truncated ciphertexts, garbage key bundles, broken epoch packets — fed to the message parser. Every single one must fail closed: throw an error, never crash, never produce plaintext.
  • Bale fallback. Registration now issues a one-time recovery code, shown once, stored server-side only as a scrypt hash, with strict rate limiting. If the Bale API is unreachable, the code is the way back in — through a new POST /api/recover endpoint. A health probe (GET /api/health) watches the Bale API and logs failures.
  • Docs. SECURITY.md, THREAT_MODEL.md, AUDIT_STATUS.md, DEPLOYMENT.md, CONTRIBUTING.md — including a plain statement that no independent audit exists.
  • Benchmarks. A small suite measuring ML-KEM-768 encapsulation and ML-DSA-65 sign/verify time for a typical message.

The final suite: 31 out of 31 tests passing, type-check clean, build clean. Version 6.1.0, merged and live.

That is the good news. Here is the part that nearly went wrong.

The twist: seven fixes, quietly undone

The round-2 changes arrived as an export built on a pre-round-1 baseline — a copy of the code from before the first hardening round. That meant the export carried the new round-2 features on top of the old, unfixed code. Merged blindly, it would have reverted seven security fixes from round 1 without a single test failing at first glance.

I caught it the same way you catch anything like this: by diffing the export against the hardened code, file by file, and asking of every difference, "is this new work, or is this an old fix disappearing?"

The seven, and why each one mattered:

  1. Unauthenticated endpoints. Round 1 had added session-token checks to public-key publication, group creation, and socket auth. The export had none of them — anyone could publish keys or create groups as anyone.
  2. Group epoch keys from public inputs. Round 1 replaced epoch keys derivable from public data with random 32-byte keys, distributed per member with ML-KEM-768 encapsulation. The export brought the derivable version back — the server could have computed every group key.
  3. A public default passphrase. Round 1 removed a hard-coded default PBKDF2 passphrase and raised new secrets to 600,000 iterations. The export restored the public default.
  4. Missing group replay tracking. Round 1 added seen-message tracking for groups. The export dropped it, reopening replay attacks on group messages.
  5. No ratchet rotation. Round 1 added rotation after 50 sent messages so chain keys don't live forever. The export had no rotation at all.
  6. Raw ECDH bits as AES keys. Round 1 routed the shared secret through HKDF with a room-id salt. The export used the raw bits directly as the key.
  7. VOPRF seed from the admin password. Round 1 replaced a single SHA-256 of the admin password with an environment seed or HKDF derivation. The export brought the weak seed back.

Every one was restored, and three new regression tests were written to pin them down — one for the group epoch/replay behavior, one for rotation, one for the passphrase. The final count, 31/31, includes those three.

And the diff turned up one more thing that round 1 had missed entirely: group creation never sent its auth token. The client called the endpoint without the Bearer header, so against its own hardened server, creating a group would have returned 401. A latent bug, invisible until the auth checks it violated actually existed.

What is still partial

I want to be straight about what round 2 did not finish, because the whole point of the exercise is honest limits:

  • The keystore is partial. WebCrypto has no native ML-KEM or ML-DSA key objects yet, so those keys are stored AES-GCM-wrapped as byte arrays. Only the ECDH P-256 key is a true non-extractable key.
  • Multi-device is docs only. What happens on a second-device login is documented in the threat model; syncing keys across devices is months of work, not one round.
  • Rotation has no previous-session slot. A message sent just before a rotation can fail closed — the safe direction, but a real edge.
  • The server endpoints were verified by code review and type-checking, not by a live HTTP test. A live two-client group test is still pending.
  • No independent audit exists. This is a self-reviewed research project, not a production-grade messenger. The whitepaper and the audit-status doc say so outright.

The lesson

The round-2 features are good work — the keystore, the KAT vectors, the recovery codes all make the app genuinely safer. But none of that would have mattered if the seven reverted fixes had shipped. The app would have looked more secure (version 6.1.0, 31 tests green, fresh docs) while being less secure than the version before it.

So the lesson I took from this round is simple, and it is now a rule for the project: never merge a large change without diffing it against your own security fixes first. A passing test suite is not proof that nothing regressed — it only proves the tests you wrote still pass. The safety net is the diff, read line by line, plus regression tests that pin down the fixes you care about most. Verification is the work; everything else is just typing.

If you work on cryptography, I'd genuinely welcome your review. The core lives in src/utils/crypto.ts, the round-2 release is v6.1.0, and the honest limits are in AUDIT_STATUS.md. Tell me what breaks — that's how it gets better.

⭐ https://github.com/indirect-b/chat-app

Top comments (0)