Skip to content

fix(settings): stop web appearance caches leaking between accounts - #478

Closed
Quick104 wants to merge 1 commit into
mainfrom
fix/settings-appearance-cache-ownership
Closed

Quick104 wants to merge 1 commit into
mainfrom
fix/settings-appearance-cache-ownership

Conversation

@Quick104

Copy link
Copy Markdown
Contributor

Signing into a second account on the same browser showed you the first account's theme, text size, contrast, and custom CSS until you saved your own. Verified finding from #376.

Incognito is not the leaking case — it starts empty. The bug is two accounts in one regular browser profile.

What changed

web/src/utils/storage.ts — new createOwnedCache(ownerKey, memberKeys) returning isTrusted / get / set / clear, plus three instances and two new owner sidecar keys.

Behavior Why
Null owner (bootstrapping / signed out) is trusted The warm start still paints instead of flashing defaults
Unstamped legacy cache is not trusted once an account is known This is the leak
get returns null when untrusted Callers fall through to the server value
clear drops every member key and re-stamps for the new owner A later single save can't re-trust the rest of the old account's state

web/src/hooks/useTheme.tsx and useCustomTheme.ts — derive an owner, read trust once per render before any effect re-stamps, gate every apiValue ?? localValue fallback on it, and clear + reset to defaults when the cache belongs to someone else. That also restores the admin default theme for the new account, which a leftover silo-theme key had been suppressing.

web/src/hooks/useDateTimeFormat.tsx — refactored onto the shared mechanism. Behavior unchanged; it already did this correctly and was the pattern the other two were missing. Reuses its existing owner key so nobody loses their warm start.

Three cache groups, three separate stamps — deliberately not one shared stamp. These are written by hooks nested inside one another and React runs effects inner-first, so a shared stamp would let whichever hook resolved first vouch for the other's still-stale values.

Verification

  • vitest run src/hooks src/utils → 36 files, 148 tests pass
  • tsc -b, format:check → clean; lint → 0 errors (160 warnings, same count as main)
  • The new tests were confirmed to catch the bug: with the two hook fixes stashed, 7/7 fail with the leak signatures (expected 'large' to be 'default', expected 'cobalt-studio' to be null, admin default not applied)
  • pnpm test has 6 pre-existing failures (SeasonContent, ServerStorageStep, all No QueryClient set), confirmed pre-existing by stashing the whole change

Known limits

  • One-time appearance reset for existing users on first load after deploy: legacy untagged caches are untrusted until the settings query resolves and re-stamps. Same tradeoff useDateTimeFormat already shipped; stamping the existing cache with whoever signs in first would reintroduce the leak.
  • Owner is the user ID, correct for today's user-scoped /settings. Household profiles share a user_id, so profile switching still shares the cache. docs(settings): define the cross-platform settings contract #477 moves appearance to profile scope — that is a one-line change in appearanceCacheOwner().
  • A hard reload can still flash the previous account's appearance for under a second while auth bootstraps. Inherent to the warm start.
  • Not exercised in a real browser with two live accounts; covered by rendering tests with mocked auth/settings.

Part of #376. Design: #477. Supersedes #476, closed when its branch was renamed.

AI-use disclosure

Implemented with AI assistance (Claude Opus 5) under maintainer direction; tests, tsc, lint, and format were re-run independently, and the ownership logic was read against the already-shipped useDateTimeFormat pattern.

The theme, text scale, text weight, high contrast, custom theme variable
and custom CSS caches in localStorage were untagged, so on a shared
browser a second account inherited the first account's appearance: with
no server value of its own, every fallback resolved to whatever the
previous account had stored, and the leftover `silo-theme` key also
suppressed the admin-configured default theme for the new account.

DateTimeFormatProvider already solved this by stamping its cache with the
authenticated user id and refusing another account's values. Extract that
mechanism into `createOwnedCache` in utils/storage.ts (where key
namespacing lives) and put all three groups behind it, so appearance and
custom theme get the same protection instead of a third copy of the rule.

- Each group carries its own owner stamp. A shared stamp would be unsafe:
  the groups are written by hooks nested inside each other, and effects
  run inner-first, so whichever hook stamped first would vouch for the
  other's still-stale values.
- A null owner (auth bootstrapping, or signed out) still trusts the
  cache, which keeps the warm start and the login screen's last look.
- An unstamped cache is not trusted once an account is known, so existing
  users take a one-time appearance reset on first load rather than a
  chance of seeing someone else's settings.
- When a foreign cache is detected the values are dropped and the empty
  cache is handed to the new account, so a later single save cannot
  re-trust the rest of the previous account's state.

Owner is the user id because /settings is user-scoped server side; it
lives in one helper (`appearanceCacheOwner`) so it can be widened if
appearance moves to profile scope. `shouldLoadApiTheme` is gone: it had
become a synonym for `appearanceCacheOwner(...) !== null` with no callers
left.

Part of #376

AI-use disclosure: implemented with Claude Code.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b85e84fa-dfa3-4669-8739-fedaf7b26063

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/settings-appearance-cache-ownership

Comment @coderabbitai help to get the list of available commands.

@Quick104

Copy link
Copy Markdown
Contributor Author

Superseded by #479, which combines the design, the manifest, the ui.theme scope decision, and the web appearance cache fix into a single server PR. Same four commits, unchanged.

@Quick104 Quick104 closed this Jul 25, 2026
@Quick104
Quick104 deleted the fix/settings-appearance-cache-ownership branch July 25, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant