np-audit 1.0.0 → 1.2.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/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "np-audit",
3
- "version": "1.0.0",
3
+ "version": "1.2.1",
4
4
  "description": "Static obfuscation detector for npm lifecycle scripts — supply chain attack prevention",
5
5
  "bin": {
6
- "npa": "./bin/npa.js",
7
- "np-audit": "./bin/npa.js"
6
+ "npa": "bin/npa.js",
7
+ "np-audit": "bin/npa.js"
8
8
  },
9
9
  "main": "./src/cli.js",
10
10
  "files": [
package/src/cli.js CHANGED
@@ -35,10 +35,6 @@ const HELP = `
35
35
  parallelFetches Concurrent downloads (default: ${DEFAULT_CONFIG.parallelFetches})
36
36
  skipScopes Array of @scopes to skip
37
37
  skipPackages Array of package names to skip
38
-
39
- Install:
40
- npm install -g np-audit
41
- npx np-audit scan
42
38
  `;
43
39
 
44
40
  function parseArgs(argv) {
@@ -60,7 +56,8 @@ function parseArgs(argv) {
60
56
  case '--json': flags.json = true; break;
61
57
  case '--no-dev': flags.noDev = true; break;
62
58
  case '--verbose': flags.verbose = true; break;
63
- case '--version': flags.version = true; break;
59
+ case '--version':
60
+ case '-v': flags.version = true; break;
64
61
  case '--help':
65
62
  case '-h': flags.help = true; break;
66
63
  default:
package/src/scanner.js CHANGED
@@ -26,9 +26,15 @@ async function scan(opts) {
26
26
  if (singlePackage) {
27
27
  packages = await resolveSinglePackage(singlePackage, config);
28
28
  } else {
29
- const parsed = parseLockfile(cwd);
30
- packages = parsed.packages;
31
- lockfileVersion = parsed.lockfileVersion;
29
+ const lockPath = path.join(cwd, 'package-lock.json');
30
+ if (fs.existsSync(lockPath)) {
31
+ const parsed = parseLockfile(cwd);
32
+ packages = parsed.packages;
33
+ lockfileVersion = parsed.lockfileVersion;
34
+ } else {
35
+ // No lockfile — resolve from package.json
36
+ packages = await resolveFromPackageJson(cwd, config, noDev);
37
+ }
32
38
  }
33
39
 
34
40
  // Apply skip filters
@@ -242,6 +248,74 @@ function verdictFromScore(score, config) {
242
248
  return 'OK';
243
249
  }
244
250
 
251
+ /**
252
+ * Extract the first clean X.Y.Z (or X.Y or X) semver from a range string.
253
+ * Returns null if no clean version can be found.
254
+ * Examples: "^5.1.0" → "5.1.0", "4.22.1 || ^5" → "4.22.1", "2" → "2", "*" → null
255
+ */
256
+ function extractSemver(range) {
257
+ const match = range.match(/(\d+\.\d+\.\d+(?:-[\w.]+)?|\d+\.\d+|\d+)(?!\S*-)/);
258
+ if (match) return match[1];
259
+ return null;
260
+ }
261
+
262
+ /**
263
+ * Resolve dependencies from package.json when no lockfile exists.
264
+ * @param {string} cwd
265
+ * @param {object} config
266
+ * @param {boolean} noDev
267
+ * @returns {Promise<PackageDescriptor[]>}
268
+ */
269
+ async function resolveFromPackageJson(cwd, config, noDev) {
270
+ const pkgPath = path.join(cwd, 'package.json');
271
+ if (!fs.existsSync(pkgPath)) {
272
+ throw new Error(`package.json not found in ${cwd}`);
273
+ }
274
+
275
+ let pkgJson;
276
+ try {
277
+ pkgJson = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
278
+ } catch (err) {
279
+ throw new Error(`Failed to parse package.json: ${err.message}`);
280
+ }
281
+
282
+ const deps = { ...pkgJson.dependencies };
283
+ if (!noDev && pkgJson.devDependencies) {
284
+ Object.assign(deps, pkgJson.devDependencies);
285
+ }
286
+
287
+ const packages = [];
288
+ const { fetchJSON } = require('./fetcher');
289
+
290
+ for (const [name, range] of Object.entries(deps)) {
291
+ const version = extractSemver(range);
292
+ if (!version) continue;
293
+
294
+ try {
295
+ const meta = await fetchJSON(`${config.registry}/${encodeURIComponent(name)}`, { timeout: config.timeout });
296
+ const versionData = meta.versions && meta.versions[version];
297
+ if (!versionData) continue;
298
+
299
+ packages.push({
300
+ name,
301
+ version,
302
+ resolved: versionData.dist && versionData.dist.tarball,
303
+ integrity: versionData.dist && versionData.dist.integrity || '',
304
+ hasInstallScript: !!(versionData.scripts &&
305
+ (versionData.scripts.preinstall || versionData.scripts.postinstall || versionData.scripts.install)),
306
+ dev: !!(pkgJson.devDependencies && pkgJson.devDependencies[name]),
307
+ optional: false,
308
+ inBundle: false,
309
+ link: false,
310
+ });
311
+ } catch {
312
+ // Skip packages we can't fetch metadata for
313
+ }
314
+ }
315
+
316
+ return packages;
317
+ }
318
+
245
319
  /**
246
320
  * Resolve a single package's dependency tree via the npm registry.
247
321
  * @param {string} packageSpec e.g. "express" or "express@4.18.0"
@@ -275,17 +349,19 @@ async function resolveSinglePackage(packageSpec, config) {
275
349
  for (const [depName, range] of Object.entries(deps || {})) {
276
350
  if (seen.has(depName)) continue;
277
351
  seen.add(depName);
278
- // We don't resolve ranges here just list direct deps; full tree would need more registry calls
352
+ // Extract the first clean semver from the range (e.g. "4.22.1 || ^5" "4.22.1", "^5.1.0" "5.1.0")
353
+ const exactVersion = extractSemver(range);
354
+ if (!exactVersion) continue; // skip unresolvable ranges — lockfile scan will cover them
279
355
  packages.push({
280
- name: depName,
281
- version: range.replace(/^[\^~>=<]/, ''),
282
- resolved: buildTarballUrl(depName, range.replace(/^[\^~>=<]/, ''), config.registry),
283
- integrity: '',
356
+ name: depName,
357
+ version: exactVersion,
358
+ resolved: buildTarballUrl(depName, exactVersion, config.registry),
359
+ integrity: '',
284
360
  hasInstallScript: false,
285
- dev: false,
286
- optional: false,
287
- inBundle: false,
288
- link: false,
361
+ dev: false,
362
+ optional: false,
363
+ inBundle: false,
364
+ link: false,
289
365
  });
290
366
  }
291
367
  }