verdaccio-security-filter 1.5.1 → 1.6.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/README.md CHANGED
@@ -6,6 +6,7 @@ Advanced security filter plugin for Verdaccio with **dual-layer protection archi
6
6
  [![npm version](https://img.shields.io/npm/v/verdaccio-security-filter.svg)](https://www.npmjs.com/package/verdaccio-security-filter)
7
7
  [![npm downloads](https://img.shields.io/npm/dm/verdaccio-security-filter.svg)](https://www.npmjs.com/package/verdaccio-security-filter)
8
8
  [![CI](https://github.com/ponomarenko/verdaccio-security-filter/actions/workflows/ci.yml/badge.svg)](https://github.com/ponomarenko/verdaccio-security-filter/actions/workflows/ci.yml)
9
+ [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/ponomarenko/verdaccio-security-filter/badge)](https://scorecard.dev/viewer/?uri=github.com/ponomarenko/verdaccio-security-filter)
9
10
  [![TypeScript](https://img.shields.io/badge/TypeScript-5.9-blue)]()
10
11
  [![Node](https://img.shields.io/badge/node-%3E%3D22-brightgreen)](./package.json)
11
12
  [![Verdaccio](https://img.shields.io/badge/Verdaccio-6.x%20%7C%207.x-orange)]()
@@ -70,6 +71,7 @@ This plugin implements **two independent security layers** for complete protecti
70
71
  - **Minimum Package Age** - Require packages to exist for a minimum number of days
71
72
  - **Minimum Version Age** - Require specific versions to exist for a minimum number of days
72
73
  - **Warn-Only Mode** - Log warnings without blocking
74
+ - **Typosquatting Detection** - Block names one typo away from a popular npm package or your own protected names (`lodahs`, `expres`)
73
75
  - **Author Filtering** - Block packages based on author information
74
76
  - **Block by Author Name** - Block specific author names or patterns
75
77
  - **Block by Email** - Block specific email addresses or patterns
@@ -101,7 +103,6 @@ This plugin implements **two independent security layers** for complete protecti
101
103
 
102
104
  #### Planned
103
105
  - [ ] **Package signature verification** - check npm registry signatures and provenance attestations
104
- - [ ] **Typosquatting detection** - flag names within a small edit distance of popular packages
105
106
 
106
107
  #### Under consideration
107
108
  These wait for a concrete request:
@@ -111,6 +112,7 @@ These wait for a concrete request:
111
112
 
112
113
  #### Done
113
114
  - [x] **Dry-run mode** - `dryRun: true`
115
+ - [x] **Typosquatting detection** - `typosquatting`: names within a small edit distance of popular packages
114
116
  - [x] **Webhook notifications** - `webhook`: block events as JSON or as a Slack message
115
117
 
116
118
  #### Not planned
@@ -123,7 +125,7 @@ These wait for a concrete request:
123
125
  ### Prerequisites
124
126
 
125
127
  - Node.js >= 22.0.0
126
- - Verdaccio >= 5.0.0
128
+ - Verdaccio >= 5.0.0; 7.x recommended. Verdaccio 6 depends on a `minimatch` version with a known denial-of-service issue ([GHSA-g3hv-75wg-m249](https://github.com/advisories/GHSA-g3hv-75wg-m249)), fixed in Verdaccio 7.
127
129
 
128
130
  ```bash
129
131
  npm install -g verdaccio
@@ -573,6 +575,26 @@ The `slack` body:
573
575
 
574
576
  Every version is checked against the author rules: versions published by a blocked author are removed from the metadata, and the package is blocked as a whole only when no version is left.
575
577
 
578
+ ### Typosquatting Options
579
+
580
+ | Option | Type | Default | Description |
581
+ |--------|------|---------|-------------|
582
+ | `typosquatting.enabled` | `boolean` | `false` | Enable typosquatting detection |
583
+ | `typosquatting.maxDistance` | `number` | `1` | Edits allowed between a name and a target; swapping two adjacent letters counts as one |
584
+ | `typosquatting.minLength` | `number` | `5` | Names shorter than this are not checked |
585
+ | `typosquatting.protectedNames` | `string[]` | `[]` | Extra names to protect, e.g. your internal packages |
586
+ | `typosquatting.ignore` | `string[]` | `[]` | Names never flagged, for false positives |
587
+ | `typosquatting.warnOnly` | `boolean` | `false` | Only warn, don't block |
588
+
589
+ The targets are the top 1000 npm packages by downloads plus `protectedNames`. A name that is itself one of the ~17,000 established npm packages (such as `preact` next to `react`) is never flagged. The list ships with the plugin, so no request leaves the registry, and results are cached per name (last 5000); maintainers refresh it with `npm run update:popular`. In `mode: whitelist` the check is skipped: the whitelist already decides which names are allowed.
590
+
591
+ ```yaml
592
+ typosquatting:
593
+ enabled: true
594
+ protectedNames: ['@acme/core', 'acme-ui']
595
+ ignore: ['lodash-ext']
596
+ ```
597
+
576
598
  ### Error Handling Options
577
599
 
578
600
  | Option | Type | Default | Description |
@@ -808,7 +830,7 @@ authorFilter:
808
830
  5. **Enable Author Filtering** - Block packages from untrusted authors or regions
809
831
  6. **Enable Metrics** - Track security events for audit and compliance
810
832
  7. **Regular Updates** - Keep the plugin and CVE database cache updated
811
- 8. **Test Rules** - Use dry run mode (planned) before applying strict rules
833
+ 8. **Test Rules** - Use dry-run mode (`dryRun: true`) before applying strict rules
812
834
  9. **Monitor Logs** - Review security logs regularly for suspicious activity
813
835
 
814
836
  ## 🐛 Troubleshooting
package/build/index.d.ts CHANGED
@@ -14,6 +14,8 @@ export default class SecurityFilterPlugin implements IPluginMiddleware<SecurityC
14
14
  private readonly licenseChecker;
15
15
  private readonly packageAgeChecker;
16
16
  private readonly authorChecker;
17
+ private readonly typosquatChecker;
18
+ private readonly typosquatWarnedAt;
17
19
  constructor(config: SecurityConfig, options: PluginOptions<SecurityConfig>);
18
20
  private _parseVersionRangeRules;
19
21
  private _logInitialization;
@@ -40,4 +42,5 @@ export { CVEChecker } from './lib/cve-checker';
40
42
  export { LicenseChecker } from './lib/license-checker';
41
43
  export { PackageAgeChecker } from './lib/package-age-checker';
42
44
  export { AuthorChecker } from './lib/author-checker';
45
+ export { TyposquatChecker } from './lib/typosquat-checker';
43
46
  export * from './types';
package/build/index.js CHANGED
@@ -45,7 +45,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
45
45
  });
46
46
  };
47
47
  Object.defineProperty(exports, "__esModule", { value: true });
48
- exports.AuthorChecker = exports.PackageAgeChecker = exports.LicenseChecker = exports.CVEChecker = exports.WhitelistChecker = exports.WebhookNotifier = exports.MetricsCollector = exports.SecurityLogger = void 0;
48
+ exports.TyposquatChecker = exports.AuthorChecker = exports.PackageAgeChecker = exports.LicenseChecker = exports.CVEChecker = exports.WhitelistChecker = exports.WebhookNotifier = exports.MetricsCollector = exports.SecurityLogger = void 0;
49
49
  const semver = __importStar(require("semver"));
50
50
  const fs_1 = require("fs");
51
51
  const path_1 = require("path");
@@ -56,15 +56,18 @@ const cve_checker_1 = require("./lib/cve-checker");
56
56
  const license_checker_1 = require("./lib/license-checker");
57
57
  const package_age_checker_1 = require("./lib/package-age-checker");
58
58
  const author_checker_1 = require("./lib/author-checker");
59
+ const typosquat_checker_1 = require("./lib/typosquat-checker");
59
60
  const lru_cache_1 = require("./lib/lru-cache");
60
61
  const webhook_1 = require("./lib/webhook");
61
62
  const PLUGIN_VERSION = JSON.parse((0, fs_1.readFileSync)((0, path_1.join)(__dirname, '..', 'package.json'), 'utf8')).version;
62
63
  const filterBlocks = new lru_cache_1.LRUCache(1000);
64
+ const TYPOSQUAT_WARN_REPEAT_MS = 10 * 60 * 1000;
63
65
  function forbiddenBody(packageName, reason, extra = {}) {
64
66
  return Object.assign(Object.assign({ error: `Package blocked by security filter: ${reason}`, package: packageName, reason }, extra), { timestamp: new Date().toISOString() });
65
67
  }
66
68
  class SecurityFilterPlugin {
67
69
  constructor(config, options) {
70
+ this.typosquatWarnedAt = new lru_cache_1.LRUCache(5000);
68
71
  this.config = config || {};
69
72
  if (!this.config.errorHandling) {
70
73
  this.config.errorHandling = {
@@ -80,6 +83,7 @@ class SecurityFilterPlugin {
80
83
  this.licenseChecker = new license_checker_1.LicenseChecker(this.config.licenses);
81
84
  this.packageAgeChecker = new package_age_checker_1.PackageAgeChecker(this.config.packageAge);
82
85
  this.authorChecker = new author_checker_1.AuthorChecker(this.config.authorFilter);
86
+ this.typosquatChecker = new typosquat_checker_1.TyposquatChecker(this.config.typosquatting);
83
87
  this.securityRules = {
84
88
  blockedVersions: this.config.blockedVersions || [],
85
89
  blockedPatterns: this.config.blockedPatterns || [],
@@ -151,7 +155,7 @@ class SecurityFilterPlugin {
151
155
  }
152
156
  }
153
157
  _getEnabledFeatures() {
154
- var _a, _b, _c, _d;
158
+ var _a, _b, _c, _d, _e;
155
159
  const features = [];
156
160
  if (this.config.dryRun)
157
161
  features.push('dry-run');
@@ -169,6 +173,8 @@ class SecurityFilterPlugin {
169
173
  features.push('package-age-verification');
170
174
  if ((_d = this.config.authorFilter) === null || _d === void 0 ? void 0 : _d.enabled)
171
175
  features.push('author-filtering');
176
+ if ((_e = this.config.typosquatting) === null || _e === void 0 ? void 0 : _e.enabled)
177
+ features.push('typosquat-detection');
172
178
  if (this.securityRules.versionRangeRules.length > 0)
173
179
  features.push('version-range-rules');
174
180
  if (this.securityRules.blockedPatterns.length > 0)
@@ -503,7 +509,9 @@ class SecurityFilterPlugin {
503
509
  }
504
510
  _checkPackageBlock(packageName, version) {
505
511
  return __awaiter(this, void 0, void 0, function* () {
506
- if (this.config.mode === 'whitelist') {
512
+ var _a;
513
+ const whitelistMode = this.config.mode === 'whitelist';
514
+ if (whitelistMode) {
507
515
  const whitelistCheck = this.whitelistChecker.isWhitelisted(packageName, version);
508
516
  if (!whitelistCheck.allowed) {
509
517
  return { blocked: true, reason: whitelistCheck.reason || 'Package is not in whitelist' };
@@ -515,6 +523,17 @@ class SecurityFilterPlugin {
515
523
  if (!this._isScopeAllowed(packageName)) {
516
524
  return { blocked: true, reason: 'Package scope not allowed' };
517
525
  }
526
+ const typosquat = whitelistMode ? { allowed: true } : this.typosquatChecker.check(packageName);
527
+ if (typosquat.warnOnly && typosquat.reason) {
528
+ const now = Date.now();
529
+ if (now - ((_a = this.typosquatWarnedAt.get(packageName)) !== null && _a !== void 0 ? _a : -Infinity) >= TYPOSQUAT_WARN_REPEAT_MS) {
530
+ this.typosquatWarnedAt.set(packageName, now);
531
+ this.logger.warn(`TYPOSQUAT WARNING: ${packageName} - ${typosquat.reason} (warn-only mode)`);
532
+ }
533
+ }
534
+ else if (!typosquat.allowed) {
535
+ return { blocked: true, reason: typosquat.reason };
536
+ }
518
537
  const versionReason = version ? this._versionBlockReason(packageName, version) : null;
519
538
  if (versionReason) {
520
539
  return { blocked: true, reason: versionReason };
@@ -542,6 +561,12 @@ class SecurityFilterPlugin {
542
561
  rules.allowedScopes = this.securityRules.allowedScopes;
543
562
  }
544
563
  }
564
+ if (this.config.mode !== 'whitelist') {
565
+ const typosquat = this.typosquatChecker.check(packageName);
566
+ if (!typosquat.allowed && typosquat.target) {
567
+ rules.typosquat = { target: typosquat.target, distance: typosquat.distance };
568
+ }
569
+ }
545
570
  const versionRules = this.securityRules.versionRangeRules.filter(rule => rule.package === packageName);
546
571
  if (versionRules.length > 0) {
547
572
  rules.versionRangeRules = versionRules.map(rule => ({
@@ -581,4 +606,6 @@ var package_age_checker_2 = require("./lib/package-age-checker");
581
606
  Object.defineProperty(exports, "PackageAgeChecker", { enumerable: true, get: function () { return package_age_checker_2.PackageAgeChecker; } });
582
607
  var author_checker_2 = require("./lib/author-checker");
583
608
  Object.defineProperty(exports, "AuthorChecker", { enumerable: true, get: function () { return author_checker_2.AuthorChecker; } });
609
+ var typosquat_checker_2 = require("./lib/typosquat-checker");
610
+ Object.defineProperty(exports, "TyposquatChecker", { enumerable: true, get: function () { return typosquat_checker_2.TyposquatChecker; } });
584
611
  __exportStar(require("./types"), exports);
@@ -0,0 +1,2 @@
1
+ export declare const POPULAR_PACKAGES: readonly string[];
2
+ export declare const KNOWN_PACKAGES: readonly string[];