Only install for install/download and wheel commands

This commit is contained in:
Reinier Criel 2025-11-10 15:36:56 -08:00
parent a502e4e49d
commit 6cc3ffc044

View file

@ -3,6 +3,26 @@ import { safeSpawn } from "../../utils/safeSpawn.js";
import { mergeSafeChainProxyEnvironmentVariables } from "../../registryProxy/registryProxy.js"; import { mergeSafeChainProxyEnvironmentVariables } from "../../registryProxy/registryProxy.js";
import { installSafeChainCA } from "../../registryProxy/certUtils.js"; import { installSafeChainCA } from "../../registryProxy/certUtils.js";
/**
* Returns true if the pip command needs the Safe-chain CA installed.
* @param {string[]} args
* @returns {boolean}
*/
function needsCaInstalled(args) {
const known = new Set(["install", "wheel", "download"]);
let startIdx = 0;
if (args[0] === "-m" && (args[1] === "pip" || args[1] === "pip3")) {
startIdx = 2;
}
for (let i = startIdx; i < args.length; i++) {
const token = args[i];
if (!token) continue;
if (token.startsWith("-")) continue; // skip flags
if (known.has(token)) return true;
}
return false;
}
/** /**
* @param {string} command * @param {string} command
* @param {string[]} args * @param {string[]} args
@ -11,9 +31,11 @@ import { installSafeChainCA } from "../../registryProxy/certUtils.js";
*/ */
export async function runPip(command, args) { export async function runPip(command, args) {
try { try {
// Install Safe Chain CA in OS trust store before running pip // Only install CA for commands that download or build packages.
// Py 3.14 requires that certs are properly installed in the OS trust store // This minimizes privilege prompts for read-only operations like 'list' or 'show'.
if (needsCaInstalled(args)) {
await installSafeChainCA(); await installSafeChainCA();
}
const env = mergeSafeChainProxyEnvironmentVariables(process.env); const env = mergeSafeChainProxyEnvironmentVariables(process.env);
const result = await safeSpawn(command, args, { const result = await safeSpawn(command, args, {
stdio: "inherit", stdio: "inherit",