The Vulnerability Unveiled
BagOS, an AI agent designed to interact with Solana launchpads like Bags, faced a critical security review. The tool's core principle was to allow an AI model to propose trades and claim creator fees, but crucially, never to execute a transaction autonomously. Every write operation was designed to return a preview and a single-use token tied to specific arguments, with trade caps and pre-transaction simulations in place. However, an external audit uncovered a severe flaw: the AI trading tool could be tricked into draining a user's wallet without ever invoking its protected write tools.
The exploit required two distinct vulnerabilities to be present simultaneously. Individually, each flaw posed no significant risk, but their confluence created a critical security gap. The primary issue was that the BagOS server implicitly trusted the current working directory of the user's environment. MCP clients, such as Claude Code, initiate a local server within the project folder the user has open. BagOS then called dotenv.config(), a function that reads environment variables from a .env file located in the current working directory.
Exploiting the Trust Flaw
The attack vector exploited this trust by manipulating the .env file. If an attacker could coerce a user into opening a directory containing a malicious .env file while their BagOS client was running, the server would load sensitive information directly from this untrusted source. This included critical credentials like API keys and, more alarmingly, private keys or seed phrases that granted access to the user's wallet.
The second, equally critical, flaw was the server's handling of the login challenge itself. When the AI agent was prompted to perform an action that required authentication, BagOS would present a login challenge. The vulnerability lay in how this challenge was constructed and verified. Instead of securely generating and validating a unique, nonce-based challenge, the system was susceptible to pre-signed or predictable challenges. An attacker could craft a malicious transaction that mimicked a legitimate login challenge, but was actually a wallet-draining operation. When the BagOS server processed this malformed challenge, it would inadvertently sign the draining transaction as if it were a valid login step.

The Chain of Events
The attack sequence would unfold as follows:
- Malicious Environment Setup: An attacker would need to convince the target user to open a directory containing a specially crafted
.envfile in their MCP client. This file would contain the necessary credentials and potentially pre-signed transaction data. - Server Initialization: The user launches their MCP client, which starts a local server in the compromised directory. BagOS, running within this context, calls
dotenv.config()and ingests the malicious environment variables, including wallet access details. - Triggering the Login Challenge: The AI agent, unaware of the compromised environment, is instructed to perform an action that requires authentication.
- Crafted Challenge and Signing: Instead of a legitimate login challenge, the system is presented with a pre-signed transaction designed to drain the user's wallet. Due to the second vulnerability, the BagOS server treats this malicious payload as a valid login challenge and signs it.
- Wallet Drain: The signed transaction, now authorized by the user's implicitly trusted system, executes, transferring all funds from the user's wallet to the attacker's address.
Broader Implications for AI Agents and Wallets
This incident highlights a fundamental challenge in building secure AI agents that interact with sensitive financial systems. The core principle of BagOS—that an AI should propose but never execute on its own—was circumvented not by breaking the write tool safeguards, but by exploiting the trust placed in the local environment and the authentication mechanism. It’s a stark reminder that security must be considered at every layer, from environment variable management to the intricacies of authentication protocols.
The reliance on dotenv.config() without stricter validation of the source or content of the .env file is a common pitfall. Many applications use this method for convenience, but in security-sensitive contexts, it represents a significant attack surface. Similarly, using a signing process that can be tricked into authorizing malicious actions under the guise of authentication is a critical failure. This scenario underscores the need for robust, cryptographically secure challenge-response mechanisms that are resistant to replay attacks and manipulation.
For developers building AI agents that handle financial transactions or control sensitive assets, this serves as a critical lesson. The security model must assume that the environment can be compromised and that authentication mechanisms are potential targets. Future implementations must incorporate more sophisticated methods for verifying the integrity of configuration files and ensuring that any operation requiring a signature is unequivocally legitimate and not a cleverly disguised exploit.
