@efraindrummer/ghostdeps 0.3.0 → 0.4.0
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/CHANGELOG.md +10 -0
- package/README.md +89 -4
- package/dist/cli.js +191 -8
- package/dist/clients/npm.js +34 -0
- package/dist/diff/inspect.js +223 -0
- package/dist/fix/remediator.js +124 -0
- package/dist/parser/lockfile.js +165 -4
- package/dist/render/html.js +562 -0
- package/dist/render/sarif.js +6 -0
- package/dist/scoring/scoring.js +10 -0
- package/dist/scoring/signals.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Motor de remediación automática (ghostdeps fix).
|
|
3
|
+
* Genera e inyecta overrides/resolutions en package.json para aislar
|
|
4
|
+
* o actualizar dependencias transitivas vulnerables o fantasmas.
|
|
5
|
+
*/
|
|
6
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
/**
|
|
9
|
+
* Detecta el gestor de paquetes predominante en base al lockfile del proyecto.
|
|
10
|
+
*/
|
|
11
|
+
export async function detectPackageManager(dir) {
|
|
12
|
+
const check = async (filename) => {
|
|
13
|
+
try {
|
|
14
|
+
await readFile(path.resolve(dir, filename));
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
if (await check('pnpm-lock.yaml'))
|
|
22
|
+
return 'pnpm';
|
|
23
|
+
if (await check('yarn.lock'))
|
|
24
|
+
return 'yarn';
|
|
25
|
+
return 'npm';
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Propone soluciones a partir de los reportes de análisis.
|
|
29
|
+
*/
|
|
30
|
+
export function generateProposals(reports, customOverrides = {}) {
|
|
31
|
+
const proposals = [];
|
|
32
|
+
// Overrides explícitos proporcionados por el usuario
|
|
33
|
+
for (const [pkgName, targetVersion] of Object.entries(customOverrides)) {
|
|
34
|
+
proposals.push({
|
|
35
|
+
packageName: pkgName,
|
|
36
|
+
currentSpec: 'custom',
|
|
37
|
+
targetVersion,
|
|
38
|
+
reason: 'Override manual indicado por el usuario',
|
|
39
|
+
severity: 'critical',
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
for (const r of reports) {
|
|
43
|
+
if (customOverrides[r.name])
|
|
44
|
+
continue;
|
|
45
|
+
// Paquetes críticos o con scripts sospechosos
|
|
46
|
+
if (r.result?.category === 'critical' || r.signals?.installScripts?.suspicious) {
|
|
47
|
+
if (r.npm?.latestVersion && r.npm.latestVersion !== r.spec) {
|
|
48
|
+
const reason = r.signals?.installScripts?.suspicious
|
|
49
|
+
? 'Posible versión comprometida por scripts de instalación'
|
|
50
|
+
: r.result?.mainAlert?.message ?? 'Dependencia identificada como crítica';
|
|
51
|
+
proposals.push({
|
|
52
|
+
packageName: r.name,
|
|
53
|
+
currentSpec: r.spec,
|
|
54
|
+
targetVersion: r.npm.latestVersion,
|
|
55
|
+
reason,
|
|
56
|
+
severity: 'critical',
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return proposals;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Genera el plan de modificación de package.json según el gestor (npm overrides, yarn resolutions, pnpm.overrides).
|
|
65
|
+
*/
|
|
66
|
+
export async function createFixPlan(packageJsonPath, proposals, pm) {
|
|
67
|
+
const raw = await readFile(packageJsonPath, 'utf8');
|
|
68
|
+
const pkgJson = JSON.parse(raw);
|
|
69
|
+
let changesMade = false;
|
|
70
|
+
if (proposals.length > 0) {
|
|
71
|
+
if (pm === 'npm') {
|
|
72
|
+
pkgJson.overrides = pkgJson.overrides ?? {};
|
|
73
|
+
for (const p of proposals) {
|
|
74
|
+
if (pkgJson.overrides[p.packageName] !== p.targetVersion) {
|
|
75
|
+
pkgJson.overrides[p.packageName] = p.targetVersion;
|
|
76
|
+
changesMade = true;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else if (pm === 'yarn') {
|
|
81
|
+
pkgJson.resolutions = pkgJson.resolutions ?? {};
|
|
82
|
+
for (const p of proposals) {
|
|
83
|
+
if (pkgJson.resolutions[p.packageName] !== p.targetVersion) {
|
|
84
|
+
pkgJson.resolutions[p.packageName] = p.targetVersion;
|
|
85
|
+
changesMade = true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else if (pm === 'pnpm') {
|
|
90
|
+
pkgJson.pnpm = pkgJson.pnpm ?? {};
|
|
91
|
+
pkgJson.pnpm.overrides = pkgJson.pnpm.overrides ?? {};
|
|
92
|
+
for (const p of proposals) {
|
|
93
|
+
if (pkgJson.pnpm.overrides[p.packageName] !== p.targetVersion) {
|
|
94
|
+
pkgJson.pnpm.overrides[p.packageName] = p.targetVersion;
|
|
95
|
+
changesMade = true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const updatedContent = JSON.stringify(pkgJson, null, 2) + '\n';
|
|
101
|
+
return {
|
|
102
|
+
packageManager: pm,
|
|
103
|
+
packageJsonPath,
|
|
104
|
+
proposals,
|
|
105
|
+
originalContent: raw,
|
|
106
|
+
updatedContent,
|
|
107
|
+
changesMade,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Aplica el plan a disco. Si backup=true genera package.json.bak.
|
|
112
|
+
*/
|
|
113
|
+
export async function applyFixPlan(plan, options = {}) {
|
|
114
|
+
if (options.dryRun || !plan.changesMade) {
|
|
115
|
+
return { written: false };
|
|
116
|
+
}
|
|
117
|
+
let backupPath;
|
|
118
|
+
if (options.backup) {
|
|
119
|
+
backupPath = `${plan.packageJsonPath}.bak`;
|
|
120
|
+
await writeFile(backupPath, plan.originalContent, 'utf8');
|
|
121
|
+
}
|
|
122
|
+
await writeFile(plan.packageJsonPath, plan.updatedContent, 'utf8');
|
|
123
|
+
return { written: true, backupPath };
|
|
124
|
+
}
|
package/dist/parser/lockfile.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
import { readFile } from 'node:fs/promises';
|
|
10
10
|
import path from 'node:path';
|
|
11
11
|
import { ManifestError } from '../errors.js';
|
|
12
|
-
export const LOCKFILE_NAMES = ['npm-shrinkwrap.json', 'package-lock.json', 'yarn.lock'];
|
|
12
|
+
export const LOCKFILE_NAMES = ['npm-shrinkwrap.json', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'];
|
|
13
13
|
function unquote(s) {
|
|
14
14
|
const t = s.trim();
|
|
15
15
|
if ((t.startsWith('"') && t.endsWith('"')) || (t.startsWith("'") && t.endsWith("'"))) {
|
|
@@ -98,6 +98,89 @@ export function parseYarnLock(content) {
|
|
|
98
98
|
flush();
|
|
99
99
|
return { type: 'yarn', entries };
|
|
100
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Parser ligero y seguro para pnpm-lock.yaml (v5/v6/v9).
|
|
103
|
+
*/
|
|
104
|
+
export function parsePnpmLock(content) {
|
|
105
|
+
const packages = new Map();
|
|
106
|
+
const lines = content.split(/\r?\n/);
|
|
107
|
+
let inPackages = false;
|
|
108
|
+
let currentKey = null;
|
|
109
|
+
let currentPkg = null;
|
|
110
|
+
let subSection = 'none';
|
|
111
|
+
const flush = () => {
|
|
112
|
+
if (currentKey && currentPkg && currentPkg.name && currentPkg.version) {
|
|
113
|
+
packages.set(currentKey, {
|
|
114
|
+
name: currentPkg.name,
|
|
115
|
+
version: currentPkg.version,
|
|
116
|
+
dependencies: currentPkg.dependencies ?? {},
|
|
117
|
+
optionalDependencies: currentPkg.optionalDependencies ?? {},
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
currentKey = null;
|
|
121
|
+
currentPkg = null;
|
|
122
|
+
subSection = 'none';
|
|
123
|
+
};
|
|
124
|
+
for (let i = 0; i < lines.length; i++) {
|
|
125
|
+
const rawLine = lines[i] ?? '';
|
|
126
|
+
const trimmed = rawLine.trim();
|
|
127
|
+
if (!trimmed || trimmed.startsWith('#'))
|
|
128
|
+
continue;
|
|
129
|
+
if (rawLine.startsWith('packages:')) {
|
|
130
|
+
inPackages = true;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (!inPackages)
|
|
134
|
+
continue;
|
|
135
|
+
if (!rawLine.startsWith(' ') && trimmed.endsWith(':')) {
|
|
136
|
+
flush();
|
|
137
|
+
inPackages = false;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (rawLine.startsWith(' ') && !rawLine.startsWith(' ') && trimmed.endsWith(':')) {
|
|
141
|
+
flush();
|
|
142
|
+
let key = trimmed.slice(0, -1).trim();
|
|
143
|
+
key = unquote(key).replace(/^\//, '').replace(/\(.*$/, '');
|
|
144
|
+
const lastAt = key.lastIndexOf('@');
|
|
145
|
+
if (lastAt > 0) {
|
|
146
|
+
const name = key.slice(0, lastAt);
|
|
147
|
+
const version = key.slice(lastAt + 1);
|
|
148
|
+
currentKey = `${name}@${version}`;
|
|
149
|
+
currentPkg = { name, version, dependencies: {}, optionalDependencies: {} };
|
|
150
|
+
}
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (!currentPkg)
|
|
154
|
+
continue;
|
|
155
|
+
if (rawLine.startsWith(' ') && !rawLine.startsWith(' ')) {
|
|
156
|
+
if (trimmed === 'dependencies:') {
|
|
157
|
+
subSection = 'dependencies';
|
|
158
|
+
}
|
|
159
|
+
else if (trimmed === 'optionalDependencies:') {
|
|
160
|
+
subSection = 'optionalDependencies';
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
subSection = 'none';
|
|
164
|
+
}
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (rawLine.startsWith(' ') && (subSection === 'dependencies' || subSection === 'optionalDependencies')) {
|
|
168
|
+
const colonIdx = trimmed.indexOf(':');
|
|
169
|
+
if (colonIdx > 0) {
|
|
170
|
+
const depName = unquote(trimmed.slice(0, colonIdx));
|
|
171
|
+
const depVersion = unquote(trimmed.slice(colonIdx + 1));
|
|
172
|
+
if (subSection === 'dependencies') {
|
|
173
|
+
currentPkg.dependencies[depName] = depVersion;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
currentPkg.optionalDependencies[depName] = depVersion;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
flush();
|
|
182
|
+
return { type: 'pnpm', packages };
|
|
183
|
+
}
|
|
101
184
|
export async function readLockfile(dir) {
|
|
102
185
|
for (const name of LOCKFILE_NAMES) {
|
|
103
186
|
const file = path.resolve(dir, name);
|
|
@@ -119,6 +202,15 @@ export async function readLockfile(dir) {
|
|
|
119
202
|
throw new ManifestError(`Error al procesar ${file}: ${cause.message}`, { cause });
|
|
120
203
|
}
|
|
121
204
|
}
|
|
205
|
+
if (name === 'pnpm-lock.yaml') {
|
|
206
|
+
try {
|
|
207
|
+
const lock = parsePnpmLock(content);
|
|
208
|
+
return { file, lock };
|
|
209
|
+
}
|
|
210
|
+
catch (cause) {
|
|
211
|
+
throw new ManifestError(`Error al procesar ${file}: ${cause.message}`, { cause });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
122
214
|
try {
|
|
123
215
|
const parsed = JSON.parse(content);
|
|
124
216
|
const lock = {
|
|
@@ -132,8 +224,8 @@ export async function readLockfile(dir) {
|
|
|
132
224
|
throw new ManifestError(`${file} no es un JSON válido.`, { cause });
|
|
133
225
|
}
|
|
134
226
|
}
|
|
135
|
-
throw new ManifestError(`--transitive requiere package-lock.json
|
|
136
|
-
'Ejecuta "npm install" o "
|
|
227
|
+
throw new ManifestError(`--transitive requiere package-lock.json, yarn.lock o pnpm-lock.yaml en ${path.resolve(dir)}. ` +
|
|
228
|
+
'Ejecuta "npm install", "yarn install" o "pnpm install".');
|
|
137
229
|
}
|
|
138
230
|
/**
|
|
139
231
|
* Resolución estilo Node para npm: busca `<from>/node_modules/<name>` y sube por los ancestros.
|
|
@@ -279,13 +371,82 @@ function resolveYarnTransitive(lock, direct, options) {
|
|
|
279
371
|
}
|
|
280
372
|
return result;
|
|
281
373
|
}
|
|
374
|
+
function resolvePnpmTransitive(lock, direct, options) {
|
|
375
|
+
const packages = lock.packages;
|
|
376
|
+
const directNames = new Set(direct.map((d) => d.name));
|
|
377
|
+
const seenNames = new Set(directNames);
|
|
378
|
+
const visited = new Set();
|
|
379
|
+
const result = [];
|
|
380
|
+
const findEntry = (name, spec) => {
|
|
381
|
+
if (spec) {
|
|
382
|
+
const cleanSpec = spec.replace(/\(.*$/, '');
|
|
383
|
+
const exactKey = `${name}@${cleanSpec}`;
|
|
384
|
+
if (packages.has(exactKey))
|
|
385
|
+
return packages.get(exactKey);
|
|
386
|
+
if (packages.has(spec))
|
|
387
|
+
return packages.get(spec);
|
|
388
|
+
}
|
|
389
|
+
const prefix = `${name}@`;
|
|
390
|
+
for (const [k, v] of packages) {
|
|
391
|
+
if (k.startsWith(prefix))
|
|
392
|
+
return v;
|
|
393
|
+
}
|
|
394
|
+
return undefined;
|
|
395
|
+
};
|
|
396
|
+
const queue = [];
|
|
397
|
+
for (const d of direct) {
|
|
398
|
+
if (d.unsupportedReason)
|
|
399
|
+
continue;
|
|
400
|
+
const pkg = findEntry(d.name, d.spec);
|
|
401
|
+
if (!pkg)
|
|
402
|
+
continue;
|
|
403
|
+
const visitKey = `${d.name}@${pkg.version}`;
|
|
404
|
+
if (visited.has(visitKey))
|
|
405
|
+
continue;
|
|
406
|
+
visited.add(visitKey);
|
|
407
|
+
queue.push({ name: d.name, pkg, depth: 1, via: [d.name] });
|
|
408
|
+
}
|
|
409
|
+
while (queue.length > 0) {
|
|
410
|
+
const node = queue.shift();
|
|
411
|
+
if (node.depth >= options.maxDepth)
|
|
412
|
+
continue;
|
|
413
|
+
const children = { ...node.pkg.dependencies, ...node.pkg.optionalDependencies };
|
|
414
|
+
for (const [childName, childSpec] of Object.entries(children).sort(([a], [b]) => a.localeCompare(b))) {
|
|
415
|
+
const childPkg = findEntry(childName, childSpec);
|
|
416
|
+
if (!childPkg)
|
|
417
|
+
continue;
|
|
418
|
+
const visitKey = `${childName}@${childPkg.version}`;
|
|
419
|
+
if (visited.has(visitKey))
|
|
420
|
+
continue;
|
|
421
|
+
visited.add(visitKey);
|
|
422
|
+
const depth = node.depth + 1;
|
|
423
|
+
if (!seenNames.has(childName)) {
|
|
424
|
+
seenNames.add(childName);
|
|
425
|
+
const spec = {
|
|
426
|
+
name: childName,
|
|
427
|
+
registryName: childPkg.name,
|
|
428
|
+
spec: childPkg.version,
|
|
429
|
+
kind: 'transitive',
|
|
430
|
+
depth,
|
|
431
|
+
via: node.via,
|
|
432
|
+
};
|
|
433
|
+
result.push(spec);
|
|
434
|
+
}
|
|
435
|
+
queue.push({ name: childName, pkg: childPkg, depth, via: [...node.via, childName] });
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return result;
|
|
439
|
+
}
|
|
282
440
|
/**
|
|
283
441
|
* Devuelve SOLO las dependencias transitivas (profundidad >= 2), deduplicadas por nombre,
|
|
284
|
-
* alcanzables desde las directas indicadas en un lockfile de npm o
|
|
442
|
+
* alcanzables desde las directas indicadas en un lockfile de npm, Yarn o pnpm.
|
|
285
443
|
*/
|
|
286
444
|
export function resolveTransitive(lock, direct, options) {
|
|
287
445
|
if (lock.type === 'yarn') {
|
|
288
446
|
return resolveYarnTransitive(lock, direct, options);
|
|
289
447
|
}
|
|
448
|
+
if (lock.type === 'pnpm') {
|
|
449
|
+
return resolvePnpmTransitive(lock, direct, options);
|
|
450
|
+
}
|
|
290
451
|
return resolveNpmTransitive(lock, direct, options);
|
|
291
452
|
}
|