Skip to content

🐛 fix(cache): reject untrusted lock directory - #291

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/cache-lock-folder-trust
Nov 30, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/cache-lock-folder-trust

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

python-discovery's disk cache keeps its locks in a locks-v2 folder, and discovery used whatever already sat there, even a symlink or another account's folder left from when the cache was group-writable. That account could stall the owner's lookups or have lock files created outside the cache. 🐛 The lock folder now passes the same ownership and permission checks as the rest of the cache; when it fails them, discovery warns and answers without the cache. Fixes #285.

$ mkdir -p -m 700 cache/py_info/4 && mkdir shared
$ ln -s ../../../shared cache/py_info/4/locks-v2
$ python -c 'import logging; logging.basicConfig(format="%(levelname)s %(message)s")
from pathlib import Path
from python_discovery import DiskCache, get_interpreter
get_interpreter("python3", cache=DiskCache(Path("cache")))'
$ ls shared

Before:

cc397cd68d6915a40fd9a4d4a7ac3f42ca48ffcee1c82d696cc7441323c1915a.lock

After:

WARNING not locking in untrusted directory /home/me/demo/cache/py_info/4/locks-v2

Path.mkdir with exist_ok=True accepts anything already in place, and filelock follows links in parent folders (open(2)), so the check has to happen in discovery. It reuses the rule that already guards the cache folders.

Caches created by released versions pass the check. A rejected folder keeps the cache off until someone moves it aside; renaming works where deleting may not. virtualenv, tox and hatch use their own caches and are not affected. Windows is unchanged.

Reported by oss-scanner: ANT-2026-RMWOKI09.

DiskContentStore.locked() creates locks-v2 with mkdir(exist_ok=True), which
accepts a symlink or a directory owned by another user. A lock taken there
could stall lookups, and the lock file could be created outside the cache.

Check the lock folder with the same rule _trusted_folder applies to the cache
directories: a real directory owned by the current user or root and not
writable by others. In a group-shared cache any real directory is accepted, as
before, but a symlink is refused. A rejected folder logs a warning and the
lookup continues without the cache.

Fixes tox-dev#285.
Reported by oss-scanner as ANT-2026-RMWOKI09.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 29, 2026
DiskCache refuses a group-writable cache directory, so the lock rule in
the explanation page only needs the private case.
@gaborbernat
gaborbernat merged commit ede5b9b into tox-dev:main Nov 30, 2026
24 checks passed
@gaborbernat
gaborbernat deleted the fix/cache-lock-folder-trust branch November 30, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DiskCache uses an existing locks-v2 directory without checking its owner or type

1 participant