Skip to content

fix(console): keep api explorer keys out of localstorage and clear them on sign-out - #14994

Merged
abnegate merged 5 commits into
mainfrom
fix/console-explorer-key-storage
Oct 27, 2026
Merged

abnegate merged 5 commits into
mainfrom
fix/console-explorer-key-storage

Conversation

@abnegate

@abnegate abnegate commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

The API explorer saved its auth state for each project to localStorage, including the manual project API key and the ephemeral key, and local sign-out (clearConsoleSessionLocally) never removed that entry, so the keys stayed in the browser after the console session ended.

  • The manualApiKey and ephemeralApiKey are now held in memory only, for the lifetime of the page, and are never written to browser storage. The platform, client auth, server auth mode and key scopes still persist in localStorage. After a reload the key has to be entered or minted again.
  • Reading the stored state removes keys that earlier versions left in any project's entry and writes the cleaned payload back.
  • Storage access that throws in a restricted context falls back to the defaults.
  • clearApiExplorerAuthStorage clears the in-memory keys and the localStorage entry, and clearConsoleSessionLocally calls it, so every sign-out path that clears the local session also clears the explorer state.
  • writeApiExplorerAuthToLocalStorage is renamed writeApiExplorerAuthToStorage.

Test Plan

  • Unit test tests/unit/api-explorer-auth-storage.test.ts (bun test):
    • keeps API keys out of browser storage: neither localStorage nor sessionStorage contains a key, and the snapshot still resolves the keys in the same page.
    • removes API keys left in localStorage for every project: keys stored by an earlier version for two projects are removed from storage, and the other settings are kept.
    • falls back to defaults when storage cannot be read: a localStorage getter that throws yields the default snapshot.
    • sign-out clears the stored API keys: after clearConsoleSessionLocally() the entry is gone and the resolved keys are empty.
  • Red/green: against the source on main (test pointed at the old function name), 3 of the 4 tests fail, for example Expected to not contain: "standard_manual". The storage-fallback test fails against this PR's first revision, which read localStorage outside the try. With the change, 4 pass, and the full console unit suite passes (701 tests).
  • ESLint and Prettier pass on the changed files. tsc reports no new errors in them.

Related PRs and Issues

  • Code scanning alert 4314

🤖 Generated with Claude Code

…em on sign-out

The API explorer persisted its whole auth snapshot, including the manual
and ephemeral project API keys, to localStorage, and local sign-out never
removed it, so the keys outlived the console session. The keys now go to
sessionStorage, which ends with the tab, while the platform, client auth
and key scopes stay in localStorage. Keys left in localStorage by earlier
versions are no longer read back, and clearConsoleSessionLocally removes
both entries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 2 medium

💬 Inline comments (1)

  • 🟡 Legacy plaintext API keys persist in localStorage until sign-out — auth-storage.ts:158
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Debounced persist is dropped without flushing pending snapshot · use-api-explorer-auth-persistence.ts:81–87 · unchanged line

// apps/console/src/lib/api-explorer/use-api-explorer-auth-persistence.ts
81	  useEffect(() => {
82	    return () => {
83	      if (persistTimerRef.current !== null) {
84	        clearTimeout(persistTimerRef.current)
85	      }
86	    }
87	  }, [])

The unmount cleanup at use-api-explorer-auth-persistence.ts:81-87 calls clearTimeout(persistTimerRef.current) without flushing pendingSnapshotRef, and the timer callback (lines 70-76) is the only writer of the pending snapshot — so any auth change made within the 300ms debounce window before unmount is silently discarded (e.g. pasting an API key and immediately navigating away). The same loss occurs on project switch: hydrateFromStorage (lines 38-56) never flushes the previous project's pending write, and the next schedulePersist clears the old timer and overwrites pendingSnapshotRef, so project A's edit within the window is dropped instead of written to A.


This PR hardens API-explorer auth handling: API keys are removed from the shared console.apiExplorer.auth localStorage entry and kept only in per-project sessionStorage, with legacy plaintext values stripped on write and storage cleared on sign-out. A new hook coordinates hydration, debounced persistence, and project switches, and unit tests cover the storage contract.

Files Change
apps/console/src/lib/api-explorer/auth-storage.ts Rewrites the storage layer: per-project sessionStorage key map, legacy plaintext keys stripped from localStorage entries on write, new clear/read helpers.
apps/console/src/lib/api-explorer/use-api-explorer-auth-persistence.ts New hook that hydrates auth from storage, debounces snapshot persistence (300ms), and handles project-switch rehydration.
apps/console/src/lib/appwrite/sdk.ts Wires the explorer SDK to the new key-retrieval path.
apps/console/tests/unit/api-explorer-auth-storage.test.ts Unit tests for the storage round-trip, legacy-entry stripping, and clear behavior.

Reviewed commit: dd82a5b

@appwrite

appwrite Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

New Console

Project ID: new-console

Sites (1)
Site Status Logs Preview QR
 New Console
new-console
Ready Ready View Logs Preview URL QR Code

Tip

Environment variable changes require redeployment to take effect

@ghost

ghost commented Oct 27, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The API Explorer now keeps manual and ephemeral project API keys in page memory while persisting the remaining auth preferences. Legacy stored keys are scrubbed, storage failures are handled, and local console sign-out clears the explorer auth state; unit tests cover these behaviors.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
apps/console/src/lib/api-explorer/auth-storage.ts Moves API keys to an in-memory map, scrubs legacy stored keys, and exposes storage cleanup.
apps/console/src/lib/api-explorer/use-api-explorer-auth-persistence.ts Updates persistence to use the renamed storage writer.
apps/console/src/lib/appwrite/sdk.ts Clears API Explorer auth state during local console sign-out.
apps/console/tests/unit/api-explorer-auth-storage.test.ts Adds coverage for key storage, legacy cleanup, storage failures, and sign-out.

Reviewed e3b4ba5 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moves API-explorer credentials from localStorage into sessionStorage with per-project key separation, plus a debounced persistence hook and unit tests.

Key findings

abnegate and others added 3 commits October 27, 2026 19:03
The stale chunk test redefines sessionStorage as a non-writable property, so
plain assignment failed when the whole unit suite ran in one process.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
CodeQL flags writing the explorer's API keys to sessionStorage as clear
text storage, so the keys now live only in memory for the page's
lifetime and are never written to browser storage. Reading the stored
auth state removes keys an earlier version left in any project's entry,
not only the current one, and storage access that throws in a
restricted context now falls back to the defaults instead of throwing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

A write failure while removing legacy keys made the read fall back to
empty defaults, dropping the saved non-secret preferences. The cleanup is
now best-effort and the scrubbed payload is returned either way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@abnegate

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@abnegate
abnegate merged commit 8dcb5e3 into main Oct 27, 2026
41 checks passed
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.

2 participants