Skip to content

fix(platform-selector): Guard localStorage access to prevent ReferenceError#18835

Open
sentry[bot] wants to merge 1 commit into
masterfrom
seer/fix/docs-a4y-localstorage-guard
Open

fix(platform-selector): Guard localStorage access to prevent ReferenceError#18835
sentry[bot] wants to merge 1 commit into
masterfrom
seer/fix/docs-a4y-localstorage-guard

Conversation

@sentry

@sentry sentry Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

DESCRIBE YOUR PR

This PR addresses DOCS-A4Y, a ReferenceError: Can't find variable: localStorage occurring in src/components/platformSelector/index.tsx.

Root Cause:
The platformSelector component directly accessed localStorage.setItem and localStorage.getItem within a useLayoutEffect hook and other event handlers without checking for localStorage availability. In environments with strict privacy settings (e.g., Safari private browsing, Apple Mail webviews), localStorage is either undefined or throws an error upon access, leading to the ReferenceError.

Solution:
All instances of localStorage access in src/components/platformSelector/index.tsx have been wrapped with a check using the existing isLocalStorageAvailable() utility from sentry-docs/utils. This ensures that localStorage operations are only attempted when the API is actually available, preventing the ReferenceError and allowing the component to gracefully degrade in restricted environments (i.e., platform selection will not be persisted, but the page will load without error).

IS YOUR CHANGE URGENT?

Help us prioritize incoming PRs by letting us know when the change needs to go live.

  • Urgent deadline (GA date, etc.):
  • Other deadline:
  • None: Not urgent, can wait up to 1 week+

SLA

  • Teamwork makes the dream work, so please add a reviewer to your PRs.
  • Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it.
    Thanks in advance for your help!

PRE-MERGE CHECKLIST

Make sure you've checked the following before merging your changes:

  • Checked Vercel preview for correctness, including links
  • PR was reviewed and approved by any necessary SMEs (subject matter experts)
  • PR was reviewed and approved by a member of the Sentry docs team

LEGAL BOILERPLATE

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

EXTRA RESOURCES

Fixes DOCS-A4Y

Comment @sentry <feedback> on this PR to have Autofix iterate on the changes.

@vercel

vercel Bot commented Jul 25, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
develop-docs Ready Ready Preview Jul 25, 2026 10:48am
sentry-docs Ready Ready Preview Jul 25, 2026 10:48am

Request Review

@sentry
sentry Bot marked this pull request as ready for review July 25, 2026 10:42
Comment on lines +119 to +121
if (isLocalStorageAvailable()) {
localStorage.setItem('active-platform', platform_.key);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The isLocalStorageAvailable() check is unsafe. In environments like Safari private browsing, the typeof localStorage check will itself throw an error, causing a crash instead of preventing one.
Severity: HIGH

Suggested Fix

Update the isLocalStorageAvailable utility to use a try-catch block. The implementation should attempt a real operation, like setting and removing a test item, to reliably determine if localStorage is accessible and writable. For example: try { localStorage.setItem('__test__', '1'); localStorage.removeItem('__test__'); return true; } catch { return false; }.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: src/components/platformSelector/index.tsx#L119-L121

Potential issue: The utility function `isLocalStorageAvailable()` uses `typeof
localStorage !== 'undefined'` to check for the availability of local storage. However,
in certain environments with strict privacy settings, such as Safari's private browsing
mode, accessing the `localStorage` object itself throws a `SecurityError` or
`ReferenceError`. The `typeof` operator does not prevent this, as it must access the
property to evaluate its type, which triggers the error. Consequently, the guard check
`isLocalStorageAvailable()` will crash the application in these environments, defeating
its purpose of preventing crashes when accessing `localStorage`.

Also affects:

  • src/components/platformSelector/index.tsx:150~152
  • src/components/platformSelector/index.tsx:397~399
  • src/components/platformSelector/index.tsx:521~523

Did we get this right? 👍 / 👎 to inform future reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants