mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Only write to stdout when safe-chain audited packages
This commit is contained in:
parent
18f30ac66e
commit
e4c40330f7
3 changed files with 225 additions and 6 deletions
|
|
@ -6,6 +6,7 @@ import { getPackageManager } from "./packagemanager/currentPackageManager.js";
|
||||||
import { initializeCliArguments } from "./config/cliArguments.js";
|
import { initializeCliArguments } from "./config/cliArguments.js";
|
||||||
import { createSafeChainProxy } from "./registryProxy/registryProxy.js";
|
import { createSafeChainProxy } from "./registryProxy/registryProxy.js";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
|
import { getAuditStats } from "./scanning/audit/index.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string[]} args
|
* @param {string[]} args
|
||||||
|
|
@ -61,12 +62,15 @@ export async function main(args) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.emptyLine();
|
const auditStats = getAuditStats();
|
||||||
ui.writeInformation(
|
if (auditStats.verifiedPackages > 0) {
|
||||||
`${chalk.green(
|
ui.emptyLine();
|
||||||
"✔"
|
ui.writeInformation(
|
||||||
)} Safe-chain: Command completed, no malicious packages found.`
|
`${chalk.green("✔")} Safe-chain: Scanned ${
|
||||||
);
|
auditStats.verifiedPackages
|
||||||
|
} packages, no malware found.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Returning the exit code back to the caller allows the promise
|
// Returning the exit code back to the caller allows the promise
|
||||||
// to be awaited in the bin files and return the correct exit code
|
// to be awaited in the bin files and return the correct exit code
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,29 @@ import {
|
||||||
* @property {boolean} isAllowed
|
* @property {boolean} isAllowed
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} AuditStats
|
||||||
|
* @property {number} verifiedPackages
|
||||||
|
* @property {number} safePackages
|
||||||
|
* @property {number} malwarePackages
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type AuditStats
|
||||||
|
*/
|
||||||
|
const auditStats = {
|
||||||
|
verifiedPackages: 0,
|
||||||
|
safePackages: 0,
|
||||||
|
malwarePackages: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {AuditStats}
|
||||||
|
*/
|
||||||
|
export function getAuditStats() {
|
||||||
|
return auditStats;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {PackageChange[]} changes
|
* @param {PackageChange[]} changes
|
||||||
*
|
*
|
||||||
|
|
@ -41,16 +64,20 @@ export async function auditChanges(changes) {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (malwarePackage) {
|
if (malwarePackage) {
|
||||||
|
auditStats.malwarePackages += 1;
|
||||||
ui.writeVerbose(
|
ui.writeVerbose(
|
||||||
`Safe-chain: Package ${change.name}@${change.version} is marked as malware: ${malwarePackage.status}`
|
`Safe-chain: Package ${change.name}@${change.version} is marked as malware: ${malwarePackage.status}`
|
||||||
);
|
);
|
||||||
disallowedChanges.push({ ...change, reason: malwarePackage.status });
|
disallowedChanges.push({ ...change, reason: malwarePackage.status });
|
||||||
} else {
|
} else {
|
||||||
|
auditStats.safePackages += 1;
|
||||||
ui.writeVerbose(
|
ui.writeVerbose(
|
||||||
`Safe-chain: Package ${change.name}@${change.version} is clean`
|
`Safe-chain: Package ${change.name}@${change.version} is clean`
|
||||||
);
|
);
|
||||||
allowedChanges.push(change);
|
allowedChanges.push(change);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auditStats.verifiedPackages += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auditResults = {
|
const auditResults = {
|
||||||
|
|
|
||||||
188
packages/safe-chain/src/scanning/audit/index.spec.js
Normal file
188
packages/safe-chain/src/scanning/audit/index.spec.js
Normal file
|
|
@ -0,0 +1,188 @@
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { describe, it, mock, beforeEach } from "node:test";
|
||||||
|
|
||||||
|
describe("audit/index", async () => {
|
||||||
|
const mockWriteVerbose = mock.fn();
|
||||||
|
|
||||||
|
// Mock UI module
|
||||||
|
mock.module("../../environment/userInteraction.js", {
|
||||||
|
namedExports: {
|
||||||
|
ui: {
|
||||||
|
writeVerbose: mockWriteVerbose,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mock malware database
|
||||||
|
const mockIsMalware = mock.fn();
|
||||||
|
mock.module("../malwareDatabase.js", {
|
||||||
|
namedExports: {
|
||||||
|
MALWARE_STATUS_MALWARE: "malware",
|
||||||
|
openMalwareDatabase: async () => ({
|
||||||
|
isMalware: mockIsMalware,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const { auditChanges, getAuditStats } = await import("./index.js");
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
mockWriteVerbose.mock.resetCalls();
|
||||||
|
mockIsMalware.mock.resetCalls();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("getAuditStats", () => {
|
||||||
|
it("should return audit stats object with correct structure", () => {
|
||||||
|
const stats = getAuditStats();
|
||||||
|
|
||||||
|
assert.ok(stats.hasOwnProperty("verifiedPackages"));
|
||||||
|
assert.ok(stats.hasOwnProperty("safePackages"));
|
||||||
|
assert.ok(stats.hasOwnProperty("malwarePackages"));
|
||||||
|
assert.equal(typeof stats.verifiedPackages, "number");
|
||||||
|
assert.equal(typeof stats.safePackages, "number");
|
||||||
|
assert.equal(typeof stats.malwarePackages, "number");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return the same object reference on multiple calls", () => {
|
||||||
|
const stats1 = getAuditStats();
|
||||||
|
const stats2 = getAuditStats();
|
||||||
|
|
||||||
|
assert.equal(stats1, stats2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("auditChanges", () => {
|
||||||
|
it("should return empty allowed and disallowed arrays when no changes provided", async () => {
|
||||||
|
const result = await auditChanges([]);
|
||||||
|
|
||||||
|
assert.deepEqual(result.allowedChanges, []);
|
||||||
|
assert.deepEqual(result.disallowedChanges, []);
|
||||||
|
assert.equal(result.isAllowed, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should mark package as allowed when not malware", async () => {
|
||||||
|
mockIsMalware.mock.mockImplementation(() => false);
|
||||||
|
|
||||||
|
const changes = [{ name: "lodash", version: "4.17.21", type: "add" }];
|
||||||
|
const result = await auditChanges(changes);
|
||||||
|
|
||||||
|
assert.equal(result.allowedChanges.length, 1);
|
||||||
|
assert.equal(result.disallowedChanges.length, 0);
|
||||||
|
assert.equal(result.isAllowed, true);
|
||||||
|
assert.deepEqual(result.allowedChanges[0], changes[0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should mark package as disallowed when malware detected", async () => {
|
||||||
|
mockIsMalware.mock.mockImplementation(() => true);
|
||||||
|
|
||||||
|
const changes = [
|
||||||
|
{ name: "malicious-pkg", version: "1.0.0", type: "add" },
|
||||||
|
];
|
||||||
|
const result = await auditChanges(changes);
|
||||||
|
|
||||||
|
assert.equal(result.allowedChanges.length, 0);
|
||||||
|
assert.equal(result.disallowedChanges.length, 1);
|
||||||
|
assert.equal(result.isAllowed, false);
|
||||||
|
assert.equal(result.disallowedChanges[0].name, "malicious-pkg");
|
||||||
|
assert.equal(result.disallowedChanges[0].version, "1.0.0");
|
||||||
|
assert.equal(result.disallowedChanges[0].reason, "malware");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle mixed safe and malware packages", async () => {
|
||||||
|
mockIsMalware.mock.mockImplementation((name) => {
|
||||||
|
return name === "malicious-pkg";
|
||||||
|
});
|
||||||
|
|
||||||
|
const changes = [
|
||||||
|
{ name: "lodash", version: "4.17.21", type: "add" },
|
||||||
|
{ name: "malicious-pkg", version: "1.0.0", type: "add" },
|
||||||
|
{ name: "express", version: "4.18.0", type: "add" },
|
||||||
|
];
|
||||||
|
const result = await auditChanges(changes);
|
||||||
|
|
||||||
|
assert.equal(result.allowedChanges.length, 2);
|
||||||
|
assert.equal(result.disallowedChanges.length, 1);
|
||||||
|
assert.equal(result.isAllowed, false);
|
||||||
|
assert.equal(result.disallowedChanges[0].name, "malicious-pkg");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should only check malware for add and change types", async () => {
|
||||||
|
mockIsMalware.mock.mockImplementation(() => false);
|
||||||
|
|
||||||
|
const changes = [
|
||||||
|
{ name: "pkg1", version: "1.0.0", type: "add" },
|
||||||
|
{ name: "pkg2", version: "2.0.0", type: "change" },
|
||||||
|
{ name: "pkg3", version: "3.0.0", type: "remove" },
|
||||||
|
];
|
||||||
|
await auditChanges(changes);
|
||||||
|
|
||||||
|
// Should only check pkg1 and pkg2, not pkg3 (remove type)
|
||||||
|
assert.equal(mockIsMalware.mock.calls.length, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should increment verifiedPackages counter for each package", async () => {
|
||||||
|
mockIsMalware.mock.mockImplementation(() => false);
|
||||||
|
|
||||||
|
const statsBefore = getAuditStats();
|
||||||
|
const initialCount = statsBefore.verifiedPackages;
|
||||||
|
|
||||||
|
const changes = [
|
||||||
|
{ name: "pkg1", version: "1.0.0", type: "add" },
|
||||||
|
{ name: "pkg2", version: "2.0.0", type: "add" },
|
||||||
|
{ name: "pkg3", version: "3.0.0", type: "add" },
|
||||||
|
];
|
||||||
|
await auditChanges(changes);
|
||||||
|
|
||||||
|
const statsAfter = getAuditStats();
|
||||||
|
assert.equal(statsAfter.verifiedPackages, initialCount + 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should increment safePackages counter for safe packages", async () => {
|
||||||
|
mockIsMalware.mock.mockImplementation(() => false);
|
||||||
|
|
||||||
|
const statsBefore = getAuditStats();
|
||||||
|
const initialCount = statsBefore.safePackages;
|
||||||
|
|
||||||
|
const changes = [
|
||||||
|
{ name: "lodash", version: "4.17.21", type: "add" },
|
||||||
|
{ name: "express", version: "4.18.0", type: "add" },
|
||||||
|
];
|
||||||
|
await auditChanges(changes);
|
||||||
|
|
||||||
|
const statsAfter = getAuditStats();
|
||||||
|
assert.equal(statsAfter.safePackages, initialCount + 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should increment malwarePackages counter for malware packages", async () => {
|
||||||
|
mockIsMalware.mock.mockImplementation(() => true);
|
||||||
|
|
||||||
|
const statsBefore = getAuditStats();
|
||||||
|
const initialCount = statsBefore.malwarePackages;
|
||||||
|
|
||||||
|
const changes = [
|
||||||
|
{ name: "malicious-1", version: "1.0.0", type: "add" },
|
||||||
|
{ name: "malicious-2", version: "2.0.0", type: "add" },
|
||||||
|
];
|
||||||
|
await auditChanges(changes);
|
||||||
|
|
||||||
|
const statsAfter = getAuditStats();
|
||||||
|
assert.equal(statsAfter.malwarePackages, initialCount + 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should accumulate stats across multiple auditChanges calls", async () => {
|
||||||
|
mockIsMalware.mock.mockImplementation(() => false);
|
||||||
|
|
||||||
|
const statsBefore = getAuditStats();
|
||||||
|
const initialVerified = statsBefore.verifiedPackages;
|
||||||
|
|
||||||
|
// First call
|
||||||
|
await auditChanges([{ name: "pkg1", version: "1.0.0", type: "add" }]);
|
||||||
|
|
||||||
|
// Second call
|
||||||
|
await auditChanges([{ name: "pkg2", version: "2.0.0", type: "add" }]);
|
||||||
|
|
||||||
|
const statsAfter = getAuditStats();
|
||||||
|
assert.equal(statsAfter.verifiedPackages, initialVerified + 2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue