DEV Community

Cover image for How I Stopped Passcode Brute-Force From Freezing Live Keystrokes in Carrel
Ayush Agarwal
Ayush Agarwal

Posted on Originally published at theayush.pages.dev

How I Stopped Passcode Brute-Force From Freezing Live Keystrokes in Carrel

When I started building Carrel, a real-time collaborative CodeMirror 6 workspace, I thought auth was the easy part: hash room passcodes with scrypt, verify on join, done.

Then I tested what happens under brute-force traffic. scrypt is slow on purpose. Run it synchronously and it blocks Node's event loop; run dozens of async scrypt calls uncapped and they saturate libuv's threadpool. Either way, anyone hammering wrong passcodes starts stuttering live keystrokes for everyone typing in the room.

Here is how I ended up structuring the server and protocol across the 3-package TypeScript monorepo (@carrel/shared, @carrel/server, @carrel/client):

1. Bounded scrypt + single-use WS tickets

In server/src/throttle.ts, POST /api/rooms/:id/ticket runs async scrypt inside a 4-slot concurrency limiter paired with per-IP progressive lockouts (and ignores X-Forwarded-For unless TRUST_PROXY is explicitly enabled). Once verified, the client gets a short-lived, single-use nonce-bound HMAC ticket to upgrade /ws, which gets burned immediately on upgrade so replay attempts fail.

2. One WebSocket with a 1-byte frame header

Instead of pushing everything through JSON messages or opening parallel sockets, shared/src/frames.ts prefixes every binary WebSocket payload with one routing byte:

[0x00] -> raw y-protocols/sync CRDT updates (Y.Doc persisted via SQLite WAL)
[0x01] -> coalesced awareness updates (remote carets, selections, 80ms lerp)
[0x02] -> typed JSON control events (role changes, kicks, host handover)
Enter fullscreen mode Exit fullscreen mode

CodeMirror 6 renders remote carets with an 80ms interpolation curve (CarrelProvider.ts) and turns interpolation off automatically when prefers-reduced-motion is set.

3. Not losing room admin on a 2-second Wi-Fi drop

If a host refreshes a tab or drops Wi-Fi briefly, promoting someone else on the exact close event creates constant role churn. In server/src/host.ts, a HOST_GRACE_MS (5000ms) timer marks the seat hostPending first. Only if that timer expires does seniority election promote the next peer by min(joinedAt) (tie-broken by min(clientId)), while SENIORITY_WINDOW_MS (30s) restores standing on quick reconnects. Multi-tab promotion and creator-key reclamation are verified in Playwright E2E specs (phase5.test.ts).

Full architecture notes and failure-mode diagrams: https://theayush.pages.dev/projects/carrel

Code on GitHub: https://github.com/theayushagarwal/carrel

Top comments (0)