Detect command injection vulnerability in MCP file reader and learn secure coding practices
The Vulnerable File Reader MCP Server is an implementation intended to read files through a secure interface, but unfortunately, it contains a critical vulnerability in its design. This server was developed for educational purposes to highlight the dangers of improper input handling and shell command usage within Python scripts. It serves as a demonstration of how seemingly harmless software can lead to severe security breaches if not designed with proper input validation and sanitization techniques.
The core feature of this server is its ease of integration into various AI-driven applications through the Model Context Protocol (MCP). MCP servers facilitate seamless communication between AI desktop clients like Claude Desktop, Continue, and Cursor. By leveraging the Vulnerable File Reader MCP Server, these tools can communicate with backend systems to perform file operations securely.
The key capabilities of this server include:
The architecture of the Vulnerable File Reader MCP Server revolves around the MCP protocol, which uses a server-client model for bidirectional communication between AI desktop clients and backend systems. The protocol involves:
graph TD
A[AI Application] -->|MCP Client| B[MCP Protocol]
B --> C[MCP Server]
C --> D[Data Source/Tool]
style A fill:#e1f5fe
style C fill:#f3e5f5
style D fill:#e8f5e8
MCP Client | Resources | Tools | Prompts | Status |
---|---|---|---|---|
Claude Desktop | ✅ | ✅ | ✅ | Full Support |
Continue | ✅ | ✅ | ✅ | Full Support |
Cursor | ❌ | ✅ | ❌ | Tools Only |
To set up and use the Vulnerable File Reader MCP Server, follow these steps:
Prerequisites: Ensure you have Python 3.12 or higher installed.
Clone Repository: Clone the repository from GitHub.
git clone https://github.com/Eliran79/Vulnerable-file-reader-server.git
cd Vulnerable-file-reader-server
Install MCP Server:
mcp install main.py
Configure MCP Client: Edit the ~/.config/claude-desktop/claude_desktop_config.json
file to include your server configuration.
{
"mcpServers": {
"file-reader": {
"command": "/ABSOLUTE/PATH/TO/uv",
"args": [
"--directory",
"/data/git/file_reader_server",
"/usr/bin/uv",
"run,--with,mcp,mcp,run,main.py"
]
}
}
}
Start MCP Server:
mcp dev main.py
This server is particularly useful for demonstrating and mitigating risks associated with improper input handling in MCP implementations. By exploring the vulnerabilities, developers can learn about secure coding practices.
In an AI workflow, this server could be used to retrieve configuration files required by an AI model before deployment:
Using this server's command injection vulnerability as a teaching tool:
shell=True
enabled.The Vulnerable File Reader MCP Server ensures compatibility with multiple client tools, making it versatile for various AI applications:
By incorporating this server into the ecosystem, developers can test and enhance their understanding of secure MCP implementation best practices.
While the performance metrics are not explicitly provided in the README, it’s important to note that:
The compatibility matrix is already illustrated above, detailing support for various MCP clients.
To enhance security in the Vulnerable File Reader MCP Server:
shell=True
by utilizing safe methods like shell-escaping.Here's an example of a secure implementation:
import os
safe_dir = "/data/git/file_reader_server" # Define the safe directory
file_name = file_name.strip() # Sanitize input
# Path validation
safe_path = os.path.abspath(safe_dir)
path_resolved = os.path.abspath(file_name)
if not path_resolved.startswith(safe_path):
raise ValueError("Path traversal attempt detected")
result = subprocess.check_output(["cat", file_name], shell=False) # Secure command execution
Can I use this for production environments?
Is there any official documentation or support for the MCP protocol?
How can I ensure secure communication between clients and servers using MCP?
Which AI tools are compatible with the current version of this server?
Where can I find more resources on Model Context Protocol (MCP) integration and security best practices?
Developers are encouraged to modify and enhance this project to create a secure and reliable MCP server. Contributions include:
For more information about the broader MCP ecosystem, refer to these resources:
By participating actively, developers can drive progress within the MCP community.
In summary, the Vulnerable File Reader MCP Server serves as an essential tool for educating developers about the importance of secure coding practices. By understanding its vulnerabilities, you can ensure your own applications are more robust and secure against similar exploits.
Next-generation MCP server enhances documentation analysis with AI-powered neural processing and multi-language support
Learn to connect to MCP servers over HTTP with Python SDK using SSE for efficient protocol communication
Python MCP client for testing servers avoid message limits and customize with API key
Discover easy deployment and management of MCP servers with Glutamate platform for Windows Linux Mac
Explore community contributions to MCP including clients, servers, and projects for seamless integration
Learn how to use MCProto Ruby gem to create and chain MCP servers for custom solutions