#!/bin/sh # Known vulnerabilities in a Node.js project's dependencies PROJECT_DIR="/srv/myapp" NPM="npm" # Use the full path (e.g. /usr/local/bin/npm) if npm isn't in root's PATH command -v "$NPM" > /dev/null 2>&1 || { echo "$NPM not found" >&2; exit 1; } cd "$PROJECT_DIR" || exit 1 # npm audit exits with a non-zero status when it finds vulnerabilities, so judge by its output instead report=$("$NPM" audit --json --omit=dev 2> /dev/null) [ -n "$report" ] || { echo "npm audit failed" >&2; exit 1; } echo "$report" | node -e ' const counts = JSON.parse(require("fs").readFileSync(0, "utf8")).metadata.vulnerabilities; const levels = ["critical", "high", "moderate", "low"].filter((level) => counts[level] > 0); console.log(counts.total ? `${counts.total} vulnerable (${levels.map((level) => `${counts[level]} ${level}`).join(", ")})` : "No known vulnerabilities"); '