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.
- package/.eslintrc.js +1 -0
- package/README.md +44 -74
- package/benchmark/README.md +323 -276
- package/benchmark/quick-demo.js +109 -0
- package/docs/SECURITY.md +177 -0
- package/examples/cve-tbd-001-fix-demo.js +134 -0
- package/package.json +2 -1
- package/src/config/default-config.js +42 -42
- package/src/config/index.js +50 -50
- package/src/config/security-policies.js +45 -45
- package/src/index.js +100 -88
- package/src/middleware/express.js +123 -123
- package/src/middleware/fastify.js +119 -119
- package/src/middleware/index.js +43 -43
- package/src/middleware/koa.js +114 -114
- package/src/middleware/optimized-skip-matcher.js +74 -59
- package/src/patterns/command-injection.js +48 -48
- package/src/patterns/index.js +96 -74
- package/src/patterns/nosql-injection.js +752 -0
- package/src/patterns/prototype-pollution.js +69 -69
- package/src/patterns/sql-injection.js +58 -56
- package/src/patterns/template-injection.js +48 -48
- package/src/sanitizer/mcp-sanitizer.js +365 -162
- package/src/sanitizer/validators/command.js +215 -207
- package/src/sanitizer/validators/file-path.js +149 -142
- package/src/sanitizer/validators/index.js +76 -76
- package/src/sanitizer/validators/sql.js +250 -208
- package/src/sanitizer/validators/url.js +256 -226
- package/src/utils/enterprise-security.js +1075 -0
- package/src/utils/index.js +35 -5
- package/src/utils/object-utils.js +92 -74
- package/src/utils/security-decoder.js +276 -96
- package/src/utils/security-enhancements.js +757 -0
- package/src/utils/string-utils.js +114 -40
- package/src/utils/unified-parser.js +364 -0
- package/src/utils/validation-utils.js +113 -94
- package/test/comprehensive-unicode-security.test.js +397 -0
- package/test/coverage-gaps.test.js +814 -0
- package/test/edge-case-fixes.test.js +91 -199
- package/test/enterprise-security-final.test.js +406 -0
- package/test/mcp-sanitizer.test.js +136 -136
- package/test/middleware/middleware.test.js +162 -162
- package/test/middleware/optimized-skip-matcher.test.js +145 -145
- package/test/middleware/skipPaths.test.js +239 -239
- package/test/nosql-injection.test.js +499 -0
- package/test/parser-differential-vulnerability.test.js +206 -0
- package/test/security-comprehensive.test.js +454 -0
- package/test/security-decoder-integration.test.js +123 -117
- package/test/security-enhancements-simple.test.js +78 -0
- package/test/security-enhancements.test.js +386 -0
- package/test/security-performance-benchmark.test.js +133 -133
- package/test/unit/config/config.test.js +129 -129
- package/test/unit/library-integration.test.js +155 -155
- package/test/unit/validation-libraries.test.js +141 -141
- package/docs/SECURITY_IMPROVEMENTS.md +0 -202
- package/docs/SECURITY_STATUS.md +0 -133
- package/test/encoding-bypass.test.js +0 -169
package/.eslintrc.js
CHANGED
package/README.md
CHANGED
|
@@ -4,33 +4,41 @@ A comprehensive security sanitization library for Model Context Protocol (MCP) s
|
|
|
4
4
|
|
|
5
5
|
[](https://badge.fury.io/js/mcp-sanitizer)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
|
-
[](./test)
|
|
8
|
+
[](./test)
|
|
9
9
|
|
|
10
|
-
## 🔒 Security
|
|
10
|
+
## 🔒 Security Features
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
MCP Sanitizer provides comprehensive, defense-in-depth protection against common attack vectors:
|
|
13
13
|
|
|
14
|
-
- ✅ **
|
|
15
|
-
- ✅ **
|
|
16
|
-
- ✅ **
|
|
17
|
-
- ✅ **
|
|
18
|
-
- ✅ **
|
|
19
|
-
- ✅ **
|
|
20
|
-
- ✅ **
|
|
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
|
-
|
|
25
|
-
- **
|
|
26
|
-
- **
|
|
27
|
-
- **
|
|
28
|
-
- **Framework Integration**:
|
|
29
|
-
- **Security Policies**: Pre-configured
|
|
30
|
-
- **
|
|
31
|
-
- **
|
|
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
|
-
|
|
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']
|
|
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
|
-
### 📊
|
|
426
|
+
### 📊 Security Testing Coverage
|
|
431
427
|
|
|
432
|
-
- **Attack
|
|
433
|
-
- **XSS
|
|
434
|
-
- **SQL Injection
|
|
435
|
-
- **Command Injection
|
|
436
|
-
- **Path Traversal
|
|
437
|
-
- **
|
|
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. **
|
|
446
|
-
3. **
|
|
447
|
-
4. **
|
|
448
|
-
5. **
|
|
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
|
|
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)
|