Skip to content

fix: add Content-Type validation before JSON parsing in API client #370

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

Merged
merged 3 commits into from
Jul 10, 2025

Conversation

dcramer
Copy link
Member

@dcramer dcramer commented Jul 10, 2025

Prevents "Unexpected token '<'" errors when servers return HTML instead of JSON (e.g., login pages, maintenance pages). The API client now:

  • Checks Content-Type header before parsing JSON responses
  • Provides clear error messages when receiving unexpected content types
  • Detects HTML responses even without proper Content-Type headers
  • Handles edge cases gracefully (missing headers, test environments)

This fixes MCP-SERVER-E9P and MCP-SERVER-E9S errors where HTML error pages were being parsed as JSON, causing cryptic parse errors.

🤖 Generated with Claude Code

Prevents "Unexpected token '<'" errors when servers return HTML instead of JSON
(e.g., login pages, maintenance pages). The API client now:

- Checks Content-Type header before parsing JSON responses
- Provides clear error messages when receiving unexpected content types
- Detects HTML responses even without proper Content-Type headers
- Handles edge cases gracefully (missing headers, test environments)

This fixes MCP-SERVER-E9P and MCP-SERVER-E9S errors where HTML error pages
were being parsed as JSON, causing cryptic parse errors.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link

codecov bot commented Jul 10, 2025

Codecov Report

Attention: Patch coverage is 94.20290% with 4 lines in your changes missing coverage. Please review.

Project coverage is 62.46%. Comparing base (cef8b2b) to head (c852c22).
Report is 1 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
packages/mcp-server/src/api-client/client.ts 94.20% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #370      +/-   ##
==========================================
+ Coverage   62.40%   62.46%   +0.05%     
==========================================
  Files          77       77              
  Lines        6828     6844      +16     
  Branches      601      613      +12     
==========================================
+ Hits         4261     4275      +14     
- Misses       2567     2569       +2     
Flag Coverage Δ
unittests 62.46% <94.20%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

cursor[bot]

This comment was marked as outdated.

dcramer and others added 2 commits July 9, 2025 19:08
The listOrganizations method was missing Content-Type validation for the
regions endpoint response, leaving it vulnerable to the same "Unexpected
token '<'" errors that the rest of the fix addresses.

This ensures all JSON parsing in the API client goes through the same
validation logic.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Centralizes JSON response handling by introducing a requestJSON method that
combines request() and parseJsonResponse(). This simplifies all API methods
and ensures consistent Content-Type validation across the entire API client.

All methods that expect JSON responses now use requestJSON directly instead
of the two-step process, making the code more maintainable and less prone
to missing validation.

The request() method is still available for non-JSON responses like binary
downloads (e.g., getEventAttachment).

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bug: JSON Parsing Fails on Non-JSON Content

The parseJsonResponse method's if (!response.headers?.get) condition incorrectly bypasses Content-Type validation when response.headers.get is falsy or not a function. This allows non-JSON responses (e.g., HTML) to be passed to response.json(), leading to parsing errors, inconsistent behavior, and potential security risks. The condition should instead verify that response.headers.get is a function.

packages/mcp-server/src/api-client/client.ts#L317-L321

private async parseJsonResponse(response: Response): Promise<unknown> {
// Handle case where response might not have all properties (e.g., in tests or promise chains)
if (!response.headers?.get) {
return response.json();
}

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎

@dcramer dcramer merged commit 56aff50 into main Jul 10, 2025
13 checks passed
@dcramer dcramer deleted the handle-invalid-urls branch July 10, 2025 03:46
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.

1 participant