Skip to content

🐛 fix(dirs): raise RuntimeError when the passwd home is empty - #757

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
sagaragas:fix/empty-passwd-home
Dec 4, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
sagaragas:fix/empty-passwd-home

Conversation

@sagaragas

@sagaragas sagaragas commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

On Unix, macOS and iOS, a user whose password database entry has an empty home directory, with HOME unset, empty or relative, got user directories rooted at /: /.config/app on Unix, and /Library/Application Support/app on macOS, which is the shared site directory. 🐛 These lookups now raise RuntimeError, as they already did for a relative password database home and, since #730, for an empty one when root runs with use_site_for_root. A home of / still resolves to /.config/app.

Without an absolute HOME, the home lookup now always reads the password database entry itself instead of calling os.path.expanduser, which turns an empty home into /. Every result then goes through the existing check that the expanded path is absolute, so an empty home, a relative one and a missing entry fail the same way. An absolute HOME, an absolute password database home and Windows are unchanged.

The example stands in a password database entry with an empty home, since creating such a user needs root:

$ cat run.py
import os
import pwd
from unittest import mock

from platformdirs.unix import Unix

os.environ.pop("HOME", None)
entry = pwd.struct_passwd(("app-user", "x", 12345, 12345, "", "", "/bin/sh"))
with mock.patch.object(pwd, "getpwuid", return_value=entry):
    print(Unix("app").user_config_dir)
$ python run.py

With the new traceback cut to its last line:

-/.config/app
+RuntimeError: could not determine the home directory for '~/.config', set an absolute HOME or XDG variable

When HOME is unset, empty, or relative on POSIX and the password
database entry has an empty pw_dir, expand_user() joined the empty
string with path[1:] and rooted user directories at / (such as
/.config/app on Unix or /Library/Application Support/app on macOS).

Check the password database home with _is_absolute() on the explicit
passwd fallback path, and reject an empty pw_dir after
os.path.expanduser() when HOME is unset and the result is rooted at /.
expand_user read the password database in two branches: one for a set but
unusable HOME, and one that undid what expanduser had made of an empty
home. With HOME unset now taking the first branch, the second goes away
and an empty home is rejected by the same absolute-path check. A relative
HOME with an empty passwd home is now tested as well.
@gaborbernat gaborbernat added the bug Something isn't working label Dec 4, 2026
@gaborbernat gaborbernat changed the title fix(dirs): raise RuntimeError when passwd home is empty 🐛 fix(dirs): raise RuntimeError when the passwd home is empty Dec 4, 2026
@gaborbernat
gaborbernat merged commit ce1f58f into tox-dev:main Dec 4, 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