@yagyeshvyas/vibeguard 1.13.0 → 1.13.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/CHANGELOG.md +11 -0
- package/README.md +18 -2
- package/bin/cli.js +2753 -2753
- package/ci/vibeguard.yml +42 -42
- package/package.json +1 -1
- package/scripts/counts.js +93 -93
- package/scripts/postinstall.js +26 -26
- package/src/auto.js +427 -427
- package/src/daemon.js +343 -343
- package/src/dashboard.js +278 -278
- package/src/engine.js +297 -297
- package/src/hook.js +151 -151
- package/src/install.js +298 -298
- package/src/integrity.json +1 -1
- package/src/plugin.js +182 -182
- package/src/privacy-audit.js +272 -272
- package/src/repl.js +150 -150
- package/src/report.js +331 -331
- package/src/rules-pack.js +3068 -3068
- package/src/rules.js +1822 -1822
- package/src/sbom.js +193 -193
- package/src/scanner.js +1032 -1032
- package/src/shell-guard.js +302 -302
- package/src/slopsquat.js +144 -144
- package/src/trace.js +220 -220
- package/src/.vibeguard/cache/file-hashes.json +0 -60
- package/src/.vibeguard/latest-snapshot.json +0 -36
- package/src/.vibeguard/mcp-pins.json +0 -1
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/auto.js +0 -428
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/daemon.js +0 -344
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/dashboard.js +0 -279
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/engine.js +0 -297
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/hook.js +0 -151
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/install.js +0 -299
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/manifest.json +0 -65
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/mcp-server.js +0 -2051
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/plugin.js +0 -168
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/pre-deploy.js +0 -362
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/repl.js +0 -150
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/rules.js +0 -1611
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/sbom.js +0 -164
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/shell-guard.js +0 -292
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/slopsquat.js +0 -144
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/trace.js +0 -220
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/auto.js +0 -428
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/daemon.js +0 -344
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/engine.js +0 -297
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/install.js +0 -299
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/manifest.json +0 -37
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/mcp-server.js +0 -2055
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/plugin.js +0 -168
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/pre-deploy.js +0 -362
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/trace.js +0 -220
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/auto.js +0 -428
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/daemon.js +0 -344
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/dashboard.js +0 -279
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/engine.js +0 -297
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/hook.js +0 -151
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/install.js +0 -299
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/manifest.json +0 -65
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/mcp-server.js +0 -2055
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/plugin.js +0 -168
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/pre-deploy.js +0 -362
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/repl.js +0 -150
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/rules.js +0 -1611
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/sbom.js +0 -164
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/shell-guard.js +0 -292
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/slopsquat.js +0 -144
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/trace.js +0 -220
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/auto.js +0 -428
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/daemon.js +0 -344
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/dashboard.js +0 -279
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/engine.js +0 -297
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/hook.js +0 -151
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/install.js +0 -299
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/manifest.json +0 -65
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/mcp-server.js +0 -2054
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/plugin.js +0 -168
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/pre-deploy.js +0 -362
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/repl.js +0 -150
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/rules.js +0 -1611
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/sbom.js +0 -164
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/shell-guard.js +0 -292
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/slopsquat.js +0 -144
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/trace.js +0 -220
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/auto.js +0 -428
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/daemon.js +0 -344
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/engine.js +0 -297
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/install.js +0 -299
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/manifest.json +0 -37
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/mcp-server.js +0 -2055
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/plugin.js +0 -168
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/pre-deploy.js +0 -362
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/trace.js +0 -220
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/auto.js +0 -428
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/daemon.js +0 -344
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/engine.js +0 -297
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/manifest.json +0 -33
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/mcp-server.js +0 -2055
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/rules.js +0 -1611
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/shell-guard.js +0 -292
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/trace.js +0 -220
- package/src/.vibeguard-baseline.json +0 -5226
package/src/sbom.js
CHANGED
|
@@ -1,193 +1,193 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* VibeGuard SBOM Generator.
|
|
5
|
-
*
|
|
6
|
-
* Generates a CycloneDX 1.5 Software Bill of Materials from:
|
|
7
|
-
* - package-lock.json (dependency tree with integrity hashes)
|
|
8
|
-
* - Source code import/require graph (actual usage)
|
|
9
|
-
*
|
|
10
|
-
* 100% local. Zero network. Zero dependencies.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
const fs = require('fs');
|
|
14
|
-
const path = require('path');
|
|
15
|
-
const crypto = require('crypto');
|
|
16
|
-
|
|
17
|
-
function readJsonSafe(filePath) {
|
|
18
|
-
try {
|
|
19
|
-
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
20
|
-
} catch {
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function extractPackageName(spec) {
|
|
26
|
-
// Handle scoped and non-scoped, strip version range chars
|
|
27
|
-
const cleaned = spec.replace(/^[@\w/.-]+/, '').trim();
|
|
28
|
-
return spec.split('@').filter(Boolean)[0] ? spec.startsWith('@')
|
|
29
|
-
? '@' + spec.split('@')[1]
|
|
30
|
-
: spec.split('@')[0] : spec;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function parseLockfile(lockfile) {
|
|
34
|
-
if (!lockfile) return [];
|
|
35
|
-
const components = [];
|
|
36
|
-
|
|
37
|
-
// lockfileVersion 2/3 (npm 7+)
|
|
38
|
-
if (lockfile.packages) {
|
|
39
|
-
for (const [pkgPath, info] of Object.entries(lockfile.packages)) {
|
|
40
|
-
if (!pkgPath || pkgPath === '') continue;
|
|
41
|
-
const name = pkgPath.replace(/^node_modules\//, '');
|
|
42
|
-
if (!name || name.includes('node_modules/')) continue; // skip nested transitive for now
|
|
43
|
-
components.push({
|
|
44
|
-
name,
|
|
45
|
-
version: info.version || 'unknown',
|
|
46
|
-
type: 'library',
|
|
47
|
-
'bom-ref': `pkg:npm/${name}@${info.version || 'unknown'}`,
|
|
48
|
-
...(info.license ? { licenses: [{ license: { id: info.license } }] } : {}),
|
|
49
|
-
...(info.resolved ? { externalReferences: [{ type: 'distribution', url: info.resolved }] } : {}),
|
|
50
|
-
...(info.integrity ? { hashes: [{ alg: info.integrity.split('-')[0] || 'sha512', content: info.integrity.split('-')[1] || '' }] } : {}),
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
return components;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// lockfileVersion 1 (npm 5-6)
|
|
57
|
-
if (lockfile.dependencies) {
|
|
58
|
-
for (const [name, info] of Object.entries(lockfile.dependencies)) {
|
|
59
|
-
components.push({
|
|
60
|
-
name,
|
|
61
|
-
version: info.version || 'unknown',
|
|
62
|
-
type: 'library',
|
|
63
|
-
'bom-ref': `pkg:npm/${name}@${info.version || 'unknown'}`,
|
|
64
|
-
...(info.resolved ? { externalReferences: [{ type: 'distribution', url: info.resolved }] } : {}),
|
|
65
|
-
...(info.integrity ? { hashes: [{ alg: info.integrity.split('-')[0] || 'sha512', content: info.integrity.split('-')[1] || '' }] } : {}),
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
return components;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return components;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function parsePackageJson(pkgJson) {
|
|
75
|
-
if (!pkgJson) return null;
|
|
76
|
-
return {
|
|
77
|
-
name: pkgJson.name || 'unnamed-project',
|
|
78
|
-
version: pkgJson.version || '0.0.0',
|
|
79
|
-
type: 'application',
|
|
80
|
-
'bom-ref': `pkg:npm/${pkgJson.name || 'unnamed'}@${pkgJson.version || '0.0.0'}`,
|
|
81
|
-
...(pkgJson.license ? { licenses: [{ license: { id: pkgJson.license } }] } : {}),
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function buildImportGraph(dir) {
|
|
86
|
-
const { walk } = require('./scanner');
|
|
87
|
-
const files = walk(dir, []);
|
|
88
|
-
const imports = {};
|
|
89
|
-
|
|
90
|
-
for (const file of files) {
|
|
91
|
-
if (!/\.(js|jsx|ts|tsx|mjs|cjs)$/.test(file)) continue;
|
|
92
|
-
try {
|
|
93
|
-
const content = fs.readFileSync(file, 'utf8');
|
|
94
|
-
const relPath = path.relative(dir, file);
|
|
95
|
-
|
|
96
|
-
// require('x') and require("x")
|
|
97
|
-
const requireMatches = content.match(/require\(\s*['"]([^'"]+)['"]\s*\)/g) || [];
|
|
98
|
-
for (const m of requireMatches) {
|
|
99
|
-
const dep = m.match(/require\(\s*['"]([^'"]+)['"]\s*\)/);
|
|
100
|
-
if (dep && !dep[1].startsWith('.') && !dep[1].startsWith('/')) {
|
|
101
|
-
const pkgName = dep[1].startsWith('@') ? dep[1].split('/').slice(0, 2).join('/') : dep[1].split('/')[0];
|
|
102
|
-
if (!imports[pkgName]) imports[pkgName] = [];
|
|
103
|
-
if (!imports[pkgName].includes(relPath)) imports[pkgName].push(relPath);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// import ... from 'x' and import 'x'
|
|
108
|
-
const importMatches = content.match(/import\s+[^'"]*['"]([^'"]+)['"]/g) || [];
|
|
109
|
-
for (const m of importMatches) {
|
|
110
|
-
const dep = m.match(/['"]([^'"]+)['"]/);
|
|
111
|
-
if (dep && !dep[1].startsWith('.') && !dep[1].startsWith('/')) {
|
|
112
|
-
const pkgName = dep[1].startsWith('@') ? dep[1].split('/').slice(0, 2).join('/') : dep[1].split('/')[0];
|
|
113
|
-
if (!imports[pkgName]) imports[pkgName] = [];
|
|
114
|
-
if (!imports[pkgName].includes(relPath)) imports[pkgName].push(relPath);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
} catch {}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return imports;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function generateSBOM(dir) {
|
|
124
|
-
const pkgJson = readJsonSafe(path.join(dir, 'package.json'));
|
|
125
|
-
const lockfile = readJsonSafe(path.join(dir, 'package-lock.json'));
|
|
126
|
-
const importGraph = buildImportGraph(dir);
|
|
127
|
-
|
|
128
|
-
const rootComponent = parsePackageJson(pkgJson);
|
|
129
|
-
const components = parseLockfile(lockfile);
|
|
130
|
-
|
|
131
|
-
// Mark components as "used" if they appear in the import graph
|
|
132
|
-
for (const comp of components) {
|
|
133
|
-
if (importGraph[comp.name]) {
|
|
134
|
-
comp.properties = comp.properties || [];
|
|
135
|
-
comp.properties.push({ name: 'vibeguard:imported', value: 'true' });
|
|
136
|
-
comp.properties.push({ name: 'vibeguard:imported_by', value: importGraph[comp.name].join(', ') });
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const bom = {
|
|
141
|
-
bomFormat: 'CycloneDX',
|
|
142
|
-
specVersion: '1.5',
|
|
143
|
-
serialNumber: 'urn:uuid:' + crypto.randomUUID(),
|
|
144
|
-
version: 1,
|
|
145
|
-
metadata: {
|
|
146
|
-
timestamp: new Date().toISOString(),
|
|
147
|
-
tools: [{ vendor: 'VibeGuard', name: 'vibeguard-sbom', version: '1.0.0' }],
|
|
148
|
-
component: rootComponent || undefined,
|
|
149
|
-
},
|
|
150
|
-
components,
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
// Dependency references (flat — just root → each component)
|
|
154
|
-
if (rootComponent && components.length > 0) {
|
|
155
|
-
bom.dependencies = components.map(c => ({
|
|
156
|
-
ref: rootComponent['bom-ref'],
|
|
157
|
-
dependsOn: [c['bom-ref']],
|
|
158
|
-
}));
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return bom;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* diffSBOM(baseDir, headDir) — compare the dependency sets of two project
|
|
166
|
-
* snapshots (e.g. main vs. your PR). Returns machine-readable drift: what was
|
|
167
|
-
* added / removed / version-changed, so a CI or reviewer can spot new
|
|
168
|
-
* dependencies, unexpected upgrades, or disappearances.
|
|
169
|
-
*/
|
|
170
|
-
function diffSBOM(baseDir, headDir) {
|
|
171
|
-
const base = generateSBOM(baseDir);
|
|
172
|
-
const head = generateSBOM(headDir);
|
|
173
|
-
const bMap = new Map(base.components.map(c => [c.name, c.version]));
|
|
174
|
-
const hMap = new Map(head.components.map(c => [c.name, c.version]));
|
|
175
|
-
const added = [], removed = [], changed = [];
|
|
176
|
-
for (const [name, version] of hMap) {
|
|
177
|
-
if (!bMap.has(name)) added.push({ name, version });
|
|
178
|
-
else if (bMap.get(name) !== version) changed.push({ name, from: bMap.get(name), to: version });
|
|
179
|
-
}
|
|
180
|
-
for (const [name, version] of bMap) {
|
|
181
|
-
if (!hMap.has(name)) removed.push({ name, version });
|
|
182
|
-
}
|
|
183
|
-
return {
|
|
184
|
-
base: baseDir,
|
|
185
|
-
head: headDir,
|
|
186
|
-
counts: { added: added.length, removed: removed.length, changed: changed.length },
|
|
187
|
-
added,
|
|
188
|
-
removed,
|
|
189
|
-
changed,
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
module.exports = { generateSBOM, parseLockfile, parsePackageJson, buildImportGraph, diffSBOM };
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* VibeGuard SBOM Generator.
|
|
5
|
+
*
|
|
6
|
+
* Generates a CycloneDX 1.5 Software Bill of Materials from:
|
|
7
|
+
* - package-lock.json (dependency tree with integrity hashes)
|
|
8
|
+
* - Source code import/require graph (actual usage)
|
|
9
|
+
*
|
|
10
|
+
* 100% local. Zero network. Zero dependencies.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const fs = require('fs');
|
|
14
|
+
const path = require('path');
|
|
15
|
+
const crypto = require('crypto');
|
|
16
|
+
|
|
17
|
+
function readJsonSafe(filePath) {
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function extractPackageName(spec) {
|
|
26
|
+
// Handle scoped and non-scoped, strip version range chars
|
|
27
|
+
const cleaned = spec.replace(/^[@\w/.-]+/, '').trim();
|
|
28
|
+
return spec.split('@').filter(Boolean)[0] ? spec.startsWith('@')
|
|
29
|
+
? '@' + spec.split('@')[1]
|
|
30
|
+
: spec.split('@')[0] : spec;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function parseLockfile(lockfile) {
|
|
34
|
+
if (!lockfile) return [];
|
|
35
|
+
const components = [];
|
|
36
|
+
|
|
37
|
+
// lockfileVersion 2/3 (npm 7+)
|
|
38
|
+
if (lockfile.packages) {
|
|
39
|
+
for (const [pkgPath, info] of Object.entries(lockfile.packages)) {
|
|
40
|
+
if (!pkgPath || pkgPath === '') continue;
|
|
41
|
+
const name = pkgPath.replace(/^node_modules\//, '');
|
|
42
|
+
if (!name || name.includes('node_modules/')) continue; // skip nested transitive for now
|
|
43
|
+
components.push({
|
|
44
|
+
name,
|
|
45
|
+
version: info.version || 'unknown',
|
|
46
|
+
type: 'library',
|
|
47
|
+
'bom-ref': `pkg:npm/${name}@${info.version || 'unknown'}`,
|
|
48
|
+
...(info.license ? { licenses: [{ license: { id: info.license } }] } : {}),
|
|
49
|
+
...(info.resolved ? { externalReferences: [{ type: 'distribution', url: info.resolved }] } : {}),
|
|
50
|
+
...(info.integrity ? { hashes: [{ alg: info.integrity.split('-')[0] || 'sha512', content: info.integrity.split('-')[1] || '' }] } : {}),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return components;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// lockfileVersion 1 (npm 5-6)
|
|
57
|
+
if (lockfile.dependencies) {
|
|
58
|
+
for (const [name, info] of Object.entries(lockfile.dependencies)) {
|
|
59
|
+
components.push({
|
|
60
|
+
name,
|
|
61
|
+
version: info.version || 'unknown',
|
|
62
|
+
type: 'library',
|
|
63
|
+
'bom-ref': `pkg:npm/${name}@${info.version || 'unknown'}`,
|
|
64
|
+
...(info.resolved ? { externalReferences: [{ type: 'distribution', url: info.resolved }] } : {}),
|
|
65
|
+
...(info.integrity ? { hashes: [{ alg: info.integrity.split('-')[0] || 'sha512', content: info.integrity.split('-')[1] || '' }] } : {}),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return components;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return components;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function parsePackageJson(pkgJson) {
|
|
75
|
+
if (!pkgJson) return null;
|
|
76
|
+
return {
|
|
77
|
+
name: pkgJson.name || 'unnamed-project',
|
|
78
|
+
version: pkgJson.version || '0.0.0',
|
|
79
|
+
type: 'application',
|
|
80
|
+
'bom-ref': `pkg:npm/${pkgJson.name || 'unnamed'}@${pkgJson.version || '0.0.0'}`,
|
|
81
|
+
...(pkgJson.license ? { licenses: [{ license: { id: pkgJson.license } }] } : {}),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function buildImportGraph(dir) {
|
|
86
|
+
const { walk } = require('./scanner');
|
|
87
|
+
const files = walk(dir, []);
|
|
88
|
+
const imports = {};
|
|
89
|
+
|
|
90
|
+
for (const file of files) {
|
|
91
|
+
if (!/\.(js|jsx|ts|tsx|mjs|cjs)$/.test(file)) continue;
|
|
92
|
+
try {
|
|
93
|
+
const content = fs.readFileSync(file, 'utf8');
|
|
94
|
+
const relPath = path.relative(dir, file);
|
|
95
|
+
|
|
96
|
+
// require('x') and require("x")
|
|
97
|
+
const requireMatches = content.match(/require\(\s*['"]([^'"]+)['"]\s*\)/g) || [];
|
|
98
|
+
for (const m of requireMatches) {
|
|
99
|
+
const dep = m.match(/require\(\s*['"]([^'"]+)['"]\s*\)/);
|
|
100
|
+
if (dep && !dep[1].startsWith('.') && !dep[1].startsWith('/')) {
|
|
101
|
+
const pkgName = dep[1].startsWith('@') ? dep[1].split('/').slice(0, 2).join('/') : dep[1].split('/')[0];
|
|
102
|
+
if (!imports[pkgName]) imports[pkgName] = [];
|
|
103
|
+
if (!imports[pkgName].includes(relPath)) imports[pkgName].push(relPath);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// import ... from 'x' and import 'x'
|
|
108
|
+
const importMatches = content.match(/import\s+[^'"]*['"]([^'"]+)['"]/g) || [];
|
|
109
|
+
for (const m of importMatches) {
|
|
110
|
+
const dep = m.match(/['"]([^'"]+)['"]/);
|
|
111
|
+
if (dep && !dep[1].startsWith('.') && !dep[1].startsWith('/')) {
|
|
112
|
+
const pkgName = dep[1].startsWith('@') ? dep[1].split('/').slice(0, 2).join('/') : dep[1].split('/')[0];
|
|
113
|
+
if (!imports[pkgName]) imports[pkgName] = [];
|
|
114
|
+
if (!imports[pkgName].includes(relPath)) imports[pkgName].push(relPath);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} catch {}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return imports;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function generateSBOM(dir) {
|
|
124
|
+
const pkgJson = readJsonSafe(path.join(dir, 'package.json'));
|
|
125
|
+
const lockfile = readJsonSafe(path.join(dir, 'package-lock.json'));
|
|
126
|
+
const importGraph = buildImportGraph(dir);
|
|
127
|
+
|
|
128
|
+
const rootComponent = parsePackageJson(pkgJson);
|
|
129
|
+
const components = parseLockfile(lockfile);
|
|
130
|
+
|
|
131
|
+
// Mark components as "used" if they appear in the import graph
|
|
132
|
+
for (const comp of components) {
|
|
133
|
+
if (importGraph[comp.name]) {
|
|
134
|
+
comp.properties = comp.properties || [];
|
|
135
|
+
comp.properties.push({ name: 'vibeguard:imported', value: 'true' });
|
|
136
|
+
comp.properties.push({ name: 'vibeguard:imported_by', value: importGraph[comp.name].join(', ') });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const bom = {
|
|
141
|
+
bomFormat: 'CycloneDX',
|
|
142
|
+
specVersion: '1.5',
|
|
143
|
+
serialNumber: 'urn:uuid:' + crypto.randomUUID(),
|
|
144
|
+
version: 1,
|
|
145
|
+
metadata: {
|
|
146
|
+
timestamp: new Date().toISOString(),
|
|
147
|
+
tools: [{ vendor: 'VibeGuard', name: 'vibeguard-sbom', version: '1.0.0' }],
|
|
148
|
+
component: rootComponent || undefined,
|
|
149
|
+
},
|
|
150
|
+
components,
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// Dependency references (flat — just root → each component)
|
|
154
|
+
if (rootComponent && components.length > 0) {
|
|
155
|
+
bom.dependencies = components.map(c => ({
|
|
156
|
+
ref: rootComponent['bom-ref'],
|
|
157
|
+
dependsOn: [c['bom-ref']],
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return bom;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* diffSBOM(baseDir, headDir) — compare the dependency sets of two project
|
|
166
|
+
* snapshots (e.g. main vs. your PR). Returns machine-readable drift: what was
|
|
167
|
+
* added / removed / version-changed, so a CI or reviewer can spot new
|
|
168
|
+
* dependencies, unexpected upgrades, or disappearances.
|
|
169
|
+
*/
|
|
170
|
+
function diffSBOM(baseDir, headDir) {
|
|
171
|
+
const base = generateSBOM(baseDir);
|
|
172
|
+
const head = generateSBOM(headDir);
|
|
173
|
+
const bMap = new Map(base.components.map(c => [c.name, c.version]));
|
|
174
|
+
const hMap = new Map(head.components.map(c => [c.name, c.version]));
|
|
175
|
+
const added = [], removed = [], changed = [];
|
|
176
|
+
for (const [name, version] of hMap) {
|
|
177
|
+
if (!bMap.has(name)) added.push({ name, version });
|
|
178
|
+
else if (bMap.get(name) !== version) changed.push({ name, from: bMap.get(name), to: version });
|
|
179
|
+
}
|
|
180
|
+
for (const [name, version] of bMap) {
|
|
181
|
+
if (!hMap.has(name)) removed.push({ name, version });
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
base: baseDir,
|
|
185
|
+
head: headDir,
|
|
186
|
+
counts: { added: added.length, removed: removed.length, changed: changed.length },
|
|
187
|
+
added,
|
|
188
|
+
removed,
|
|
189
|
+
changed,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
module.exports = { generateSBOM, parseLockfile, parsePackageJson, buildImportGraph, diffSBOM };
|