Skip to content
38 changes: 24 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 18 additions & 11 deletions scripts/advisory-early-warning-scan.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//
// CLI entry for the advisory early-warning path (#7338). Correlates public
// GitHub Security Advisory JSON with the reviewed npm inventory derived from
// ci/reviewed-npm-audit.json (committed package specs plus the locked-graph
// package-locks) and prints structured, NON-blocking signals. Signals never
// ci/reviewed-npm-audit.json (committed package specs plus the root and
// locked-graph production package-locks) and prints structured, NON-blocking signals. Signals never
// fail the process: enforcement stays with the reviewed npm audit gate.
//
// Usage:
Expand All @@ -17,7 +17,7 @@
// --inventory replaces the repo-derived reviewed inventory with an explicit
// JSON array of {name, version[, origin]} entries, so offline callers and
// tests can run hermetically; without it (the workflow default) the inventory
// is built from ci/reviewed-npm-audit.json and the locked-graph package-locks.
// is built from ci/reviewed-npm-audit.json and the production package-locks.
//
// --nvd-records attaches supplementary NVD reconciliations from a file of
// previously fetched NVD 2.0 API responses. The CLI itself never performs
Expand All @@ -36,21 +36,28 @@ import { attachNvdReconciliations, type NvdAnnotatedSignal } from "./lib/nvd-rec

const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const CONFIG_RELATIVE_PATH = path.join("ci", "reviewed-npm-audit.json");
const ROOT_LOCK_RELATIVE_PATH = "package-lock.json";

function loadReviewedInventory(): InventoryEntry[] {
export function loadReviewedInventory(repoRoot = REPO_ROOT): InventoryEntry[] {
const config = JSON.parse(
fs.readFileSync(path.join(REPO_ROOT, CONFIG_RELATIVE_PATH), "utf-8"),
fs.readFileSync(path.join(repoRoot, CONFIG_RELATIVE_PATH), "utf-8"),
) as Record<string, unknown>;
const inventory = parseInventoryFromAuditConfig(config, CONFIG_RELATIVE_PATH);
const lockedGraphs = Array.isArray(config.lockedGraphs) ? config.lockedGraphs : [];
for (const graph of lockedGraphs) {
const directory = (graph as Record<string, unknown> | null)?.directory;
if (typeof directory !== "string" || directory.length === 0) continue;
const lockRelativePath = path.join(directory, "package-lock.json");
const lockPath = path.join(REPO_ROOT, lockRelativePath);
const lockRelativePaths = [
ROOT_LOCK_RELATIVE_PATH,
...lockedGraphs.flatMap((graph) => {
const directory = (graph as Record<string, unknown> | null)?.directory;
return typeof directory === "string" && directory.length > 0
? [path.join(directory, "package-lock.json")]
: [];
}),
];
for (const lockRelativePath of lockRelativePaths) {
const lockPath = path.join(repoRoot, lockRelativePath);
if (!fs.existsSync(lockPath)) continue;
const lock = JSON.parse(fs.readFileSync(lockPath, "utf-8")) as unknown;
inventory.push(...parseInventoryFromPackageLock(lock, lockRelativePath));
inventory.push(...parseInventoryFromPackageLock(lock, lockRelativePath, { omitDev: true }));
}
return inventory;
}
Expand Down
134 changes: 123 additions & 11 deletions scripts/audit-reviewed-npm-graph.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

import { spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
Expand All @@ -12,11 +13,13 @@ import {
packReviewedNpmArchive,
verifyInstalledNpmLock,
verifyReviewedNpmLock,
verifyReviewedNpmLockPackages,
} from "./lib/reviewed-npm-archive.mts";
import {
assertExceptionGraphs,
readAuditExceptionRegistry,
runReviewedNpmAudit,
type AuditPolicyResult,
type Severity,
} from "./lib/reviewed-npm-audit.mts";

Expand All @@ -39,13 +42,18 @@ type AuditConfig = Readonly<{
schemaVersion: 2;
severityThreshold: Severity;
}>;
type ReviewedAuditReport = Readonly<{ label: string; result: AuditPolicyResult }>;

const TRUSTED_REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const TARGET_REPO_ROOT = fs.realpathSync(
path.resolve(process.env.NEMOCLAW_REVIEWED_NPM_AUDIT_TARGET_ROOT ?? TRUSTED_REPO_ROOT),
);
const CONFIG_PATH = resolveTrustedAuditConfigPath(TRUSTED_REPO_ROOT);
const SEVERITIES: readonly Severity[] = ["info", "low", "moderate", "high", "critical"];
const SOURCE_GRAPH = {
id: "nemoclaw-cli",
label: "NemoClaw CLI locked production graph",
} as const;
const OPENCLAW_DOMEXCEPTION_ALIAS = {
actualName: "@nolyfill/domexception",
aliasPackagePath: "node_modules/openclaw/node_modules/node-domexception",
Expand Down Expand Up @@ -230,6 +238,32 @@ function assertRegularFile(file: string, label: string): void {
}
}

export function materializeSourceGraph(
sourcePackage: string,
sourceLock: string,
destination: string,
registryOrigin: string,
installProductionDependencies: (directory: string) => void = (directory) =>
void run("npm", ["ci", "--ignore-scripts", "--omit=dev", "--no-audit", "--no-fund"], directory),
): string {
assertRegularFile(sourcePackage, "NemoClaw CLI package manifest");
assertRegularFile(sourceLock, "NemoClaw CLI lockfile");
verifyReviewedNpmLockPackages({ lockfilePath: sourceLock, omitDev: true, registryOrigin });
const lockSha256 = createHash("sha256").update(fs.readFileSync(sourceLock)).digest("hex");
fs.mkdirSync(destination);
fs.copyFileSync(sourcePackage, path.join(destination, "package.json"));
const installedLock = path.join(destination, "package-lock.json");
fs.copyFileSync(sourceLock, installedLock);
installProductionDependencies(destination);
const installedLockSha256 = createHash("sha256")
.update(fs.readFileSync(installedLock))
.digest("hex");
if (installedLockSha256 !== lockSha256) {
throw new Error("NemoClaw CLI lockfile changed while materializing its production graph");
}
return destination;
}

export function normalizeOpenClawSignatureAlias(directory: string): void {
const {
actualName,
Expand Down Expand Up @@ -332,6 +366,85 @@ function auditLockedGraph(
return result;
}

function auditSourceGraph(
config: AuditConfig,
tempRoot: string,
exceptionFile: string,
artifactDirectory: string,
npmVersion: string,
) {
const sourcePackage = targetRepositoryPath("package.json", "NemoClaw CLI package manifest");
const sourceLock = targetRepositoryPath("package-lock.json", "NemoClaw CLI lockfile");
const sourceManifest = readJsonObject(sourcePackage, "NemoClaw CLI package manifest");
if (typeof sourceManifest.name !== "string" || typeof sourceManifest.version !== "string") {
throw new Error("NemoClaw CLI package manifest must declare its name and version");
}
const directory = materializeSourceGraph(
sourcePackage,
sourceLock,
path.join(tempRoot, "source-graph"),
config.registryOrigin,
);
return auditMaterializedSourceGraph({
directory,
exceptionFile,
artifactDirectory,
npmVersion,
packageSpec: `${sourceManifest.name}@${sourceManifest.version}`,
threshold: config.severityThreshold,
});
}

export function auditMaterializedSourceGraph(
options: Readonly<{
artifactDirectory: string;
directory: string;
exceptionFile: string;
npmVersion: string;
packageSpec: string;
threshold: Severity;
}>,
dependencies: Readonly<{
runAudit?: typeof runReviewedNpmAudit;
verifySignatures?: (directory: string) => void;
}> = {},
): AuditPolicyResult {
const result = (dependencies.runAudit ?? runReviewedNpmAudit)({
directory: options.directory,
exceptionFile: options.exceptionFile,
graph: SOURCE_GRAPH.id,
provenance: {
label: SOURCE_GRAPH.label,
nodeVersion: process.version,
npmVersion: options.npmVersion,
packageSpecs: [options.packageSpec],
},
reportFile: path.join(options.artifactDirectory, "source-graph.json"),
resultFile: path.join(options.artifactDirectory, "source-graph-policy.json"),
threshold: options.threshold,
throwOnBlock: false,
});
(
dependencies.verifySignatures ??
((directory) => run("npm", ["audit", "signatures", "--omit=dev"], directory))
)(options.directory);
return result;
}

export function assertReviewedAuditReportsPass(
reports: readonly ReviewedAuditReport[],
threshold: Severity,
): void {
const failures = reports
.filter(({ result }) => result.unacceptedBlockingAdvisories.length > 0)
.map(
({ label, result }) =>
`${label}: ${result.unacceptedBlockingAdvisories.length} unaccepted at or above ${threshold}`,
);
if (failures.length > 0)
throw new Error(`reviewed npm audit threshold failed\n${failures.join("\n")}`);
}

function main(): void {
const config = readConfig();
const expectedNode = `v${config.nodeVersion}`;
Expand All @@ -346,14 +459,22 @@ function main(): void {
const exceptionRegistry = readAuditExceptionRegistry(exceptionFile);
assertExceptionGraphs(
exceptionRegistry.policy,
new Set([config.archiveGraphId, ...config.lockedGraphs.map((graph) => graph.id)]),
new Set([
SOURCE_GRAPH.id,
config.archiveGraphId,
...config.lockedGraphs.map((graph) => graph.id),
]),
);
fs.rmSync(artifactDirectory, { recursive: true, force: true });
fs.mkdirSync(artifactDirectory, { recursive: true });
const npmVersion = run("npm", ["--version"], TRUSTED_REPO_ROOT).stdout.trim();
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-reviewed-npm-audit-"));
try {
const reports = [
{
label: SOURCE_GRAPH.label,
result: auditSourceGraph(config, tempRoot, exceptionFile, artifactDirectory, npmVersion),
},
{
label: "reviewed archive graph",
result: runReviewedNpmAudit({
Expand Down Expand Up @@ -385,16 +506,7 @@ function main(): void {
),
})),
];
const failures: string[] = [];
for (const { label, result } of reports) {
if (result.unacceptedBlockingAdvisories.length > 0) {
failures.push(
`${label}: ${result.unacceptedBlockingAdvisories.length} unaccepted at or above ${config.severityThreshold}`,
);
}
}
if (failures.length > 0)
throw new Error(`reviewed npm audit threshold failed\n${failures.join("\n")}`);
assertReviewedAuditReportsPass(reports, config.severityThreshold);
} finally {
fs.rmSync(tempRoot, { recursive: true, force: true });
}
Expand Down
7 changes: 6 additions & 1 deletion scripts/lib/advisory-early-warning.mts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ export function parseInventoryFromAuditConfig(config: unknown, origin: string):
* Build an installed-package inventory from a lockfile-version-3 package-lock
* subset: every `node_modules/...` entry that records an installed version.
*/
export function parseInventoryFromPackageLock(lock: unknown, origin: string): InventoryEntry[] {
export function parseInventoryFromPackageLock(
lock: unknown,
origin: string,
options: Readonly<{ omitDev?: boolean }> = {},
): InventoryEntry[] {
if (typeof lock !== "object" || lock === null) return [];
const packages = (lock as Record<string, unknown>).packages;
if (typeof packages !== "object" || packages === null || Array.isArray(packages)) return [];
Expand All @@ -210,6 +214,7 @@ export function parseInventoryFromPackageLock(lock: unknown, origin: string): In
const pathName = location.slice(marker + "node_modules/".length);
if (pathName.length === 0) continue;
if (typeof entry !== "object" || entry === null) continue;
if (options.omitDev && (entry as Record<string, unknown>).dev === true) continue;
const version = (entry as Record<string, unknown>).version;
if (typeof version !== "string" || version.length === 0) continue;
// Aliased installs (`npm install alias@npm:real-name`) live under the alias
Expand Down
Loading
Loading