Skip to content

fix: add error handling for JSON.parse in _restorePowers()#2975

Open
lawrence3699 wants to merge 1 commit intoFreezingMoon:masterfrom
lawrence3699:fix/unsafe-json-parse-in-meta-powers
Open

fix: add error handling for JSON.parse in _restorePowers()#2975
lawrence3699 wants to merge 1 commit intoFreezingMoon:masterfrom
lawrence3699:fix/unsafe-json-parse-in-meta-powers

Conversation

@lawrence3699
Copy link
Copy Markdown

Summary

  • Wraps JSON.parse(Cookies.get(COOKIE_KEY)) in try-catch with null checks
  • Cookies.get() can return undefined, causing JSON.parse(undefined) to throw SyntaxError
  • Corrupted cookie data also causes unhandled crash in the meta powers UI

Changes

  • Added null check for cookie value before parsing
  • Added null check for parsed.toggles before iterating
  • Added optional chaining on powers[key]?.enabled
  • Wrapped in try-catch with console error logging

Test plan

  • Toggle meta powers on and off — verify persistence works
  • Clear cookies and reload — verify no crash
  • Manually corrupt the cookie value — verify graceful fallback

Cookies.get() can return undefined, and the cookie value may contain
invalid JSON (browser storage corruption, manual editing). The current
code calls JSON.parse() without try-catch, which throws an unhandled
exception and crashes the meta powers UI. Added null checks and
try-catch with console error logging.
Copilot AI review requested due to automatic review settings April 2, 2026 04:46
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 2, 2026

@lawrence3699 is attempting to deploy a commit to the FreezingMoon Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 2, 2026

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

Project Deployment Actions Updated (UTC)
ancientbeast Ready Ready Preview Apr 2, 2026 4:48am

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens the Meta Powers UI cookie restore path to avoid crashes when the persisted cookie is missing, malformed, or contains unexpected data.

Changes:

  • Added null checks around Cookies.get() and parsed JSON content before accessing toggles.
  • Wrapped cookie parsing/restoration in a try/catch and added error logging.
  • Made restoration more tolerant via optional chaining on powers[key]?.enabled.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +187 to +189
if (powers[key]?.enabled) {
this._togglePower(key, this[`btn${capitalize(key)}`]);
}
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

key comes from cookie data and may not correspond to a valid meta power. If key is unknown, _togglePower(key, ...) will throw when it accesses this.toggles[stateKey], and this[btn${capitalize(key)}] may be undefined. Consider restricting restoration to known keys (e.g., only keys present in this.toggles) and verifying the button instance exists before calling _togglePower, so a partially corrupted cookie can still restore valid toggles without hitting the catch-all error path.

Suggested change
if (powers[key]?.enabled) {
this._togglePower(key, this[`btn${capitalize(key)}`]);
}
// Only restore powers that are known and have an associated button
if (!Object.prototype.hasOwnProperty.call(this.toggles, key)) {
return;
}
const button = this[`btn${capitalize(key)}` as keyof this] as unknown as Button | undefined;
if (!button) {
return;
}
if (powers[key]?.enabled && !this.toggles[key].enabled) {
this._togglePower(key, button);
}

Copilot uses AI. Check for mistakes.
Comment on lines +191 to +193
} catch (error) {
console.error('Failed to restore meta powers from cookie:', error);
}
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

In the catch, the code logs the parse failure but leaves the corrupted cookie intact. This will trigger the same error on every page load until the user clears cookies manually. Consider removing/resetting COOKIE_KEY when restoration fails (or when the parsed shape is invalid) to allow a clean fallback on subsequent loads.

Copilot uses AI. Check for mistakes.
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.

2 participants