Repository navigation
fix(core,appcheck,web): start when the browser blocks web storage - #18946
Conversation
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Hey @LasseLegarth, thanks for the PR. Kindly rebase with current main.
db9d432 to
a0eb184
Compare
|
@SelaseKay Thanks! Rebased onto current |
SelaseKay
left a comment
There was a problem hiding this comment.
Hi @LasseLegarth, thanks again. LGTM!
|
Hi @LasseLegarth, could you resolve the merge conflicts? |
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.
a0eb184 to
19f37fb
Compare
|
Thanks, rebased on main and resolved the conflict in |
|
@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; |
There was a problem hiding this comment.
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.
Description
When the user blocks site data for the origin (Chrome: "Don't allow sites to save data on your device"), accessing
window.sessionStorageorwindow.localStoragethrows aSecurityError. Several reads on the web startup path were unguarded, and each one makesFirebase.initializeApp()throw, sorunAppis never reached and the user sees a blank page:firebase_core_web:_registerVersionIfNeededuses sessionStorage to avoid registering the same library version twice. It runs insideinitializeApp().firebase_app_check_web:ensurePluginInitializedreads the persisted reCAPTCHA type and site key from localStorage / sessionStorage, andactivate()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:
registerVersion, just without the session cache.activate()still activates, it just cannot persist the settings for the next startup.Behaviour with working storage is unchanged.
firebase_auth_webhad the same problem inensurePluginInitialized, but #18926 has since fixed it onmain(storage is only read in debug builds and aSecurityErroris ignored). After rebasing, this PR no longer changesfirebase_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.sessionStorageandwindow.localStoragewith getters that throw aSecurityErrorDOMException (as Chrome does with site data blocked) and restores the original property descriptors intearDown:firebase_core_web/test/firebase_core_web_blocked_storage_test.dart:initializeAppcompletes and library versions are still registered with the platform logger.firebase_app_check_web/test/blocked_web_storage_test.dart:initializeAppwith App Check registered completes;activatecompletes.firebase_auth_web/test/blocked_web_storage_test.dart:initializeAppwith Auth registered completes;useAuthEmulatorcompletes. 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, sinceinitializeAppfails 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_core5.1.0 +firebase_auth7.0.1, release build, headless Chrome with site data blocked): blank page before, app starts after.Checklist
///).melos run analyze) does not report any problems on my PR.Breaking Change