fix(ai,mcp): use prototype-safe writes for dynamic-key lookup maps - #20005
Open
trmxvibs wants to merge 4 commits into
Open
fix(ai,mcp): use prototype-safe writes for dynamic-key lookup maps#20005trmxvibs wants to merge 4 commits into
trmxvibs wants to merge 4 commits into
Conversation
Five call sites built lookup objects keyed by external/dynamic strings (MCP tool names, provider names, provider ids) using plain bracket assignment on a {}-literal, which lets a key of "__proto__" mutate the object's prototype instead of being stored as an own property.
- packages/mcp/src/tool/mcp-client.ts and packages/ai/src/registry/provider-registry.ts now initialize their maps with Object.create(null), matching the codebase's existing createIdMap convention.
- embed-many.ts, generate-image.ts, and generate-video.ts accumulate providerMetadata that is returned to callers, and existing tests assert strict shape equality against plain objects, so changing the accumulator's own prototype would be an observable behavior change. Added a new setOwn() helper (symmetric with the existing getOwn()) that uses Object.defineProperty to always write an own property regardless of key, without touching the target's prototype.
Fixes vercel#18187 (finding 1: prototype-pollution-adjacent merge pattern)
| // plain bracket assignment here would let a key named `__proto__` | ||
| // mutate Object.prototype instead of being stored as an own | ||
| // property. | ||
| if (providerMetadata[providerName] == null) { |
Contributor
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.
Fixes #18187 (finding 1: prototype-pollution-adjacent merge pattern)
Background
While auditing packages/ai and packages/mcp, five call sites were found building lookup objects keyed by external/dynamic strings using plain bracket assignment on a {}-literal (
obj[dynamicKey] = value), instead of the codebase's own established prototype-safe helpers (getOwn,createIdMap→Object.create(null)). A key of__proto__at any of these sites would mutate the object's prototype instead of being stored as an own property.Summary
packages/mcp/src/tool/mcp-client.tsandpackages/ai/src/registry/provider-registry.ts: initialize the lookup map withObject.create(null), matching the existingcreateIdMapconvention used elsewhere in the codebase.packages/ai/src/embed/embed-many.ts,packages/ai/src/generate-image/generate-image.ts,packages/ai/src/generate-video/generate-video.ts: these accumulateproviderMetadatathat is returned to callers, and existing tests assert strict shape equality (toStrictEqual) against plain objects — changing the accumulator's own prototype broke those tests. Added a newsetOwn()helper (packages/ai/src/util/set-own.ts, symmetric with the existinggetOwn()) that usesObject.definePropertyto always write an own property regardless of key, without touching the target's prototype.Contributor Credit
Reported and fixed by @trmxvibs (issue #18187, finding 1).
End-to-End Verification
packages/ai/src/util/set-own.test.tscovering__proto__,constructor,toString,valueOf,hasOwnPropertykeys.tsc --noEmitclean on bothpackages/aiandpackages/mcp.Object.getPrototypeOf(obj)no longer resolves to an attacker-controlled object after assigning a__proto__key through the fixed code paths.Checklist
Future Work
Findings 2–4 from #18187 (canonicalJSON collision, missing chatId encoding, onError bypass for providerExecuted tool errors) are tracked separately and already have PRs open (#18394, #18395, #20001).
Related Issues
Fixes #18187