Summary
matlab-mcp-server contains a path traversal vulnerability in the MCP Interface functions generate_matlab_code / execute_matlab_code, where the scriptPath argument is insufficiently validated.
By manipulating scriptPath, a remote attacker may traverse directories on the server and access or execute files outside the intended workspace, especially when the application builds or runs MATLAB scripts based on this path.
Affected context
This vulnerability occurs in the MCP Interface of matlab-mcp-server, specifically in src/index.ts within the generate_matlab_code and execute_matlab_code functions.
It is relevant when:
- The server accepts a
scriptPathvalue from a remote client or integration, - The value is used to build filesystem paths for reading, writing, or executing MATLAB scripts,
- And there is no robust normalization, canonicalization, or restriction to a trusted base directory.
In such deployments, directory traversal sequences (such as parent-directory references) may allow access to files outside the expected MATLAB project or workspace.
Exploit preconditions
- Attack surface: Network-exposed MCP Interface endpoints that accept
scriptPathas an argument. - Required access: Ability to send requests to the matlab-mcp-server instance (remote client or integrated tool with network connectivity).
- Trigger point: Any call path that passes attacker-controlled
scriptPathintogenerate_matlab_codeorexecute_matlab_code, leading to filesystem operations. - Why exploitation is possible:
- The implementation trusts the
scriptPathvalue and combines it with filesystem locations without enforcing a strict root or performing security-focused path normalization. - As a result, directory traversal tokens (e.g.,
../) can escape the intended directory and point to arbitrary locations on the server.
Why this happens
The underlying flaw aligns with CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal').
The MCP Interface functions generate_matlab_code and execute_matlab_code appear to:
- Accept a user-controllable
scriptPath, - Concatenate or otherwise incorporate it directly into a filesystem path,
- And then read, write, or execute files based on that resulting path.
Without enforcing that scriptPath resolves within a specific base directory or validating against directory traversal patterns on a canonicalized path, the code effectively delegates directory selection to the client.
This pattern allows an attacker to reference files outside the intended MATLAB scripts directory, and in some configurations, to influence what code is generated or executed.
Potential impact
- Access to files outside the intended MATLAB project or workspace directory.
- Reading sensitive files that the server process can access (e.g., configuration files, credentials, logs).
- Overwriting or creating files in directories reachable by the server process, potentially leading to data corruption.
- Indirect remote code execution if the attacker can cause untrusted MATLAB or script content to be written and then executed.
- Escalation of impact when combined with other vulnerabilities or insecure deployment practices (e.g., writable directories on the executable search path).
General mitigation guidance
Effective mitigation focuses on constraining how paths derived from client input are used and reducing trust in remote-controlled path parameters:
- Enforce a fixed base directory: Resolve all script operations relative to a single, predefined root directory for MATLAB scripts; reject any path that attempts to escape it.
- Canonicalize paths before validation: Use a secure, platform-aware method to normalize and resolve
scriptPathto an absolute path, then verify that it remains under the allowed base directory. - Reject unsafe patterns: As a defense-in-depth measure, block obvious traversal tokens (e.g.,
../, absolute paths, drive letters) and disallow null bytes or other control characters in path values. - Use whitelisting or resource identifiers: Where possible, reference scripts by logical IDs or known filenames rather than accepting arbitrary filesystem paths from the client.
- Run with least privilege: Configure the matlab-mcp-server process to run under an account and with filesystem permissions that restrict which directories it can read or write, limiting damage if a path traversal occurs.
- Isolate execution environments: Use sandboxing or isolated working directories for code generation and execution, to keep sensitive system and application files out of reach.
How teams detect issues like this
Detection can combine code-level analysis with operational monitoring:
- Static analysis patterns:
- Search for uses of
scriptPathin path-building code (e.g.,path.join, string concatenation with/or\) without subsequent checks against a base directory. - Flag any filesystem operations (
readFile,writeFile,execor MATLAB invocation routines) that accept variables sourced directly from network input or request parameters. - Add custom rules to static analysis tools to detect user-controlled input flowing into path operations without canonicalization and boundary checks.
- Code review signals:
- Paths constructed from request properties or parameters in
src/index.tsand related modules. - Absence of centralized helper functions for safe path resolution.
- Any conditional checks that only filter substrings but do not operate on canonical paths.
- Runtime and log monitoring:
- Monitor logs for requests containing suspicious path sequences (e.g.,
../,..\\, URL-encoded traversal). - Enable application logging around path resolution logic to capture abnormal or cross-directory access attempts.
- Security testing and policies:
- Include directory traversal test cases in automated API security tests against the MCP Interface.
- Use automated review tools or CI checks to enforce use of safe path utility functions and disallow direct concatenation of filesystem paths from user input.

