forged-cli 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -3
- package/package.json +1 -1
- package/src/commands/scan.js +2 -2
- package/src/utils/freshness.js +25 -0
- package/src/utils/levenshtein.js +6 -1
- package/src/utils/semver.js +48 -0
- package/src/utils/trustedPublishers.js +23 -0
- package/src/utils/verifyIntegrity.js +29 -15
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Brandon Kelly
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -43,10 +43,10 @@ forged readme
|
|
|
43
43
|
`forged scan [path]` audits every package in your `package-lock.json`:
|
|
44
44
|
|
|
45
45
|
- **Known malware** — checked against [OSV.dev](https://osv.dev), including OpenSSF `MAL-` reports and GitHub malware advisories (CWE-506)
|
|
46
|
-
- **Brand-new versions** — flags versions published in the last 72 hours, the window when hijacked releases usually go unnoticed
|
|
46
|
+
- **Brand-new versions** — flags versions published in the last 72 hours, the window when hijacked releases usually go unnoticed (packages that routinely release every week or two, like `caniuse-lite`, are skipped)
|
|
47
47
|
- **Tarball integrity** — lockfile hashes vs. the npm registry
|
|
48
|
-
- **Publisher changes** — flags a version
|
|
49
|
-
- **Typosquats** — names one or two characters away from popular packages
|
|
48
|
+
- **Publisher changes** — flags a version whose publisher has never released this package before. Returning maintainers, known teams, and moves to npm trusted publishing are suppressed; security-critical packages like `jsonwebtoken` and `bcrypt` are always flagged for review
|
|
49
|
+
- **Typosquats** — names one or two characters away from popular packages (one for short names, so `tsx` isn't mistaken for `nx`)
|
|
50
50
|
- **Suspicious install scripts** — `curl | sh`, `eval`, base64 decoding and similar
|
|
51
51
|
|
|
52
52
|
```
|
package/package.json
CHANGED
package/src/commands/scan.js
CHANGED
|
@@ -232,7 +232,7 @@ export async function scanCommand(cwd = process.cwd(), opts = {}) {
|
|
|
232
232
|
|
|
233
233
|
// Verbose: show suppressed trusted rotations
|
|
234
234
|
if (verbose && findings.suppressed.length > 0) {
|
|
235
|
-
console.log(`${bold}SUPPRESSED — trusted
|
|
235
|
+
console.log(`${bold}SUPPRESSED — trusted, returning, or trusted-publishing publishers:${reset}`);
|
|
236
236
|
for (const s of findings.suppressed) {
|
|
237
237
|
console.log(` ${green}~${reset} ${s.package}@${s.version}: ${s.message}`);
|
|
238
238
|
}
|
|
@@ -244,7 +244,7 @@ export async function scanCommand(cwd = process.cwd(), opts = {}) {
|
|
|
244
244
|
}
|
|
245
245
|
if (findings.suppressed.length > 0) {
|
|
246
246
|
const note = verbose ? '' : ' (run with --verbose to see them)';
|
|
247
|
-
console.log(` ${green}ℹ${reset} ${findings.suppressed.length}
|
|
247
|
+
console.log(` ${green}ℹ${reset} ${findings.suppressed.length} publisher change(s) suppressed${note}`);
|
|
248
248
|
}
|
|
249
249
|
console.log();
|
|
250
250
|
|
package/src/utils/freshness.js
CHANGED
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
// time to be vetted by the community yet.
|
|
4
4
|
export const FRESH_HOURS = 72;
|
|
5
5
|
|
|
6
|
+
// Packages that ship data updates every few days (caniuse-lite,
|
|
7
|
+
// electron-to-chromium, ...) are always "fresh" — a hijack stands out as an
|
|
8
|
+
// unexpected release, not another routine one.
|
|
9
|
+
const ROUTINE_SAMPLE = 10; // look at the last N releases before this one
|
|
10
|
+
const ROUTINE_MIN = 5; // need at least this many to judge cadence
|
|
11
|
+
const ROUTINE_GAP_DAYS = 14; // median gap at or under this = routine
|
|
12
|
+
|
|
6
13
|
// Returns hours since publish if the version is younger than `hours`, else null
|
|
7
14
|
export function hoursSincePublish(time, version, now = Date.now(), hours = FRESH_HOURS) {
|
|
8
15
|
const published = Date.parse(time?.[version]);
|
|
@@ -10,3 +17,21 @@ export function hoursSincePublish(time, version, now = Date.now(), hours = FRESH
|
|
|
10
17
|
const age = (now - published) / 3_600_000;
|
|
11
18
|
return age >= 0 && age < hours ? Math.floor(age) : null;
|
|
12
19
|
}
|
|
20
|
+
|
|
21
|
+
// True when the package normally publishes every couple of weeks or faster
|
|
22
|
+
export function isRoutineRelease(time, version) {
|
|
23
|
+
const published = Date.parse(time?.[version]);
|
|
24
|
+
if (Number.isNaN(published)) return false;
|
|
25
|
+
const earlier = Object.entries(time)
|
|
26
|
+
.filter(([v]) => v !== 'created' && v !== 'modified' && v !== version)
|
|
27
|
+
.map(([, t]) => Date.parse(t))
|
|
28
|
+
.filter((t) => !Number.isNaN(t) && t < published)
|
|
29
|
+
.sort((a, b) => a - b)
|
|
30
|
+
.slice(-ROUTINE_SAMPLE);
|
|
31
|
+
if (earlier.length < ROUTINE_MIN) return false;
|
|
32
|
+
|
|
33
|
+
const stamps = [...earlier, published];
|
|
34
|
+
const gaps = stamps.slice(1).map((t, i) => t - stamps[i]).sort((a, b) => a - b);
|
|
35
|
+
const median = gaps[Math.floor(gaps.length / 2)];
|
|
36
|
+
return median <= ROUTINE_GAP_DAYS * 86_400_000;
|
|
37
|
+
}
|
package/src/utils/levenshtein.js
CHANGED
|
@@ -12,12 +12,17 @@ export function levenshtein(a, b) {
|
|
|
12
12
|
return dp[m][n];
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
// Names this short are within 2 edits of lots of unrelated packages
|
|
16
|
+
// (tsx → nx, ws, tar), so only a 1-character difference counts for them.
|
|
17
|
+
const SHORT_NAME = 4;
|
|
18
|
+
|
|
15
19
|
export function isSuspiciousName(name, popularPackages, threshold = 2) {
|
|
16
20
|
const suspects = [];
|
|
17
21
|
for (const known of popularPackages) {
|
|
18
22
|
if (name === known) continue;
|
|
23
|
+
const limit = Math.min(name.length, known.length) <= SHORT_NAME ? Math.min(threshold, 1) : threshold;
|
|
19
24
|
const dist = levenshtein(name, known);
|
|
20
|
-
if (dist > 0 && dist <=
|
|
25
|
+
if (dist > 0 && dist <= limit) {
|
|
21
26
|
suspects.push({ known, distance: dist });
|
|
22
27
|
}
|
|
23
28
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Minimal semver ordering — enough to find "the version before this one".
|
|
2
|
+
// Registry `versions` keys are in publish order, so a backport (e.g. 0.34.3
|
|
3
|
+
// published after 0.41.1) would otherwise look like 0.41.2's predecessor.
|
|
4
|
+
|
|
5
|
+
function parse(v) {
|
|
6
|
+
const [core, pre = ''] = String(v).split('+')[0].split(/-(.*)/s);
|
|
7
|
+
const nums = core.split('.').map((n) => Number.parseInt(n, 10) || 0);
|
|
8
|
+
return { nums: [nums[0] ?? 0, nums[1] ?? 0, nums[2] ?? 0], pre };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function comparePre(a, b) {
|
|
12
|
+
if (a === b) return 0;
|
|
13
|
+
if (!a) return 1; // release sorts after its prereleases
|
|
14
|
+
if (!b) return -1;
|
|
15
|
+
const as = a.split('.'), bs = b.split('.');
|
|
16
|
+
for (let i = 0; i < Math.max(as.length, bs.length); i++) {
|
|
17
|
+
if (as[i] === undefined) return -1;
|
|
18
|
+
if (bs[i] === undefined) return 1;
|
|
19
|
+
const an = /^\d+$/.test(as[i]), bn = /^\d+$/.test(bs[i]);
|
|
20
|
+
if (an && bn) {
|
|
21
|
+
const d = Number(as[i]) - Number(bs[i]);
|
|
22
|
+
if (d) return Math.sign(d);
|
|
23
|
+
} else if (an !== bn) {
|
|
24
|
+
return an ? -1 : 1; // numeric identifiers sort before alphanumeric
|
|
25
|
+
} else if (as[i] !== bs[i]) {
|
|
26
|
+
return as[i] < bs[i] ? -1 : 1;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function compareVersions(a, b) {
|
|
33
|
+
const pa = parse(a), pb = parse(b);
|
|
34
|
+
for (let i = 0; i < 3; i++) {
|
|
35
|
+
const d = pa.nums[i] - pb.nums[i];
|
|
36
|
+
if (d) return Math.sign(d);
|
|
37
|
+
}
|
|
38
|
+
return comparePre(pa.pre, pb.pre);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Highest version strictly below `version`. Stable releases skip prereleases.
|
|
42
|
+
export function previousVersion(versions, version) {
|
|
43
|
+
const isPre = parse(version).pre !== '';
|
|
44
|
+
return versions
|
|
45
|
+
.filter((v) => compareVersions(v, version) < 0 && (isPre || parse(v).pre === ''))
|
|
46
|
+
.sort(compareVersions)
|
|
47
|
+
.at(-1) ?? null;
|
|
48
|
+
}
|
|
@@ -57,6 +57,10 @@ export const TRUSTED_COMMUNITY = new Set([
|
|
|
57
57
|
'jcrugzz',
|
|
58
58
|
// prebuild-install: known npm ecosystem contributor, took over from lovell
|
|
59
59
|
'vweevers',
|
|
60
|
+
// Fastify core team (safe-regex2 and other fastify-org packages)
|
|
61
|
+
'climba03003',
|
|
62
|
+
// Better Auth team — now maintains Auth.js / @auth/core
|
|
63
|
+
'better-gustavo', 'bekacru',
|
|
60
64
|
// Expo core team (bot → human publisher rotations)
|
|
61
65
|
'brentvatne', 'alanhughes', 'kudochien', 'philpl',
|
|
62
66
|
// Software Mansion team (React Native Reanimated / Screens / Worklets)
|
|
@@ -67,6 +71,25 @@ export const TRUSTED_COMMUNITY = new Set([
|
|
|
67
71
|
'flowtype',
|
|
68
72
|
]);
|
|
69
73
|
|
|
74
|
+
// Security-critical packages: any publisher change gets a human look, even
|
|
75
|
+
// from a returning maintainer (e.g. jsonwebtoken, julien.wollscheid).
|
|
76
|
+
export const ALWAYS_REVIEW = new Set([
|
|
77
|
+
'jsonwebtoken', 'jose', 'bcrypt', 'bcryptjs', 'argon2',
|
|
78
|
+
'node-forge', 'crypto-js', 'tweetnacl', 'express-session', 'cookie-session',
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
// "GitHub Actions" as publisher = npm trusted publishing (OIDC from CI)
|
|
82
|
+
const TRUSTED_PUBLISHING = 'GitHub Actions';
|
|
83
|
+
|
|
84
|
+
// → { type: 'warn' | 'info', reason: 'review' | 'oidc' | 'returning' | 'trusted' | 'new' }
|
|
85
|
+
export function publisherChangeSeverity({ pkg, from, to, returning }) {
|
|
86
|
+
if (ALWAYS_REVIEW.has(pkg)) return { type: 'warn', reason: 'review' };
|
|
87
|
+
if (to === TRUSTED_PUBLISHING) return { type: 'info', reason: 'oidc' };
|
|
88
|
+
if (returning) return { type: 'info', reason: 'returning' };
|
|
89
|
+
const type = classifyPublisherChange(from, to);
|
|
90
|
+
return { type, reason: type === 'info' ? 'trusted' : 'new' };
|
|
91
|
+
}
|
|
92
|
+
|
|
70
93
|
export function classifyPublisherChange(from, to) {
|
|
71
94
|
const fromTrusted = NPM_TEAM.has(from) || TRUSTED_COMMUNITY.has(from);
|
|
72
95
|
const toTrusted = NPM_TEAM.has(to) || TRUSTED_COMMUNITY.has(to);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { readFileSync, existsSync } from 'fs';
|
|
2
2
|
import { join } from 'path';
|
|
3
|
-
import {
|
|
4
|
-
import { hoursSincePublish, FRESH_HOURS } from './freshness.js';
|
|
3
|
+
import { publisherChangeSeverity } from './trustedPublishers.js';
|
|
4
|
+
import { hoursSincePublish, isRoutineRelease, FRESH_HOURS } from './freshness.js';
|
|
5
|
+
import { previousVersion } from './semver.js';
|
|
5
6
|
|
|
6
7
|
// Fetch registry metadata with a simple in-memory cache to avoid duplicate requests
|
|
7
8
|
const registryCache = new Map();
|
|
@@ -111,7 +112,7 @@ export async function verifyTarballIntegrity(cwd, onProgress) {
|
|
|
111
112
|
|
|
112
113
|
// Brand-new versions haven't been vetted yet — the usual window for hijacks
|
|
113
114
|
const ageHours = hoursSincePublish(registryMeta.time, meta.version);
|
|
114
|
-
if (ageHours !== null) {
|
|
115
|
+
if (ageHours !== null && !isRoutineRelease(registryMeta.time, meta.version)) {
|
|
115
116
|
findings.push({
|
|
116
117
|
type: 'warning',
|
|
117
118
|
package: name,
|
|
@@ -121,20 +122,33 @@ export async function verifyTarballIntegrity(cwd, onProgress) {
|
|
|
121
122
|
}
|
|
122
123
|
|
|
123
124
|
// Check if the author/publisher changed in this version vs the previous one
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const
|
|
125
|
+
// Compare by version number, not registry order — backports would
|
|
126
|
+
// otherwise look like the previous release.
|
|
127
|
+
const allVersions = Object.keys(registryMeta.versions || {});
|
|
128
|
+
const prevVersion = previousVersion(allVersions, meta.version);
|
|
129
|
+
if (prevVersion) {
|
|
130
|
+
const prevPublisher = registryMeta.versions[prevVersion]?._npmUser?.name;
|
|
131
|
+
const currPublisher = versionData._npmUser?.name;
|
|
130
132
|
if (prevPublisher && currPublisher && prevPublisher !== currPublisher) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
133
|
+
// An account that already published an earlier version is a returning
|
|
134
|
+
// maintainer. The hijack pattern is a first-time publisher.
|
|
135
|
+
const publishedAt = Date.parse(registryMeta.time?.[meta.version]);
|
|
136
|
+
const returning = allVersions.some((v) =>
|
|
137
|
+
v !== meta.version &&
|
|
138
|
+
registryMeta.versions[v]?._npmUser?.name === currPublisher &&
|
|
139
|
+
Date.parse(registryMeta.time?.[v]) < publishedAt);
|
|
140
|
+
const { type, reason } = publisherChangeSeverity({
|
|
141
|
+
pkg: name, from: prevPublisher, to: currPublisher, returning,
|
|
137
142
|
});
|
|
143
|
+
const change = `Publisher changed from "${prevPublisher}" to "${currPublisher}"`;
|
|
144
|
+
const message = {
|
|
145
|
+
review: `${change} — security-critical package, review this release`,
|
|
146
|
+
oidc: `Moved from "${prevPublisher}" to npm trusted publishing (GitHub Actions)`,
|
|
147
|
+
returning: `${change} (returning maintainer)`,
|
|
148
|
+
trusted: `${change} (trusted publisher)`,
|
|
149
|
+
new: `${change} — first release by this account`,
|
|
150
|
+
}[reason];
|
|
151
|
+
findings.push({ type, package: name, version: meta.version, message });
|
|
138
152
|
}
|
|
139
153
|
}
|
|
140
154
|
}));
|