Skip to content

🔏 fix: MCP Server URL Schema Validation#12204

Merged
danny-avila merged 3 commits into
devfrom
fix/mcp-ui-url
Mar 13, 2026
Merged

🔏 fix: MCP Server URL Schema Validation#12204
danny-avila merged 3 commits into
devfrom
fix/mcp-ui-url

Conversation

@danny-avila

@danny-avila danny-avila commented Mar 13, 2026

Copy link
Copy Markdown
Owner

No description provided.

- Added tests to reject URLs containing environment variable references for SSE, streamable-http, and websocket types in the MCP routes.
- Introduced a new schema in the data provider to ensure user input URLs do not resolve environment variables, enhancing security against potential leaks.
- Updated existing MCP server user input schema to utilize the new validation logic, ensuring consistent handling of user-supplied URLs across the application.
Copilot AI review requested due to automatic review settings March 13, 2026 00:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR tightens MCP server URL handling for user-provided configurations by preventing environment-variable template patterns from being accepted (mitigating potential secret exfiltration), and adds regression tests in both the data-provider and API route layers.

Changes:

  • Add a userUrlSchema in librechat-data-provider that rejects URLs containing ${...} patterns and enforces transport protocol constraints without env-var resolution.
  • Update MCPServerUserInputSchema to use userUrlSchema for WebSocket/SSE/Streamable HTTP transports.
  • Add Jest tests to ensure MCP routes reject configs whose URLs contain ${...} references.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
packages/data-provider/src/mcp.ts Introduces env-var pattern rejection for user URL fields and applies it to the MCP user input schema.
packages/data-provider/specs/mcp.spec.ts Adds unit tests around MCPServerUserInputSchema’s handling of ${...} in URLs.
api/server/routes/tests/mcp.spec.js Adds route-level tests asserting POST/PATCH reject configs with ${...} in MCP URLs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +237 to +244
const userUrlSchema = (protocolCheck: (val: string) => boolean, message: string) =>
z
.string()
.refine((val) => !envVarPattern.test(val), {
message: 'Environment variable references are not allowed in URLs',
})
.pipe(z.string().url())
.refine(protocolCheck, { message });
Comment on lines +18 to +55
it('should NOT resolve env variables in user-supplied SSE URLs', () => {
const result = MCPServerUserInputSchema.safeParse({
type: 'sse',
url: 'http://attacker.com/?secret=${FAKE_SECRET}',
});

if (result.success) {
expect(result.data.url).not.toContain('leaked-secret-value');
} else {
expect(result.success).toBe(false);
}
});

it('should NOT resolve env variables in user-supplied HTTP URLs', () => {
const result = MCPServerUserInputSchema.safeParse({
type: 'streamable-http',
url: 'http://attacker.com/?jwt=${JWT_SECRET}',
});

if (result.success) {
expect(result.data.url).not.toContain('super-secret-jwt');
} else {
expect(result.success).toBe(false);
}
});

it('should NOT resolve env variables in user-supplied WebSocket URLs', () => {
const result = MCPServerUserInputSchema.safeParse({
type: 'websocket',
url: 'ws://attacker.com/?secret=${FAKE_SECRET}',
});

if (result.success) {
expect(result.data.url).not.toContain('leaked-secret-value');
} else {
expect(result.success).toBe(false);
}
});
- Updated tests to ensure that URLs for SSE, streamable-http, and websocket types containing environment variable patterns are rejected, improving security against potential leaks.
- Refactored the MCP server user input schema to enforce stricter validation rules, preventing the resolution of environment variables in user-supplied URLs.
- Introduced new test cases for various URL types to validate the rejection logic, ensuring consistent handling across the application.
… handling

- Introduced new test cases to validate the prevention of environment variable exfiltration through user input URLs in the MCPServerUserInputSchema.
- Updated existing tests to confirm that URLs containing environment variable patterns are correctly resolved or rejected, improving security against potential leaks.
- Refactored test structure to better organize environment variable handling scenarios, ensuring comprehensive coverage of edge cases.
@danny-avila danny-avila changed the title fix: MCP server configuration validation and schema 🔏 fix: MCP Server URL Schema Validation Mar 13, 2026
@danny-avila danny-avila merged commit f32907c into dev Mar 13, 2026
13 checks passed
@danny-avila danny-avila deleted the fix/mcp-ui-url branch March 13, 2026 03:19
jcbartle pushed a commit to jcbartle/LibreChat that referenced this pull request May 11, 2026
* fix: MCP server configuration validation and schema

- Added tests to reject URLs containing environment variable references for SSE, streamable-http, and websocket types in the MCP routes.
- Introduced a new schema in the data provider to ensure user input URLs do not resolve environment variables, enhancing security against potential leaks.
- Updated existing MCP server user input schema to utilize the new validation logic, ensuring consistent handling of user-supplied URLs across the application.

* fix: MCP URL validation to reject env variable references

- Updated tests to ensure that URLs for SSE, streamable-http, and websocket types containing environment variable patterns are rejected, improving security against potential leaks.
- Refactored the MCP server user input schema to enforce stricter validation rules, preventing the resolution of environment variables in user-supplied URLs.
- Introduced new test cases for various URL types to validate the rejection logic, ensuring consistent handling across the application.

* test: Enhance MCPServerUserInputSchema tests for environment variable handling

- Introduced new test cases to validate the prevention of environment variable exfiltration through user input URLs in the MCPServerUserInputSchema.
- Updated existing tests to confirm that URLs containing environment variable patterns are correctly resolved or rejected, improving security against potential leaks.
- Refactored test structure to better organize environment variable handling scenarios, ensuring comprehensive coverage of edge cases.
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.

2 participants