Repository navigation
fix(core,appcheck,web): start when the browser blocks web storage #18946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright 2026 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| @TestOn('browser') | ||
| library; | ||
|
|
||
| import 'dart:js_interop'; | ||
|
|
||
| import 'package:firebase_app_check_platform_interface/firebase_app_check_platform_interface.dart'; | ||
| import 'package:firebase_app_check_web/firebase_app_check_web.dart'; | ||
| import 'package:firebase_core/firebase_core.dart'; | ||
| import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart'; | ||
| import 'package:firebase_core_web/firebase_core_web.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:flutter_web_plugins/flutter_web_plugins.dart'; | ||
|
|
||
| @JS('eval') | ||
| external JSAny? _eval(String code); | ||
|
|
||
| /// Makes `window.sessionStorage` and `window.localStorage` throw like they do | ||
| /// when the user has blocked site data for the origin. | ||
| void _blockWebStorage() { | ||
| _eval(r''' | ||
| window.__savedWebStorage = {}; | ||
| for (const name of ['sessionStorage', 'localStorage']) { | ||
| window.__savedWebStorage[name] = | ||
| Object.getOwnPropertyDescriptor(window, name); | ||
| Object.defineProperty(window, name, { | ||
| configurable: true, | ||
| get() { | ||
| throw new DOMException( | ||
| `Failed to read the '${name}' property from 'Window': ` + | ||
| 'Access is denied for this document.', | ||
| 'SecurityError', | ||
| ); | ||
| }, | ||
| }); | ||
| } | ||
| '''); | ||
| } | ||
|
|
||
| void _restoreWebStorage() { | ||
| _eval(''' | ||
| for (const [name, descriptor] of Object.entries(window.__savedWebStorage)) { | ||
| Object.defineProperty(window, name, descriptor); | ||
| } | ||
| delete window.__savedWebStorage; | ||
| '''); | ||
| } | ||
|
|
||
| void main() { | ||
| group('FirebaseAppCheckWeb with web storage blocked', () { | ||
| setUpAll(() { | ||
| FirebasePlatform.instance = FirebaseCoreWeb(); | ||
| FirebaseAppCheckWeb.registerWith(webPluginRegistrar); | ||
| }); | ||
|
|
||
| setUp(_blockWebStorage); | ||
|
|
||
| tearDown(_restoreWebStorage); | ||
|
|
||
| test('initializeApp with App Check succeeds', () async { | ||
| const options = FirebaseOptions( | ||
| apiKey: 'fake-api-key', | ||
| appId: '1:1234567890:web:fake', | ||
| messagingSenderId: '1234567890', | ||
| projectId: 'fake-project', | ||
| ); | ||
|
|
||
| await expectLater( | ||
| FirebasePlatform.instance.initializeApp(options: options), | ||
| completes, | ||
| ); | ||
| }); | ||
|
|
||
| test('activate succeeds', () async { | ||
| final appCheck = FirebaseAppCheckWeb(app: Firebase.app()); | ||
|
|
||
| await expectLater( | ||
| appCheck.activate( | ||
| providerWeb: WebDebugProvider(debugToken: 'fake-debug-token'), | ||
| ), | ||
| completes, | ||
| ); | ||
| }); | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Copyright 2026 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| @TestOn('browser') | ||
| library; | ||
|
|
||
| import 'dart:js_interop'; | ||
|
|
||
| import 'package:firebase_auth_web/firebase_auth_web.dart'; | ||
| import 'package:firebase_core/firebase_core.dart'; | ||
| import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart'; | ||
| import 'package:firebase_core_web/firebase_core_web.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:flutter_web_plugins/flutter_web_plugins.dart'; | ||
|
|
||
| @JS('eval') | ||
| external JSAny? _eval(String code); | ||
|
|
||
| /// Makes `window.sessionStorage` and `window.localStorage` throw like they do | ||
| /// when the user has blocked site data for the origin. | ||
| void _blockWebStorage() { | ||
| _eval(r''' | ||
| window.__savedWebStorage = {}; | ||
| for (const name of ['sessionStorage', 'localStorage']) { | ||
| window.__savedWebStorage[name] = | ||
| Object.getOwnPropertyDescriptor(window, name); | ||
| Object.defineProperty(window, name, { | ||
| configurable: true, | ||
| get() { | ||
| throw new DOMException( | ||
| `Failed to read the '${name}' property from 'Window': ` + | ||
| 'Access is denied for this document.', | ||
| 'SecurityError', | ||
| ); | ||
| }, | ||
| }); | ||
| } | ||
| '''); | ||
| } | ||
|
|
||
| void _restoreWebStorage() { | ||
| _eval(''' | ||
| for (const [name, descriptor] of Object.entries(window.__savedWebStorage)) { | ||
| Object.defineProperty(window, name, descriptor); | ||
| } | ||
| delete window.__savedWebStorage; | ||
| '''); | ||
| } | ||
|
LasseLegarth marked this conversation as resolved.
Resolved
|
||
|
|
||
| void main() { | ||
| group('FirebaseAuthWeb with web storage blocked', () { | ||
| setUpAll(() { | ||
| FirebasePlatform.instance = FirebaseCoreWeb(); | ||
| FirebaseAuthWeb.registerWith(webPluginRegistrar); | ||
| }); | ||
|
|
||
| setUp(_blockWebStorage); | ||
|
|
||
| tearDown(_restoreWebStorage); | ||
|
|
||
| test('initializeApp with Auth succeeds', () async { | ||
| const options = FirebaseOptions( | ||
| apiKey: 'fake-api-key', | ||
| appId: '1:1234567890:web:fake', | ||
| messagingSenderId: '1234567890', | ||
| projectId: 'fake-project', | ||
| authDomain: 'fake-project.firebaseapp.com', | ||
| ); | ||
|
|
||
| await expectLater( | ||
| FirebasePlatform.instance.initializeApp(options: options), | ||
| completes, | ||
| ); | ||
| }); | ||
|
|
||
| test('useAuthEmulator succeeds', () async { | ||
| final auth = FirebaseAuthWeb(app: Firebase.app()); | ||
|
|
||
| await expectLater(auth.useAuthEmulator('localhost', 9099), completes); | ||
| }); | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,11 +77,15 @@ class FirebaseCoreWeb extends FirebasePlatform { | |
| String packageVersion, | ||
| ) { | ||
| final sessionKey = 'flutterfire-$libraryName-$packageVersion'; | ||
| final sessionItem = web.window.sessionStorage.getItem(sessionKey); | ||
| if (sessionItem == null) { | ||
| try { | ||
| if (web.window.sessionStorage.getItem(sessionKey) != null) return; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| web.window.sessionStorage.setItem(sessionKey, packageVersion); | ||
| firebase.registerVersion(libraryName, packageVersion); | ||
| } catch (_) { | ||
| // Accessing `window.sessionStorage` throws a SecurityError when the user | ||
| // blocks site data. The cache only avoids duplicate registrations, so | ||
| // register anyway instead of failing `initializeApp`. | ||
| } | ||
| firebase.registerVersion(libraryName, packageVersion); | ||
| } | ||
|
|
||
| static void registerLibraryVersion(String libraryName, String version) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright 2026 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| @TestOn('browser') | ||
| library; | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
|
|
||
| import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart'; | ||
| import 'package:firebase_core_web/firebase_core_web.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| @JS('eval') | ||
| external JSAny? _eval(String code); | ||
|
|
||
| /// Makes `window.sessionStorage` and `window.localStorage` throw like they do | ||
| /// when the user has blocked site data for the origin. | ||
| void _blockWebStorage() { | ||
| _eval(r''' | ||
| window.__savedWebStorage = {}; | ||
| for (const name of ['sessionStorage', 'localStorage']) { | ||
| window.__savedWebStorage[name] = | ||
| Object.getOwnPropertyDescriptor(window, name); | ||
| Object.defineProperty(window, name, { | ||
| configurable: true, | ||
| get() { | ||
| throw new DOMException( | ||
| `Failed to read the '${name}' property from 'Window': ` + | ||
| 'Access is denied for this document.', | ||
| 'SecurityError', | ||
| ); | ||
| }, | ||
| }); | ||
| } | ||
| '''); | ||
| } | ||
|
|
||
| void _restoreWebStorage() { | ||
| _eval(''' | ||
| for (const [name, descriptor] of Object.entries(window.__savedWebStorage)) { | ||
| Object.defineProperty(window, name, descriptor); | ||
| } | ||
| delete window.__savedWebStorage; | ||
| '''); | ||
| } | ||
|
LasseLegarth marked this conversation as resolved.
Resolved
|
||
|
|
||
| String _getPlatformInfoString() { | ||
| final core = globalContext.getProperty<JSObject>('firebase_core'.toJS); | ||
| final jsApp = core.callMethod<JSObject>('getApp'.toJS); | ||
| final container = jsApp.getProperty<JSObject>('container'.toJS); | ||
| final provider = container.callMethod<JSObject>( | ||
| 'getProvider'.toJS, | ||
| 'platform-logger'.toJS, | ||
| ); | ||
| final logger = provider.callMethod<JSObject>('getImmediate'.toJS); | ||
| return logger.callMethod<JSString>('getPlatformInfoString'.toJS).toDart; | ||
| } | ||
|
|
||
| void main() { | ||
| group('FirebaseCoreWeb with web storage blocked', () { | ||
| setUp(() { | ||
| FirebasePlatform.instance = FirebaseCoreWeb(); | ||
| _blockWebStorage(); | ||
| }); | ||
|
|
||
| tearDown(_restoreWebStorage); | ||
|
|
||
| test( | ||
| 'initializeApp succeeds and still registers library versions', | ||
| () async { | ||
| const options = FirebaseOptions( | ||
| apiKey: 'fake-api-key', | ||
| appId: '1:1234567890:web:fake', | ||
| messagingSenderId: '1234567890', | ||
| projectId: 'fake-project', | ||
| ); | ||
|
|
||
| await FirebasePlatform.instance.initializeApp(options: options); | ||
| expect(_getPlatformInfoString(), contains('flutter-fire-core/')); | ||
|
|
||
| FirebaseCoreWeb.registerLibraryVersion('flutter-fire-blocked', '1.0.0'); | ||
| expect( | ||
| _getPlatformInfoString(), | ||
| contains('flutter-fire-blocked/1.0.0'), | ||
| ); | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.