Skip to content

fix: safely parse portal URL hash params to prevent crash#26910

Open
Rohit7241 wants to merge 2 commits intoTryGhost:mainfrom
Rohit7241:fix-safe-json-parse-portal
Open

fix: safely parse portal URL hash params to prevent crash#26910
Rohit7241 wants to merge 2 commits intoTryGhost:mainfrom
Rohit7241:fix-safe-json-parse-portal

Conversation

@Rohit7241
Copy link

🐛 Fixed portal URL hash parsing to prevent crash

closes #26399

The previous implementation of parsing URL hash parameters could
throw errors if the hash was malformed or missing. This change
introduces a safe JSON parse utility that ensures invalid or empty
hashes do not crash the portal. This improves stability and prevents
unexpected runtime errors for users navigating with unusual URLs.

@cursor
Copy link

cursor bot commented Mar 22, 2026

You have used all of your free Bugbot PR reviews.

To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 22, 2026

Walkthrough

The change introduces a safeJSONParse() helper function in apps/portal/src/app.js that wraps JSON parsing in a try/catch block and logs warnings instead of throwing errors. All direct JSON.parse() calls processing URL query parameters—including button, name, portal_signup_checkbox_required, disableBackground, and transistorPortalSettings—were replaced with this helper. Boolean plan flag handling (isFree, isMonthly, isYearly) was adjusted to use the safe parser in conditional checks. The updateStateForPreviewLinks() function was also wrapped in try/catch to handle failures during preview data fetching and state updates.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: safely parse portal URL hash params to prevent crash' directly and clearly summarizes the main change: introducing safe JSON parsing for portal URL hash parameters to prevent crashes.
Description check ✅ Passed The description explains the fix for portal URL hash parsing, references the linked issue, and describes the improvements to stability—all directly related to the changeset.
Linked Issues check ✅ Passed The PR addresses all objectives from issue #26399: implements a safeJSONParse wrapper for the nine parameters, protects both initial load and hashchange paths, and adds error handling to updateStateForPreviewLinks().
Out of Scope Changes check ✅ Passed All changes in the PR are scoped to fixing the JSON parsing crash issue in apps/portal/src/app.js with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/portal/src/app.js`:
- Around line 29-37: In safeJSONParse, replace the incorrect JSON.Parse call
with the correct JSON.parse to avoid the TypeError and allow valid JSON to
parse; update the return statement inside the try block to call
JSON.parse(value) (leaving fallback behavior and the warning in the catch
unchanged) and run a quick smoke test to confirm valid URL JSON now returns
parsed objects from safeJSONParse.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 922cbdf1-8266-4144-8353-41bdb1ca2586

📥 Commits

Reviewing files that changed from the base of the PR and between 248fe42 and ed640ae.

📒 Files selected for processing (1)
  • apps/portal/src/app.js

Comment on lines +29 to +37
function safeJSONParse(value, fallback = null) {
try {
return JSON.Parse(value);
} catch (e) {
/* eslint-disable no-console */
console.warn('[Portal] Invalid JSON in URL parameter:', e.message);
return fallback;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Critical: JSON.Parse is not a valid function — should be JSON.parse.

JavaScript is case-sensitive. JSON.Parse does not exist and will throw TypeError: JSON.Parse is not a function on every call. This causes all valid JSON to fall through to the catch block, returning null and defeating the entire purpose of this fix.

🐛 Proposed fix
 function safeJSONParse(value, fallback = null) {
     try {
-        return JSON.Parse(value);
+        return JSON.parse(value);
     } catch (e) {
         /* eslint-disable no-console */
         console.warn('[Portal] Invalid JSON in URL parameter:', e.message);
         return fallback;
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function safeJSONParse(value, fallback = null) {
try {
return JSON.Parse(value);
} catch (e) {
/* eslint-disable no-console */
console.warn('[Portal] Invalid JSON in URL parameter:', e.message);
return fallback;
}
}
function safeJSONParse(value, fallback = null) {
try {
return JSON.parse(value);
} catch (e) {
/* eslint-disable no-console */
console.warn('[Portal] Invalid JSON in URL parameter:', e.message);
return fallback;
}
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/portal/src/app.js` around lines 29 - 37, In safeJSONParse, replace the
incorrect JSON.Parse call with the correct JSON.parse to avoid the TypeError and
allow valid JSON to parse; update the return statement inside the try block to
call JSON.parse(value) (leaving fallback behavior and the warning in the catch
unchanged) and run a quick smoke test to confirm valid URL JSON now returns
parsed objects from safeJSONParse.

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.

Unhandled JSON.parse() exceptions in Portal's fetchQueryStrData() crash widget on malformed preview URLs

1 participant