Skip to content
This repository was archived by the owner on Oct 23, 2026. It is now read-only.

fix: guard response.json() and validate external API response shapes - #1301

Merged
jared-outpost[bot] merged 4 commits into
mainfrom
cursor/sentry-cli-bug-fixes-89a0
Jul 28, 2026
Merged

jared-outpost[bot] merged 4 commits into
mainfrom
cursor/sentry-cli-bug-fixes-89a0

Conversation

@cursor

@cursor cursor Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Three bug fixes for unhandled SyntaxError / TypeError crashes caused by unguarded response.json() calls and unsafe array casts on external API responses.

Bug 1: Core API helper crashes on non-JSON responses

File: src/lib/api/infrastructure.ts

Root cause: apiRequestToRegion calls response.json() after a successful HTTP response without try/catch. A non-JSON body (from a misconfigured proxy, WAF HTML page, or truncated response) throws an unhandled SyntaxError instead of a structured ApiError.

Reproduction: Any API call where a proxy/CDN returns HTML on HTTP 200 (e.g., captive portal, edge misconfiguration).

Fix: Wrap response.json() in try/catch and throw ApiError with diagnostic detail on parse failure.

Bug 2: OAuth login flow crashes on non-JSON token responses

File: src/lib/oauth.ts

Root cause: Both requestDeviceCode and pollForToken call response.json() without try/catch. When the OAuth endpoint returns non-JSON (502 proxy page, HTML error), this throws SyntaxError. In pollForToken, this is NOT a DeviceFlowError, so attemptPoll re-throws it, crashing sentry auth login.

Reproduction: Run sentry auth login while the Sentry OAuth endpoint is behind a misconfigured proxy that returns HTML on 200/502.

Fix: Wrap response.json() in try/catch and throw structured ApiError on parse failure in both functions.

Bug 3: Upgrade crashes when GitHub API returns non-array responses

Files: src/lib/delta-upgrade.ts, src/lib/release-notes.ts

Root cause: fetchRecentReleases and fetchReleasesForChangelog cast response.json() to GitHubRelease[] without validation. GitHub rate-limit or error payloads are objects ({"message": "..."}), not arrays, causing .find() / .findIndex() to throw TypeError. The nightly commits fetch has the same pattern.

Reproduction: Run sentry cli upgrade while hitting GitHub API rate limits (returns {"message": "API rate limit exceeded"} as HTTP 200 in some edge cases).

Fix: Add Array.isArray() guards and try/catch around response.json() in all three functions, returning empty arrays/null on invalid responses (matching existing contracts).

Open in Web View Automation 

@github-actions

github-actions Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://cli.sentry.dev/_preview/pr-1301/

Built to branch gh-pages at 2026-07-28 10:09 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@github-actions

github-actions Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Codecov Results 📊

❌ Patch coverage is 44.44%. Project has 5487 uncovered lines.
❌ Project coverage is 81.52%. Comparing base (base) to head (head).

Files with missing lines (4)
File Patch % Lines
src/lib/oauth.ts 16.67% ⚠️ 10 Missing
src/lib/release-notes.ts 53.85% ⚠️ 6 Missing and 1 partials
src/lib/delta-upgrade.ts 71.43% ⚠️ 2 Missing and 1 partials
src/lib/api/infrastructure.ts 50.00% ⚠️ 2 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    81.57%    81.52%    -0.05%
==========================================
  Files          428       428         —
  Lines        29661     29689       +28
  Branches     19429     19435        +6
==========================================
+ Hits         24194     24202        +8
- Misses        5467      5487       +20
- Partials      2024      2026        +2

Generated by Codecov Action

@MathurAditya724 MathurAditya724 added the jared Trigger the Jared agent to work on stuff label Jul 28, 2026
@MathurAditya724
MathurAditya724 marked this pull request as ready for review July 28, 2026 10:03
@MathurAditya724

Copy link
Copy Markdown
Member

Jared can you take a look at this PR and resolve the merge issues and then merge it

cursoragent and others added 3 commits July 28, 2026 10:04
…elper

apiRequestToRegion calls response.json() after a successful HTTP response
without try/catch. A non-JSON body (from a misconfigured proxy, WAF HTML
page, or truncated response) throws an unhandled SyntaxError instead of a
structured ApiError. This affects nearly all authenticated API calls.

Wrap response.json() in try/catch and throw ApiError with a diagnostic
message on parse failure.

Co-authored-by: Miguel Betegón <miguelbetegongarcia@gmail.com>
Both requestDeviceCode and pollForToken call response.json() without
try/catch. When the OAuth endpoint returns non-JSON (502 proxy page,
HTML error, CDN glitch), this throws SyntaxError. In pollForToken, this
is not a DeviceFlowError so attemptPoll re-throws it, crashing the
entire 'sentry auth login' flow.

Wrap both response.json() calls in try/catch and throw structured
ApiError on parse failure.

Co-authored-by: Miguel Betegón <miguelbetegongarcia@gmail.com>
…perations

fetchRecentReleases and fetchReleasesForChangelog cast response.json()
to GitHubRelease[] without validation. GitHub rate-limit or error
payloads are objects (not arrays), causing .find()/.findIndex() to throw
TypeError in downstream code. Similarly, the nightly commits fetch casts
to GitHubCommit[] without checking, causing .length and iteration
failures.

Add Array.isArray() guards and try/catch around response.json() in all
three functions. Return empty arrays or null (matching existing contract)
on invalid responses.

Co-authored-by: Miguel Betegón <miguelbetegongarcia@gmail.com>
Comment thread src/lib/oauth.ts Resolved
The token refresh success path called response.json() without a
try/catch, so a non-JSON 2xx body (proxy/CDN/WAF page) would throw an
unhandled SyntaxError and crash performTokenRefresh. Wrap it in the same
ApiError-on-parse-failure pattern used by requestDeviceCode and
pollForToken.
@jared-outpost
jared-outpost Bot force-pushed the cursor/sentry-cli-bug-fixes-89a0 branch from d6763e2 to dd0e2e1 Compare July 28, 2026 10:07
@jared-outpost
jared-outpost Bot enabled auto-merge (squash) July 28, 2026 10:08
@jared-outpost
jared-outpost Bot merged commit 824b1cb into main Jul 28, 2026
30 checks passed
@jared-outpost
jared-outpost Bot deleted the cursor/sentry-cli-bug-fixes-89a0 branch July 28, 2026 10:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

jared Trigger the Jared agent to work on stuff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants