Skip to content

Commit 4f2487f

Browse files
committed
Add try-catch around window.sessionStorage access
1 parent 04d4283 commit 4f2487f

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased changes
22

3+
* [#1977](https://github.com/mozilla/glean.js/pull/1977): Add try-catch around window.sessionStorage access to prevent uncaught error upon `import`.
4+
35
[Full changelog](https://github.com/mozilla/glean.js/compare/v5.0.3...main)
46

57
# v5.0.3 (2024-08-02)

glean/src/core/glean.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,19 @@ declare global {
533533
}
534534

535535
// Only set `Glean` values whenever running inside of a browser.
536-
if (
537-
typeof window !== "undefined" &&
538-
typeof window.sessionStorage !== "undefined"
539-
) {
536+
// When cookies/localStorage/sessionStorage are disabled (at least in Firefox
537+
// and Chrome), an access attempt on the window.sessionStorage property will
538+
// throw an error ("The operation is insecure." in Firefox).
539+
// So try-catch is needed here.
540+
let hasStorage = false;
541+
try {
542+
hasStorage = typeof window !== "undefined" &&
543+
typeof window.sessionStorage !== "undefined";
544+
} catch (e) {
545+
console.error("No session storage available", e);
546+
}
547+
548+
if (hasStorage) {
540549
window.Glean = {
541550
setLogPings: (flag: boolean) => {
542551
setDebugOptionInSessionStorage(DebugOption.LogPings, flag);

0 commit comments

Comments
 (0)