fix(platform-selector): Guard localStorage access to prevent ReferenceError#18835
fix(platform-selector): Guard localStorage access to prevent ReferenceError#18835sentry[bot] wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| if (isLocalStorageAvailable()) { | ||
| localStorage.setItem('active-platform', platform_.key); | ||
| } |
There was a problem hiding this comment.
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~152src/components/platformSelector/index.tsx:397~399src/components/platformSelector/index.tsx:521~523
Did we get this right? 👍 / 👎 to inform future reviews.
DESCRIBE YOUR PR
This PR addresses DOCS-A4Y, a
ReferenceError: Can't find variable: localStorageoccurring insrc/components/platformSelector/index.tsx.Root Cause:
The
platformSelectorcomponent directly accessedlocalStorage.setItemandlocalStorage.getItemwithin auseLayoutEffecthook and other event handlers without checking forlocalStorageavailability. In environments with strict privacy settings (e.g., Safari private browsing, Apple Mail webviews),localStorageis either undefined or throws an error upon access, leading to theReferenceError.Solution:
All instances of
localStorageaccess insrc/components/platformSelector/index.tsxhave been wrapped with a check using the existingisLocalStorageAvailable()utility fromsentry-docs/utils. This ensures thatlocalStorageoperations are only attempted when the API is actually available, preventing theReferenceErrorand 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.
SLA
Thanks in advance for your help!
PRE-MERGE CHECKLIST
Make sure you've checked the following before merging your changes:
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.