Skip to content

fix(core,appcheck,web): start when the browser blocks web storage - #18946

Merged
SelaseKay merged 1 commit into
firebase:mainfrom
LasseLegarth:fix/web-blocked-session-storage
Nov 16, 2026
Merged

SelaseKay merged 1 commit into
firebase:mainfrom
LasseLegarth:fix/web-blocked-session-storage

Conversation

@LasseLegarth

@LasseLegarth LasseLegarth commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

When the user blocks site data for the origin (Chrome: "Don't allow sites to save data on your device"), accessing window.sessionStorage or window.localStorage throws a SecurityError. Several reads on the web startup path were unguarded, and each one makes Firebase.initializeApp() throw, so runApp is never reached and the user sees a blank page:

  • firebase_core_web: _registerVersionIfNeeded uses sessionStorage to avoid registering the same library version twice. It runs inside initializeApp().
  • firebase_app_check_web: ensurePluginInitialized reads the persisted reCAPTCHA type and site key from localStorage / sessionStorage, and activate() writes them back.

Both are caches (for de-duplication or for the next startup), so this PR treats blocked storage as "nothing persisted" and skips the writes:

  • Core: the version is still registered with registerVersion, just without the session cache.
  • App Check: no persisted provider on startup; activate() still activates, it just cannot persist the settings for the next startup.

Behaviour with working storage is unchanged.

firebase_auth_web had the same problem in ensurePluginInitialized, but #18926 has since fixed it on main (storage is only read in debug builds and a SecurityError is ignored). After rebasing, this PR no longer changes firebase_auth_web's code; it only keeps a browser test that covers the plugin with storage blocked.

Related Issues

Fixes #18945

Tests

Each package gets a browser test that replaces window.sessionStorage and window.localStorage with getters that throw a SecurityError DOMException (as Chrome does with site data blocked) and restores the original property descriptors in tearDown:

  • firebase_core_web/test/firebase_core_web_blocked_storage_test.dart: initializeApp completes and library versions are still registered with the platform logger.
  • firebase_app_check_web/test/blocked_web_storage_test.dart: initializeApp with App Check registered completes; activate completes.
  • firebase_auth_web/test/blocked_web_storage_test.dart: initializeApp with Auth registered completes; useAuthEmulator completes. Kept as a regression test for the Auth plugin path.

The core and App Check tests fail without their fix and pass with it, in Chrome with DDC and with --wasm. The Auth test fails on the commit before #18926 and passes with it (both with this PR's core fix applied, since initializeApp fails in core first otherwise). All tests pass after the rebase (core 11, auth web 62, app check web 9, DDC and Wasm).

Also checked end to end with a minimal app (firebase_core 5.1.0 + firebase_auth 7.0.1, release build, headless Chrome with site data blocked): blank page before, app starts after.

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request improves the robustness of Firebase web plugins by wrapping web storage access (localStorage and sessionStorage) in try-catch blocks to handle SecurityErrors that occur when users block site data. Additionally, new test files have been added across the App Check, Auth, and Core packages to verify that initialization and core functionality remain operational under these restricted conditions. The reviewer provided valid technical feedback regarding the implementation of the _restoreWebStorage helper function in the new test files, suggesting a safer approach to restoring window properties to avoid potential TypeErrors in standard-compliant browsers.

@SelaseKay SelaseKay left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey @LasseLegarth, thanks for the PR. Kindly rebase with current main.

@LasseLegarth
LasseLegarth force-pushed the fix/web-blocked-session-storage branch from db9d432 to a0eb184 Compare November 13, 2026 11:29
@LasseLegarth

Copy link
Copy Markdown
Contributor Author

@SelaseKay Thanks! Rebased onto current main (no conflicts). The new tests, analyze and format still pass for core, auth and App Check web (DDC and Wasm).

@SelaseKay SelaseKay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @LasseLegarth, thanks again. LGTM!

@SelaseKay

Copy link
Copy Markdown
Contributor

Hi @LasseLegarth, could you resolve the merge conflicts?

@SelaseKay SelaseKay added the blocked: do-not-merge PR blocked externally label Nov 16, 2026
When the user blocks site data, accessing window.sessionStorage or
window.localStorage throws a SecurityError. Several web startup paths
accessed them unguarded, so Firebase.initializeApp threw and the app
never started (blank page):

- firebase_core_web read sessionStorage while registering library
  versions.
- firebase_app_check_web read the persisted reCAPTCHA settings in
  ensurePluginInitialized, and activate() wrote them back.

Both are caches for the next startup or for de-duplication, so blocked
storage is now treated as "nothing persisted" and writes are skipped.

firebase_auth_web already handles blocked storage since firebase#18926. Its
browser test is kept to cover initializeApp and useAuthEmulator with
storage blocked.
@LasseLegarth
LasseLegarth force-pushed the fix/web-blocked-session-storage branch from a0eb184 to 19f37fb Compare November 16, 2026 12:08
@LasseLegarth LasseLegarth changed the title fix(core,auth,appcheck,web): start when the browser blocks web storage fix(core,appcheck,web): start when the browser blocks web storage Nov 16, 2026
@LasseLegarth

Copy link
Copy Markdown
Contributor Author

Thanks, rebased on main and resolved the conflict in firebase_auth_web.dart. #18926 already guards the storage reads and writes there, so I took main's version and dropped my change to that file. The PR now changes core and App Check only, plus a browser test for each of the three packages. Updated the title and description to match.

@SelaseKay SelaseKay removed the blocked: do-not-merge PR blocked externally label Nov 16, 2026
@LasseLegarth

Copy link
Copy Markdown
Contributor Author

@SelaseKay the merge conflicts are resolved (details above), ready for another look.

final sessionItem = web.window.sessionStorage.getItem(sessionKey);
if (sessionItem == null) {
try {
if (web.window.sessionStorage.getItem(sessionKey) != null) return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Optional, could be a follow-up. The JS SDK already ignores a second registerVersion for the same library (it logs at debug level and returns); the new core test registers flutter-fire-core twice with storage blocked (registerLibraryVersion, then _registerAllLibraryVersions in initializeApp) and passes. This cache also drops registrations: sessionStorage survives a reload but the JS SDK doesn't, so on later loads in the same tab the FlutterFire versions are not registered. With flutterfire-flutter-fire-core-5.1.0 already in sessionStorage before initializeApp, the platform string was fire-core/0.16.6 fire-core-esm2020/0.16.6 fire-js/ fire-js-all-cdn/13.3.0, with no flutter-fire-core. Calling firebase.registerVersion directly would fix that and remove the storage access here. firebase_ai and firebase_data_connect have their own copy of this cache.

@SelaseKay
SelaseKay merged commit 229e08e into firebase:main Nov 16, 2026
108 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.

[firebase_core_web]: Firebase.initializeApp throws and the app never starts when the browser blocks site storage (unguarded sessionStorage access)

3 participants