-
Notifications
You must be signed in to change notification settings - Fork 257
Fix Safari SecurityError when Block All Cookies is enabled #2539
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
base: main
Are you sure you want to change the base?
Changes from 13 commits
e1a71fb
d1ffffc
c13a592
16f3b92
a488d75
fd68d94
831c9b4
d472d65
d2cd04d
93c0f44
d4677e0
cdf088a
22169a2
cee30cd
9e5c985
d0ee829
2274771
59362d4
b9789af
e775471
d25a325
7e32aa3
ec81543
23f5e50
98c0e2c
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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import { | |
| IDiagnosticLogger, _eInternalMessageId, _throwInternal, dumpObj, eLoggingSeverity, getExceptionName, getGlobal, getGlobalInst, | ||
| isNullOrUndefined, objForEachKey | ||
| } from "@microsoft/applicationinsights-core-js"; | ||
| import { objGetOwnPropertyDescriptor } from "@nevware21/ts-utils"; | ||
| import { StorageType } from "./Enums"; | ||
|
|
||
| let _canUseLocalStorage: boolean = undefined; | ||
|
|
@@ -30,24 +31,52 @@ function _getLocalStorageObject(): Storage { | |
| * @returns {Storage} Returns storage object verified that it is usable | ||
| */ | ||
| function _getVerifiedStorageObject(storageType: StorageType): Storage { | ||
| let result = null; | ||
| const storageTypeName = storageType === StorageType.LocalStorage ? "localStorage" : "sessionStorage"; | ||
|
|
||
| try { | ||
| if (isNullOrUndefined(getGlobal())) { | ||
| return null; | ||
| // Default to false - assume storage is not available | ||
| let canAccessStorage = false; | ||
| const gbl = getGlobal(); | ||
|
|
||
| // Only proceed if we have a global object | ||
| if (!isNullOrUndefined(gbl)) { | ||
| // Try the safest method first (property descriptor) | ||
| try { | ||
| const descriptor = objGetOwnPropertyDescriptor(gbl, storageTypeName); | ||
| if (descriptor && descriptor.get) { | ||
| canAccessStorage = true; | ||
| } | ||
| } catch (e) { | ||
| // If descriptor check fails, try direct access | ||
| // This will be caught by the outer try-catch if it fails | ||
| canAccessStorage = !!gbl[storageTypeName]; | ||
| } | ||
| } | ||
| let uid = (new Date).toString(); | ||
| let storage: Storage = getGlobalInst(storageType === StorageType.LocalStorage ? "localStorage" : "sessionStorage"); | ||
| let name:string = _storagePrefix + uid; | ||
| storage.setItem(name, uid); | ||
| let fail = storage.getItem(name) !== uid; | ||
| storage.removeItem(name); | ||
| if (!fail) { | ||
| return storage; | ||
|
|
||
| // If we determined storage might be accessible, verify it works | ||
| if (canAccessStorage) { | ||
MSNev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| const uid = (new Date).toString(); | ||
MSNev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const storage: Storage = getGlobalInst(storageTypeName); | ||
| const name = _storagePrefix + uid; | ||
MSNev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| storage.setItem(name, uid); | ||
| const success = storage.getItem(name) === uid; | ||
| storage.removeItem(name); | ||
|
|
||
| if (success) { | ||
| result = storage; | ||
|
||
| } | ||
| } catch (e) { | ||
| // Storage exists but can't be used (quota exceeded, etc.) | ||
| } | ||
| } | ||
| } catch (exception) { | ||
| // eslint-disable-next-line no-empty | ||
| } catch (e) { | ||
| // Catch any unexpected errors | ||
| } | ||
|
|
||
| return null; | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.