Skip to content

Commit

Permalink
_virtual_env: handle PermissionError (#737)
Browse files Browse the repository at this point in the history
* _virtual_env: handle PermissionError

See #732

Signed-off-by: William Woodruff <[email protected]>

* _virtual_env: move the PermissionError around

Signed-off-by: William Woodruff <[email protected]>

* docstring coverage, add test

Signed-off-by: William Woodruff <[email protected]>

* CHANGELOG: record changes

Signed-off-by: William Woodruff <[email protected]>

---------

Signed-off-by: William Woodruff <[email protected]>
  • Loading branch information
woodruffw authored Feb 12, 2024
1 parent bc90ca5 commit db8b260
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ All versions prior to 0.0.9 are untracked.

## [Unreleased]

### Fixed

* Improved the error returned to users when their default temporary
directory lacks execute permissions
([#737](https://github.com/pypa/pip-audit/pull/737))

## [2.7.0]

### Added
Expand Down
29 changes: 28 additions & 1 deletion pip_audit/_virtual_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import json
import logging
import venv
from tempfile import NamedTemporaryFile, TemporaryDirectory
from os import PathLike
from tempfile import NamedTemporaryFile, TemporaryDirectory, gettempdir
from types import SimpleNamespace
from typing import Iterator

Expand Down Expand Up @@ -70,6 +71,32 @@ def __init__(
self._packages: list[tuple[str, Version]] | None = None
self._state = state

def create(self, env_dir: str | bytes | PathLike[str] | PathLike[bytes]) -> None:
"""
Creates the virtual environment.
"""

try:
return super().create(env_dir)
except PermissionError:
# `venv` uses a subprocess internally to bootstrap pip, but
# some Linux distributions choose to mark the system temporary
# directory as `noexec`. Apart from having only nominal security
# benefits, this completely breaks our ability to execute from
# within the temporary virtualenv.
#
# We may be able to hack around this in the future, but doing so
# isn't straightforward or reliable. So we bail for now.
#
# See: https://github.com/pypa/pip-audit/issues/732
base_tmpdir = gettempdir()
raise VirtualEnvError(
f"Couldn't execute in a temporary directory under {base_tmpdir}. "
"This is sometimes caused by a noexec mount flag or other setting. "
"Consider changing this setting or explicitly specifying a different "
"temporary directory via the TMPDIR environment variable."
)

def post_setup(self, context: SimpleNamespace) -> None:
"""
Install the custom package and populate the list of installed packages.
Expand Down
16 changes: 16 additions & 0 deletions test/test_virtual_env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import subprocess
from tempfile import TemporaryDirectory

import pretend
import pytest
from packaging.version import Version

Expand Down Expand Up @@ -58,3 +60,17 @@ def run_mock(args, **kwargs):
ve = VirtualEnv(["flask==2.0.1"])
with pytest.raises(VirtualEnvError):
ve.create(ve_dir)


def test_virtual_env_failed_permission_error(monkeypatch):
"""
This is a mocked test for GH#732, which is really caused by a user's
default `$TMPDIR` having the `noexec` flag set. We have no easy way
to unit test this, so we hopefully replicate its effect with a monkeypatch.
"""

monkeypatch.setattr(subprocess, "run", pretend.raiser(PermissionError))
with TemporaryDirectory() as ve_dir:
ve = VirtualEnv(["flask==2.0.1"])
with pytest.raises(VirtualEnvError, match=r"^Couldn't execute in a temporary directory .+"):
ve.create(ve_dir)

0 comments on commit db8b260

Please sign in to comment.