Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This action compares dependencies between your base branch and current branch, a
- 🔒 **Package trust levels** - Detects decreases in package trust levels (provenance and trusted publisher status)
- 📈 **Dependency growth** - Warns when dependency count increases significantly
- 📦 **Install size** - Warns when package size increases significantly
- 🛠️ **Install scripts** - Warns when a new or upgraded package introduces install scripts
- 🔄 **Duplicate versions** - Detects when multiple versions of a package are introduced
- ⚠️ **Module replacements** - Identifies new packages that have community-recommended alternatives

Expand Down Expand Up @@ -50,6 +51,7 @@ jobs:
| `pack-size-threshold` | Threshold (in bytes) for warning about significant increase in total pack size. Set to `-1` to always report size changes. | No | `50000` |
| `detect-replacements` | Detect modules which have community suggested alternatives | No | `true` |
| `detect-license-changes` | Detect when a dependency changes its license between versions | No | `true` |
| `detect-install-scripts` | Detect when a newly added or upgraded dependency introduces install scripts | No | `true` |
| `exclude-dev-dependencies` | Exclude devDependencies and their exclusive transitive dependencies from analysis | No | `false` |
| `exclude-packages` | Comma-separated package names or wildcard patterns (e.g., `"@types/*, eslint-*"`) to exclude with their dependency trees | No | None |
| `working-directory` | Working directory to scan for package lock file | No | None |
Expand Down Expand Up @@ -128,6 +130,21 @@ The recommendations come from the [e18e community](https://e18e.dev) and include
> [!NOTE]
> Module replacement suggestions are advisory and may not always be straightforward migrations. Review each recommendation carefully and use the `exclude-packages` input if needed.

## Install Scripts

This action warns when a dependency introduces install scripts, which run arbitrary code during `npm install`. This includes newly added packages (often transitive dependencies pulled in by an upgrade) and upgraded packages that add a script they did not previously have.

The `preinstall`, `install` and `postinstall` lifecycle scripts are checked. Packages that already ran install scripts in the base branch are not reported unless they add a new script.

You can disable this check with:

```yaml
- name: Create Diff
uses: e18e/action-dependency-diff@v1
with:
detect-install-scripts: false
```

## Supported package managers

- npm (package-lock.json)
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ inputs:
description: 'Detect when a dependency changes its license between versions'
required: false
default: true
detect-install-scripts:
description: 'Detect when a newly added or upgraded dependency introduces install scripts'
required: false
default: true
exclude-dev-dependencies:
description: 'Exclude devDependencies and their exclusive transitive dependencies from analysis'
required: false
Expand Down
83 changes: 83 additions & 0 deletions build/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24303,6 +24303,26 @@ function getTrustLevelName(status) {
}

// src/npm.ts
var installScriptNames = ["preinstall", "install", "postinstall"];
function getInstallScriptNames(metadata) {
if (!metadata?.scripts) {
return [];
}
return installScriptNames.filter((name) => {
const script = metadata.scripts?.[name];
return typeof script === "string" && script.trim() !== "";
});
}
async function getInstallScriptsForPackageVersions(packageName, versions) {
const result = /* @__PURE__ */ new Map();
for (const version of versions) {
const metadata = await fetchPackageMetadata(packageName, version);
if (metadata) {
result.set(version, getInstallScriptNames(metadata));
}
}
return result;
}
async function getProvenanceForPackageVersions(packageName, versions) {
const result = /* @__PURE__ */ new Map();
for (const version of versions) {
Expand Down Expand Up @@ -25063,6 +25083,65 @@ ${licenseRows.join("\n")}`
}
}

// src/checks/install-scripts.ts
async function scanForInstallScripts(messages, currentDeps, baseDeps) {
const installScriptRows = [];
for (const [packageName, currentVersionSet] of currentDeps) {
const baseVersionSet = baseDeps.get(packageName);
if (baseVersionSet && currentVersionSet.isSubsetOf(baseVersionSet)) {
continue;
}
try {
const baseScripts = /* @__PURE__ */ new Set();
if (baseVersionSet) {
const baseScriptsMap = await getInstallScriptsForPackageVersions(
packageName,
baseVersionSet
);
for (const scripts of baseScriptsMap.values()) {
for (const script of scripts) {
baseScripts.add(script);
}
}
}
const currentScriptsMap = await getInstallScriptsForPackageVersions(
packageName,
currentVersionSet
);
for (const [version, scripts] of currentScriptsMap) {
if (baseVersionSet?.has(version)) {
continue;
}
const addedScripts = scripts.filter(
(script) => !baseScripts.has(script)
);
if (addedScripts.length === 0) {
continue;
}
const change = baseVersionSet ? "Upgraded" : "New package";
const scriptsText = addedScripts.map((script) => `\`${script}\``).join(", ");
installScriptRows.push(
`| ${packageName}@${version} | ${change} | ${scriptsText} |`
);
}
} catch (err) {
info(`Failed to check install scripts for ${packageName}: ${err}`);
}
}
if (installScriptRows.length > 0) {
messages.push(
`## \u26A0\uFE0F Install Scripts Added

> [!WARNING]
> These packages run scripts during \`npm install\`. New install scripts can execute arbitrary code and increase supply chain risk. Please review them carefully.

| \u{1F4E6} Package | \u{1F504} Change | \u{1F527} Added Install Scripts |
| --- | --- | --- |
${installScriptRows.join("\n")}`
);
}
}

// src/checks/bundle-size.ts
function formatBytesSigned(bytes) {
return `${bytes > 0 ? "+" : ""}${formatBytes(bytes)}`;
Expand Down Expand Up @@ -25183,6 +25262,7 @@ async function analyzeAndComment() {
const prNumber = parseInt(getInput("pr-number", { required: true }), 10);
const detectReplacements = getBooleanInput("detect-replacements");
const detectLicenseChanges = getBooleanInput("detect-license-changes");
const detectInstallScripts = getBooleanInput("detect-install-scripts");
const excludeDevDependencies = getBooleanInput(
"exclude-dev-dependencies"
);
Expand Down Expand Up @@ -25304,6 +25384,9 @@ async function analyzeAndComment() {
parsedBaseLock
);
await scanForProvenance(messages, currentDeps, baseDeps);
if (detectInstallScripts) {
await scanForInstallScripts(messages, currentDeps, baseDeps);
}
if (detectLicenseChanges) {
await scanForLicenses(messages, currentDeps, baseDeps);
}
Expand Down
73 changes: 73 additions & 0 deletions src/checks/install-scripts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as core from '@actions/core';
import {getInstallScriptsForPackageVersions} from '../npm.js';

export async function scanForInstallScripts(
messages: string[],
currentDeps: Map<string, Set<string>>,
baseDeps: Map<string, Set<string>>
): Promise<void> {
const installScriptRows: string[] = [];

for (const [packageName, currentVersionSet] of currentDeps) {
const baseVersionSet = baseDeps.get(packageName);

if (baseVersionSet && currentVersionSet.isSubsetOf(baseVersionSet)) {
continue;
}

try {
const baseScripts = new Set<string>();
if (baseVersionSet) {
const baseScriptsMap = await getInstallScriptsForPackageVersions(
packageName,
baseVersionSet
);
for (const scripts of baseScriptsMap.values()) {
for (const script of scripts) {
baseScripts.add(script);
}
}
}

const currentScriptsMap = await getInstallScriptsForPackageVersions(
packageName,
currentVersionSet
);

for (const [version, scripts] of currentScriptsMap) {
if (baseVersionSet?.has(version)) {
continue;
}
const addedScripts = scripts.filter(
(script) => !baseScripts.has(script)
);
if (addedScripts.length === 0) {
continue;
}

const change = baseVersionSet ? 'Upgraded' : 'New package';
const scriptsText = addedScripts
.map((script) => `\`${script}\``)
.join(', ');
installScriptRows.push(
`| ${packageName}@${version} | ${change} | ${scriptsText} |`
);
}
} catch (err) {
core.info(`Failed to check install scripts for ${packageName}: ${err}`);
}
}

if (installScriptRows.length > 0) {
messages.push(
`## ⚠️ Install Scripts Added
> [!WARNING]
> These packages run scripts during \`npm install\`. New install scripts can execute arbitrary code and increase supply chain risk. Please review them carefully.
| 📦 Package | 🔄 Change | 🔧 Added Install Scripts |
| --- | --- | --- |
${installScriptRows.join('\n')}`
);
}
}
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {scanForDependencyCount} from './checks/dependency-count.js';
import {scanForDependencySize} from './checks/dependency-size.js';
import {scanForProvenance} from './checks/provenance.js';
import {scanForLicenses} from './checks/license.js';
import {scanForInstallScripts} from './checks/install-scripts.js';
import {scanForBundleSize} from './checks/bundle-size.js';
import {formatBytes, parseCommaList} from './common.js';
import {ARTIFACT_FILENAME, COMMENT_TAG} from './constants.js';
Expand Down Expand Up @@ -73,6 +74,7 @@ async function analyzeAndComment(): Promise<void> {
const prNumber = parseInt(core.getInput('pr-number', {required: true}), 10);
const detectReplacements = core.getBooleanInput('detect-replacements');
const detectLicenseChanges = core.getBooleanInput('detect-license-changes');
const detectInstallScripts = core.getBooleanInput('detect-install-scripts');
const excludeDevDependencies = core.getBooleanInput(
'exclude-dev-dependencies'
);
Expand Down Expand Up @@ -210,6 +212,10 @@ async function analyzeAndComment(): Promise<void> {
);
await scanForProvenance(messages, currentDeps, baseDeps);

if (detectInstallScripts) {
await scanForInstallScripts(messages, currentDeps, baseDeps);
}

if (detectLicenseChanges) {
await scanForLicenses(messages, currentDeps, baseDeps);
}
Expand Down
29 changes: 29 additions & 0 deletions src/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface PackageMetadata {
name: string;
version: string;
license?: string;
scripts?: Record<string, string>;
os?: string[];
cpu?: string[];
libc?: string[];
Expand All @@ -35,6 +36,34 @@ export interface PackageIndex {
versions: Record<string, PackageMetadata>;
}

const installScriptNames = ['preinstall', 'install', 'postinstall'] as const;

export function getInstallScriptNames(
metadata: PackageMetadata | null
): string[] {
if (!metadata?.scripts) {
return [];
}
return installScriptNames.filter((name) => {
const script = metadata.scripts?.[name];
return typeof script === 'string' && script.trim() !== '';
});
}

export async function getInstallScriptsForPackageVersions(
packageName: string,
versions: Set<string>
): Promise<Map<string, string[]>> {
const result = new Map<string, string[]>();
for (const version of versions) {
const metadata = await fetchPackageMetadata(packageName, version);
if (metadata) {
result.set(version, getInstallScriptNames(metadata));
}
}
return result;
}

export async function getProvenanceForPackageVersions(
packageName: string,
versions: Set<string>
Expand Down
Loading