|
11 | 11 | from ._util import ensure_directory_exists, raise_on_not_writable_file |
12 | 12 |
|
13 | 13 | if sys.platform == "win32": # pragma: win32 cover |
| 14 | + import ctypes |
14 | 15 | import msvcrt |
| 16 | + from ctypes import wintypes |
| 17 | + |
| 18 | + # Windows API constants for reparse point detection |
| 19 | + FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400 |
| 20 | + INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF |
| 21 | + |
| 22 | + # Load kernel32.dll |
| 23 | + _kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) |
| 24 | + _kernel32.GetFileAttributesW.argtypes = [wintypes.LPCWSTR] |
| 25 | + _kernel32.GetFileAttributesW.restype = wintypes.DWORD |
| 26 | + |
| 27 | + def _is_reparse_point(path: str) -> bool: |
| 28 | + """ |
| 29 | + Check if a path is a reparse point (symlink, junction, etc.) on Windows. |
| 30 | +
|
| 31 | + :param path: Path to check |
| 32 | + :return: True if path is a reparse point, False otherwise |
| 33 | + :raises OSError: If GetFileAttributesW fails for reasons other than file-not-found |
| 34 | + """ |
| 35 | + attrs = _kernel32.GetFileAttributesW(path) |
| 36 | + if attrs == INVALID_FILE_ATTRIBUTES: |
| 37 | + # File doesn't exist yet - that's fine, we'll create it |
| 38 | + err = ctypes.get_last_error() |
| 39 | + if err == 2: # noqa: PLR2004 # ERROR_FILE_NOT_FOUND |
| 40 | + return False |
| 41 | + if err == 3: # noqa: PLR2004 # ERROR_PATH_NOT_FOUND |
| 42 | + return False |
| 43 | + # Some other error - let caller handle it |
| 44 | + return False |
| 45 | + return bool(attrs & FILE_ATTRIBUTE_REPARSE_POINT) |
15 | 46 |
|
16 | 47 | class WindowsFileLock(BaseFileLock): |
17 | 48 | """Uses the :func:`msvcrt.locking` function to hard lock the lock file on Windows systems.""" |
18 | 49 |
|
19 | 50 | def _acquire(self) -> None: |
20 | 51 | raise_on_not_writable_file(self.lock_file) |
21 | 52 | ensure_directory_exists(self.lock_file) |
| 53 | + |
| 54 | + # Security check: Refuse to open reparse points (symlinks, junctions) |
| 55 | + # This prevents TOCTOU symlink attacks (CVE-TBD) |
| 56 | + if _is_reparse_point(self.lock_file): |
| 57 | + msg = f"Lock file is a reparse point (symlink/junction): {self.lock_file}" |
| 58 | + raise OSError(msg) |
| 59 | + |
22 | 60 | flags = ( |
23 | 61 | os.O_RDWR # open for read and write |
24 | 62 | | os.O_CREAT # create file if not exists |
|
0 commit comments