-
Notifications
You must be signed in to change notification settings - Fork 229
Fix SecurityError when accessing localStorage in restricted browser environments #1452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix SecurityError when accessing localStorage in restricted browser environments #1452
Conversation
|
Thanks for your pull request. Is this your first contribution to a Snowplow open source project? Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://docs.snowplowanalytics.com/docs/contributing/contributor-license-agreement/ to learn more and sign. Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks. |
| try { | ||
| window.localStorage.setItem(queueName, JSON.stringify(events)); | ||
| } catch (e) { | ||
| // Silently fail if localStorage is not accessible |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Rather than failing silently, can we here and above in the other catch block call console.error to log the failure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added console.error logging in both catch blocks to help with debugging localStorage access issues. The error messages now indicate:
- "Failed to access localStorage when initializing event store" (line 29)
- "Failed to persist events to localStorage" (line 45)
Co-authored-by: matus-tomlein <[email protected]>
|
@copilot running the tests results in an error: |
Co-authored-by: matus-tomlein <[email protected]>
... Fixed the test to use the proper |
…nvironments (#1452) * Initial plan * Wrap localStorage access in try-catch blocks to handle SecurityError * Add console.error logging for localStorage access failures Co-authored-by: matus-tomlein <[email protected]> * Fix test to use proper EventStorePayload format Co-authored-by: matus-tomlein <[email protected]> * Run rush change --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: matus-tomlein <[email protected]> Co-authored-by: Matus Tomlein <[email protected]>
Fix: Wrap localStorage access in try-catch blocks to prevent app crashes
Problem
The Snowplow JavaScript tracker was directly accessing
window.localStoragewithout proper error handling, which throws aSecurityErrorin certain browser configurations:dom.storage.enableddisabledThis caused the tracker to break the host application.
Solution
Wrapped both localStorage access points in
local_storage_event_store.tswith try-catch blocks:window.localStorage.getItem()- Now wrapped in try-catch, falls back to empty in-memory store on error and logs error to consolewindow.localStorage.setItem()- Now wrapped in try-catch, silently continues with in-memory storage on error and logs error to consoleBehavior After Fix
Testing
Added comprehensive test suite (
test/tracker/local_storage_event_store.test.ts) that verifies:getItem()throws SecurityErrorsetItem()throws SecurityErrorChanges Made:
window.localStorage.getItem()call in try-catch (line 24)window.localStorage.setItem()call in try-catch (line 42)Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.