Security hardening: remove eval() from MCP plugin#537
Open
Security hardening: remove eval() from MCP plugin#537
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes dynamic code evaluation from the TypeScript MCP plugin by replacing the json-schema-to-zod dependency (string-emitting) with a local converter that builds Zod schemas directly, mitigating an RCE vector via eval().
Changes:
- Replaced
eval(jsonSchemaToZod(...))usage inMcpPlugin.use()with a localjsonSchemaToZod()implementation that returns live Zod types. - Removed
json-schema-to-zodfromexternal/mcpdependencies and updated the root lockfile accordingly. - Added unit tests for the new JSON-Schema-to-Zod converter.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| package-lock.json | Updates lockfile to drop json-schema-to-zod and reflect workspace dependency changes. |
| external/mcp/package.json | Removes the json-schema-to-zod dependency from the MCP package. |
| external/mcp/src/plugin.ts | Stops using eval() and switches MCP tool registration to the local converter output. |
| external/mcp/src/json-schema-to-zod.ts | New local Schema→Zod converter implementation (no codegen/eval). |
| external/mcp/src/json-schema-to-zod.spec.ts | Adds Jest coverage for converter behavior and edge cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
008ddbf to
e56ba3c
Compare
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.
Summary
Removes
eval()fromMcpPlugin.use(prompt)by replacing the externaljson-schema-to-zodlibrary (which emits JavaScript source that must be evaluated) with a local converter that builds Zod schemas directly.Addresses security finding #13 (RCE via
eval()in MCP plugin). TypeScript-only; Python and C# MCP plugins are not affected.Approach
The MCP TypeScript SDK requires Zod schemas for
server.tool()registration — we can't skip conversion the way the PY/C# SDKs do. Rather than hardening theevalcall (e.g. scoping withnew Function), we eliminate code generation entirely:external/mcp/src/json-schema-to-zod.tswalks aSchemaobject and returns a livez.ZodTypeAny. No string emission, noeval, nonew Function.json-schema-to-zodremoved fromexternal/mcp/package.jsondependencies.McpPlugin.use(prompt)now calls the local converter and defensively checksinstanceof z.ZodObjectbefore accessing.shape.Coverage scope is bounded by the
Schematype in@microsoft/teams.ai(a closed, typed subset of JSON Schema). Constructs not expressible in that type —anyOf,oneOf,allOf,const,patternProperties,not— are not supported and not needed.\$refis in the type but unused in practice; the converter throws a clear error if it appears.Context and alternatives considered:
design/mcp-eval-removal-options.md.Behavior change to be aware of
The prior
json-schema-to-zodlibrary silently ignoredmin,max, andmultipleOfonNumberSchema(those are non-standard JSON Schema keywords; the spec usesminimum/maximum). The new converter honors them, matching theSchematype's stated contract.Customers whose schemas already used these constraints will see:
InvalidParamserror (e.g. "Number must be less than or equal to 85") instead of silently passing invalid data to the handler. Modern MCP clients surface this error back to the LLM, which typically retries with a corrected value.Example — a thermostat tool with
temperature: { type: 'number', min: 50, max: 85 }:95, handler receives95, thermostat may or may not clamp.95, MCP returns anInvalidParamserror, LLM typically retries with85, handler runs with a valid value.For the narrow case where a schema has typo'd bounds (e.g.
min: 10, max: 5), every call will now fail — previously silently ignored.