fix(providers): use secureJsonParse instead of raw JSON.parse in production code#15813
Open
Abuhaithem wants to merge 1 commit into
Open
fix(providers): use secureJsonParse instead of raw JSON.parse in production code#15813Abuhaithem wants to merge 1 commit into
Abuhaithem wants to merge 1 commit into
Conversation
Author
|
cc: @lgrammel |
Author
|
cc: @dancer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several production provider files parse untrusted strings with raw
JSON.parse. These strings come from model output and upstream API responses — tool-call inputs, provider tool error results, and API error response bodies — all of which can be influenced by prompt injection or a misbehaving/malicious upstream.This violates the project coding standard ("Never use
JSON.parsedirectly in production code to prevent security risks") and leaves a prototype-pollution vector open: a payload such as{"__proto__":{"polluted":true}}parses without complaint and can pollute objects during later recursive merges.The repo already ships
secureJsonParse(packages/provider-utils/src/secure-json-parse.ts), which rejects__proto__/constructor.prototypekeys. However it was not exported from@ai-sdk/provider-utils, and the publicparseJSON/safeParseJSONhelpers are async, so these synchronous call sites had no safe option.Summary
@ai-sdk/provider-utils: exportsecureJsonParsefrom the package entry point.Replace raw
JSON.parsewithsecureJsonParseat six call sites (all already wrapped intry/catch, so error handling is unchanged — a rejected payload now falls back instead of being parsed):@ai-sdk/googlesrc/interactions/convert-to-google-interactions-input.ts@ai-sdk/googlesrc/convert-to-google-messages.ts@ai-sdk/anthropicsrc/convert-to-anthropic-prompt.ts(×2)@ai-sdk/anthropicsrc/anthropic-language-model.tscode_executiontool input@ai-sdk/gatewaysrc/errors/extract-api-call-response.tsAdd regression tests that fail on the previous
JSON.parseimplementation:@ai-sdk/gateway:__proto__andconstructor.prototypepayloads fall back to the raw response string.@ai-sdk/google: a malicious stringified tool-call input is rejected and wrapped safely instead of parsed.Manual Verification
Targeted test suites (all pass):
Additional checks:
npx ultracite check <changed files>— clean (formatting + lint).npx tsc --build packages/provider-utils packages/google packages/anthropic packages/gateway— exit 0.Checklist
pnpm changesetin the project root)Future Work
JSON.parseusages in other production packages (e.g.@ai-sdk/langchain) and migrate any that handle untrusted input tosecureJsonParse.Related Issues
Fixes #15812