hb-auth workers can keep accounts unlocked after a sign-out: worker outliving its tab, signing-out app's own worker, failed switch, replaced workers (follow-up to #1397)
Follow-up to #1391 (closed)/#1397 (closed) (hb-auth worker locking on sign-out). The #1397 (closed) review (commit 34891988) found these remaining paths where an account can stay unlocked in an app's hb-auth worker after a sign-out, so a later page of that app could sign without a password prompt. All are traced from code, not reproduced. Blog (/blog) and wallet (/wallet) share one origin but each has its own SharedWorker.
1. The worker outlives its last tab
hb-auth opens its SharedWorker with extendedLifetime: true (hb-auth dist/hb-auth.js), so the browser can keep it running for a while after the app's last page closes. common-hiveio-packages/src/hb-auth/hbauth-service.ts assumes a running worker is always held by a page that will hear the sign-out.
Scenario:
- The only Wallet tab unlocks A's active key.
- That tab navigates to /blog, signs out, then signs back in as A with the posting key only.
- No Wallet page heard the sign-out. A Wallet page opened within the window reconnects to the same worker with A still unlocked, and signs active-key operations with no prompt.
2. The signing-out app's own worker isn't fully logged out
packages/smart-signer/lib/auth/use-logout.ts logs out only the stored accounts list in its own worker. An account unlocked in that worker but missing from the list stays unlocked. In the same way, when an account switch fails because the server session lost the account, use-switch-account.ts drops the account from the list but neither locks it nor broadcasts removedUsername.
3. Replaced workers are only locked, not logged out
In hbauth-service.ts logoutEveryoneFromStartedWorkers, own is computed from the old onlineClientGeneration before a pending replacement updates it. Right after another app writes to safe storage, logoutAll() reaches only the new worker. The old one gets lock() only when it is retired after going idle, so its sessions stay authorized, and an in-flight signing (e.g. at the password prompt) keeps it unlocked until the operation finishes.
Expected
- A sign-out ends every account's session in every worker of every app on the origin, including workers with no live page. Example design: the signing-out tab writes a sign-out epoch (counter or timestamp) to localStorage. Each page, when it starts or reconnects an hb-auth client, compares the worker's last-seen epoch with the stored one and runs
logoutAll()first if it is behind. - The signing-out tab runs
logoutAll()in its own app's workers, not only per stored account. - A failed switch that drops an account locks it and broadcasts
removedUsername. - Replaced workers still running get
logoutAll()too, not only a deferredlock(). - Keep DR-005: a page that never needs hb-auth doesn't load it just because of a sign-out.
Acceptance
Unit tests in common-hiveio-packages/src/hb-auth/hbauth-service.test.ts and packages/smart-signer/lib/hbauth-service.test.ts (hb-auth faked as now), each failing on the current code:
- (a) a worker that stayed alive with an unlocked account, connected after a sign-out epoch advance, is logged out before use;
- (b) the signing-out tab's own worker gets
logoutAll; - (c) a dropped account after a failed switch is locked and broadcast;
- (d) a replaced, still-running worker gets
logoutAllon another tab's sign-out.