Wrap bun with safe-chain to block downloads of packages with malware

This commit is contained in:
Sander Declerck 2025-10-08 15:12:06 +02:00
parent d737abd24a
commit b08b4e2d4e
No known key found for this signature in database
11 changed files with 181 additions and 2 deletions

View file

@ -0,0 +1,42 @@
import { ui } from "../../environment/userInteraction.js";
import { safeSpawn } from "../../utils/safeSpawn.js";
import { mergeSafeChainProxyEnvironmentVariables } from "../../registryProxy/registryProxy.js";
export function createBunPackageManager() {
return {
runCommand: (args) => runBunCommand("bun", args),
// For bun, we use the proxy-only approach to block package downloads,
// so we don't need to analyze commands.
isSupportedCommand: () => false,
getDependencyUpdatesForCommand: () => [],
};
}
export function createBunxPackageManager() {
return {
runCommand: (args) => runBunCommand("bunx", args),
// For bunx, we use the proxy-only approach to block package downloads,
// so we don't need to analyze commands.
isSupportedCommand: () => false,
getDependencyUpdatesForCommand: () => [],
};
}
async function runBunCommand(command, args) {
try {
const result = await safeSpawn(command, args, {
stdio: "inherit",
env: mergeSafeChainProxyEnvironmentVariables(process.env),
});
return { status: result.status };
} catch (error) {
if (error.status) {
return { status: error.status };
} else {
ui.writeError("Error executing command:", error.message);
return { status: 1 };
}
}
}

View file

@ -1,3 +1,7 @@
import {
createBunPackageManager,
createBunxPackageManager,
} from "./bun/createBunPackageManager.js";
import { createNpmPackageManager } from "./npm/createPackageManager.js";
import { createNpxPackageManager } from "./npx/createPackageManager.js";
import {
@ -21,6 +25,10 @@ export function initializePackageManager(packageManagerName, version) {
state.packageManagerName = createPnpmPackageManager();
} else if (packageManagerName === "pnpx") {
state.packageManagerName = createPnpxPackageManager();
} else if (packageManagerName === "bun") {
state.packageManagerName = createBunPackageManager();
} else if (packageManagerName === "bunx") {
state.packageManagerName = createBunxPackageManager();
} else {
throw new Error("Unsupported package manager: " + packageManagerName);
}