Skip to content

fix(web): tag local appearance caches with their owning account - #476

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

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

Conversation

@Quick104

Copy link
Copy Markdown
Contributor

Problem

A second account signing in on the same browser inherits the first account's appearance until it saves its own value. Theme, text scale, text weight, high contrast, custom theme variables, and custom CSS are all read from localStorage with no check on who stored them, and are kept when the newly authenticated account has no server value.

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

Verified finding from #376 (P2).

Fix

useDateTimeFormat already solved this correctly by tagging its warm cache with the authenticated user ID and rejecting another account's cache. This extracts that mechanism instead of copy-pasting it a third time.

createOwnedCache(ownerKey, memberKeys) in web/src/utils/storage.ts exposes isTrusted / get / set / clear:

  • A null owner (auth bootstrapping, or signed out) still trusts the cache, so the warm start still paints.
  • An unstamped legacy cache is not trusted once an account is known — that is the leak.
  • set stamps; clear drops every member key and hands an empty cache to the new owner.

Three instances: appearanceCache, customThemeCache, and dateTimeFormatCache (reusing the existing owner key so nobody loses their warm start). The groups keep separate stamps on purpose — they are written by hooks nested inside one another and React runs effects inner-first, so one shared stamp would let whichever hook resolved first vouch for the other's still-stale values.

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 another account. That also restores the admin default theme for the new account, which a leftover silo-theme key was suppressing. useDateTimeFormat.tsx is refactored onto the shared mechanism with no behavior change.

Verification

  • pnpm exec vitest run src/hooks src/utils → 36 files, 148 tests pass
  • pnpm exec tsc -b → clean
  • pnpm run format:check → clean
  • pnpm run lint → 0 errors, 160 warnings (same count as main)
  • The new tests were confirmed to actually 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 in SeasonContent.test.tsx and ServerStorageStep.test.tsx, all No QueryClient set. Confirmed pre-existing by stashing the whole change and re-running; neither file touches theme, appearance, or storage.

Known limits

  • Existing users take a one-time appearance reset 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: define cross-platform user settings contract #377 moves appearance to profile scope — that is a one-line change in appearanceCacheOwner().
  • While auth is bootstrapping the cache is still trusted, so a hard reload can show a sub-second flash of the previous account's appearance. Inherent to the warm start, unchanged from the datetime behavior.
  • Not verified in a real browser against two live accounts; covered by rendering tests with mocked auth/settings.

Part of #376.

AI-use disclosure

Implemented with AI assistance (Claude Opus 5) under maintainer direction, and independently re-verified (tests, tsc, lint, format re-run; ownership logic read against the 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: 233767ff-17a5-449b-8454-7c5b1333aa6e

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/web-settings-cache-ownership

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

@Quick104 Quick104 closed this Jul 25, 2026
@Quick104
Quick104 deleted the fix/web-settings-cache-ownership branch July 25, 2026 19:08
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