Skip to content

Conversation

@RinZ27
Copy link
Contributor

@RinZ27 RinZ27 commented Jan 17, 2026

I was looking through the CLI code and spotted a potential security risk when using the mcp dev command on Windows. Because shell=True is required for npx, passing raw arguments can lead to command injection if a user provides a file path containing shell metacharacters.

I decided to use shlex.quote to sanitize these arguments before they are joined into the final command string. This way, I ensure that any special characters are safely escaped, keeping the execution restricted to the intended command. I've verified the fix and it correctly handles paths with spaces and other characters.

@RinZ27 RinZ27 force-pushed the fix/windows-command-injection-v2 branch 2 times, most recently from f7b9f72 to a63bde1 Compare January 20, 2026 14:10
Comment on lines 279 to 284
cmd_args = [npx_cmd, "@modelcontextprotocol/inspector"] + uv_cmd

if shell:
# On Windows with shell=True, I need to quote arguments to prevent injection
# and join them into a single string, as passing a list with shell=True is unsafe/undefined behavior
cmd_args = " ".join(shlex.quote(arg) for arg in cmd_args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we apply shlex on the whole thing? we don't need to check if shell, do we?

@RinZ27 RinZ27 force-pushed the fix/windows-command-injection-v2 branch from a63bde1 to 4f4dba9 Compare January 23, 2026 11:58
@RinZ27
Copy link
Contributor Author

RinZ27 commented Jan 23, 2026

@Kludex I can't apply it universally because on POSIX I'm using shell=False (passing a raw list), where subprocess handles the arguments directly. If I quoted them there, the quotes would be treated as literal parts of the filename. Manual quoting is only necessary when I'm forced to use shell=True on Windows.

Also, I realized shlex.quote uses single quotes which isn't ideal for cmd.exe. I'll update this to use list2cmdline instead to ensure it works correctly on Windows.

I noticed that shlex.quote uses single quotes which cmd.exe doesn't handle correctly. Switched to list2cmdline to ensure proper escaping when shell=True is used on Windows. Also removed the unused shlex import.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants