-
Notifications
You must be signed in to change notification settings - Fork 132
Allow for insecure https connections to upstream MCP servers w/ --insecure flag #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jessesanford
wants to merge
1
commit into
geelen:main
Choose a base branch
from
jessesanford:allow-insecure-tls-with-override
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
.mcp-cli | ||
dist | ||
.pnpm-store |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"packages": { | ||
".": { | ||
"name": "mcp-remote", | ||
"template": { | ||
"name": "mcp-remote-example", | ||
"label": "Try mcp-remote", | ||
"description": "Test the mcp-remote package with this example" | ||
} | ||
} | ||
}, | ||
"comment": { | ||
"header": "📦 **Package Preview Available**", | ||
"footer": "Install this preview: `npm install https://pkg.pr.new/mcp-remote@{{pr}}`" | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,213 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' | ||
import { parseCommandLineArgs, connectToRemoteServer } from './utils' | ||
import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js' | ||
|
||
// All sanitizeUrl tests have been moved to the strict-url-sanitise package | ||
|
||
describe('parseCommandLineArgs', () => { | ||
it('should parse --insecure flag correctly', async () => { | ||
const args = ['https://example.com', '--insecure'] | ||
const result = await parseCommandLineArgs(args, 'Test usage') | ||
|
||
expect(result.insecure).toBe(true) | ||
expect(result.serverUrl).toBe('https://example.com') | ||
}) | ||
|
||
it('should default insecure to false when not provided', async () => { | ||
const args = ['https://example.com'] | ||
const result = await parseCommandLineArgs(args, 'Test usage') | ||
|
||
expect(result.insecure).toBe(false) | ||
expect(result.serverUrl).toBe('https://example.com') | ||
}) | ||
|
||
it('should work with multiple flags including --insecure', async () => { | ||
const args = ['https://example.com', '--debug', '--insecure', '--allow-http'] | ||
const result = await parseCommandLineArgs(args, 'Test usage') | ||
|
||
expect(result.insecure).toBe(true) | ||
expect(result.debug).toBe(true) | ||
expect(result.serverUrl).toBe('https://example.com') | ||
}) | ||
}) | ||
|
||
describe('connectToRemoteServer insecure flag', () => { | ||
let originalTlsReject: string | undefined | ||
let mockExit: any | ||
let mockLog: any | ||
|
||
beforeEach(() => { | ||
// Save original environment variable | ||
originalTlsReject = process.env.NODE_TLS_REJECT_UNAUTHORIZED | ||
|
||
// Mock process.exit to prevent actual exits during tests | ||
mockExit = vi.spyOn(process, 'exit').mockImplementation(() => { | ||
throw new Error('process.exit called') | ||
}) | ||
|
||
// Mock console.error to capture log messages | ||
mockLog = vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
}) | ||
|
||
afterEach(() => { | ||
// Restore original environment variable | ||
if (originalTlsReject !== undefined) { | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = originalTlsReject | ||
} else { | ||
delete process.env.NODE_TLS_REJECT_UNAUTHORIZED | ||
} | ||
|
||
// Restore mocks | ||
mockExit.mockRestore() | ||
mockLog.mockRestore() | ||
}) | ||
|
||
it('should fail when --insecure conflicts with NODE_TLS_REJECT_UNAUTHORIZED=1', async () => { | ||
// Set environment variable to enable cert verification (conflicts with --insecure) | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1' | ||
|
||
const mockAuthProvider = {} as OAuthClientProvider | ||
const mockAuthInitializer = async () => ({ waitForAuthCode: async () => 'test', skipBrowserAuth: false }) | ||
|
||
await expect( | ||
connectToRemoteServer( | ||
null, | ||
'https://example.com', | ||
mockAuthProvider, | ||
{}, | ||
mockAuthInitializer, | ||
'http-first', | ||
new Set(), | ||
true // insecure flag | ||
) | ||
).rejects.toThrow('process.exit called') | ||
|
||
// Check that error message was logged | ||
expect(mockLog).toHaveBeenCalledWith( | ||
expect.stringContaining('Cannot use --insecure flag while NODE_TLS_REJECT_UNAUTHORIZED') | ||
) | ||
}) | ||
|
||
it('should fail when --insecure conflicts with NODE_TLS_REJECT_UNAUTHORIZED=true', async () => { | ||
// Set environment variable to enable cert verification (conflicts with --insecure) | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 'true' | ||
|
||
const mockAuthProvider = {} as OAuthClientProvider | ||
const mockAuthInitializer = async () => ({ waitForAuthCode: async () => 'test', skipBrowserAuth: false }) | ||
|
||
await expect( | ||
connectToRemoteServer( | ||
null, | ||
'https://example.com', | ||
mockAuthProvider, | ||
{}, | ||
mockAuthInitializer, | ||
'http-first', | ||
new Set(), | ||
true // insecure flag | ||
) | ||
).rejects.toThrow('process.exit called') | ||
|
||
// Check that error message was logged | ||
expect(mockLog).toHaveBeenCalledWith( | ||
expect.stringContaining('Cannot use --insecure flag while NODE_TLS_REJECT_UNAUTHORIZED') | ||
) | ||
}) | ||
|
||
it('should proceed when --insecure is compatible with NODE_TLS_REJECT_UNAUTHORIZED=0', async () => { | ||
// Set environment variable to disable cert verification (compatible with --insecure) | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' | ||
|
||
const mockAuthProvider = {} as OAuthClientProvider | ||
const mockAuthInitializer = async () => ({ waitForAuthCode: async () => 'test', skipBrowserAuth: false }) | ||
|
||
// This test will likely fail due to network issues, but we're just testing that | ||
// the conflict detection doesn't trigger process.exit | ||
try { | ||
await connectToRemoteServer( | ||
null, | ||
'https://example.com', | ||
mockAuthProvider, | ||
{}, | ||
mockAuthInitializer, | ||
'http-first', | ||
new Set(), | ||
true // insecure flag | ||
) | ||
} catch (error) { | ||
// We expect this to fail due to network/connection issues, not conflict detection | ||
expect(error).not.toEqual(new Error('process.exit called')) | ||
} | ||
|
||
// Should not have called process.exit | ||
expect(mockExit).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should set and restore NODE_TLS_REJECT_UNAUTHORIZED when unset with --insecure', async () => { | ||
// Ensure environment variable is unset | ||
delete process.env.NODE_TLS_REJECT_UNAUTHORIZED | ||
|
||
const mockAuthProvider = {} as OAuthClientProvider | ||
const mockAuthInitializer = async () => ({ waitForAuthCode: async () => 'test', skipBrowserAuth: false }) | ||
|
||
// This test will likely fail due to network issues, but we're testing env var handling | ||
try { | ||
await connectToRemoteServer( | ||
null, | ||
'https://example.com', | ||
mockAuthProvider, | ||
{}, | ||
mockAuthInitializer, | ||
'http-first', | ||
new Set(), | ||
true // insecure flag | ||
) | ||
} catch (error) { | ||
// We expect this to fail due to network/connection issues | ||
expect(error).not.toEqual(new Error('process.exit called')) | ||
} | ||
|
||
// Environment variable should be restored to unset state | ||
expect(process.env.NODE_TLS_REJECT_UNAUTHORIZED).toBeUndefined() | ||
|
||
// Should not have called process.exit | ||
expect(mockExit).not.toHaveBeenCalled() | ||
|
||
// Should have logged that we're setting the env var | ||
expect(mockLog).toHaveBeenCalledWith( | ||
expect.stringContaining('Setting NODE_TLS_REJECT_UNAUTHORIZED=0 for --insecure connection') | ||
) | ||
}) | ||
|
||
it('should not modify NODE_TLS_REJECT_UNAUTHORIZED when --insecure is false', async () => { | ||
// Set a specific value | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1' | ||
const originalValue = process.env.NODE_TLS_REJECT_UNAUTHORIZED | ||
|
||
const mockAuthProvider = {} as OAuthClientProvider | ||
const mockAuthInitializer = async () => ({ waitForAuthCode: async () => 'test', skipBrowserAuth: false }) | ||
|
||
// This test will likely fail due to network issues | ||
try { | ||
await connectToRemoteServer( | ||
null, | ||
'https://example.com', | ||
mockAuthProvider, | ||
{}, | ||
mockAuthInitializer, | ||
'http-first', | ||
new Set(), | ||
false // insecure flag is false | ||
) | ||
} catch (error) { | ||
// We expect this to fail due to network/connection issues | ||
expect(error).not.toEqual(new Error('process.exit called')) | ||
} | ||
|
||
// Environment variable should remain unchanged | ||
expect(process.env.NODE_TLS_REJECT_UNAUTHORIZED).toBe(originalValue) | ||
|
||
// Should not have called process.exit | ||
expect(mockExit).not.toHaveBeenCalled() | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you show me a screenshot of what this will look like? I'm not sure if it's an upgrade on what we have right now. In any case I usually prefer to keep changes like this separate to the new functionality, unless there's a good reason to include it at the same time.