Arbitrary code execution via eval in MongoDB agent toolkit aggregation parsing
Description
parse_command extracted the text between .aggregate( and its closing paren and passed it to eval(). The evaluation globals defined no __builtins__, so Python supplied the real builtins and __import__ was reachable. Because MongoDBDatabase.run receives a free-form query string written by an LLM, text that reaches the model could become Python that executes in the process hosting the agent.
Through 0.11.0 the eval lived in langchain_mongodb/agent_toolkit/database.py. In 0.12.0 it moved to pymongo-search-utils, which langchain-mongodb depended on as >=0.3.0 — so 0.12.0 installs still resolved to a vulnerable parser.
Required conditions
All three must hold:
- The application builds an agent from
MongoDBDatabaseToolkit, or otherwise callsMongoDBDatabase.run/parse_commandwith model-generated text. - Untrusted input reaches that model — user chat, a support ticket, a scraped page, a retrieved document.
- The model emits the payload verbatim as the
queryargument. The shipped prompt routes queries through a checker tool first, and reproduction is probabilistic — though an attacker can retry.
Text stored in the database is a weaker path: sample document values are truncated before the model sees them, so a stored payload has to occupy a field name, which requires write access to a collection the agent inspects.
Impact
Arbitrary Python in the agent's process — its files, environment variables, API keys, and outbound network. Not limited to the database.
Patches
pymongo-search-utils 0.3.1 replaces eval with ast.parse(..., mode="eval") and a default-deny AST walker: literals and containers only, calls restricted to ObjectId and datetime, attribute access to timezone.utc, everything else rejected.
langchain-mongodb 0.12.1 raises the dependency floor to pymongo-search-utils>=0.3.1.
Workarounds
On langchain-mongodb 0.12.0, upgrading pymongo-search-utils to >=0.3.1 alone is sufficient — no langchain-mongodb change needed. On <= 0.11.0, upgrade, as the vulnerable code is in the package itself.
Independently of this issue, give the agent's MongoDB credential read-only access scoped to the collections in play. The toolkit's read-only intent is otherwise expressed only in the system prompt.