mcp-sanitizer 1.1.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 (57) hide show
  1. package/.eslintrc.js +1 -0
  2. package/README.md +44 -74
  3. package/benchmark/README.md +323 -276
  4. package/benchmark/quick-demo.js +109 -0
  5. package/docs/SECURITY.md +177 -0
  6. package/examples/cve-tbd-001-fix-demo.js +134 -0
  7. package/package.json +2 -1
  8. package/src/config/default-config.js +42 -42
  9. package/src/config/index.js +50 -50
  10. package/src/config/security-policies.js +45 -45
  11. package/src/index.js +100 -88
  12. package/src/middleware/express.js +123 -123
  13. package/src/middleware/fastify.js +119 -119
  14. package/src/middleware/index.js +43 -43
  15. package/src/middleware/koa.js +114 -114
  16. package/src/middleware/optimized-skip-matcher.js +74 -59
  17. package/src/patterns/command-injection.js +48 -48
  18. package/src/patterns/index.js +96 -74
  19. package/src/patterns/nosql-injection.js +752 -0
  20. package/src/patterns/prototype-pollution.js +69 -69
  21. package/src/patterns/sql-injection.js +58 -56
  22. package/src/patterns/template-injection.js +48 -48
  23. package/src/sanitizer/mcp-sanitizer.js +365 -162
  24. package/src/sanitizer/validators/command.js +215 -207
  25. package/src/sanitizer/validators/file-path.js +149 -142
  26. package/src/sanitizer/validators/index.js +76 -76
  27. package/src/sanitizer/validators/sql.js +250 -208
  28. package/src/sanitizer/validators/url.js +256 -226
  29. package/src/utils/enterprise-security.js +1075 -0
  30. package/src/utils/index.js +35 -5
  31. package/src/utils/object-utils.js +92 -74
  32. package/src/utils/security-decoder.js +276 -96
  33. package/src/utils/security-enhancements.js +757 -0
  34. package/src/utils/string-utils.js +114 -40
  35. package/src/utils/unified-parser.js +364 -0
  36. package/src/utils/validation-utils.js +113 -94
  37. package/test/comprehensive-unicode-security.test.js +397 -0
  38. package/test/coverage-gaps.test.js +814 -0
  39. package/test/edge-case-fixes.test.js +91 -199
  40. package/test/enterprise-security-final.test.js +406 -0
  41. package/test/mcp-sanitizer.test.js +136 -136
  42. package/test/middleware/middleware.test.js +162 -162
  43. package/test/middleware/optimized-skip-matcher.test.js +145 -145
  44. package/test/middleware/skipPaths.test.js +239 -239
  45. package/test/nosql-injection.test.js +499 -0
  46. package/test/parser-differential-vulnerability.test.js +206 -0
  47. package/test/security-comprehensive.test.js +454 -0
  48. package/test/security-decoder-integration.test.js +123 -117
  49. package/test/security-enhancements-simple.test.js +78 -0
  50. package/test/security-enhancements.test.js +386 -0
  51. package/test/security-performance-benchmark.test.js +133 -133
  52. package/test/unit/config/config.test.js +129 -129
  53. package/test/unit/library-integration.test.js +155 -155
  54. package/test/unit/validation-libraries.test.js +141 -141
  55. package/docs/SECURITY_IMPROVEMENTS.md +0 -202
  56. package/docs/SECURITY_STATUS.md +0 -133
  57. package/test/encoding-bypass.test.js +0 -169
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,33 +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
- [![Security Coverage](https://img.shields.io/badge/Security%20Coverage-100%25-brightgreen)](./benchmark/README.md)
8
- [![Attack Vectors Blocked](https://img.shields.io/badge/Attack%20Vectors%20Blocked-42%2F42-brightgreen)](./benchmark/advanced-security-benchmark.js)
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
9
 
10
- ## 🔒 Security Posture
10
+ ## 🔒 Security Features
11
11
 
12
- **Version 1.1.0** - Production Ready with 100% attack vector coverage
12
+ MCP Sanitizer provides comprehensive, defense-in-depth protection against common attack vectors:
13
13
 
14
- - ✅ **100% XSS Protection** - All 13 vectors blocked
15
- - ✅ **100% SQL Injection Protection** - All 10 vectors blocked
16
- - ✅ **100% Command Injection Protection** - All 10 vectors blocked
17
- - ✅ **100% Path Traversal Protection** - All 9 vectors blocked
18
- - ✅ **Timing Attack Resistant** - <2% variance (prevents side-channel attacks)
19
- - ✅ **Unicode/Encoding Defense** - Handles `\uXXXX`, URL encoding, HTML entities
20
- - ✅ **Memory Safe** - Bounded at <100MB under attack
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.
21
31
 
22
32
  ## Features
23
33
 
24
- - **Multi-layered Protection**: Guards against command injection, SQL injection, XSS, prototype pollution, and template injection
25
- - **Advanced Decoding Layer**: Pre-processes Unicode, URL encoding, and control characters before validation
26
- - **Context-aware Sanitization**: Different validation rules for file paths, URLs, commands, and SQL queries
27
- - **Trusted Security Libraries**: Built on industry-standard libraries like `escape-html`, `sqlstring`, `shell-quote`, `validator`, `sanitize-filename`, and `path-is-inside`
28
- - **Framework Integration**: Ready-to-use middleware for Express, Fastify, and Koa with `skipPaths` support
29
- - **Security Policies**: Pre-configured policies (STRICT, MODERATE, PERMISSIVE, DEVELOPMENT, PRODUCTION)
30
- - **Timing Attack Mitigation**: Optional timing protection with configurable random delays
31
- - **Configurable & Extensible**: Fluent API and comprehensive configuration options
32
- - **High Performance**: Sub-millisecond operations with optional caching
33
- - **Comprehensive Testing**: 230+ 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
34
42
 
35
43
  ## Installation
36
44
 
@@ -86,21 +94,9 @@ const customSanitizer = new MCPSanitizer({
86
94
  policy: 'MODERATE',
87
95
  maxStringLength: 15000,
88
96
  allowedProtocols: ['https', 'mcp'],
89
- enableTimingProtection: true // New: Prevent timing attacks
97
+ blockSeverity: 'MEDIUM' // Block medium severity and above
90
98
  });
91
99
  ```
92
-
93
- ## What's New in v1.1.0 🚀
94
-
95
- - **Advanced Security Decoder**: New `security-decoder.js` module handles Unicode, URL encoding, and control characters
96
- - **Timing Attack Protection**: Optional timing noise to prevent side-channel attacks
97
- - **Enhanced Path Validation**: Better Windows path normalization and absolute path blocking
98
- - **skipPaths Support**: Middleware can now skip sanitization for specific routes (health checks, metrics, etc.)
99
- - **Improved Coverage**: From 76.2% to 100% attack vector blocking
100
- - **Performance Optimizations**: O(1) path matching for skipPaths feature
101
-
102
- See [Security Improvements](./docs/SECURITY_IMPROVEMENTS.md) for detailed changes.
103
-
104
100
  ## Framework Middleware
105
101
 
106
102
  ### Express.js
@@ -119,8 +115,7 @@ app.use(createMCPMiddleware());
119
115
  app.use(createMCPMiddleware({
120
116
  policy: 'PRODUCTION',
121
117
  mode: 'sanitize', // or 'block'
122
- skipPaths: ['/health', '/metrics'], // New: Skip sanitization for these paths
123
- enableTimingProtection: true
118
+ skipPaths: ['/health', '/metrics'] // Skip sanitization for these paths
124
119
  }));
125
120
 
126
121
  app.post('/tools/:toolName/execute', (req, res) => {
@@ -318,6 +313,7 @@ The MCP Sanitizer leverages trusted, industry-standard security libraries:
318
313
  - **[validator](https://github.com/validatorjs/validator.js)** - String validation and sanitization
319
314
  - **[sanitize-filename](https://github.com/parshap/node-sanitize-filename)** - Filename sanitization
320
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)
321
317
 
322
318
  ## Testing
323
319
 
@@ -427,30 +423,26 @@ node benchmark/library-performance.js
427
423
  node benchmark/skip-paths-performance.js
428
424
  ```
429
425
 
430
- ### 📊 Current Security Metrics
426
+ ### 📊 Security Testing Coverage
431
427
 
432
- - **Attack Vector Coverage**: 100% (42/42 vectors blocked)
433
- - **XSS Protection**: 100% (13/13 vectors)
434
- - **SQL Injection Protection**: 100% (10/10 vectors)
435
- - **Command Injection Protection**: 100% (10/10 vectors)
436
- - **Path Traversal Protection**: 100% (9/9 vectors)
437
- - **Timing Attack Resistance**: <2% variance
438
- - **Memory Usage Under Attack**: <100MB bounded
439
-
440
- See [Security Status](./docs/SECURITY_STATUS.md) for detailed vulnerability analysis.
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
441
434
 
442
435
  ### 🔒 Security Best Practices
443
436
 
444
437
  1. **Always use STRICT or PRODUCTION policy for untrusted input**
445
- 2. **Enable timing protection for sensitive operations**
446
- 3. **Regularly update to get latest security patches**
447
- 4. **Test with your specific attack vectors**
448
- 5. **Monitor sanitization warnings and blocked attempts in production**
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**
449
442
 
450
443
  ### 📝 Security Documentation
451
444
 
452
- - [Security Status Report](./docs/SECURITY_STATUS.md) - Current vulnerabilities and mitigations
453
- - [Security Improvements](./docs/SECURITY_IMPROVEMENTS.md) - v1.1.0 security enhancements
445
+ - [Security Documentation](./docs/SECURITY.md) - Comprehensive security information
454
446
  - [Benchmark Documentation](./benchmark/README.md) - Performance and security testing
455
447
 
456
448
  ## Security Reporting
@@ -461,28 +453,6 @@ If you discover a security vulnerability, please email me instead of using the p
461
453
 
462
454
  MIT License - see [LICENSE](LICENSE) file for details.
463
455
 
464
- ## Changelog
465
-
466
- ### v1.1.0 (2025-08-23) - Security Hardening Release
467
- - 🔒 **Security Coverage**: Improved from 76.2% to 100% attack vector blocking
468
- - 🛡️ **Advanced Decoder**: New `security-decoder.js` module for Unicode/URL/HTML entity decoding
469
- - ⏱️ **Timing Attack Protection**: Added configurable timing noise (<2% variance achieved)
470
- - 📁 **Path Security**: Enhanced Windows path normalization and absolute path blocking
471
- - 🚀 **skipPaths Feature**: Middleware can skip sanitization for specific routes (O(1) performance)
472
- - 🔍 **Command Validation**: Blocks access to sensitive files (/etc/passwd, .ssh/, .aws/, etc.)
473
- - 📊 **Security Benchmarks**: Added advanced-security-benchmark.js with 42 attack vectors
474
- - 📝 **Documentation**: Comprehensive security status and improvement documentation
475
-
476
- ### v1.0.0
477
- - Initial release with comprehensive security features
478
- - Integrated trusted security libraries (escape-html, sqlstring, shell-quote, validator, sanitize-filename, path-is-inside)
479
- - Pre-configured security policies (STRICT, MODERATE, PERMISSIVE, DEVELOPMENT, PRODUCTION)
480
- - Middleware support for Express, Fastify, and Koa
481
- - Fluent configuration builder API
482
- - High performance (sub-millisecond operations)
483
- - Comprehensive test suite (116+ tests)
484
- - Support for all major attack vector prevention
485
-
486
456
  ## Related Projects
487
457
 
488
458
  - [Model Context Protocol](https://github.com/anthropics/model-context-protocol)