Skip to content

Unprotected JSON.parse in provider fetch wrapper can crash requests #11698

Closed

Description

@ryan-mt

Summary

The provider fetch wrapper contains an unprotected JSON.parse call that can crash API requests if the body is malformed.

Location

packages/opencode/src/provider/provider.ts:1010

Issue

if (model.api.npm === "@ai-sdk/openai" && opts.body && opts.method === "POST") {
  const body = JSON.parse(opts.body as string)  // NO TRY-CATCH
  const isAzure = model.providerID.includes("azure")
  // ...
}

If opts.body is not valid JSON (which shouldn't happen normally, but could due to a bug elsewhere), this will throw an unhandled error and crash the entire request.

Impact

  • Severity: Medium
  • Type: Error Handling
  • Effect: Malformed request bodies cause unhandled exceptions

Suggested Fix

Wrap in try-catch:

if (model.api.npm === "@ai-sdk/openai" && opts.body && opts.method === "POST") {
  try {
    const body = JSON.parse(opts.body as string)
    const isAzure = model.providerID.includes("azure")
    // ... rest of logic
  } catch (e) {
    // Log warning and continue with original body
    log.warn("failed to parse request body for ID stripping", { error: e })
  }
}

Activity

  1. github-actions commented on Feb 2, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of or closely related to existing error handling issues. Please check:

    These are part of a broader pattern of insufficient error handling in critical code paths that deserves systematic review and fixes.

    Feel free to ignore if this specific case addresses your particular scenario.

  2. github-actions commented on Apr 3, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions