mcp-sanitizer 1.0.0 → 1.2.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.
Files changed (64) hide show
  1. package/.eslintrc.js +1 -0
  2. package/README.md +77 -24
  3. package/benchmark/README.md +445 -0
  4. package/benchmark/advanced-security-benchmark.js +313 -0
  5. package/benchmark/library-performance.js +17 -0
  6. package/benchmark/quick-demo.js +109 -0
  7. package/benchmark/skip-paths-performance.js +227 -0
  8. package/docs/SECURITY.md +177 -0
  9. package/examples/README.md +186 -0
  10. package/examples/cve-tbd-001-fix-demo.js +134 -0
  11. package/examples/edge-case-validation.js +130 -0
  12. package/examples/mcp-server-basic.js +441 -0
  13. package/examples/mcp-server.js +409 -0
  14. package/examples/security-bypass-demo.js +294 -0
  15. package/examples/test-server.js +401 -0
  16. package/package.json +2 -1
  17. package/src/config/default-config.js +42 -42
  18. package/src/config/index.js +50 -50
  19. package/src/config/security-policies.js +45 -45
  20. package/src/index.js +100 -88
  21. package/src/middleware/express.js +158 -139
  22. package/src/middleware/fastify.js +155 -134
  23. package/src/middleware/index.js +44 -43
  24. package/src/middleware/koa.js +148 -130
  25. package/src/middleware/optimized-skip-matcher.js +238 -0
  26. package/src/patterns/command-injection.js +48 -48
  27. package/src/patterns/index.js +96 -74
  28. package/src/patterns/nosql-injection.js +752 -0
  29. package/src/patterns/prototype-pollution.js +69 -69
  30. package/src/patterns/sql-injection.js +58 -56
  31. package/src/patterns/template-injection.js +48 -48
  32. package/src/sanitizer/mcp-sanitizer.js +424 -129
  33. package/src/sanitizer/validators/command.js +233 -191
  34. package/src/sanitizer/validators/file-path.js +181 -128
  35. package/src/sanitizer/validators/index.js +76 -76
  36. package/src/sanitizer/validators/sql.js +250 -208
  37. package/src/sanitizer/validators/url.js +256 -226
  38. package/src/utils/enterprise-security.js +1075 -0
  39. package/src/utils/index.js +35 -5
  40. package/src/utils/object-utils.js +92 -74
  41. package/src/utils/security-decoder.js +470 -0
  42. package/src/utils/security-enhancements.js +757 -0
  43. package/src/utils/string-utils.js +114 -40
  44. package/src/utils/unified-parser.js +364 -0
  45. package/src/utils/validation-utils.js +209 -80
  46. package/test/comprehensive-unicode-security.test.js +397 -0
  47. package/test/coverage-gaps.test.js +814 -0
  48. package/test/edge-case-fixes.test.js +109 -0
  49. package/test/enterprise-security-final.test.js +406 -0
  50. package/test/mcp-sanitizer.test.js +136 -136
  51. package/test/middleware/middleware.test.js +162 -162
  52. package/test/middleware/optimized-skip-matcher.test.js +311 -0
  53. package/test/middleware/skipPaths.test.js +551 -0
  54. package/test/nosql-injection.test.js +499 -0
  55. package/test/parser-differential-vulnerability.test.js +206 -0
  56. package/test/security-comprehensive.test.js +454 -0
  57. package/test/security-decoder-integration.test.js +225 -0
  58. package/test/security-enhancements-simple.test.js +78 -0
  59. package/test/security-enhancements.test.js +386 -0
  60. package/test/security-performance-benchmark.test.js +296 -0
  61. package/test/unit/config/config.test.js +129 -129
  62. package/test/unit/library-integration.test.js +155 -155
  63. package/test/unit/validation-libraries.test.js +141 -141
  64. package/PROJECT_STATUS.md +0 -79
package/.eslintrc.js CHANGED
@@ -13,6 +13,7 @@ module.exports = {
13
13
  sourceType: 'module'
14
14
  },
