Skip to content

Commit 48c26bb

Browse files
authored
fix: apply secure coding convention with execFile to prevent command injection in MCP server code (#1)
1 parent 32e03b8 commit 48c26bb

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
22
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
33
import { z } from "zod";
4-
import { exec } from "child_process";
4+
import { execFile } from "child_process";
55
// Create an MCP server
66
const server = new McpServer({
77
name: "Demo",
@@ -23,13 +23,13 @@ interface ProcessInfo {
2323

2424
server.tool("which-app-on-port", { port: z.number() }, async ({ port }) => {
2525
const result = await new Promise<ProcessInfo>((resolve, reject) => {
26-
exec(`lsof -t -i tcp:${port}`, (error, pidStdout) => {
26+
execFile('lsof', ['-t', '-i', `tcp:${port}`], (error, pidStdout) => {
2727
if (error) {
2828
reject(error);
2929
return;
3030
}
3131
const pid = pidStdout.trim();
32-
exec(`ps -p ${pid} -o comm=`, (error, stdout) => {
32+
execFile('ps', ['-o', 'comm=', '-p', pid], (error, stdout) => {
3333
if (error) {
3434
reject(error);
3535
return;

0 commit comments

Comments
 (0)