From 99f2c621ff1dd0c4b068072b083df532ea4bacd8 Mon Sep 17 00:00:00 2001 From: outslept <135520429+outslept@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:50:26 +0300 Subject: [PATCH 1/2] feat: detect install scripts introduced by dependency updates Warn when newly added packages or upgraded dependencies start running preinstall, install or postinstall scripts. New packages that run install scripts are reported, as are upgraded packages that add a script not present in the base version. The check is controlled by the detect-install-scripts input and is enabled by default. --- README.md | 17 ++++ action.yml | 4 + build/main.js | 67 +++++++++++++ src/checks/install-scripts.ts | 66 ++++++++++++ src/main.ts | 6 ++ src/npm.ts | 15 +++ test/checks/install-scripts_test.ts | 149 ++++++++++++++++++++++++++++ 7 files changed, 324 insertions(+) create mode 100644 src/checks/install-scripts.ts create mode 100644 test/checks/install-scripts_test.ts diff --git a/README.md b/README.md index 6fee2ab..72e5fa9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 | @@ -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) diff --git a/action.yml b/action.yml index 52bea3f..86a5618 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/build/main.js b/build/main.js index bbec6f0..8e61023 100644 --- a/build/main.js +++ b/build/main.js @@ -24303,6 +24303,16 @@ 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 getProvenanceForPackageVersions(packageName, versions) { const result = /* @__PURE__ */ new Map(); for (const version of versions) { @@ -25063,6 +25073,59 @@ ${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); + const newVersions = Array.from(currentVersionSet).filter( + (version) => !baseVersionSet?.has(version) + ); + if (newVersions.length === 0) { + continue; + } + try { + const baseScripts = /* @__PURE__ */ new Set(); + if (baseVersionSet) { + for (const version of baseVersionSet) { + const metadata = await fetchPackageMetadata(packageName, version); + for (const script of getInstallScriptNames(metadata)) { + baseScripts.add(script); + } + } + } + for (const version of newVersions) { + const metadata = await fetchPackageMetadata(packageName, version); + const addedScripts = getInstallScriptNames(metadata).filter( + (script) => !baseScripts.has(script) + ); + if (addedScripts.length === 0) { + continue; + } + const change = baseVersionSet ? "Upgraded" : "New package"; + const scripts = addedScripts.map((script) => `\`${script}\``).join(", "); + installScriptRows.push( + `| ${packageName}@${version} | ${change} | ${scripts} |` + ); + } + } 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)}`; @@ -25183,6 +25246,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" ); @@ -25304,6 +25368,9 @@ async function analyzeAndComment() { parsedBaseLock ); await scanForProvenance(messages, currentDeps, baseDeps); + if (detectInstallScripts) { + await scanForInstallScripts(messages, currentDeps, baseDeps); + } if (detectLicenseChanges) { await scanForLicenses(messages, currentDeps, baseDeps); } diff --git a/src/checks/install-scripts.ts b/src/checks/install-scripts.ts new file mode 100644 index 0000000..7f9c488 --- /dev/null +++ b/src/checks/install-scripts.ts @@ -0,0 +1,66 @@ +import * as core from '@actions/core'; +import {fetchPackageMetadata, getInstallScriptNames} from '../npm.js'; + +export async function scanForInstallScripts( + messages: string[], + currentDeps: Map>, + baseDeps: Map> +): Promise { + const installScriptRows: string[] = []; + + for (const [packageName, currentVersionSet] of currentDeps) { + const baseVersionSet = baseDeps.get(packageName); + + const newVersions = Array.from(currentVersionSet).filter( + (version) => !baseVersionSet?.has(version) + ); + if (newVersions.length === 0) { + continue; + } + + try { + const baseScripts = new Set(); + if (baseVersionSet) { + for (const version of baseVersionSet) { + const metadata = await fetchPackageMetadata(packageName, version); + for (const script of getInstallScriptNames(metadata)) { + baseScripts.add(script); + } + } + } + + for (const version of newVersions) { + const metadata = await fetchPackageMetadata(packageName, version); + const addedScripts = getInstallScriptNames(metadata).filter( + (script) => !baseScripts.has(script) + ); + if (addedScripts.length === 0) { + continue; + } + + const change = baseVersionSet ? 'Upgraded' : 'New package'; + const scripts = addedScripts + .map((script) => `\`${script}\``) + .join(', '); + installScriptRows.push( + `| ${packageName}@${version} | ${change} | ${scripts} |` + ); + } + } 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')}` + ); + } +} diff --git a/src/main.ts b/src/main.ts index bd2a6e7..431d4cd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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'; @@ -73,6 +74,7 @@ async function analyzeAndComment(): Promise { 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' ); @@ -210,6 +212,10 @@ async function analyzeAndComment(): Promise { ); await scanForProvenance(messages, currentDeps, baseDeps); + if (detectInstallScripts) { + await scanForInstallScripts(messages, currentDeps, baseDeps); + } + if (detectLicenseChanges) { await scanForLicenses(messages, currentDeps, baseDeps); } diff --git a/src/npm.ts b/src/npm.ts index e8ef396..3cef3d7 100644 --- a/src/npm.ts +++ b/src/npm.ts @@ -13,6 +13,7 @@ export interface PackageMetadata { name: string; version: string; license?: string; + scripts?: Record; os?: string[]; cpu?: string[]; libc?: string[]; @@ -35,6 +36,20 @@ export interface PackageIndex { versions: Record; } +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 getProvenanceForPackageVersions( packageName: string, versions: Set diff --git a/test/checks/install-scripts_test.ts b/test/checks/install-scripts_test.ts new file mode 100644 index 0000000..fde1fa0 --- /dev/null +++ b/test/checks/install-scripts_test.ts @@ -0,0 +1,149 @@ +import {describe, expect, it} from 'vitest'; +import {scanForInstallScripts} from '../../src/checks/install-scripts.js'; +import {metaCache, type PackageMetadata} from '../../src/npm.js'; + +function createMeta( + name: string, + version: string, + scripts?: Record +): PackageMetadata { + return { + name, + version, + scripts, + _npmUser: {name: 'test', email: 'test@example.com'} + }; +} + +describe('scanForInstallScripts', () => { + it('should report a newly added package with install scripts', async () => { + metaCache.set( + 'pkg-new@1.0.0', + createMeta('pkg-new', '1.0.0', {postinstall: 'node install.js'}) + ); + + const messages: string[] = []; + const baseDeps = new Map>(); + const currentDeps = new Map([['pkg-new', new Set(['1.0.0'])]]); + + await scanForInstallScripts(messages, currentDeps, baseDeps); + + expect(messages).toHaveLength(1); + expect(messages[0]).toContain('## ⚠️ Install Scripts Added'); + expect(messages[0]).toContain( + '| pkg-new@1.0.0 | New package | `postinstall` |' + ); + }); + + it('should report an upgraded package that introduces install scripts', async () => { + metaCache.set('pkg-upgrade@1.0.0', createMeta('pkg-upgrade', '1.0.0')); + metaCache.set( + 'pkg-upgrade@2.0.0', + createMeta('pkg-upgrade', '2.0.0', { + install: 'node-gyp rebuild', + postinstall: 'node install.js' + }) + ); + + const messages: string[] = []; + const baseDeps = new Map([['pkg-upgrade', new Set(['1.0.0'])]]); + const currentDeps = new Map([['pkg-upgrade', new Set(['2.0.0'])]]); + + await scanForInstallScripts(messages, currentDeps, baseDeps); + + expect(messages).toHaveLength(1); + expect(messages[0]).toContain( + '| pkg-upgrade@2.0.0 | Upgraded | `install`, `postinstall` |' + ); + }); + + it('should report only scripts that were not present in the base version', async () => { + metaCache.set( + 'pkg-added-script@1.0.0', + createMeta('pkg-added-script', '1.0.0', {postinstall: 'node install.js'}) + ); + metaCache.set( + 'pkg-added-script@2.0.0', + createMeta('pkg-added-script', '2.0.0', { + preinstall: 'node preinstall.js', + postinstall: 'node install.js' + }) + ); + + const messages: string[] = []; + const baseDeps = new Map([['pkg-added-script', new Set(['1.0.0'])]]); + const currentDeps = new Map([['pkg-added-script', new Set(['2.0.0'])]]); + + await scanForInstallScripts(messages, currentDeps, baseDeps); + + expect(messages).toHaveLength(1); + expect(messages[0]).toContain( + '| pkg-added-script@2.0.0 | Upgraded | `preinstall` |' + ); + }); + + it('should not report when the package already had install scripts', async () => { + metaCache.set( + 'pkg-existing@1.0.0', + createMeta('pkg-existing', '1.0.0', {postinstall: 'node install.js'}) + ); + metaCache.set( + 'pkg-existing@2.0.0', + createMeta('pkg-existing', '2.0.0', {postinstall: 'node install.js'}) + ); + + const messages: string[] = []; + const baseDeps = new Map([['pkg-existing', new Set(['1.0.0'])]]); + const currentDeps = new Map([['pkg-existing', new Set(['2.0.0'])]]); + + await scanForInstallScripts(messages, currentDeps, baseDeps); + + expect(messages).toHaveLength(0); + }); + + it('should ignore non-install lifecycle scripts', async () => { + metaCache.set( + 'pkg-other-scripts@1.0.0', + createMeta('pkg-other-scripts', '1.0.0', { + build: 'node build.js', + prepare: 'node prepare.js', + test: 'node test.js' + }) + ); + + const messages: string[] = []; + const baseDeps = new Map>(); + const currentDeps = new Map([['pkg-other-scripts', new Set(['1.0.0'])]]); + + await scanForInstallScripts(messages, currentDeps, baseDeps); + + expect(messages).toHaveLength(0); + }); + + it('should skip packages with no metadata', async () => { + metaCache.set('pkg-missing@1.0.0', null); + + const messages: string[] = []; + const baseDeps = new Map>(); + const currentDeps = new Map([['pkg-missing', new Set(['1.0.0'])]]); + + await scanForInstallScripts(messages, currentDeps, baseDeps); + + expect(messages).toHaveLength(0); + }); + + it('should not report packages that were not upgraded', async () => { + metaCache.set( + 'pkg-unchanged@1.0.0', + createMeta('pkg-unchanged', '1.0.0', {postinstall: 'node install.js'}) + ); + + const messages: string[] = []; + const baseDeps = new Map([['pkg-unchanged', new Set(['1.0.0'])]]); + const currentDeps = new Map([['pkg-unchanged', new Set(['1.0.0'])]]); + + await scanForInstallScripts(messages, currentDeps, baseDeps); + + expect(messages).toHaveLength(0); + }); +}); From 46983595ec5b120332d35d752f20f5552bdfdc82 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:22:33 +0000 Subject: [PATCH 2/2] chore: reuse script collection logic --- build/main.js | 40 ++++++++++++++++++++++++----------- src/checks/install-scripts.ts | 33 +++++++++++++++++------------ src/npm.ts | 14 ++++++++++++ 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/build/main.js b/build/main.js index 8e61023..6e5a097 100644 --- a/build/main.js +++ b/build/main.js @@ -24313,6 +24313,16 @@ function getInstallScriptNames(metadata) { 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) { @@ -25078,34 +25088,40 @@ async function scanForInstallScripts(messages, currentDeps, baseDeps) { const installScriptRows = []; for (const [packageName, currentVersionSet] of currentDeps) { const baseVersionSet = baseDeps.get(packageName); - const newVersions = Array.from(currentVersionSet).filter( - (version) => !baseVersionSet?.has(version) - ); - if (newVersions.length === 0) { + if (baseVersionSet && currentVersionSet.isSubsetOf(baseVersionSet)) { continue; } try { const baseScripts = /* @__PURE__ */ new Set(); if (baseVersionSet) { - for (const version of baseVersionSet) { - const metadata = await fetchPackageMetadata(packageName, version); - for (const script of getInstallScriptNames(metadata)) { + const baseScriptsMap = await getInstallScriptsForPackageVersions( + packageName, + baseVersionSet + ); + for (const scripts of baseScriptsMap.values()) { + for (const script of scripts) { baseScripts.add(script); } } } - for (const version of newVersions) { - const metadata = await fetchPackageMetadata(packageName, version); - const addedScripts = getInstallScriptNames(metadata).filter( + 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 scripts = addedScripts.map((script) => `\`${script}\``).join(", "); + const scriptsText = addedScripts.map((script) => `\`${script}\``).join(", "); installScriptRows.push( - `| ${packageName}@${version} | ${change} | ${scripts} |` + `| ${packageName}@${version} | ${change} | ${scriptsText} |` ); } } catch (err) { diff --git a/src/checks/install-scripts.ts b/src/checks/install-scripts.ts index 7f9c488..fddea60 100644 --- a/src/checks/install-scripts.ts +++ b/src/checks/install-scripts.ts @@ -1,5 +1,5 @@ import * as core from '@actions/core'; -import {fetchPackageMetadata, getInstallScriptNames} from '../npm.js'; +import {getInstallScriptsForPackageVersions} from '../npm.js'; export async function scanForInstallScripts( messages: string[], @@ -11,27 +11,34 @@ export async function scanForInstallScripts( for (const [packageName, currentVersionSet] of currentDeps) { const baseVersionSet = baseDeps.get(packageName); - const newVersions = Array.from(currentVersionSet).filter( - (version) => !baseVersionSet?.has(version) - ); - if (newVersions.length === 0) { + if (baseVersionSet && currentVersionSet.isSubsetOf(baseVersionSet)) { continue; } try { const baseScripts = new Set(); if (baseVersionSet) { - for (const version of baseVersionSet) { - const metadata = await fetchPackageMetadata(packageName, version); - for (const script of getInstallScriptNames(metadata)) { + const baseScriptsMap = await getInstallScriptsForPackageVersions( + packageName, + baseVersionSet + ); + for (const scripts of baseScriptsMap.values()) { + for (const script of scripts) { baseScripts.add(script); } } } - for (const version of newVersions) { - const metadata = await fetchPackageMetadata(packageName, version); - const addedScripts = getInstallScriptNames(metadata).filter( + 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) { @@ -39,11 +46,11 @@ export async function scanForInstallScripts( } const change = baseVersionSet ? 'Upgraded' : 'New package'; - const scripts = addedScripts + const scriptsText = addedScripts .map((script) => `\`${script}\``) .join(', '); installScriptRows.push( - `| ${packageName}@${version} | ${change} | ${scripts} |` + `| ${packageName}@${version} | ${change} | ${scriptsText} |` ); } } catch (err) { diff --git a/src/npm.ts b/src/npm.ts index 3cef3d7..fe4548c 100644 --- a/src/npm.ts +++ b/src/npm.ts @@ -50,6 +50,20 @@ export function getInstallScriptNames( }); } +export async function getInstallScriptsForPackageVersions( + packageName: string, + versions: Set +): Promise> { + const result = new Map(); + 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