Skip to content

🐛 fix(api): skip unsearchable directories in find_*_file - #703

Merged
gaborbernat merged 9 commits into
tox-dev:mainfrom
darrenhuai:fix-find-file-denied-dir
Nov 17, 2026
Merged

gaborbernat merged 9 commits into
tox-dev:mainfrom
darrenhuai:fix-find-file-denied-dir

Conversation

@darrenhuai

@darrenhuai darrenhuai commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

On Python 3.13 and older, find_config_file and the other find_*_file and find_*_files lookups raise PermissionError when the user cannot enter one of the directories on the search path, instead of moving on to the next one. It takes an unusual setup, such as a site config directory that only root can open, but once that exists every lookup that reaches it fails. On main, as an unprivileged user:

$ ls -ld xdg-a/app xdg-b/app/app.toml
drwx------ 2 root root 4096 Nov 11 13:19 xdg-a/app
-rw-r--r-- 1 root root    6 Nov 11 13:19 xdg-b/app/app.toml
$ export XDG_CONFIG_DIRS=$PWD/xdg-a:$PWD/xdg-b
$ python3.13 -c 'from platformdirs import PlatformDirs; print(PlatformDirs("app").find_config_file("app.toml"))'
Traceback (most recent call last):
  ...
PermissionError: [Errno 13] Permission denied: '/tmp/tmp.GipFxBFn9A/xdg-a/app/app.toml'
$ python3.14 -c 'from platformdirs import PlatformDirs; print(PlatformDirs("app").find_config_file("app.toml"))'
/tmp/tmp.GipFxBFn9A/xdg-b/app/app.toml

_find_files, added in #586, checks each candidate with Path.is_file(). Before Python 3.14 that only ignores a few stat errors, such as ENOENT and ENOTDIR, and re-raises the rest, EACCES included. From 3.14 on, Path.is_file() calls os.path.isfile(), which returns False on any OSError, so the same lookup gives a different answer depending on the interpreter.

The lookup now calls os.path.isfile() directly, so every supported Python skips such a directory the way 3.14 does. A name too long for the file system is the other case I could trigger. In an existing directory it raised OSError on 3.11 to 3.13; now it counts as not found, which is what 3.14 already did.

The new tests fail on main under Python 3.11 to 3.13 on Linux, and the #687 fallback test now stubs os.path.isfile, because the lookup no longer goes through Path.is_file.

The find how-to now says that a directory the user cannot search is skipped, next to the existing note that a directory with the file's name is skipped.

find_config_file and the other find_* lookups check each candidate with
Path.is_file(). Until Python 3.14 that only swallows ENOENT, ENOTDIR,
EBADF and ELOOP, so a directory on the search path that the caller may
not enter makes stat() fail with EACCES and the whole lookup raises
PermissionError. A root-owned 0700 directory in XDG_CONFIG_DIRS is
enough: on 3.11-3.13 find_config_file crashes, on 3.14 it moves on and
returns the file from the next directory, because 3.14's Path.is_file
delegates to os.path.isfile.

Use os.path.isfile directly so every supported Python gets the 3.14
behaviour. The test denies access to the first site directory with a
real chmod, so it runs on POSIX as a non-root user; on 3.14+ it passes
with or without the fix.
main moved _find_files up next to find_config_file and reworded its suppressions, so the os.path.isfile check now sits in that copy with the reason on the ignore comment.
The runtime fallback test counted files through Path.is_file, which the lookup no longer calls, so it stubs os.path.isfile instead. Add a case that needs no permission bits: a name over the file system limit makes stat fail with ENAMETOOLONG, which Path.is_file re-raised before Python 3.14 just like EACCES. Reword the changelog entry to fit 120 columns.
The entry now reads as what the lookups do, and also mentions that a name too long for the file system counts as not found instead of raising on Python 3.13 and older.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 17, 2026
@gaborbernat
gaborbernat merged commit f444c37 into tox-dev:main Nov 17, 2026
40 checks passed
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.

2 participants