Summary
Langroid's TableChatAgent and VectorStore components contain a critical code injection flaw in how they evaluate LLM-generated tool messages using Python's eval() with incomplete sandboxing.
When full_eval=True, an attacker who can influence prompts or LLM output can execute arbitrary OS commands on the host by abusing implicitly available Python built-ins.
Affected context
This vulnerability appears in Langroid-based applications that:
- Use
TableChatAgent.pandas_eval()and/orVectorStoreevaluation helpers - Enable
full_eval=Truefor LLM-generated tool messages or expressions - Run these evaluations on the same host (or container) as sensitive resources
- Accept prompts or tool instructions from users or external systems without strict trust guarantees
In these environments, LLM outputs are executed using eval() under the assumption that the sandboxed locals dictionary is sufficient to constrain behavior.
Exploit preconditions
- Attack surface: Application endpoints or workflows that send user-controlled or external text into Langroid agents configured with
full_eval=True. - Required access: Ability to influence prompts, tool calls, or any text that reaches
TableChatAgent.pandas_eval()or equivalentVectorStoreevaluation paths. - Trigger point: Execution of Python's
eval()/ expression evaluation on LLM-generated content, with globals that still expose__builtins__. - Why exploitation is possible: The sandbox only clears
locals, not theglobalsbuilt-in namespace. Python automatically populates__builtins__, granting access to powerful primitives such as__import__, which can be chained to OS-level execution.
Why this happens
The defect stems from relying on Python's eval() with a partially constrained execution context, assuming that providing an empty locals dictionary yields a safe sandbox.
In reality, Python's evaluation model populates __builtins__ into the global namespace when not explicitly overridden, exposing functions like __import__ that can be used to access modules such as os and subprocess.
This behavior maps directly to CWE-94: Improper Control of Generation of Code ('Code Injection'), where dynamically generated or externally influenced code is executed without adequate restriction or validation.
The insecure pattern is: taking LLM-generated strings, passing them directly to eval() or similar dynamic execution functions, and assuming that simple namespace tweaks suffice to prevent access to powerful built-ins.
Potential impact
- Attackers may execute arbitrary Python code on the host running the Langroid-based application.
- Attackers could run OS commands (for example, via
os.systemor similar mechanisms), leading to full host compromise. - Attackers may exfiltrate secrets such as environment variables, API keys, database credentials, or configuration files.
- Attackers can modify or delete application data, including databases and vector indices.
- Attackers may pivot laterally to other systems reachable from the compromised host.
General mitigation guidance
Effective mitigation focuses on eliminating unsafe dynamic code execution and constraining what LLM outputs are allowed to do:
- Avoid using
eval(),exec(), or similar dynamic execution primitives on any LLM-generated or user-controlled content; prefer structured, declarative APIs or whitelisted operations. - If expression evaluation is necessary, use restricted evaluators or interpreters that provide a minimal, explicitly defined environment with no implicit built-ins.
- Treat LLM outputs as untrusted input and subject them to strict validation, parsing, and allowlists before interpretation.
- Design agent tool interfaces as parameterized functions where arguments are validated types and values, rather than free-form code fragments.
- Run LLM tools and evaluation logic under least-privilege principles (e.g., non-root users, constrained containers, limited filesystem and network access) to reduce blast radius.
- Separate sensitive system capabilities (file access, networking, process execution) into dedicated, tightly controlled services instead of exposing them directly to LLM-controlled code paths.
How teams detect issues like this
Detection can focus both on code-level patterns and runtime behaviors:
- Use static analysis or linters to flag any use of
eval(),exec(), orast.literal_eval()-like APIs where input originates from LLM outputs, prompts, or user-controlled data. - Establish code review checklists that highlight dynamic execution, sandbox implementations, and any manual management of
globals/localsas high-risk areas requiring security sign-off. - Enforce policies (for example, via automated pull request checks) that prohibit direct execution of model-generated strings; require mediation through safe interpreters or whitelisted actions.
- Monitor application logs and telemetry for unusual tool messages, unexpected Python expression patterns, or OS command invocations initiated from LLM agent subsystems.
- Instrument runtime environments (via EDR, audit logs, or container security tools) to detect anomalous process creation, file access, or network connections originating from Langroid agent processes.

