From 6444b3403952769f7a3f56f54c3e6628b1bf30dc Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 17 Jul 2023 14:12:53 +0200 Subject: [PATCH] fix(replay): Better session storage check (#8547) Apparently accessing sessionStorage in an iframe with certain permissions can result in a throw, so we try-catch this to ensure we do not produce any errors. Closes https://github.com/getsentry/sentry-javascript/issues/8392 --- packages/replay/src/util/hasSessionStorage.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/replay/src/util/hasSessionStorage.ts b/packages/replay/src/util/hasSessionStorage.ts index f242df101c25..e7be6788eb1a 100644 --- a/packages/replay/src/util/hasSessionStorage.ts +++ b/packages/replay/src/util/hasSessionStorage.ts @@ -2,5 +2,10 @@ import { WINDOW } from '../constants'; /** If sessionStorage is available. */ export function hasSessionStorage(): boolean { - return 'sessionStorage' in WINDOW && !!WINDOW.sessionStorage; + try { + // This can throw, e.g. when being accessed in a sandboxed iframe + return 'sessionStorage' in WINDOW && !!WINDOW.sessionStorage; + } catch { + return false; + } }