Skip to content

Commit 7c153fb

Browse files
Copilotbytemain
andauthored
Harden PowerShell uv-build pip invocation
Agent-Logs-Url: https://github.com/version-fox/vfox-python/sessions/62cb80ef-ebad-4aa5-8791-70cc74b8efc5 Co-authored-by: bytemain <13938334+bytemain@users.noreply.github.com>
1 parent 55bd1a7 commit 7c153fb

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

lib/util.lua

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,32 @@ local function shellQuote(value)
259259
return "'" .. string.gsub(value, "'", "'\\''") .. "'"
260260
end
261261

262-
local function powerShellCommand(script, args)
263-
local command = "powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command " .. shellQuote(script)
264-
for _, arg in ipairs(args) do
265-
command = command .. " " .. shellQuote(arg)
262+
local function powerShellQuote(value)
263+
if string.find(value, "[\r\n%z]") then
264+
error("PowerShell argument contains unsupported control character: " .. value)
265+
end
266+
if containsTraversalSegment(value) then
267+
error("PowerShell argument contains unsupported traversal segment: " .. value)
268+
end
269+
if string.find(value, '"', 1, true) then
270+
error("PowerShell argument contains unsupported quote character: " .. value)
266271
end
272+
return "'" .. string.gsub(value, "'", "''") .. "'"
273+
end
274+
275+
local function powerShellCommand(script)
276+
local command = "powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command " .. shellQuote(script)
267277
return command
268278
end
269279

280+
local function powerShellPythonCommand(pythonExe, pythonArgs)
281+
local script = "& " .. powerShellQuote(pythonExe)
282+
for _, arg in ipairs(pythonArgs) do
283+
script = script .. " " .. powerShellQuote(arg)
284+
end
285+
return powerShellCommand(script)
286+
end
287+
270288
local function startsWith(value, prefix)
271289
return string.sub(value, 1, string.len(prefix)) == prefix
272290
end
@@ -556,7 +574,7 @@ local function ensureWindowsUvBuildPip(path)
556574
end
557575

558576
print("Installing pip for uv-build Python on Windows...")
559-
local command = powerShellCommand("& { & $args[0] -E -s -m ensurepip -U --default-pip }", { pythonExe })
577+
local command = powerShellPythonCommand(pythonExe, { "-E", "-s", "-m", "ensurepip", "-U", "--default-pip" })
560578
local exitCode = os.execute(command)
561579
if not commandSucceeded(exitCode) then
562580
error("ensurepip failed while installing pip. Exit code: " .. tostring(exitCode))
@@ -566,8 +584,10 @@ local function ensureWindowsUvBuildPip(path)
566584
return
567585
end
568586

569-
command = powerShellCommand("& { & $args[0] -E -s -m pip install --force-reinstall --no-index --find-links $args[1] pip }",
570-
{ pythonExe, path .. "\\Lib\\ensurepip\\_bundled" })
587+
command = powerShellPythonCommand(pythonExe, {
588+
"-E", "-s", "-m", "pip", "install", "--force-reinstall", "--no-index",
589+
"--find-links", path .. "\\Lib\\ensurepip\\_bundled", "pip"
590+
})
571591
exitCode = os.execute(command)
572592
if not commandSucceeded(exitCode) then
573593
error("pip force-reinstall failed while creating pip scripts. Exit code: " .. tostring(exitCode))

0 commit comments

Comments
 (0)