🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🔒 fix(mcp): stop leaking server process.env to stdio pre-check subprocess (#17323)
Arvin Xuf56bf98c25e2
diff --git a/src/libs/mcp/__tests__/index.test.ts b/src/libs/mcp/__tests__/index.test.tsindex efda6d67fca..ba3672ebc99 100644--- a/src/libs/mcp/__tests__/index.test.ts+++ b/src/libs/mcp/__tests__/index.test.ts@@ -90,6 +90,90 @@ describe('MCPClient', () => {);});+ // Regression for https://github.com/lobehub/lobehub/issues/17307:+ // neither the main stdio transport nor the failure-path pre-check may spread+ // the full server process.env into the spawned subprocess, otherwise+ // server-side secrets leak to the MCP process.+ describe('Stdio env isolation (#17307)', () => {+ const TIMEOUT = 120_000;++ it('does not pass server process.env secrets to the main stdio transport', () => {+ const SECRET_KEY = 'LOBE_TEST_SECRET_LEAK';+ const SECRET_VALUE = 'super-secret-should-not-leak-1234';+ const ALLOWED_KEY = 'LOBE_TEST_USER_ENV';+ const ALLOWED_VALUE = 'user-configured-value';++ process.env[SECRET_KEY] = SECRET_VALUE;+ try {+ const mcpClient = new MCPClient({+ id: 'env-leak-transport-test',+ name: 'Env Leak Transport Test',+ type: 'stdio',+ command: process.execPath,+ args: ['-e', ''],+ env: { [ALLOWED_KEY]: ALLOWED_VALUE },+ } as any);++ // The SDK stores the env we hand it verbatim on the transport's server+ // params (default inherited vars are only merged later, at spawn time).+ const transportEnv: Record<string, string> = (mcpClient as any).transport?._serverParams+ ?.env;++ expect(transportEnv).toBeDefined();+ // server secret from process.env must NOT be handed to the transport+ expect(transportEnv[SECRET_KEY]).toBeUndefined();+ // user-configured env vars are still forwarded+ expect(transportEnv[ALLOWED_KEY]).toBe(ALLOWED_VALUE);+ } finally {+ delete process.env[SECRET_KEY];+ }+ });++ it(+ 'does not leak server process.env secrets to the pre-check subprocess',+ async () => {+ const SECRET_KEY = 'LOBE_TEST_SECRET_LEAK';+ const SECRET_VALUE = 'super-secret-should-not-leak-1234';+ const ALLOWED_KEY = 'LOBE_TEST_USER_ENV';+ const ALLOWED_VALUE = 'user-configured-value';++ process.env[SECRET_KEY] = SECRET_VALUE;+ try {+ // Print ONLY the two probed keys to stderr (never the whole env), then+ // exit non-zero so the main transport connect fails and the pre-check+ // path we are guarding runs. Keeping the dump narrow avoids writing+ // unrelated CI/server secrets into errorLog if this test ever fails.+ const childScript = `console.error('${SECRET_KEY}=' + (process.env.${SECRET_KEY} ?? '') + '\\n${ALLOWED_KEY}=' + (process.env.${ALLOWED_KEY} ?? '')); process.exit(1);`;+ const mcpClient = new MCPClient({+ id: 'env-leak-test',+ name: 'Env Leak Test',+ type: 'stdio',+ command: process.execPath,+ args: ['-e', childScript],+ env: { [ALLOWED_KEY]: ALLOWED_VALUE },+ } as any);++ let thrown: any;+ try {+ await mcpClient.initialize();+ } catch (error) {+ thrown = error;+ }++ expect(thrown).toBeDefined();+ const errorLog: string = thrown?.data?.metadata?.errorLog ?? '';+ // the child env dumped to stderr must not contain the server secret+ expect(errorLog).not.toContain(SECRET_VALUE);+ // sanity: user-configured env vars are still forwarded to the subprocess+ expect(errorLog).toContain(ALLOWED_VALUE);+ } finally {+ delete process.env[SECRET_KEY];+ }+ },+ TIMEOUT,+ );+ });+// Error Handling tests remain the same...describe('Error Handling', () => {it('should throw error for unsupported connection type', () => {diff --git a/src/libs/mcp/client.ts b/src/libs/mcp/client.tsindex feabe411fa7..dbe60b7ae4b 100644--- a/src/libs/mcp/client.ts+++ b/src/libs/mcp/client.ts@@ -42,12 +42,19 @@ async function preCheckStdioCommand(params: {return new Promise((resolve) => {log('Pre-checking stdio command: %s with args: %O', params.command, params.args);+ // Do NOT spread the full process.env here: it would leak server-side secrets+ // (DATABASE_URL, KEY_VAULTS_SECRET, provider API keys, OAuth/JWT secrets, ...)+ // into the spawned MCP subprocess. Match the main StdioClientTransport below,+ // which only exposes the safe default environment plus user-configured vars.+ // The cast is required because this repo augments NodeJS.ProcessEnv with+ // required app keys that a restricted env intentionally omits.+ const env = {+ ...getDefaultEnvironment(),+ ...params.env,+ } as NodeJS.ProcessEnv;+const child = spawn(params.command, params.args, {- env: {- ...process.env,- ...getDefaultEnvironment(),- ...params.env,- },+ env,stdio: ['pipe', 'pipe', 'pipe'],});