Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/virtualenv_tools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,8 @@ def test_virtualenv_path_works_with_nonquoted_path(fake_venv, capsys):
assert out.startswith('Updated: ')
assert '/venv ->' in out


def test_get_orig_path(venv):
activate = venv.before
orig_path = virtualenv_tools.get_orig_path(activate)
assert orig_path == venv.before.strpath
17 changes: 3 additions & 14 deletions virtualenv_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import re
import shlex
import shutil
import subprocess
import sys
from types import CodeType
from typing import NamedTuple
Expand Down Expand Up @@ -246,25 +247,13 @@ def update_paths(venv: Virtualenv, new_path: str) -> None:
remove_local(venv.path)
update_scripts(venv.bin_dir, venv.orig_path, new_path, activation=True)


def get_orig_path(venv_path: str) -> str:
"""This helps us know whether someone has tried to relocate the
virtualenv
"""
activate_path = os.path.join(venv_path, 'bin/activate')

with open(activate_path) as activate:
venv_var_prefix = 'VIRTUAL_ENV='
for line in activate:
# virtualenv 20 changes the position
if line.startswith(venv_var_prefix):
return shlex.split(line[len(venv_var_prefix):])[0]
else:
raise AssertionError(
'Could not find VIRTUAL_ENV= in activation script: %s' %
activate_path
)

result = subprocess.run(["bash", "-c", f"source {activate_path} && echo $VIRTUAL_ENV"], capture_output=True, text=True)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to consider using shlex.quote on the activate_path. I think this will fail for paths with spaces or other special characters in them.

return result.stdout.strip()
Comment on lines +255 to +256

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this introduces several arbitrary code execution vulnerabilities:

  • activate_path is not properly quoted
  • the activate script itself is arbitrary code and may not be a virtualenv activate script

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is parsing then still the best option even though its brittle?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is the only approach I would take personally -- you'll still have problems with substitution either way

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #36, I'm doing a str.replace for the virtualenv path. Because the bash script now has 3 lines to update, the existing regex doesn't cover all the cases. Though, maybe I should just create 2 more regexes to be more explicit about the replacement.


class NotAVirtualenvError(ValueError):
def __str__(self) -> str:
Expand Down
Loading