Skip to content

Commit e79d14a

Browse files
authored
fix: prevent command injection in example URL opening (v1.x backport) (#1579)
1 parent 342ea39 commit e79d14a

2 files changed

Lines changed: 4 additions & 36 deletions

File tree

src/examples/client/elicitationUrlExample.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
} from '../../types.js';
2626
import { getDisplayName } from '../../shared/metadataUtils.js';
2727
import { OAuthClientMetadata } from '../../shared/auth.js';
28-
import { exec } from 'node:child_process';
2928
import { InMemoryOAuthClientProvider } from './simpleOAuthClientProvider.js';
3029
import { UnauthorizedError } from '../../client/auth.js';
3130
import { createServer } from 'node:http';
@@ -45,8 +44,7 @@ const clientMetadata: OAuthClientMetadata = {
4544
scope: 'mcp:tools'
4645
};
4746
oauthProvider = new InMemoryOAuthClientProvider(OAUTH_CALLBACK_URL, clientMetadata, (redirectUrl: URL) => {
48-
console.log(`🌐 Opening browser for OAuth redirect: ${redirectUrl.toString()}`);
49-
openBrowser(redirectUrl.toString());
47+
console.log(`\n🔗 Please open this URL in your browser to authorize:\n ${redirectUrl.toString()}`);
5048
});
5149

5250
// Create readline interface for user input
@@ -259,17 +257,6 @@ async function elicitationLoop(): Promise<void> {
259257
}
260258
}
261259

262-
async function openBrowser(url: string): Promise<void> {
263-
const command = `open "${url}"`;
264-
265-
exec(command, error => {
266-
if (error) {
267-
console.error(`Failed to open browser: ${error.message}`);
268-
console.log(`Please manually open: ${url}`);
269-
}
270-
});
271-
}
272-
273260
/**
274261
* Enqueues an elicitation request and returns the result.
275262
*
@@ -402,9 +389,8 @@ async function handleURLElicitation(params: ElicitRequestURLParams): Promise<Eli
402389
console.error('Background completion wait failed:', error);
403390
});
404391

405-
// 4. Open the URL in the browser
406-
console.log(`\n🚀 Opening browser to: ${url}`);
407-
await openBrowser(url);
392+
// 4. Direct user to open the URL in their browser
393+
console.log(`\n🔗 Please open this URL in your browser:\n ${url}`);
408394

409395
console.log('\n⏳ Waiting for you to complete the interaction in your browser...');
410396
console.log(' The server will send a notification once you complete the action.');

src/examples/client/simpleOAuthClient.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { createServer } from 'node:http';
44
import { createInterface } from 'node:readline';
55
import { URL } from 'node:url';
6-
import { exec } from 'node:child_process';
76
import { Client } from '../../client/index.js';
87
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
98
import { OAuthClientMetadata } from '../../shared/auth.js';
@@ -41,21 +40,6 @@ class InteractiveOAuthClient {
4140
});
4241
}
4342

44-
/**
45-
* Opens the authorization URL in the user's default browser
46-
*/
47-
private async openBrowser(url: string): Promise<void> {
48-
console.log(`🌐 Opening browser for authorization: ${url}`);
49-
50-
const command = `open "${url}"`;
51-
52-
exec(command, error => {
53-
if (error) {
54-
console.error(`Failed to open browser: ${error.message}`);
55-
console.log(`Please manually open: ${url}`);
56-
}
57-
});
58-
}
5943
/**
6044
* Example OAuth callback handler - in production, use a more robust approach
6145
* for handling callbacks and storing tokens
@@ -166,9 +150,7 @@ class InteractiveOAuthClient {
166150
CALLBACK_URL,
167151
clientMetadata,
168152
(redirectUrl: URL) => {
169-
console.log(`📌 OAuth redirect handler called - opening browser`);
170-
console.log(`Opening browser to: ${redirectUrl.toString()}`);
171-
this.openBrowser(redirectUrl.toString());
153+
console.log(`\n🔗 Please open this URL in your browser to authorize:\n ${redirectUrl.toString()}`);
172154
},
173155
this.clientMetadataUrl
174156
);

0 commit comments

Comments
 (0)