Files
KomediruzeckiandGitHub 22819d7220 Sign-in: try every session cookie, count only failed attempts, make sessions per-device (#425)
* fix(auth): try every session cookie, and make sessions per-device

Three separate ways a correct password produced a login screen, or a
sign-out reached devices it had no business touching.

**Two cookies, one name.** Cookie identity is (name, Domain, Path). A
browser that had used both tokencircles.com and dev.tokencircles.com held
two fm_session cookies, and the prod-scoped one domain-matches the dev
API. RFC 6265 sorts equal-path cookies oldest-first, so a first-match-wins
reader picked the stalest one every time: login succeeded, the redirect
authenticated with the wrong token, and you were back on the login screen.
Only on a browser that had seen both origins, which is why it reproduced
on one machine and nowhere else. readCookies now returns every value and
the first that verifies wins.

**The rate limit counted successes.** A successful login proves the
credentials are right; charging it to the anti-guessing budget means
signing in and out across a few devices locks out a legitimate user.
Per-IP (30/15min) and per-email (10/15min), failures only, both cleared on
success. The message says how long the wait is.

**Logout meant logout everywhere.** The only revocation primitive was
users.token_version, which invalidates every token the account has ever
been issued, and the "Logout" button used it — so leaving on a laptop
ejected you from your phone mid-use. Sessions are now rows: the JWT
carries a sid, deleting the row ends exactly one device, and Settings
lists where you are signed in with the current device flagged. Sign out
everywhere survives as the separate, deliberate thing it is for.

Named auth_sessions, not sessions: 0001_init already creates a
sessions(sid, sess, expire) table for the retired Express session store,
so CREATE TABLE IF NOT EXISTS is a no-op there and the index on user_id
fails.

Also adds the audit trail this was diagnosed without: auth_logs persists
every login and logout outcome, plus session denials that arrived with a
cookie.

* test(auth): wait for the retry fetch instead of counting ticks

The device-list retry assertion drained six microtask ticks and then
asserted. Enough here, not enough on the CI runner, where the second
fetch had simply not resolved: "expected 1 row, got 0".
2026-08-24 02:36:01 +02:00

68 lines
2.7 KiB
TypeScript

/**
* Turning a user agent into something a person recognises.
*
* The question the session list answers is "is this the laptop in front of me, or the tablet I
* lent to someone" — so the label only has to be right enough to tell two or three devices apart.
* It is derived at render time rather than stored, so this can be improved without a backfill.
*/
import { describe, expect, it } from 'vitest';
import { deviceLabel } from '../src/deviceLabel';
describe('deviceLabel', () => {
it('names the common desktop browsers', () => {
expect(
deviceLabel(
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36'
)
).toBe('Chrome on Linux');
expect(
deviceLabel('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Gecko/20100101 Firefox/130.0')
).toBe('Firefox on Mac');
});
it('names the phones and tablets', () => {
expect(
deviceLabel(
'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1'
)
).toBe('Safari on iPhone');
expect(
deviceLabel(
'Mozilla/5.0 (Linux; Android 14; SM-X200) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36'
)
).toBe('Chrome on Android');
});
it('does not let Chrome claim every browser that mentions it', () => {
// Order matters: Edge, Opera and Samsung Internet all carry "Chrome/" in their UA, and every
// iOS browser carries "Safari/". A confident wrong label is worse than none when the question
// is "should I sign that one out".
expect(
deviceLabel(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36 Edg/151.0.0.0'
)
).toBe('Edge on Windows');
expect(
deviceLabel(
'Mozilla/5.0 (Linux; Android 14) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/23.0 Chrome/115.0.0.0 Mobile Safari/537.36'
)
).toBe('Samsung Internet on Android');
expect(
deviceLabel(
'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/151.0 Mobile/15E148 Safari/604.1'
)
).toBe('Chrome on iPhone');
});
it('says so rather than guessing', () => {
expect(deviceLabel(null)).toBe('Unknown device');
expect(deviceLabel('')).toBe('Unknown device');
expect(deviceLabel(' ')).toBe('Unknown device');
expect(deviceLabel('curl/8.4.0')).toBe('Unknown device');
});
it('gives half an answer when that is all there is', () => {
expect(deviceLabel('Mozilla/5.0 (Windows NT 10.0; Win64; x64)')).toBe('Windows');
});
});