[recovery] Guard localStorage access in TranslationBanner against SecurityError - #19192
Draft
ethorg-agent-code[bot] wants to merge 1 commit into
Draft
Conversation
Some browsers deny access to the localStorage property entirely (e.g. strict privacy settings), which throws a SecurityError rather than returning null. Wrap both the read and write in try/catch so the banner degrades gracefully instead of crashing the effect.
✅ Deploy Preview for ethereumorg ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Production error
SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.sentry-ETHORG-1AF)/:localeRoot cause
TranslationBanner(src/components/TranslationBanner/index.tsx) is rendered on every page viaBaseLayout, matching the reported/:localeculprit. ItsuseEffectreadlocalStorage.getItem(lsKey)directly, andhandleDontShowwrote vialocalStorage.setItem(lsKey, "true")directly, with no error handling.Some browsers/environments deny access to the
localStorageproperty itself (not just individual keys) -- e.g. strict privacy settings, certain private-browsing modes, or storage-partitioning restrictions -- which throws aSecurityErrorsynchronously rather than returningnull. Since this call happened directly in auseEffect/event handler with no try/catch, the exception propagated uncaught.The project's own
useLocalStoragehook (src/hooks/useLocalStorage.ts) already wraps alllocalStorageaccess in try/catch for this exact reason, butTranslationBannerpredates/bypasses that hook and accesseslocalStoragedirectly.Fix
Wrap both the read (in the
useEffect) and the write (inhandleDontShow) in try/catch, defaulting to showing the banner if the read fails, matching the defensive pattern already used elsewhere in the codebase (src/lib/utils/matomo.ts,useLocalStorage.ts).Verification
pnpm type-check-- passespnpm exec eslint src/components/TranslationBanner/index.tsx-- clean, no warnings/errors