You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow to change permissions of temp directories created with tmp_path #10679
Any temp directory created with the fixture tmp_path has read/write permissions only for the specific user running the tests because mode is set to 0o700.
Although this is probably motivated by security concerns, there are scenarios where more relaxed permissions can be useful.
Describe the solution you'd like
I'd like to be able to override the 0o700 mode that is currently hardcoded in pytest.
For example, the tests may be run in a CI pipeline by a special service user, that's different from the user that should investigate any possible error.
If the mode is set 0o750 and the users belong to the same group, the second user would be able to read the temp files.
Alternative Solutions
I tried to enforce chmod on the temp directory after the tests are executed, but this adds complexity and it's not enough if the pipeline is killed due to a timeout.
Reacted by jehturner and Romulo Pulcinelli Benedetti
Given the security implications (as you say), I'm a bit lukewarm on the idea of allowing to change it in a config file or somesuch. Why not something like (untested):
Or you could perhaps override tmp_path_factory with your own fixture instead, and pytest's tmp_path should pick that one up - though I haven't tried if that actually works.
However, checking if running in xdist works as you suggested, so I ended up with this in conftest.py (working with tmp_path, while it would need additional changes to work with tmp_path_factory but it's not needed at the moment):
defpytest_addoption(parser):
parser.addoption(
"--basetemp-permissions",
action="store",
help=(
"Permissions of the base temporary directory used by tmp_path, ""as octal value. Examples: 700 (default), 750, 770"
),
)
@pytest.fixturedeftmp_path(tmp_path, request):
permissions=request.config.getoption("--basetemp-permissions")
ifpermissions:
permissions=int(permissions, 8)
# enforce the permissions according to the temp directory layout:# - <basetemp>/test_<name>, when running without pytest-xdist# - <basetemp>/popen-gw<num>/test_<name>, when running with pytest-xdisttmp_path.chmod(permissions)
tmp_path.parent.chmod(permissions)
ifos.getenv("PYTEST_XDIST_WORKER_COUNT"):
tmp_path.parent.parent.chmod(permissions)
returntmp_path
Activity
The-Compiler commentedon Jan 23, 2023
Given the security implications (as you say), I'm a bit lukewarm on the idea of allowing to change it in a config file or somesuch. Why not something like (untested):
and use that fixture where you need it?
GianlucaFicarelli commentedon Jan 23, 2023
The modified fixture would almost work, but, since the layout of directories is (assuming to execute
pytest --basetemp=/tmp/mybasetemp ...):both
mybasetempandtest_tmp0need the permissions fixed, so the fixture could be:However this isn't enough when using
pytest-xdist, because in that case the layout of directories has an additional level like in this example:Do you have any idea on how to handle both the cases?
The-Compiler commentedon Jan 24, 2023
I suppose you could check if running in xdist and then handle that case appropriately.
Or you could perhaps override
tmp_path_factorywith your own fixture instead, and pytest'stmp_pathshould pick that one up - though I haven't tried if that actually works.GianlucaFicarelli commentedon Jan 25, 2023
Overriding
tmp_path_factoryis possible, but it seems not enough becausepytest-xdistcalls directlyself.config._tmp_path_factory.getbasetemp()in https://github.com/pytest-dev/pytest-xdist/blob/9373ddb3f841a009bc9669b514923b6ecba724e7/src/xdist/workermanage.py#L272and the original basedir is still created with the hardcoded permissions.
However, checking if running in xdist works as you suggested, so I ended up with this in
conftest.py(working withtmp_path, while it would need additional changes to work withtmp_path_factorybut it's not needed at the moment):and pytest can be executed with