Summary
QWED-MCP versions before 0.2.1 allow attacker-controlled mathematical expressions and claimed results to reach SymPy's parse_expr() without sufficient restrictions. When a downstream integration passes untrusted input to this public function, an attacker may execute operating-system commands with the privileges of the qwed-mcp process.
Affected context
The vulnerability occurs in integrations that invoke QWED-MCP's verify_math_expression() function with attacker-controlled strings. The default MCP tool registry does not expose this function, so the issue generally requires a downstream application, service, or wrapper to connect external input to the library API without enforcing a trusted expression grammar.
Exploit preconditions
- Attack surface: Any downstream integration exposing
verify_math_expression()through an externally reachable interface - Required access: Ability to influence the expression or claimed-result arguments supplied to the function
- Trigger point: Expression parsing by SymPy's
parse_expr()during verification - Why exploitation is possible: The parser evaluates attacker-controlled content with Python evaluation capabilities, while built-ins, globals, and the expression structure are not sufficiently restricted
Why this happens
The underlying bug is code injection through unsafe evaluation of untrusted mathematical expressions. The function normalizes syntax but does not establish a constrained parsing environment, remove Python built-ins, or validate the resulting expression against an approved abstract syntax tree before evaluation. Because parse_expr() can invoke Python evaluation with accessible execution features, input intended as data may be interpreted as executable logic, mapping to CWE-94: Improper Control of Generation of Code ('Code Injection').
Potential impact
- An attacker may execute arbitrary operating-system commands
- The qwed-mcp process and its accessible resources could be compromised
- Accessible files and application data could be read, modified, or deleted
- Process secrets, credentials, or tokens could be exfiltrated
- The compromised process could access internal services or enable lateral movement
General mitigation guidance
Mitigation generally focuses on eliminating evaluation of attacker-controlled language constructs and reducing the trust placed in expression inputs.
Common defensive patterns include:
- Replacing general-purpose expression evaluation with a dedicated mathematical grammar or safe, non-evaluating parser
- Allowlisting operators, literals, functions, and symbol names before any computation
- Parsing into an approved AST and rejecting imports, attributes, calls, assignments, comprehensions, and other executable constructs
- Supplying tightly restricted parser globals and removing access to Python built-ins where evaluation cannot be avoided
- Validating both expression and claimed-result inputs at the integration boundary
- Exposing only narrowly scoped verification operations and running the process with least privilege
- Limiting filesystem, network, secret, and subprocess access available to the service
How teams detect issues like this
Detection approaches may include:
- Static analysis rules for
eval(), SymPyparse_expr(), and other dynamic evaluation APIs receiving request-derived values - Code review checks for parser calls that do not define restricted namespaces or validate an AST
- Data-flow analysis tracing HTTP, RPC, MCP, or user-provided input into mathematical expression evaluators
- Tests containing disallowed Python constructs to confirm that the integration accepts only the intended mathematical grammar
- Policy enforcement that requires allowlisted parsers and prohibits dynamic code evaluation in request-processing paths
- Neutral automated code-review or SAST tools that flag unsafe evaluation patterns

