Summary
A code injection vulnerability in Langroid's TableChatAgent allows attackers to bypass the Web Application Firewall (WAF) in the pandas_eval tool and reach Python's eval builtin.
When an attacker can influence expressions evaluated by pandas_eval, they may chain allowed DataFrame methods with dangerous dunder attributes to execute arbitrary code within the application environment.
Affected context
This vulnerability affects Langroid-based applications that:
- Use
TableChatAgentor other components that expose thepandas_evaltool to user-controlled input - Rely on the WAF in
langroid/utils/pandas_utils.pyto filter unsafe expressions - Run in environments where the Python process has access to sensitive data, services, or the underlying OS
The risk is highest when end users can submit free-form analytical expressions that are passed directly into pandas_eval without additional server-side validation or sandboxing.
Exploit preconditions
- Attack surface: Application interfaces (chat prompts, APIs, or UI fields) that accept table or DataFrame queries and route them into
TableChatAgent'spandas_evaltool. - Required access: Ability to supply or influence the expression string evaluated by
pandas_eval(typically unprivileged, remote user input). - Trigger point: Evaluation of the crafted expression inside
pandas_eval, after it passes through_literal_ok()and the WAF logic. - Why exploitation is possible:
_literal_ok()returnsFalseinstead of raisingUnsafeCommandErroron invalid input, allowing processing to continue.- The WAF does not fully prevent access to dangerous dunder attributes (such as
__init__,__globals__,__builtins__). - An attacker can chain whitelisted DataFrame methods with these attributes to reveal the
evalbuiltin and execute arbitrary Python code.
Why this happens
The core issue aligns with CWE-94: Improper Control of Generation of Code ('Code Injection').
Langroid's pandas_eval feature is designed to interpret user-supplied expressions against pandas DataFrames. Instead of enforcing strict, safe evaluation semantics, the implementation:
- Relies on a WAF-style filter (
_literal_ok()and related checks) to distinguish safe from unsafe expressions. - Allows unrestricted access to powerful Python dunder attributes that can be used to escape the intended evaluation scope.
- Treats expressions as sufficiently sanitized even when
_literal_ok()returnsFalse, because it does not raise an exception to halt processing.
This design combines dynamic expression evaluation with incomplete validation and strong reflection capabilities. As a result, user-controlled input can be transformed into executable Python code by navigating from DataFrame objects to interpreter internals and the eval builtin.
Potential impact
- Execution of arbitrary Python code in the Langroid application process
- Reading or modification of sensitive data accessible to the LLM application
- Lateral movement to connected services (databases, APIs, message queues) using application credentials
- File system access, including reading configuration files, secrets, or model artifacts
- Denial of service by spawning expensive computations or processes
- Use of the compromised application environment for further supply-chain or infrastructure attacks
General mitigation guidance
Effective mitigation focuses on eliminating unsafe code evaluation paths and constraining what expressions can do:
- Replace generic
eval-style mechanisms with strict, domain-specific expression interpreters that support only a fixed set of safe operations. - Treat all user-supplied expressions as untrusted, and avoid passing them directly to Python evaluation primitives or objects that can reach them.
- Restrict or remove access to dangerous attributes and capabilities (e.g.,
__globals__,__builtins__,__dict__, module-level objects) from the evaluation context. - Apply positive allowlists of permitted DataFrame methods and expressions, and block all others by default.
- Run code that processes user expressions in a sandboxed, least-privilege environment (separate process, container, or VM) with minimized OS and network permissions.
- Log rejected expressions and suspicious evaluation attempts to support ongoing hardening and tuning of filters.
How teams detect issues like this
Detection can combine static review of code paths and runtime monitoring:
- Static analysis and code review:
- Search for any use of
eval,exec,ast.literal_eval, or pandas evaluation helpers in code that can receive user input. - Flag flows where user-controlled strings reach
pandas_evalor similar utilities without strict parsing or schema enforcement. - Review any WAF-style filters (
_literal_ok(), pattern-based blockers) to ensure that failing validation halts execution rather than silently continuing. - Watch for access to powerful attributes such as
__globals__,__builtins__,__dict__, and reflection APIs reachable from user expressions. - Policy and tooling:
- Enforce secure-coding guidelines that discourage generic code evaluation and mandate allowlisted, declarative query languages.
- Use SAST/IAST tools configured with rules for CWE-94 patterns and dataflow from user inputs to evaluation primitives.
- Runtime monitoring:
- Instrument logging around expression evaluation to record suspicious constructs or repeated failures.
- Monitor for anomalous behavior originating from the application process (unexpected network calls, filesystem access, or subprocess creation).

