-
Notifications
You must be signed in to change notification settings - Fork 875
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Auth] Make auth resilient against localStorage and sessionStorage pe…
…rmissions errors (#5635) * Make auth resilient against localStorage and sessionStorage permissions errors * Restore demo code * Add changeset
- Loading branch information
Showing
8 changed files
with
139 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@firebase/auth": patch | ||
--- | ||
|
||
Make the library resilient against localStorage and sessionStorage permissions errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/auth/src/platform_browser/persistence/browser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
import { expect } from 'chai'; | ||
import { | ||
PersistenceType | ||
} from '../../core/persistence'; | ||
import { BrowserPersistenceClass } from './browser'; | ||
|
||
// Most tests for this class exist in the tests for the subclasses. | ||
|
||
class TestPersistence extends BrowserPersistenceClass { | ||
constructor(storageRetriever: () => Storage) { | ||
super(storageRetriever, PersistenceType.NONE); | ||
} | ||
} | ||
|
||
describe('platform_browser/persistence/browser', () => { | ||
it('_isAvailable works if reading the storage accessor throws', async () => { | ||
const browserPersistence = new TestPersistence(() => { | ||
throw new DOMException('no'); | ||
}); | ||
expect(await browserPersistence._isAvailable()).to.be.false; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/auth/test/helpers/erroring_unavailable_persistence.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { PersistenceInternal, PersistenceType, PersistenceValue } from '../../src/core/persistence'; | ||
|
||
const PERMISSION_ERROR = typeof window !== 'undefined' ? new DOMException( | ||
'Failed to read this storage class from the Window; access is denied') | ||
: new Error('This is Node.'); | ||
|
||
/** | ||
* Helper class for mocking completely broken persistence that errors when | ||
* accessed. | ||
* | ||
* When disabling cookies in Chrome entirely, for example, simply reading the | ||
* "localStorage" field in "window" will throw an error, but this can't be | ||
* checked for by calling `'localStorage' in window`. This class simulates a | ||
* situation where _isAvailable works correctly but all other methods fail. | ||
*/ | ||
export class ErroringUnavailablePersistence implements PersistenceInternal { | ||
type = PersistenceType.NONE; | ||
async _isAvailable(): Promise<boolean> { | ||
return false; | ||
} | ||
async _set(): Promise<void> { | ||
throw PERMISSION_ERROR; | ||
} | ||
async _get<T extends PersistenceValue>(): Promise<T | null> { | ||
throw PERMISSION_ERROR; | ||
} | ||
async _remove(): Promise<void> { | ||
throw PERMISSION_ERROR; | ||
} | ||
_addListener(): void { | ||
throw PERMISSION_ERROR; | ||
} | ||
_removeListener(): void { | ||
throw PERMISSION_ERROR; | ||
} | ||
_shouldAllowMigration = false; | ||
} | ||
|