{"id":"4339b8d650adc2d1","repo":"pytest-dev/pytest","slug":"the-temporary-directory-rootdir-is-not-owned-by","errorCode":null,"errorMessage":"The temporary directory {rootdir} is not owned by the current user. Fix this and try again.","messagePattern":"The temporary directory (.+?) is not owned by the current user\\. Fix this and try again\\.","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"src/_pytest/tmpdir.py","lineNumber":194,"sourceCode":"            # TOCTOU vulnerability.\n            # This check makes us vulnerable to a DoS - a user can `mkdir\n            # /tmp/pytest-of-otheruser` and then `otheruser` will fail this\n            # check. For now we don't consider it a real problem. otheruser can\n            # change their TMPDIR or --basetemp, and maybe give the prankster a\n            # good scolding.\n            uid = get_user_id()\n            if uid is not None:\n                stat_follow_symlinks = (\n                    False if os.stat in os.supports_follow_symlinks else True\n                )\n                rootdir_stat = rootdir.stat(follow_symlinks=stat_follow_symlinks)\n                if stat.S_ISLNK(rootdir_stat.st_mode):\n                    raise OSError(\n                        f\"The temporary directory {rootdir} is a symbolic link. \"\n                        \"Fix this and try again.\"\n                    )\n                if rootdir_stat.st_uid != uid:\n                    raise OSError(\n                        f\"The temporary directory {rootdir} is not owned by the current user. \"\n                        \"Fix this and try again.\"\n                    )\n                if (rootdir_stat.st_mode & 0o077) != 0:\n                    chmod_follow_symlinks = (\n                        False if os.chmod in os.supports_follow_symlinks else True\n                    )\n                    rootdir.chmod(\n                        rootdir_stat.st_mode & ~0o077,\n                        follow_symlinks=chmod_follow_symlinks,\n                    )\n            keep = self._retention_count\n            if self._retention_policy == \"none\":\n                keep = 0\n            basetemp = make_numbered_dir_with_cleanup(\n                prefix=\"pytest-\",\n                root=rootdir,\n                keep=keep,","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/tmpdir.py#L176-L212","documentation":"Raised by TempPathFactory.getbasetemp as an OSError when the basetemp root (/tmp/pytest-of-<user>) exists but its owning uid does not match the current process uid. Because the temp root lives under a shared /tmp, pytest enforces ownership to prevent another user from pre-creating the directory (a known DoS / data-leak vector) before this user writes private test artifacts into it.","triggerScenarios":"A different OS user previously ran pytest and created /tmp/pytest-of-<user>, then the current process runs as a different uid (e.g. via sudo, container user remap, or a username collision across machines). The ownership check fails at the first tmp_path usage.","commonSituations":"Running tests as root after a non-root user created the dir; container images that bake in a /tmp/pytest-of-* from a build stage; shared dev boxes where multiple humans share a username but different uids; CI that runs some jobs as one user and others as another.","solutions":["Delete the stale directory: `sudo rm -rf /tmp/pytest-of-<user>` so pytest recreates it as the current user.","Run pytest with `--basetemp=<dir>` owned by the current user, or set TMPDIR to a directory you own.","Run the test process as the same uid that owns /tmp/pytest-of-<user> (e.g. drop sudo).","In Dockerfiles, avoid creating /tmp/pytest-of-* at build time."],"exampleFix":"// before\n$ sudo -u bob pytest tests/   # /tmp/pytest-of-bob owned by uid 1000, sudo makes you uid 0\n\n# OSError: not owned by the current user\n\n// after\n$ sudo rm -rf /tmp/pytest-of-bob\n$ pytest tests/   # recreates as current user","handlingStrategy":"validation","validationCode":"import os, pathlib, getpass\n\ndef ensure_basetemp_owned(p: pathlib.Path):\n    p.mkdir(parents=True, exist_ok=True)\n    if os.stat(p).st_uid != os.getuid():\n        raise OSError(f\"{p} not owned by uid {os.getuid()}; chown or remove it\")\n    return p","typeGuard":"import os\n\ndef is_owned_by_me(p) -> bool:\n    try:\n        return os.stat(p).st_uid == os.getuid()\n    except OSError:\n        return False","tryCatchPattern":null,"preventionTips":["Run pytest as the same uid that owns /tmp/pytest-of-<user>; avoid sudo for test runs.","In Dockerfiles, do not create /tmp/pytest-of-* at build time.","Set --basetemp or TMPDIR to a user-owned directory in shared/CI environments."],"tags":["tmpdir","ownership","security","filesystem","permissions"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}