mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
167 lines
6 KiB
JavaScript
167 lines
6 KiB
JavaScript
import { describe, it, before, beforeEach, afterEach } from "node:test";
|
|
import { DockerTestContainer } from "./DockerTestContainer.js";
|
|
import assert from "node:assert";
|
|
|
|
describe("E2E: yarn coverage", () => {
|
|
let container;
|
|
|
|
before(async () => {
|
|
DockerTestContainer.buildImage();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
// Run a new Docker container for each test
|
|
container = new DockerTestContainer();
|
|
await container.start();
|
|
|
|
const installationShell = await container.openShell("zsh");
|
|
await installationShell.runCommand("safe-chain setup");
|
|
});
|
|
|
|
afterEach(async () => {
|
|
// Stop and clean up the container after each test
|
|
if (container) {
|
|
await container.stop();
|
|
container = null;
|
|
}
|
|
});
|
|
|
|
it(`safe-chain succesfully installs safe packages`, async () => {
|
|
const shell = await container.openShell("zsh");
|
|
const result = await shell.runCommand(
|
|
"yarn add axios@1.13.0 --safe-chain-logging=verbose"
|
|
);
|
|
|
|
assert.ok(
|
|
result.output.includes("no malware found."),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
});
|
|
|
|
it(`safe-chain blocks installation of malicious packages`, async () => {
|
|
const shell = await container.openShell("zsh");
|
|
const result = await shell.runCommand("yarn add safe-chain-test");
|
|
|
|
assert.ok(
|
|
result.output.includes("Malicious changes detected:"),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
assert.ok(
|
|
result.output.includes("- safe-chain-test"),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
assert.ok(
|
|
result.output.includes("Exiting without installing malicious packages."),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
|
|
const listResult = await shell.runCommand("yarn list");
|
|
assert.ok(
|
|
!listResult.output.includes("safe-chain-test"),
|
|
`Malicious package was installed despite safe-chain protection. Output of 'yarn list' was:\n${listResult.output}`
|
|
);
|
|
});
|
|
|
|
it(`safe-chain blocks download of malicious packages already in package.json`, async () => {
|
|
const shell = await container.openShell("zsh");
|
|
await shell.runCommand(
|
|
'echo \'{"name":"test-project","version":"1.0.0","dependencies":{"safe-chain-test":"0.0.1-security"}}\' > package.json'
|
|
);
|
|
|
|
var result = await shell.runCommand("yarn");
|
|
|
|
assert.ok(
|
|
result.output.includes("blocked 1 malicious package downloads"),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
assert.ok(
|
|
result.output.includes("- safe-chain-test"),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
assert.ok(
|
|
result.output.includes("Exiting without installing malicious packages."),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
});
|
|
|
|
it(`safe-chain blocks installation of malicious packages`, async () => {
|
|
const shell = await container.openShell("zsh");
|
|
const result = await shell.runCommand("yarn add safe-chain-test");
|
|
|
|
assert.ok(
|
|
result.output.includes("Malicious changes detected:"),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
assert.ok(
|
|
result.output.includes("- safe-chain-test"),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
assert.ok(
|
|
result.output.includes("Exiting without installing malicious packages."),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
|
|
const listResult = await shell.runCommand("yarn list");
|
|
assert.ok(
|
|
!listResult.output.includes("safe-chain-test"),
|
|
`Malicious package was installed despite safe-chain protection. Output of 'yarn list' was:\n${listResult.output}`
|
|
);
|
|
});
|
|
|
|
it("safe-chain blocks yarn dlx from executing malicious packages", async () => {
|
|
const shell = await container.openShell("zsh");
|
|
const result = await shell.runCommand("yarn dlx safe-chain-test");
|
|
|
|
assert.ok(
|
|
result.output.includes("Malicious changes detected:"),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
assert.ok(
|
|
result.output.includes("- safe-chain-test"),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
assert.ok(
|
|
result.output.includes("Exiting without installing malicious packages."),
|
|
`Output did not include expected text. Output was:\n${result.output}`
|
|
);
|
|
});
|
|
|
|
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
|
|
const CUSTOM_DIR = "/usr/local/.safe-chain";
|
|
let customContainer;
|
|
|
|
beforeEach(async () => {
|
|
customContainer = new DockerTestContainer();
|
|
await customContainer.start();
|
|
|
|
// Run setup with the custom dir — init-posix.sh is copied to the custom
|
|
// scripts dir, and ~/.zshrc gets a source line pointing there
|
|
const setupShell = await customContainer.openShell("zsh");
|
|
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
|
|
await setupShell.runCommand("safe-chain setup");
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (customContainer) {
|
|
await customContainer.stop();
|
|
customContainer = null;
|
|
}
|
|
});
|
|
|
|
it("blocks malicious yarn packages when scripts are in a custom directory", async () => {
|
|
// New shell sources ~/.zshrc → sources init-posix.sh from custom dir
|
|
// → defines yarn() shell function that routes through safe-chain
|
|
const shell = await customContainer.openShell("zsh");
|
|
const result = await shell.runCommand("yarn add safe-chain-test");
|
|
|
|
assert.ok(
|
|
result.output.includes("Malicious changes detected:"),
|
|
`Expected malicious package to be blocked. Output:\n${result.output}`
|
|
);
|
|
assert.ok(
|
|
result.output.includes("Exiting without installing malicious packages."),
|
|
`Expected malicious package to be blocked. Output:\n${result.output}`
|
|
);
|
|
});
|
|
});
|
|
});
|