mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Add support for setup-ci with custom install dir
This commit is contained in:
parent
422963b38a
commit
1635bee387
6 changed files with 135 additions and 6 deletions
|
|
@ -4,13 +4,21 @@
|
||||||
|
|
||||||
# Function to remove shim from PATH (POSIX-compliant)
|
# Function to remove shim from PATH (POSIX-compliant)
|
||||||
remove_shim_from_path() {
|
remove_shim_from_path() {
|
||||||
echo "$PATH" | sed "s|$HOME/.safe-chain/shims:||g"
|
_safe_chain_shims="${SAFE_CHAIN_DIR:-$HOME/.safe-chain}/shims"
|
||||||
|
echo "$PATH" | sed "s|${_safe_chain_shims}:||g"
|
||||||
}
|
}
|
||||||
|
|
||||||
if command -v safe-chain >/dev/null 2>&1; then
|
if command -v safe-chain >/dev/null 2>&1; then
|
||||||
# Remove shim directory from PATH when calling {{AIKIDO_COMMAND}} to prevent infinite loops
|
# Remove shim directory from PATH when calling {{AIKIDO_COMMAND}} to prevent infinite loops
|
||||||
PATH=$(remove_shim_from_path) exec safe-chain {{PACKAGE_MANAGER}} "$@"
|
PATH=$(remove_shim_from_path) exec safe-chain {{PACKAGE_MANAGER}} "$@"
|
||||||
else
|
else
|
||||||
|
# safe-chain is not reachable — warn the user so they know protection is inactive
|
||||||
|
if [ -n "$SAFE_CHAIN_DIR" ]; then
|
||||||
|
printf "\033[43;30mWarning:\033[0m safe-chain is not accessible. Check that '%s/bin' is readable and executable by the current user.\n" "$SAFE_CHAIN_DIR" >&2
|
||||||
|
else
|
||||||
|
printf "\033[43;30mWarning:\033[0m safe-chain is not available to protect you from installing malware. {{PACKAGE_MANAGER}} will run without it.\n" >&2
|
||||||
|
fi
|
||||||
|
|
||||||
# Dynamically find original {{PACKAGE_MANAGER}} (excluding this shim directory)
|
# Dynamically find original {{PACKAGE_MANAGER}} (excluding this shim directory)
|
||||||
original_cmd=$(PATH=$(remove_shim_from_path) command -v {{PACKAGE_MANAGER}})
|
original_cmd=$(PATH=$(remove_shim_from_path) command -v {{PACKAGE_MANAGER}})
|
||||||
if [ -n "$original_cmd" ]; then
|
if [ -n "$original_cmd" ]; then
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
set -gx PATH $PATH $HOME/.safe-chain/bin
|
set -l safe_chain_base (if set -q SAFE_CHAIN_DIR; echo $SAFE_CHAIN_DIR; else; echo $HOME/.safe-chain; end)
|
||||||
|
set -gx PATH $PATH $safe_chain_base/bin
|
||||||
|
|
||||||
function npx
|
function npx
|
||||||
wrapSafeChainCommand "npx" $argv
|
wrapSafeChainCommand "npx" $argv
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
export PATH="$PATH:$HOME/.safe-chain/bin"
|
export PATH="$PATH:${SAFE_CHAIN_DIR:-$HOME/.safe-chain}/bin"
|
||||||
|
|
||||||
function npx() {
|
function npx() {
|
||||||
wrapSafeChainCommand "npx" "$@"
|
wrapSafeChainCommand "npx" "$@"
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,8 @@
|
||||||
# $IsWindows is only available in PowerShell Core 6.0+. If it doesn't exist, assume Windows PowerShell
|
# $IsWindows is only available in PowerShell Core 6.0+. If it doesn't exist, assume Windows PowerShell
|
||||||
$isWindowsPlatform = if (Test-Path variable:IsWindows) { $IsWindows } else { $true }
|
$isWindowsPlatform = if (Test-Path variable:IsWindows) { $IsWindows } else { $true }
|
||||||
$pathSeparator = if ($isWindowsPlatform) { ';' } else { ':' }
|
$pathSeparator = if ($isWindowsPlatform) { ';' } else { ':' }
|
||||||
$safeChainBin = Join-Path (Join-Path $HOME '.safe-chain') 'bin'
|
$safeChainBase = if ($env:SAFE_CHAIN_DIR) { $env:SAFE_CHAIN_DIR } else { Join-Path $HOME '.safe-chain' }
|
||||||
|
$safeChainBin = Join-Path $safeChainBase 'bin'
|
||||||
$env:PATH = "$env:PATH$pathSeparator$safeChainBin"
|
$env:PATH = "$env:PATH$pathSeparator$safeChainBin"
|
||||||
|
|
||||||
function npx {
|
function npx {
|
||||||
|
|
|
||||||
|
|
@ -84,10 +84,14 @@ export class DockerTestContainer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async openShell(shell) {
|
async openShell(shell, { user } = {}) {
|
||||||
|
const execArgs = user
|
||||||
|
? ["exec", "-it", "-u", user, this.containerName, shell]
|
||||||
|
: ["exec", "-it", this.containerName, shell];
|
||||||
|
|
||||||
let ptyProcess = pty.spawn(
|
let ptyProcess = pty.spawn(
|
||||||
"docker",
|
"docker",
|
||||||
["exec", "-it", this.containerName, shell],
|
execArgs,
|
||||||
{
|
{
|
||||||
name: "xterm-color",
|
name: "xterm-color",
|
||||||
cols: 80,
|
cols: 80,
|
||||||
|
|
|
||||||
115
test/e2e/safe-chain-dir.e2e.spec.js
Normal file
115
test/e2e/safe-chain-dir.e2e.spec.js
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
import { describe, it, before, beforeEach, afterEach } from "node:test";
|
||||||
|
import { DockerTestContainer } from "./DockerTestContainer.js";
|
||||||
|
import assert from "node:assert";
|
||||||
|
|
||||||
|
const CUSTOM_DIR = "/usr/local/.safe-chain";
|
||||||
|
|
||||||
|
describe("E2E: SAFE_CHAIN_DIR support", () => {
|
||||||
|
let container;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
DockerTestContainer.buildImage();
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
container = new DockerTestContainer();
|
||||||
|
await container.start();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
if (container) {
|
||||||
|
await container.stop();
|
||||||
|
container = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("setup-ci installs shims in the custom directory when SAFE_CHAIN_DIR is set", async () => {
|
||||||
|
const shell = await container.openShell("bash");
|
||||||
|
await shell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
|
||||||
|
await shell.runCommand("safe-chain setup-ci");
|
||||||
|
|
||||||
|
// Shims should be in the custom dir
|
||||||
|
const customShimResult = await shell.runCommand(
|
||||||
|
`test -f ${CUSTOM_DIR}/shims/npm && echo "EXISTS"`
|
||||||
|
);
|
||||||
|
assert.ok(
|
||||||
|
customShimResult.output.includes("EXISTS"),
|
||||||
|
`Expected npm shim at ${CUSTOM_DIR}/shims/npm. Output:\n${customShimResult.output}`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Default location should NOT have been created
|
||||||
|
const defaultShimResult = await shell.runCommand(
|
||||||
|
`test -d $HOME/.safe-chain/shims && echo "EXISTS" || echo "ABSENT"`
|
||||||
|
);
|
||||||
|
assert.ok(
|
||||||
|
defaultShimResult.output.includes("ABSENT"),
|
||||||
|
`Expected default shims dir to be absent. Output:\n${defaultShimResult.output}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("setup-ci writes the custom directory path to GITHUB_PATH when SAFE_CHAIN_DIR is set", async () => {
|
||||||
|
const shell = await container.openShell("bash");
|
||||||
|
await shell.runCommand("export GITHUB_PATH=/tmp/github_path");
|
||||||
|
await shell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
|
||||||
|
await shell.runCommand("safe-chain setup-ci");
|
||||||
|
|
||||||
|
const result = await shell.runCommand("cat /tmp/github_path");
|
||||||
|
assert.ok(
|
||||||
|
result.output.includes(`${CUSTOM_DIR}/shims`),
|
||||||
|
`Expected GITHUB_PATH to contain custom shims dir. Output:\n${result.output}`
|
||||||
|
);
|
||||||
|
assert.ok(
|
||||||
|
result.output.includes(`${CUSTOM_DIR}/bin`),
|
||||||
|
`Expected GITHUB_PATH to contain custom bin dir. Output:\n${result.output}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("safe-chain protects a non-root user when installed to a shared dir with SAFE_CHAIN_DIR", async () => {
|
||||||
|
// Step 1: create a non-root user inside the container
|
||||||
|
container.dockerExec("useradd -m safeuser");
|
||||||
|
|
||||||
|
// Step 2: as root, run setup-ci with the shared SAFE_CHAIN_DIR
|
||||||
|
const rootShell = await container.openShell("bash");
|
||||||
|
await rootShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
|
||||||
|
await rootShell.runCommand("safe-chain setup-ci");
|
||||||
|
|
||||||
|
// Step 3: simulate what install-safe-chain.sh does — place the safe-chain binary
|
||||||
|
// in SAFE_CHAIN_DIR/bin. In Docker tests safe-chain is installed via npm/Volta,
|
||||||
|
// so we symlink it there.
|
||||||
|
container.dockerExec(`mkdir -p ${CUSTOM_DIR}/bin`);
|
||||||
|
container.dockerExec(
|
||||||
|
`ln -sf \\$(which safe-chain) ${CUSTOM_DIR}/bin/safe-chain`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Step 4: make npm accessible to all users (in real Dockerfiles npm is installed
|
||||||
|
// before the user switch; here Volta manages it for root, so we symlink it).
|
||||||
|
container.dockerExec("ln -sf \\$(which npm) /usr/local/bin/npm");
|
||||||
|
|
||||||
|
// Step 5: make the shared safe-chain dir readable + executable by all users
|
||||||
|
container.dockerExec(`chmod -R a+rx ${CUSTOM_DIR}`);
|
||||||
|
|
||||||
|
// Step 6: Volta installs under /root/.volta which is only accessible to root by
|
||||||
|
// default. /root/ itself is mode 700, so safeuser can't traverse into it even
|
||||||
|
// if .volta/ is world-readable. Fix both levels. Safe in a throw-away container.
|
||||||
|
container.dockerExec("chmod a+x /root && chmod -R a+rX /root/.volta");
|
||||||
|
|
||||||
|
// Step 7: as the non-root user, set SAFE_CHAIN_DIR and PATH, then run npm.
|
||||||
|
// SAFE_CHAIN_DIR must be set so the shim knows which dir to strip from PATH
|
||||||
|
// when invoking the real npm (prevents infinite loop).
|
||||||
|
const userShell = await container.openShell("bash", { user: "safeuser" });
|
||||||
|
await userShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
|
||||||
|
// Reuse root's Volta dir so safeuser doesn't trigger a slow first-run setup
|
||||||
|
await userShell.runCommand("export VOLTA_HOME=/root/.volta");
|
||||||
|
await userShell.runCommand(
|
||||||
|
`export PATH="${CUSTOM_DIR}/shims:${CUSTOM_DIR}/bin:$PATH"`
|
||||||
|
);
|
||||||
|
const result = await userShell.runCommand(
|
||||||
|
"npm i axios@1.13.0 --safe-chain-logging=verbose"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.ok(
|
||||||
|
result.output.includes("Safe-chain: Scanned"),
|
||||||
|
`Expected safe-chain to protect non-root user. Output:\n${result.output}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue