|
|
99a46d |
From 82dbaffa36fe317e331b2d22c5efbb6a0b6c7b19 Mon Sep 17 00:00:00 2001
|
|
|
99a46d |
From: Lumir Balhar <lbalhar@redhat.com>
|
|
|
99a46d |
Date: Tue, 8 Oct 2024 09:15:09 +0200
|
|
|
99a46d |
Subject: [PATCH] Prevent command injection
|
|
|
99a46d |
|
|
|
99a46d |
---
|
|
|
99a46d |
docs/changelog/2768.bugfix.rst | 2 ++
|
|
|
99a46d |
src/virtualenv/activation/bash/activate.sh | 9 +++++----
|
|
|
99a46d |
src/virtualenv/activation/batch/__init__.py | 4 ++++
|
|
|
99a46d |
src/virtualenv/activation/cshell/activate.csh | 10 +++++-----
|
|
|
99a46d |
src/virtualenv/activation/fish/activate.fish | 8 ++++----
|
|
|
99a46d |
src/virtualenv/activation/nushell/__init__.py | 19 ++++++++++++++++++
|
|
|
99a46d |
src/virtualenv/activation/nushell/activate.nu | 8 ++++----
|
|
|
99a46d |
.../activation/powershell/__init__.py | 12 +++++++++++
|
|
|
99a46d |
.../activation/powershell/activate.ps1 | 6 +++---
|
|
|
99a46d |
src/virtualenv/activation/python/__init__.py | 6 +++++-
|
|
|
99a46d |
.../activation/python/activate_this.py | 6 +++---
|
|
|
99a46d |
src/virtualenv/activation/via_template.py | 15 ++++++++++++--
|
|
|
99a46d |
tests/conftest.py | 6 +++++-
|
|
|
99a46d |
tests/unit/activation/conftest.py | 3 +--
|
|
|
99a46d |
tests/unit/activation/test_batch.py | 10 +++++-----
|
|
|
99a46d |
tests/unit/activation/test_powershell.py | 20 ++++++++++++++-----
|
|
|
99a46d |
16 files changed, 105 insertions(+), 39 deletions(-)
|
|
|
99a46d |
create mode 100644 docs/changelog/2768.bugfix.rst
|
|
|
99a46d |
|
|
|
99a46d |
diff --git a/docs/changelog/2768.bugfix.rst b/docs/changelog/2768.bugfix.rst
|
|
|
99a46d |
new file mode 100644
|
|
|
99a46d |
index 0000000..7651eb6
|
|
|
99a46d |
--- /dev/null
|
|
|
99a46d |
+++ b/docs/changelog/2768.bugfix.rst
|
|
|
99a46d |
@@ -0,0 +1,2 @@
|
|
|
99a46d |
+Properly quote string placeholders in activation script templates to mitigate
|
|
|
99a46d |
+potential command injection - by :user:`y5c4l3`.
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/bash/activate.sh b/src/virtualenv/activation/bash/activate.sh
|
|
|
99a46d |
index fb40db6..d047ea4 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/bash/activate.sh
|
|
|
99a46d |
+++ b/src/virtualenv/activation/bash/activate.sh
|
|
|
99a46d |
@@ -44,14 +44,14 @@ deactivate () {
|
|
|
99a46d |
# unset irrelevant variables
|
|
|
99a46d |
deactivate nondestructive
|
|
|
99a46d |
|
|
|
99a46d |
-VIRTUAL_ENV='__VIRTUAL_ENV__'
|
|
|
99a46d |
+VIRTUAL_ENV=__VIRTUAL_ENV__
|
|
|
99a46d |
if ([ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ]) && $(command -v cygpath &> /dev/null) ; then
|
|
|
99a46d |
VIRTUAL_ENV=$(cygpath -u "$VIRTUAL_ENV")
|
|
|
99a46d |
fi
|
|
|
99a46d |
export VIRTUAL_ENV
|
|
|
99a46d |
|
|
|
99a46d |
_OLD_VIRTUAL_PATH="$PATH"
|
|
|
99a46d |
-PATH="$VIRTUAL_ENV/__BIN_NAME__:$PATH"
|
|
|
99a46d |
+PATH="$VIRTUAL_ENV/"__BIN_NAME__":$PATH"
|
|
|
99a46d |
export PATH
|
|
|
99a46d |
|
|
|
99a46d |
# unset PYTHONHOME if set
|
|
|
99a46d |
@@ -62,8 +62,9 @@ fi
|
|
|
99a46d |
|
|
|
99a46d |
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
|
|
|
99a46d |
_OLD_VIRTUAL_PS1="${PS1-}"
|
|
|
99a46d |
- if [ "x__VIRTUAL_PROMPT__" != x ] ; then
|
|
|
99a46d |
- PS1="(__VIRTUAL_PROMPT__) ${PS1-}"
|
|
|
99a46d |
+ if [ "x"__VIRTUAL_PROMPT__ != x ] ; then
|
|
|
99a46d |
+ PROMPT=__VIRTUAL_PROMPT__
|
|
|
99a46d |
+ PS1="(${PROMPT}) ${PS1-}"
|
|
|
99a46d |
else
|
|
|
99a46d |
PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"
|
|
|
99a46d |
fi
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/batch/__init__.py b/src/virtualenv/activation/batch/__init__.py
|
|
|
99a46d |
index 13ba097..b3e8540 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/batch/__init__.py
|
|
|
99a46d |
+++ b/src/virtualenv/activation/batch/__init__.py
|
|
|
99a46d |
@@ -13,6 +13,10 @@ class BatchActivator(ViaTemplateActivator):
|
|
|
99a46d |
yield "deactivate.bat"
|
|
|
99a46d |
yield "pydoc.bat"
|
|
|
99a46d |
|
|
|
99a46d |
+ @staticmethod
|
|
|
99a46d |
+ def quote(string):
|
|
|
99a46d |
+ return string
|
|
|
99a46d |
+
|
|
|
99a46d |
def instantiate_template(self, replacements, template, creator):
|
|
|
99a46d |
# ensure the text has all newlines as \r\n - required by batch
|
|
|
99a46d |
base = super().instantiate_template(replacements, template, creator)
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/cshell/activate.csh b/src/virtualenv/activation/cshell/activate.csh
|
|
|
99a46d |
index 837dcda..fcec426 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/cshell/activate.csh
|
|
|
99a46d |
+++ b/src/virtualenv/activation/cshell/activate.csh
|
|
|
99a46d |
@@ -10,15 +10,15 @@ alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PA
|
|
|
99a46d |
# Unset irrelevant variables.
|
|
|
99a46d |
deactivate nondestructive
|
|
|
99a46d |
|
|
|
99a46d |
-setenv VIRTUAL_ENV '__VIRTUAL_ENV__'
|
|
|
99a46d |
+setenv VIRTUAL_ENV __VIRTUAL_ENV__
|
|
|
99a46d |
|
|
|
99a46d |
set _OLD_VIRTUAL_PATH="$PATH:q"
|
|
|
99a46d |
-setenv PATH "$VIRTUAL_ENV:q/__BIN_NAME__:$PATH:q"
|
|
|
99a46d |
+setenv PATH "$VIRTUAL_ENV:q/"__BIN_NAME__":$PATH:q"
|
|
|
99a46d |
|
|
|
99a46d |
|
|
|
99a46d |
|
|
|
99a46d |
-if ('__VIRTUAL_PROMPT__' != "") then
|
|
|
99a46d |
- set env_name = '(__VIRTUAL_PROMPT__) '
|
|
|
99a46d |
+if (__VIRTUAL_PROMPT__ != "") then
|
|
|
99a46d |
+ set env_name = __VIRTUAL_PROMPT__
|
|
|
99a46d |
else
|
|
|
99a46d |
set env_name = '('"$VIRTUAL_ENV:t:q"') '
|
|
|
99a46d |
endif
|
|
|
99a46d |
@@ -42,7 +42,7 @@ if ( $do_prompt == "1" ) then
|
|
|
99a46d |
if ( "$prompt:q" =~ *"$newline:q"* ) then
|
|
|
99a46d |
:
|
|
|
99a46d |
else
|
|
|
99a46d |
- set prompt = "$env_name:q$prompt:q"
|
|
|
99a46d |
+ set prompt = '('"$env_name:q"') '"$prompt:q"
|
|
|
99a46d |
endif
|
|
|
99a46d |
endif
|
|
|
99a46d |
endif
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/fish/activate.fish b/src/virtualenv/activation/fish/activate.fish
|
|
|
99a46d |
index 62f631e..3637d55 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/fish/activate.fish
|
|
|
99a46d |
+++ b/src/virtualenv/activation/fish/activate.fish
|
|
|
99a46d |
@@ -57,7 +57,7 @@ end
|
|
|
99a46d |
# Unset irrelevant variables.
|
|
|
99a46d |
deactivate nondestructive
|
|
|
99a46d |
|
|
|
99a46d |
-set -gx VIRTUAL_ENV '__VIRTUAL_ENV__'
|
|
|
99a46d |
+set -gx VIRTUAL_ENV __VIRTUAL_ENV__
|
|
|
99a46d |
|
|
|
99a46d |
# https://github.com/fish-shell/fish-shell/issues/436 altered PATH handling
|
|
|
99a46d |
if test (echo $FISH_VERSION | head -c 1) -lt 3
|
|
|
99a46d |
@@ -65,7 +65,7 @@ if test (echo $FISH_VERSION | head -c 1) -lt 3
|
|
|
99a46d |
else
|
|
|
99a46d |
set -gx _OLD_VIRTUAL_PATH $PATH
|
|
|
99a46d |
end
|
|
|
99a46d |
-set -gx PATH "$VIRTUAL_ENV"'/__BIN_NAME__' $PATH
|
|
|
99a46d |
+set -gx PATH "$VIRTUAL_ENV"'/'__BIN_NAME__ $PATH
|
|
|
99a46d |
|
|
|
99a46d |
# Unset `$PYTHONHOME` if set.
|
|
|
99a46d |
if set -q PYTHONHOME
|
|
|
99a46d |
@@ -87,8 +87,8 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
|
|
|
99a46d |
|
|
|
99a46d |
# Prompt override provided?
|
|
|
99a46d |
# If not, just prepend the environment name.
|
|
|
99a46d |
- if test -n '__VIRTUAL_PROMPT__'
|
|
|
99a46d |
- printf '(%s) ' '__VIRTUAL_PROMPT__'
|
|
|
99a46d |
+ if test -n __VIRTUAL_PROMPT__
|
|
|
99a46d |
+ printf '(%s) ' __VIRTUAL_PROMPT__
|
|
|
99a46d |
else
|
|
|
99a46d |
printf '(%s) ' (basename "$VIRTUAL_ENV")
|
|
|
99a46d |
end
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/nushell/__init__.py b/src/virtualenv/activation/nushell/__init__.py
|
|
|
99a46d |
index 4e2ea77..c96af22 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/nushell/__init__.py
|
|
|
99a46d |
+++ b/src/virtualenv/activation/nushell/__init__.py
|
|
|
99a46d |
@@ -5,6 +5,25 @@ class NushellActivator(ViaTemplateActivator):
|
|
|
99a46d |
def templates(self):
|
|
|
99a46d |
yield "activate.nu"
|
|
|
99a46d |
|
|
|
99a46d |
+ @staticmethod
|
|
|
99a46d |
+ def quote(string):
|
|
|
99a46d |
+ """
|
|
|
99a46d |
+ Nushell supports raw strings like: r###'this is a string'###.
|
|
|
99a46d |
+
|
|
|
99a46d |
+ This method finds the maximum continuous sharps in the string and then
|
|
|
99a46d |
+ quote it with an extra sharp.
|
|
|
99a46d |
+ """
|
|
|
99a46d |
+ max_sharps = 0
|
|
|
99a46d |
+ current_sharps = 0
|
|
|
99a46d |
+ for char in string:
|
|
|
99a46d |
+ if char == "#":
|
|
|
99a46d |
+ current_sharps += 1
|
|
|
99a46d |
+ max_sharps = max(current_sharps, max_sharps)
|
|
|
99a46d |
+ else:
|
|
|
99a46d |
+ current_sharps = 0
|
|
|
99a46d |
+ wrapping = "#" * (max_sharps + 1)
|
|
|
99a46d |
+ return f"r{wrapping}'{string}'{wrapping}"
|
|
|
99a46d |
+
|
|
|
99a46d |
def replacements(self, creator, dest_folder): # noqa: U100
|
|
|
99a46d |
return {
|
|
|
99a46d |
"__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt,
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/nushell/activate.nu b/src/virtualenv/activation/nushell/activate.nu
|
|
|
99a46d |
index 3da1519..569a8a1 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/nushell/activate.nu
|
|
|
99a46d |
+++ b/src/virtualenv/activation/nushell/activate.nu
|
|
|
99a46d |
@@ -31,8 +31,8 @@ export-env {
|
|
|
99a46d |
}
|
|
|
99a46d |
|
|
|
99a46d |
let is_windows = ($nu.os-info.name | str downcase) == 'windows'
|
|
|
99a46d |
- let virtual_env = '__VIRTUAL_ENV__'
|
|
|
99a46d |
- let bin = '__BIN_NAME__'
|
|
|
99a46d |
+ let virtual_env = __VIRTUAL_ENV__
|
|
|
99a46d |
+ let bin = __BIN_NAME__
|
|
|
99a46d |
let path_sep = (char esep)
|
|
|
99a46d |
let path_name = (if $is_windows {
|
|
|
99a46d |
if (has-env 'Path') {
|
|
|
99a46d |
@@ -73,10 +73,10 @@ export-env {
|
|
|
99a46d |
$new_env
|
|
|
99a46d |
} else {
|
|
|
99a46d |
# Creating the new prompt for the session
|
|
|
99a46d |
- let virtual_prompt = (if ('__VIRTUAL_PROMPT__' == '') {
|
|
|
99a46d |
+ let virtual_prompt = (if (__VIRTUAL_PROMPT__ == '') {
|
|
|
99a46d |
$'(char lparen)($virtual_env | path basename)(char rparen) '
|
|
|
99a46d |
} else {
|
|
|
99a46d |
- '(__VIRTUAL_PROMPT__) '
|
|
|
99a46d |
+ (__VIRTUAL_PROMPT__)
|
|
|
99a46d |
})
|
|
|
99a46d |
|
|
|
99a46d |
# Back up the old prompt builder
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/powershell/__init__.py b/src/virtualenv/activation/powershell/__init__.py
|
|
|
99a46d |
index 4e74ecb..773d690 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/powershell/__init__.py
|
|
|
99a46d |
+++ b/src/virtualenv/activation/powershell/__init__.py
|
|
|
99a46d |
@@ -5,6 +5,18 @@ class PowerShellActivator(ViaTemplateActivator):
|
|
|
99a46d |
def templates(self):
|
|
|
99a46d |
yield "activate.ps1"
|
|
|
99a46d |
|
|
|
99a46d |
+ @staticmethod
|
|
|
99a46d |
+ def quote(string):
|
|
|
99a46d |
+ """
|
|
|
99a46d |
+ This should satisfy PowerShell quoting rules [1], unless the quoted
|
|
|
99a46d |
+ string is passed directly to Windows native commands [2].
|
|
|
99a46d |
+
|
|
|
99a46d |
+ [1]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules
|
|
|
99a46d |
+ [2]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing#passing-arguments-that-contain-quote-characters
|
|
|
99a46d |
+ """ # noqa: D205
|
|
|
99a46d |
+ string = string.replace("'", "''")
|
|
|
99a46d |
+ return f"'{string}'"
|
|
|
99a46d |
+
|
|
|
99a46d |
|
|
|
99a46d |
__all__ = [
|
|
|
99a46d |
"PowerShellActivator",
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/powershell/activate.ps1 b/src/virtualenv/activation/powershell/activate.ps1
|
|
|
99a46d |
index d524347..346980d 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/powershell/activate.ps1
|
|
|
99a46d |
+++ b/src/virtualenv/activation/powershell/activate.ps1
|
|
|
99a46d |
@@ -35,18 +35,18 @@ $env:VIRTUAL_ENV = $VIRTUAL_ENV
|
|
|
99a46d |
|
|
|
99a46d |
New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH
|
|
|
99a46d |
|
|
|
99a46d |
-$env:PATH = "$env:VIRTUAL_ENV/__BIN_NAME____PATH_SEP__" + $env:PATH
|
|
|
99a46d |
+$env:PATH = "$env:VIRTUAL_ENV/" + __BIN_NAME__ + __PATH_SEP__ + $env:PATH
|
|
|
99a46d |
if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) {
|
|
|
99a46d |
function global:_old_virtual_prompt {
|
|
|
99a46d |
""
|
|
|
99a46d |
}
|
|
|
99a46d |
$function:_old_virtual_prompt = $function:prompt
|
|
|
99a46d |
|
|
|
99a46d |
- if ("__VIRTUAL_PROMPT__" -ne "") {
|
|
|
99a46d |
+ if (__VIRTUAL_PROMPT__ -ne "") {
|
|
|
99a46d |
function global:prompt {
|
|
|
99a46d |
# Add the custom prefix to the existing prompt
|
|
|
99a46d |
$previous_prompt_value = & $function:_old_virtual_prompt
|
|
|
99a46d |
- ("(__VIRTUAL_PROMPT__) " + $previous_prompt_value)
|
|
|
99a46d |
+ ((__VIRTUAL_PROMPT__) + $previous_prompt_value)
|
|
|
99a46d |
}
|
|
|
99a46d |
}
|
|
|
99a46d |
else {
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/python/__init__.py b/src/virtualenv/activation/python/__init__.py
|
|
|
99a46d |
index a49444b..8f0971e 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/python/__init__.py
|
|
|
99a46d |
+++ b/src/virtualenv/activation/python/__init__.py
|
|
|
99a46d |
@@ -9,10 +9,14 @@ class PythonActivator(ViaTemplateActivator):
|
|
|
99a46d |
def templates(self):
|
|
|
99a46d |
yield "activate_this.py"
|
|
|
99a46d |
|
|
|
99a46d |
+ @staticmethod
|
|
|
99a46d |
+ def quote(string):
|
|
|
99a46d |
+ return repr(string)
|
|
|
99a46d |
+
|
|
|
99a46d |
def replacements(self, creator, dest_folder):
|
|
|
99a46d |
replacements = super().replacements(creator, dest_folder)
|
|
|
99a46d |
lib_folders = OrderedDict((os.path.relpath(str(i), str(dest_folder)), None) for i in creator.libs)
|
|
|
99a46d |
- lib_folders = os.pathsep.join(lib_folders.keys()).replace("\\", "\\\\") # escape Windows path characters
|
|
|
99a46d |
+ lib_folders = os.pathsep.join(lib_folders.keys())
|
|
|
99a46d |
win_py2 = creator.interpreter.platform == "win32" and creator.interpreter.version_info.major == 2
|
|
|
99a46d |
replacements.update(
|
|
|
99a46d |
{
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/python/activate_this.py b/src/virtualenv/activation/python/activate_this.py
|
|
|
99a46d |
index e8eeb84..976952e 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/python/activate_this.py
|
|
|
99a46d |
+++ b/src/virtualenv/activation/python/activate_this.py
|
|
|
99a46d |
@@ -14,7 +14,7 @@ except NameError:
|
|
|
99a46d |
raise AssertionError("You must use exec(open(this_file).read(), {'__file__': this_file}))")
|
|
|
99a46d |
|
|
|
99a46d |
bin_dir = os.path.dirname(abs_file)
|
|
|
99a46d |
-base = bin_dir[: -len("__BIN_NAME__") - 1] # strip away the bin part from the __file__, plus the path separator
|
|
|
99a46d |
+base = bin_dir[: -len(__BIN_NAME__) - 1] # strip away the bin part from the __file__, plus the path separator
|
|
|
99a46d |
|
|
|
99a46d |
# prepend bin to PATH (this file is inside the bin directory)
|
|
|
99a46d |
os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep))
|
|
|
99a46d |
@@ -22,9 +22,9 @@ os.environ["VIRTUAL_ENV"] = base # virtual env is right above bin directory
|
|
|
99a46d |
|
|
|
99a46d |
# add the virtual environments libraries to the host python import mechanism
|
|
|
99a46d |
prev_length = len(sys.path)
|
|
|
99a46d |
-for lib in "__LIB_FOLDERS__".split(os.pathsep):
|
|
|
99a46d |
+for lib in __LIB_FOLDERS__.split(os.pathsep):
|
|
|
99a46d |
path = os.path.realpath(os.path.join(bin_dir, lib))
|
|
|
99a46d |
- site.addsitedir(path.decode("utf-8") if "__DECODE_PATH__" else path)
|
|
|
99a46d |
+ site.addsitedir(path.decode("utf-8") if __DECODE_PATH__ else path)
|
|
|
99a46d |
sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]
|
|
|
99a46d |
|
|
|
99a46d |
sys.real_prefix = sys.prefix
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/via_template.py b/src/virtualenv/activation/via_template.py
|
|
|
99a46d |
index cc9dbda..b1dfa6b 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/via_template.py
|
|
|
99a46d |
+++ b/src/virtualenv/activation/via_template.py
|
|
|
99a46d |
@@ -1,4 +1,5 @@
|
|
|
99a46d |
import os
|
|
|
99a46d |
+import shlex
|
|
|
99a46d |
import sys
|
|
|
99a46d |
from abc import ABCMeta, abstractmethod
|
|
|
99a46d |
|
|
|
99a46d |
@@ -19,6 +20,16 @@ class ViaTemplateActivator(Activator, metaclass=ABCMeta):
|
|
|
99a46d |
def templates(self):
|
|
|
99a46d |
raise NotImplementedError
|
|
|
99a46d |
|
|
|
99a46d |
+ @staticmethod
|
|
|
99a46d |
+ def quote(string):
|
|
|
99a46d |
+ """
|
|
|
99a46d |
+ Quote strings in the activation script.
|
|
|
99a46d |
+
|
|
|
99a46d |
+ :param string: the string to quote
|
|
|
99a46d |
+ :return: quoted string that works in the activation script
|
|
|
99a46d |
+ """
|
|
|
99a46d |
+ return shlex.quote(string)
|
|
|
99a46d |
+
|
|
|
99a46d |
def generate(self, creator):
|
|
|
99a46d |
dest_folder = creator.bin_dir
|
|
|
99a46d |
replacements = self.replacements(creator, dest_folder)
|
|
|
99a46d |
@@ -58,8 +69,8 @@ class ViaTemplateActivator(Activator, metaclass=ABCMeta):
|
|
|
99a46d |
binary = read_binary(self.__module__, template)
|
|
|
99a46d |
text = binary.decode("utf-8", errors="strict")
|
|
|
99a46d |
for key, value in replacements.items():
|
|
|
99a46d |
- value = self._repr_unicode(creator, value)
|
|
|
99a46d |
- text = text.replace(key, value)
|
|
|
99a46d |
+ value_uni = self._repr_unicode(creator, value)
|
|
|
99a46d |
+ text = text.replace(key, self.quote(value_uni))
|
|
|
99a46d |
return text
|
|
|
99a46d |
|
|
|
99a46d |
@staticmethod
|
|
|
99a46d |
diff --git a/tests/conftest.py b/tests/conftest.py
|
|
|
99a46d |
index a7ec4e0..b2fae66 100644
|
|
|
99a46d |
--- a/tests/conftest.py
|
|
|
99a46d |
+++ b/tests/conftest.py
|
|
|
99a46d |
@@ -292,7 +292,11 @@ def is_inside_ci():
|
|
|
99a46d |
|
|
|
99a46d |
@pytest.fixture(scope="session")
|
|
|
99a46d |
def special_char_name():
|
|
|
99a46d |
- base = "e-$ Γ¨ΡΡπβδΈη-j"
|
|
|
99a46d |
+ base = "'\";&&e-$ Γ¨ΡΡπβδΈη-j"
|
|
|
99a46d |
+ if IS_WIN:
|
|
|
99a46d |
+ # get rid of invalid characters on Windows
|
|
|
99a46d |
+ base = base.replace('"', "")
|
|
|
99a46d |
+ base = base.replace(";", "")
|
|
|
99a46d |
# workaround for pypy3 https://bitbucket.org/pypy/pypy/issues/3147/venv-non-ascii-support-windows
|
|
|
99a46d |
encoding = "ascii" if IS_WIN else sys.getfilesystemencoding()
|
|
|
99a46d |
# let's not include characters that the file system cannot encode)
|
|
|
99a46d |
diff --git a/tests/unit/activation/conftest.py b/tests/unit/activation/conftest.py
|
|
|
99a46d |
index e24a4fc..2d8b3ce 100644
|
|
|
99a46d |
--- a/tests/unit/activation/conftest.py
|
|
|
99a46d |
+++ b/tests/unit/activation/conftest.py
|
|
|
99a46d |
@@ -4,7 +4,6 @@ import subprocess
|
|
|
99a46d |
import sys
|
|
|
99a46d |
from os.path import dirname, normcase
|
|
|
99a46d |
from pathlib import Path
|
|
|
99a46d |
-from shlex import quote
|
|
|
99a46d |
from subprocess import Popen
|
|
|
99a46d |
|
|
|
99a46d |
import pytest
|
|
|
99a46d |
@@ -146,7 +145,7 @@ class ActivationTester:
|
|
|
99a46d |
assert out[-1] == "None", raw
|
|
|
99a46d |
|
|
|
99a46d |
def quote(self, s):
|
|
|
99a46d |
- return quote(s)
|
|
|
99a46d |
+ return self.of_class.quote(s)
|
|
|
99a46d |
|
|
|
99a46d |
def python_cmd(self, cmd):
|
|
|
99a46d |
return f"{os.path.basename(sys.executable)} -c {self.quote(cmd)}"
|
|
|
99a46d |
diff --git a/tests/unit/activation/test_batch.py b/tests/unit/activation/test_batch.py
|
|
|
99a46d |
index 9e6d6d6..e95f7ea 100644
|
|
|
99a46d |
--- a/tests/unit/activation/test_batch.py
|
|
|
99a46d |
+++ b/tests/unit/activation/test_batch.py
|
|
|
99a46d |
@@ -1,5 +1,3 @@
|
|
|
99a46d |
-from shlex import quote
|
|
|
99a46d |
-
|
|
|
99a46d |
import pytest
|
|
|
99a46d |
|
|
|
99a46d |
from virtualenv.activation import BatchActivator
|
|
|
99a46d |
@@ -25,10 +23,12 @@ def test_batch(activation_tester_class, activation_tester, tmp_path):
|
|
|
99a46d |
return ["@echo off", "", "chcp 65001 1>NUL"] + super()._get_test_lines(activate_script)
|
|
|
99a46d |
|
|
|
99a46d |
def quote(self, s):
|
|
|
99a46d |
- """double quotes needs to be single, and single need to be double"""
|
|
|
99a46d |
- return "".join(("'" if c == '"' else ('"' if c == "'" else c)) for c in quote(s))
|
|
|
99a46d |
+ if '"' in s or " " in s:
|
|
|
99a46d |
+ text = s.replace('"', r"\"")
|
|
|
99a46d |
+ return f'"{text}"'
|
|
|
99a46d |
+ return s
|
|
|
99a46d |
|
|
|
99a46d |
def print_prompt(self):
|
|
|
99a46d |
- return "echo %PROMPT%"
|
|
|
99a46d |
+ return 'echo "%PROMPT%"'
|
|
|
99a46d |
|
|
|
99a46d |
activation_tester(Batch)
|
|
|
99a46d |
diff --git a/tests/unit/activation/test_powershell.py b/tests/unit/activation/test_powershell.py
|
|
|
99a46d |
index 761237f..f495353 100644
|
|
|
99a46d |
--- a/tests/unit/activation/test_powershell.py
|
|
|
99a46d |
+++ b/tests/unit/activation/test_powershell.py
|
|
|
99a46d |
@@ -1,5 +1,4 @@
|
|
|
99a46d |
import sys
|
|
|
99a46d |
-from shlex import quote
|
|
|
99a46d |
|
|
|
99a46d |
import pytest
|
|
|
99a46d |
|
|
|
99a46d |
@@ -19,10 +18,6 @@ def test_powershell(activation_tester_class, activation_tester, monkeypatch):
|
|
|
99a46d |
self.activate_cmd = "."
|
|
|
99a46d |
self.script_encoding = "utf-16"
|
|
|
99a46d |
|
|
|
99a46d |
- def quote(self, s):
|
|
|
99a46d |
- """powershell double quote needed for quotes within single quotes"""
|
|
|
99a46d |
- return quote(s).replace('"', '""')
|
|
|
99a46d |
-
|
|
|
99a46d |
def _get_test_lines(self, activate_script):
|
|
|
99a46d |
# for BATCH utf-8 support need change the character code page to 650001
|
|
|
99a46d |
return super()._get_test_lines(activate_script)
|
|
|
99a46d |
@@ -33,4 +28,19 @@ def test_powershell(activation_tester_class, activation_tester, monkeypatch):
|
|
|
99a46d |
def print_prompt(self):
|
|
|
99a46d |
return "prompt"
|
|
|
99a46d |
|
|
|
99a46d |
+ def quote(self, s):
|
|
|
99a46d |
+ """
|
|
|
99a46d |
+ Tester will pass strings to native commands on Windows so extra
|
|
|
99a46d |
+ parsing rules are used. Check `PowerShellActivator.quote` for more
|
|
|
99a46d |
+ details.
|
|
|
99a46d |
+ """
|
|
|
99a46d |
+ text = PowerShellActivator.quote(s)
|
|
|
99a46d |
+ return text.replace('"', '""') if sys.platform == "win32" else text
|
|
|
99a46d |
+
|
|
|
99a46d |
+ def activate_call(self, script):
|
|
|
99a46d |
+ # Commands are called without quotes in PowerShell
|
|
|
99a46d |
+ cmd = self.activate_cmd
|
|
|
99a46d |
+ scr = self.quote(str(script))
|
|
|
99a46d |
+ return f"{cmd} {scr}".strip()
|
|
|
99a46d |
+
|
|
|
99a46d |
activation_tester(PowerShell)
|
|
|
99a46d |
--
|
|
|
99a46d |
2.46.2
|
|
|
99a46d |
|