15
15
  rules: {
16
+ 'semi': ['error', 'always'],
16
17
  'no-console': 'warn',
17
18
  'no-eval': 'error',
18
19
  'no-implied-eval': 'error',
package/README.md CHANGED
@@ -4,17 +4,41 @@ A comprehensive security sanitization library for Model Context Protocol (MCP) s
4
4
 
5
5
  [![npm version](https://badge.fury.io/js/mcp-sanitizer.svg)](https://badge.fury.io/js/mcp-sanitizer)
6
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+ [![Test Coverage](https://img.shields.io/badge/Test%20Coverage-Comprehensive-brightgreen)](./test)
8
+ [![Security Tests](https://img.shields.io/badge/Security%20Tests-500%2B-brightgreen)](./test)
9
+
10
+ ## 🔒 Security Features
11
+
12
+ MCP Sanitizer provides comprehensive, defense-in-depth protection against common attack vectors:
13
+
14
+ - ✅ **Multi-layered validation** for command injection, SQL injection, and XSS
15
+ - ✅ **Advanced Unicode normalization** to prevent homograph attacks
16
+ - ✅ **Context-aware sanitization** for different input types
17
+ - ✅ **NoSQL injection protection** for MongoDB and similar databases
18
+ - ✅ **Path traversal prevention** with robust normalization
19
+ - ✅ **Database-specific SQL protection** for PostgreSQL, MySQL, MSSQL, Oracle
20
+ - ✅ **Comprehensive test coverage** with 500+ security tests
21
+
22
+ ### Security Philosophy
23
+
24
+ While we maintain rigorous security standards and comprehensive test coverage, we acknowledge that:
25
+ - No security solution is 100% bulletproof
26
+ - Zero-day vulnerabilities may exist
27
+ - Defense-in-depth is essential
28
+ - Regular updates are crucial
29
+
30
+ We encourage responsible disclosure of any security issues via GitHub Security Advisories.
7
31
 
8
32
  ## Features
9
33
 
10
- - **Multi-layered Protection**: Guards against command injection, SQL injection, XSS, prototype pollution, and template injection
11
- - **Context-aware Sanitization**: Different validation rules for file paths, URLs, commands, and SQL queries
12
- - **Trusted Security Libraries**: Built on industry-standard libraries like `escape-html`, `sqlstring`, `shell-quote`, `validator`, `sanitize-filename`, and `path-is-inside`
13
- - **Framework Integration**: Ready-to-use middleware for Express, Fastify, and Koa
14
- - **Security Policies**: Pre-configured policies (STRICT, MODERATE, PERMISSIVE, DEVELOPMENT, PRODUCTION)
15
- - **Configurable & Extensible**: Fluent API and comprehensive configuration options
16
- - **High Performance**: Sub-millisecond operations with optional caching
17
- - **Comprehensive Testing**: 116+ tests covering attack vectors and edge cases
34
+ ### Core Capabilities
35
+ - **Multi-layered Protection**: Command injection, SQL injection, XSS, NoSQL injection, path traversal
36
+ - **Advanced Unicode Defense**: Homograph detection, normalization, zero-width character removal
37
+ - **Context-aware Validation**: Different rules for file paths, URLs, commands, and SQL queries
38
+ - **Framework Integration**: Express, Fastify, and Koa middleware with `skipPaths` support
39
+ - **Security Policies**: Pre-configured (STRICT, MODERATE, PERMISSIVE, DEVELOPMENT, PRODUCTION)
40
+ - **Performance Optimized**: Sub-millisecond operations, <10ms latency
41
+ - **Comprehensive Testing**: 500+ security tests
18
42
 
19
43
  ## Installation
20
44
 
@@ -69,10 +93,10 @@ const sanitizer = new MCPSanitizer('PRODUCTION');
69
93
  const customSanitizer = new MCPSanitizer({
70
94
  policy: 'MODERATE',
71
95
  maxStringLength: 15000,
72
- allowedProtocols: ['https', 'mcp']
96
+ allowedProtocols: ['https', 'mcp'],
97
+ blockSeverity: 'MEDIUM' // Block medium severity and above
73
98
  });
74
99
  ```
75
-
76
100
  ## Framework Middleware
77
101
 
78
102
  ### Express.js
@@ -91,7 +115,7 @@ app.use(createMCPMiddleware());
91
115
  app.use(createMCPMiddleware({
92
116
  policy: 'PRODUCTION',
93
117
  mode: 'sanitize', // or 'block'
94
- skipPaths: ['/health', '/metrics']
118
+ skipPaths: ['/health', '/metrics'] // Skip sanitization for these paths
95
119
  }));
96
120
 
97
121
  app.post('/tools/:toolName/execute', (req, res) => {
@@ -289,6 +313,7 @@ The MCP Sanitizer leverages trusted, industry-standard security libraries:
289
313
  - **[validator](https://github.com/validatorjs/validator.js)** - String validation and sanitization
290
314
  - **[sanitize-filename](https://github.com/parshap/node-sanitize-filename)** - Filename sanitization
291
315
  - **[path-is-inside](https://github.com/domenic/path-is-inside)** - Path containment checking
316
+ - **[unorm](https://github.com/walling/unorm)** - Unicode normalization (NFC, NFD, NFKC, NFKD)
292
317
 
293
318
  ## Testing
294
319
 
@@ -380,26 +405,54 @@ if (urlResult.blocked) {
380
405
  7. Push to the branch (`git push origin feature/amazing-feature`)
381
406
  8. Open a Pull Request
382
407
 
408
+ ## Security
409
+
410
+ ### 🛡️ Security Testing
411
+
412
+ Run comprehensive security benchmarks to validate protection:
413
+
414
+ ```bash
415
+ # Run all benchmarks
416
+ npm run benchmark
417
+
418
+ # Run security-specific benchmark (42 attack vectors)
419
+ node benchmark/advanced-security-benchmark.js
420
+
421
+ # Run performance benchmarks
422
+ node benchmark/library-performance.js
423
+ node benchmark/skip-paths-performance.js
424
+ ```
425
+
426
+ ### 📊 Security Testing Coverage
427
+
428
+ - **Attack Vectors Tested**: 42 comprehensive test cases
429
+ - **XSS Vectors**: 13 test cases
430
+ - **SQL Injection Vectors**: 10 test cases
431
+ - **Command Injection Vectors**: 10 test cases
432
+ - **Path Traversal Vectors**: 9 test cases
433
+ - **Memory Safety**: Bounded memory usage under attack
434
+
435
+ ### 🔒 Security Best Practices
436
+
437
+ 1. **Always use STRICT or PRODUCTION policy for untrusted input**
438
+ 2. **Regularly update to get latest security patches**
439
+ 3. **Test with your specific attack vectors**
440
+ 4. **Monitor sanitization warnings and blocked attempts in production**
441
+ 5. **Implement defense-in-depth - don't rely on a single security layer**
442
+
443
+ ### 📝 Security Documentation
444
+
445
+ - [Security Documentation](./docs/SECURITY.md) - Comprehensive security information
446
+ - [Benchmark Documentation](./benchmark/README.md) - Performance and security testing
447
+
383
448
  ## Security Reporting
384
449
 
385
- If you discover a security vulnerability, please email me instead of using the public issue tracker.
450
+ If you discover a security vulnerability, please email me instead of using the public issue tracker. We follow responsible disclosure practices and will credit researchers.
386
451
 
387
452
  ## License
388
453
 
389
454
  MIT License - see [LICENSE](LICENSE) file for details.
390
455
 
391
- ## Changelog
392
-
393
- ### v1.0.0
394
- - Initial release with comprehensive security features
395
- - Integrated trusted security libraries (escape-html, sqlstring, shell-quote, validator, sanitize-filename, path-is-inside)
396
- - Pre-configured security policies (STRICT, MODERATE, PERMISSIVE, DEVELOPMENT, PRODUCTION)
397
- - Middleware support for Express, Fastify, and Koa
398
- - Fluent configuration builder API
399
- - High performance (sub-millisecond operations)
400
- - Comprehensive test suite (116+ tests)
401
- - Support for all major attack vector prevention
402
-
403
456
  ## Related Projects
404
457
 
405
458
  - [Model Context Protocol](https://github.com/anthropics/model-context-protocol)
@@ -0,0 +1,445 @@
1
+ # MCP Sanitizer - Performance & Security Benchmarks
2
+
3
+ ## 🎯 Benchmarking Philosophy
4
+
5
+ **Security First, Performance Second, Developer Experience Third**
6
+
7
+ This directory contains comprehensive benchmarks for the MCP Sanitizer library. These benchmarks ensure our security implementations maintain excellent performance while prioritizing security integrity above all else.
8
+
9
+ ## ⚠️ Critical Security Principles
10
+
11
+ > **Security is not negotiable**. Performance optimizations must never compromise security.
12
+ >
13
+ > - False negatives (missed attacks) are unacceptable
14
+ > - Memory usage must remain bounded during attacks
15
+ > - Response times should be consistent to mitigate timing attacks
16
+ > - Every optimization must pass security validation
17
+
18
+ ---
19
+
20
+ ## 📊 Benchmark Scripts
21
+
22
+ ### 1. `library-performance.js`
23
+
24
+ **Purpose**: Validates the performance of industry-standard security libraries versus custom implementations.
25
+
26
+ **What it measures**:
27
+ - **HTML Encoding**: `escape-html` library vs regex-based encoding
28
+ - **SQL Escaping**: `sqlstring` library vs custom SQL escape functions
29
+ - **Command Escaping**: `shell-quote` library vs custom shell escaping
30
+ - **Unicode Normalization**: `unorm` library performance characteristics
31
+
32
+ **Why this matters**:
33
+ - Validates our decision to use battle-tested security libraries
34
+ - Ensures we're not sacrificing security for marginal performance gains
35
+ - Provides baseline performance metrics for security operations
36
+ - Demonstrates that proper security doesn't require poor performance
37
+
38
+ **Run it**:
39
+ ```bash
40
+ node benchmark/library-performance.js
41
+ ```
42
+
43
+ **Expected output**:
44
+ - Operations per second for each approach
45
+ - Relative performance comparison
46
+ - Memory usage statistics
47
+ - Security validation confirmation
48
+
49
+ ### 2. `skip-paths-performance.js`
50
+
51
+ **Purpose**: Benchmarks the middleware path-skipping optimization for health checks and metrics endpoints.
52
+
53
+ **What it measures**:
54
+ - Path matching with various configuration sizes (50-5000 paths)
55
+ - O(n) linear search vs O(1) hash lookup vs O(log n) tree structures
56
+ - String exact matching vs prefix matching vs RegExp patterns
57
+ - Memory usage and cache efficiency
58
+ - Real-world request routing scenarios
59
+
60
+ **Why this matters**:
61
+ - Ensures path skipping doesn't become a bottleneck
62
+ - Validates that optimization doesn't introduce security vulnerabilities
63
+ - Confirms performance scales appropriately with configuration size
64
+ - Demonstrates efficient handling of non-security-critical paths
65
+
66
+ **Run it**:
67
+ ```bash
68
+ node benchmark/skip-paths-performance.js
69
+ ```
70
+
71
+ **Key metrics**:
72
+ - Microseconds per path check
73
+ - Memory overhead per configured path
74
+ - Cache hit/miss ratios
75
+ - Performance degradation curve
76
+
77
+ ### 3. `advanced-security-benchmark.js` 🔐
78
+
79
+ **Purpose**: Comprehensive security validation against real-world attack vectors.
80
+
81
+ **What it tests**:
82
+ - **XSS Vectors**: DOM-based, attribute injection, CSS injection, polyglot payloads
83
+ - **SQL Injection**: Blind, time-based, second-order, database-specific variants
84
+ - **Command Injection**: Shell metacharacters, environment variables, Unicode bypass
85
+ - **Path Traversal**: Encoded sequences, Windows/Unix paths, symlink attacks
86
+ - **NoSQL Injection**: MongoDB operators, JSON-based attacks
87
+ - **Unicode Attacks**: Homographs, normalization bypasses, directional overrides
88
+
89
+ **Security validation criteria**:
90
+ - Zero false negatives (all attacks must be detected)
91
+ - Bounded memory usage (prevents DoS)
92
+ - Consistent processing time (mitigates timing attacks)
93
+ - Comprehensive attack coverage
94
+
95
+ **Run it**:
96
+ ```bash
97
+ node benchmark/advanced-security-benchmark.js
98
+ ```
99
+
100
+ ⚠️ **WARNING**: This benchmark uses REAL attack vectors. Failures indicate actual security vulnerabilities.
101
+
102
+ ### 4. `quick-demo.js` 🎯
103
+
104
+ **Purpose**: Quick demonstration of security validation and performance without running full benchmark suite.
105
+
106
+ **What it tests**:
107
+ - 10 common attack vectors across all categories
108
+ - Processing time for each attack
109
+ - Performance with safe inputs (operations per second)
110
+
111
+ **Why this matters**:
112
+ - Quick validation that the sanitizer is working correctly
113
+ - Immediate feedback on security and performance
114
+ - Good starting point before running full benchmarks
115
+
116
+ **Run it**:
117
+ ```bash
118
+ node benchmark/quick-demo.js
119
+ ```
120
+
121
+ **Expected runtime**: ~2 seconds (vs 2+ minutes for full benchmarks)
122
+
123
+ ---
124
+
125
+ ## 📈 Performance Benchmarking Guide
126
+
127
+ ### Running Benchmarks Effectively
128
+
129
+ #### 1. Environment Preparation
130
+ ```bash
131
+ # Ensure clean environment
132
+ npm ci # Clean install dependencies
133
+ node --version # Document Node.js version
134
+ ```
135
+
136
+ #### 2. Warm-up Considerations
137
+ ```javascript
138
+ // Always include warm-up runs to:
139
+ // - Populate V8 optimization caches
140
+ // - Stabilize memory allocation
141
+ // - Eliminate JIT compilation variance
142
+ ```
143
+
144
+ #### 3. Statistical Significance
145
+ - Run multiple iterations (minimum 100)
146
+ - Report percentiles (p50, p95, p99) not just averages
147
+ - Include standard deviation
148
+ - Document margin of error
149
+
150
+ ### Key Performance Metrics
151
+
152
+ #### Response Time Distribution
153
+ ```
154
+ Percentile | Target | Actual | Status
155
+ -----------|-----------|-----------|--------
156
+ p50 | < 0.5ms | 0.3ms | ✅ Pass
157
+ p95 | < 2ms | 1.2ms | ✅ Pass
158
+ p99 | < 5ms | 3.8ms | ✅ Pass
159
+ p99.9 | < 10ms | 7.2ms | ✅ Pass
160
+ ```
161
+
162
+ #### Throughput Characteristics
163
+ - **Sustained load**: Operations per second under continuous load
164
+ - **Burst capacity**: Maximum spike handling capability
165
+ - **Degradation curve**: Performance at 50%, 75%, 100% capacity
166
+
167
+ #### Resource Utilization
168
+ - **Memory footprint**: Base + per-request overhead
169
+ - **CPU efficiency**: Cycles per operation
170
+ - **GC pressure**: Allocation rate and pause frequency
171
+
172
+ ---
173
+
174
+ ## 🔬 Security Benchmarking Guide
175
+
176
+ ### Attack Vector Coverage
177
+
178
+ The benchmark suite tests against comprehensive attack vectors:
179
+
180
+ | Category | Vectors Tested | Description |
181
+ |----------|---------------|-------------|
182
+ | **XSS** | 13 | Script injection, event handlers, data URIs |
183
+ | **SQL Injection** | 10 | Union, blind, time-based, stacked queries |
184
+ | **Command Injection** | 10 | Shell metacharacters, command chaining |
185
+ | **Path Traversal** | 9 | Directory traversal, symlinks, encodings |
186
+ | **Total** | **42** | Comprehensive security validation |
187
+
188
+ ### Security Validation Process
189
+
190
+ ```javascript
191
+ // Every benchmark must follow this pattern:
192
+ async function runSecurityBenchmark() {
193
+ // 1. Validate security FIRST
194
+ const securityPassed = await validateAllAttackVectors();
195
+ if (!securityPassed) {
196
+ throw new Error('CRITICAL: Security validation failed');
197
+ }
198
+
199
+ // 2. Measure performance SECOND
200
+ const perfMetrics = await measurePerformance();
201
+
202
+ // 3. Verify security AGAIN after optimization
203
+ const stillSecure = await validateAllAttackVectors();
204
+ if (!stillSecure) {
205
+ throw new Error('CRITICAL: Optimization broke security');
206
+ }
207
+
208
+ return perfMetrics;
209
+ }
210
+ ```
211
+
212
+ ### False Negative Testing
213
+
214
+ **Zero tolerance policy**: Any false negative is a critical failure.
215
+
216
+ ```javascript
217
+ // Test for false negatives
218
+ const mustBlockAttacks = [
219
+ '../../../etc/passwd',
220
+ "'; DROP TABLE users; --",
221
+ '$(cat /etc/passwd)',
222
+ '<script>alert(1)</script>'
223
+ ];
224
+
225
+ for (const attack of mustBlockAttacks) {
226
+ const result = sanitizer.sanitize(attack);
227
+ assert(result.blocked === true, `Failed to block: ${attack}`);
228
+ }
229
+ ```
230
+
231
+ ---
232
+
233
+ ## 📊 Interpreting Benchmark Results
234
+
235
+ ### Performance Indicators
236
+
237
+ #### 🟢 Healthy Performance
238
+ - Consistent response times (low variance)
239
+ - Linear scaling with input size
240
+ - Predictable memory usage
241
+ - No performance cliffs
242
+
243
+ #### 🟡 Warning Signs
244
+ - High variance between runs (>10%)
245
+ - Exponential scaling patterns
246
+ - Memory growth over time
247
+ - Significant GC pauses
248
+
249
+ #### 🔴 Critical Issues
250
+ - False negatives in security tests
251
+ - Unbounded memory growth
252
+ - Timing attack vulnerabilities
253
+ - Performance degradation under attack
254
+
255
+ ### Benchmark Report Format
256
+
257
+ ```
258
+ =================================================
259
+ MCP Sanitizer Benchmark Report
260
+ =================================================
261
+ Date: 2025-08-23
262
+ Version: Current
263
+ Node.js: Current Version
264
+ Platform: linux x64
265
+
266
+ SECURITY VALIDATION
267
+ -------------------------------------------------
268
+ Attack Vectors Tested: 42
269
+ Vectors Blocked: 42
270
+ False Negatives: 0
271
+ False Positive Rate: <0.1%
272
+ Status: ✅ SECURE
273
+
274
+ PERFORMANCE METRICS
275
+ -------------------------------------------------
276
+ Operation: Input Sanitization
277
+ Average Processing: 9.95ms
278
+ Max Processing: 11.94ms
279
+ Min Processing: 8.78ms
280
+ Memory/op: 1.2KB
281
+ CPU Usage: 14%
282
+
283
+ LIBRARY COMPARISON
284
+ -------------------------------------------------
285
+ escape-html: 31.8M ops/sec (baseline)
286
+ sqlstring: 42.9M ops/sec (baseline)
287
+ shell-quote: 28.4M ops/sec (baseline)
288
+ unorm: 15.2M ops/sec (Unicode normalization)
289
+
290
+ RECOMMENDATIONS
291
+ -------------------------------------------------
292
+ ✅ Security validation passed
293
+ ✅ Performance within acceptable bounds
294
+ ✅ No memory leaks detected
295
+ ✅ Production ready
296
+ =================================================
297
+ ```
298
+
299
+ ---
300
+
301
+ ## 🛠️ Creating Custom Benchmarks
302
+
303
+ ### Security Benchmark Template
304
+
305
+ ```javascript
306
+ const Benchmark = require('benchmark');
307
+ const MCPSanitizer = require('../src/index');
308
+
309
+ class SecurityBenchmark {
310
+ constructor(name) {
311
+ this.name = name;
312
+ this.sanitizer = new MCPSanitizer('STRICT');
313
+ this.attackVectors = [];
314
+ this.results = [];
315
+ }
316
+
317
+ // Step 1: Define attack vectors
318
+ addAttackVector(vector, shouldBlock = true) {
319
+ this.attackVectors.push({ vector, shouldBlock });
320
+ }
321
+
322
+ // Step 2: Validate security
323
+ validateSecurity() {
324
+ for (const { vector, shouldBlock } of this.attackVectors) {
325
+ const result = this.sanitizer.sanitize(vector);
326
+ if (result.blocked !== shouldBlock) {
327
+ throw new Error(`Security validation failed for: ${vector}`);
328
+ }
329
+ }
330
+ return true;
331
+ }
332
+
333
+ // Step 3: Measure performance
334
+ measurePerformance() {
335
+ const suite = new Benchmark.Suite(this.name);
336
+
337
+ // Add benchmarks
338
+ suite.add('Benign Input', () => {
339
+ this.sanitizer.sanitize('safe input string');
340
+ });
341
+
342
+ suite.add('Attack Vector', () => {
343
+ this.sanitizer.sanitize(this.attackVectors[0].vector);
344
+ });
345
+
346
+ // Run and report
347
+ suite.on('complete', function() {
348
+ console.log('Fastest:', this.filter('fastest').map('name'));
349
+ });
350
+
351
+ suite.run({ async: true });
352
+ }
353
+
354
+ // Step 4: Generate report
355
+ report() {
356
+ this.validateSecurity();
357
+ this.measurePerformance();
358
+ }
359
+ }
360
+
361
+ // Usage
362
+ const benchmark = new SecurityBenchmark('XSS Protection');
363
+ benchmark.addAttackVector('<script>alert(1)</script>', true);
364
+ benchmark.addAttackVector('Normal <b>text</b>', false);
365
+ benchmark.report();
366
+ ```
367
+
368
+ ---
369
+
370
+ ## 🎯 Benchmark Best Practices
371
+
372
+ ### Do's ✅
373
+
374
+ 1. **Security First**
375
+ - Always validate security before measuring performance
376
+ - Include real attack vectors in benchmarks
377
+ - Test boundary conditions and edge cases
378
+
379
+ 2. **Realistic Testing**
380
+ - Use real-world input distributions
381
+ - Include both benign and malicious inputs
382
+ - Test with various input sizes
383
+
384
+ 3. **Statistical Rigor**
385
+ - Run sufficient iterations for significance
386
+ - Report percentiles, not just averages
387
+ - Include error margins and confidence intervals
388
+
389
+ 4. **Documentation**
390
+ - Document system specifications
391
+ - Include Node.js and dependency versions
392
+ - Explain what each metric means
393
+
394
+ ### Don'ts ❌
395
+
396
+ 1. **Never Compromise Security**
397
+ - Don't optimize if it reduces security
398
+ - Don't skip attack vector validation
399
+ - Don't ignore false negatives
400
+
401
+ 2. **Avoid Misleading Metrics**
402
+ - Don't cherry-pick best results
403
+ - Don't hide security failures
404
+ - Don't claim 100% protection
405
+
406
+ 3. **Prevent Invalid Comparisons**
407
+ - Don't compare different security levels
408
+ - Don't benchmark without warm-up
409
+ - Don't ignore GC impact
410
+
411
+ ---
412
+
413
+ ## 📚 Additional Resources
414
+
415
+ ### Security Testing
416
+ - [OWASP Testing Guide](https://owasp.org/www-project-web-security-testing-guide/)
417
+ - [OWASP Benchmark Project](https://owasp.org/www-project-benchmark/)
418
+ - [Node.js Security Best Practices](https://nodejs.org/en/docs/guides/security/)
419
+
420
+ ### Performance Analysis
421
+ - [Node.js Performance Profiling](https://nodejs.org/en/docs/guides/simple-profiling/)
422
+ - [V8 Performance Tips](https://v8.dev/docs)
423
+ - [Benchmark.js Documentation](https://benchmarkjs.com/)
424
+
425
+ ### Attack Vector References
426
+ - [OWASP Top 10](https://owasp.org/www-project-top-ten/)
427
+ - [CWE Top 25](https://cwe.mitre.org/top25/)
428
+ - [PayloadsAllTheThings](https://github.com/swisskyrepo/PayloadsAllTheThings)
429
+
430
+ ---
431
+
432
+ ## ⚠️ Security Notice
433
+
434
+ **Remember**: In security libraries, a 10% performance improvement is worthless if it introduces even a 0.01% security vulnerability.
435
+
436
+ Always prioritize:
437
+ 1. **Security** - Must be uncompromised
438
+ 2. **Performance** - Should be acceptable
439
+ 3. **Usability** - Can be improved
440
+
441
+ ---
442
+
443
+ ## License
444
+
445
+ Benchmarks are part of the MCP Sanitizer project under MIT License.