Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
fix(sdk): prevent shell injection in MCP config via proper escaping (#1276)
Kagura2f0ff5f0f7e5
diff --git a/.changeset/fix-mcp-config-shell-injection.md b/.changeset/fix-mcp-config-shell-injection.mdnew file mode 100644index 000000000..bb8c15174--- /dev/null+++ b/.changeset/fix-mcp-config-shell-injection.md@@ -0,0 +1,6 @@+---+"e2b": patch+"@e2b/python-sdk": patch+---++fix(sdk): prevent shell injection in MCP config by using proper shell escaping (shlex.quote in Python, shellQuote helper in JS/TS)diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.tsindex cfa51504f..c223c6b6f 100644--- a/packages/js-sdk/src/sandbox/index.ts+++ b/packages/js-sdk/src/sandbox/index.ts@@ -29,6 +29,7 @@ import { getSignature } from './signature'import { compareVersions } from 'compare-versions'import { SandboxError } from '../errors'import { ENVD_DEBUG_FALLBACK, ENVD_DEFAULT_USER } from '../envd/versions'+import { shellQuote } from '../utils'/*** Options for sandbox upload/download URL generation.@@ -301,7 +302,7 @@ export class Sandbox extends SandboxApi {if (sandboxOpts?.mcp) {sandbox.mcpToken = crypto.randomUUID()const res = await sandbox.commands.run(- `mcp-gateway --config '${JSON.stringify(sandboxOpts?.mcp)}'`,+ `mcp-gateway --config ${shellQuote(JSON.stringify(sandboxOpts.mcp))}`,{user: 'root',envs: {@@ -398,7 +399,7 @@ export class Sandbox extends SandboxApi {if (sandboxOpts?.mcp) {sandbox.mcpToken = crypto.randomUUID()const res = await sandbox.commands.run(- `mcp-gateway --config '${JSON.stringify(sandboxOpts?.mcp)}'`,+ `mcp-gateway --config ${shellQuote(JSON.stringify(sandboxOpts.mcp))}`,{user: 'root',envs: {diff --git a/packages/js-sdk/src/utils.ts b/packages/js-sdk/src/utils.tsindex 052574b50..d571e9104 100644--- a/packages/js-sdk/src/utils.ts+++ b/packages/js-sdk/src/utils.ts@@ -125,6 +125,14 @@ export function toBlob(return new Response(data).blob()}+/**+ * Escape a string for safe inclusion in a single-quoted shell argument.+ * Equivalent to Python's shlex.quote().+ */+export function shellQuote(s: string): string {+ return "'" + s.replace(/'/g, "'\\''") + "'"+}+/*** Prepare data for upload as a BodyInit, optionally gzip-compressed.* When gzip is enabled, compresses the data and returns a Blob.diff --git a/packages/python-sdk/e2b/sandbox_async/main.py b/packages/python-sdk/e2b/sandbox_async/main.pyindex 0fe175de3..3dd7044ea 100644--- a/packages/python-sdk/e2b/sandbox_async/main.py+++ b/packages/python-sdk/e2b/sandbox_async/main.py@@ -1,6 +1,7 @@import datetimeimport jsonimport logging+import shleximport uuidfrom typing import Dict, List, Optional, Union, overload@@ -235,7 +236,7 @@ class AsyncSandbox(SandboxApi):sandbox._mcp_token = tokenres = await sandbox.commands.run(- f"mcp-gateway --config '{json.dumps(mcp)}'",+ f"mcp-gateway --config {shlex.quote(json.dumps(mcp))}",user="root",envs={"GATEWAY_ACCESS_TOKEN": token},)@@ -616,7 +617,7 @@ class AsyncSandbox(SandboxApi):sandbox._mcp_token = tokenres = await sandbox.commands.run(- f"mcp-gateway --config '{json.dumps(mcp)}'",+ f"mcp-gateway --config {shlex.quote(json.dumps(mcp))}",user="root",envs={"GATEWAY_ACCESS_TOKEN": token},)diff --git a/packages/python-sdk/e2b/sandbox_sync/main.py b/packages/python-sdk/e2b/sandbox_sync/main.pyindex a52cb4c30..43f3a858c 100644--- a/packages/python-sdk/e2b/sandbox_sync/main.py+++ b/packages/python-sdk/e2b/sandbox_sync/main.py@@ -1,6 +1,7 @@import datetimeimport jsonimport logging+import shleximport uuidfrom typing import Dict, List, Optional, Union, overload@@ -233,7 +234,7 @@ class Sandbox(SandboxApi):sandbox._mcp_token = tokenres = sandbox.commands.run(- f"mcp-gateway --config '{json.dumps(mcp)}'",+ f"mcp-gateway --config {shlex.quote(json.dumps(mcp))}",user="root",envs={"GATEWAY_ACCESS_TOKEN": token},)@@ -617,7 +618,7 @@ class Sandbox(SandboxApi):sandbox._mcp_token = tokenres = sandbox.commands.run(- f"mcp-gateway --config '{json.dumps(mcp)}'",+ f"mcp-gateway --config {shlex.quote(json.dumps(mcp))}",user="root",envs={"GATEWAY_ACCESS_TOKEN": token},)