feat(security): add MIME type restrictions for resources (US-2)#3847
feat(security): add MIME type restrictions for resources (US-2)#3847crivetimihai merged 1 commit intomainfrom
Conversation
c77cb69 to
f7a38a6
Compare
|
Thanks @msureshkumar88. Comprehensive implementation of US-2 from #538 — the feature-flagged approach with log-only mode is a good migration strategy. Given the size (+1899 lines, 17 files), please ensure all tests pass and check for merge conflicts. One note: the README appears to have a duplicate "Content Security" section heading — please consolidate. |
7dddd2c to
7e0e18c
Compare
|
@crivetimihai
|
|
@madhu-mohan-jaishankar |
fd5e924 to
845b7d5
Compare
ce1798e to
141cc7a
Compare
Review & Rebase SummaryRebased onto current Bugs fixed
Security fixes
Tests added/fixed
All 1857 affected tests passing, 100% coverage on |
crivetimihai
left a comment
There was a problem hiding this comment.
Reviewed, rebased, and fixed 15 issues (8 bugs, 7 security). All tests passing. LGTM.
Add content security validation for MIME types on resources: - ContentSecurityService.validate_resource_mime_type() with configurable allowlist (CONTENT_ALLOWED_RESOURCE_MIMETYPES) and strict/log-only modes (CONTENT_STRICT_MIME_VALIDATION, default: false) - Log-only mode checks the allowlist and logs violations at WARNING level with Prometheus metrics, enabling monitoring before enforcement - ContentTypeError exception with HTTP 415 global handler - URL-detected MIME type priority over user-provided values - Prometheus counters for size and MIME type violations - PII-safe audit logging (hashed emails, masked IPs) - Validation in register_resource, update_resource (including MIME-only updates without content changes), and bulk import Partially closes #538 Signed-off-by: Suresh Kumar Moharajan <suresh.kumar.m@ibm.com> Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
141cc7a to
6eed931
Compare
Add content security validation for MIME types on resources: - ContentSecurityService.validate_resource_mime_type() with configurable allowlist (CONTENT_ALLOWED_RESOURCE_MIMETYPES) and strict/log-only modes (CONTENT_STRICT_MIME_VALIDATION, default: false) - Log-only mode checks the allowlist and logs violations at WARNING level with Prometheus metrics, enabling monitoring before enforcement - ContentTypeError exception with HTTP 415 global handler - URL-detected MIME type priority over user-provided values - Prometheus counters for size and MIME type violations - PII-safe audit logging (hashed emails, masked IPs) - Validation in register_resource, update_resource (including MIME-only updates without content changes), and bulk import Partially closes #538 Signed-off-by: Suresh Kumar Moharajan <suresh.kumar.m@ibm.com> Signed-off-by: Mihai Criveti <crivetimihai@gmail.com> Co-authored-by: Suresh Kumar Moharajan <suresh.kumar.m@ibm.com>
Add content security validation for MIME types on resources: - ContentSecurityService.validate_resource_mime_type() with configurable allowlist (CONTENT_ALLOWED_RESOURCE_MIMETYPES) and strict/log-only modes (CONTENT_STRICT_MIME_VALIDATION, default: false) - Log-only mode checks the allowlist and logs violations at WARNING level with Prometheus metrics, enabling monitoring before enforcement - ContentTypeError exception with HTTP 415 global handler - URL-detected MIME type priority over user-provided values - Prometheus counters for size and MIME type violations - PII-safe audit logging (hashed emails, masked IPs) - Validation in register_resource, update_resource (including MIME-only updates without content changes), and bulk import Partially closes #538 Signed-off-by: Suresh Kumar Moharajan <suresh.kumar.m@ibm.com> Signed-off-by: Mihai Criveti <crivetimihai@gmail.com> Co-authored-by: Suresh Kumar Moharajan <suresh.kumar.m@ibm.com>
🔗 Related Issue
Partially closed #538 (US-2: MIME Type Restrictions for Resources)
📝 Summary
Implements comprehensive content security validation for the MCP Context Forge gateway, adding content size limits and MIME type restrictions for resources and prompts. This addresses US-1 and US-2 from issue #538.
Key Features:
🏷️ Type of Change
🧪 Verification
make lintmake testmake coverageTest Coverage:
test_content_security.py✅ Checklist
make black isort pre-commit)Documentation Added:
US-2-IMPLEMENTATION-PLAN.md(825 lines) - Complete implementation plandocs/docs/architecture/security-features.md- Architecture documentationdocs/docs/manage/content-security.md(394 lines) - Operational guide📓 Notes
Implementation Details
New Files:
mcpgateway/services/content_security.py(378 lines) - Core validation servicetests/unit/mcpgateway/services/test_content_security.py(526 lines) - Comprehensive testsModified Files:
mcpgateway/main.py- Added HTTP 413/415 exception handlersmcpgateway/config.py- Added 4 new configuration settingsmcpgateway/services/resource_service.py- Integrated size & MIME validationmcpgateway/services/prompt_service.py- Integrated size validationConfiguration
Default settings (safe for production):
Deployment Strategy
Phase 1 (Recommended): Deploy with
CONTENT_STRICT_MIME_VALIDATION=falsePhase 2: Enable strict mode with
CONTENT_STRICT_MIME_VALIDATION=trueMetrics
Monitor these Prometheus counters:
content_security_size_violations_total{entity_type="resource|prompt"}content_security_mime_violations_totalRemaining Work (Issue #538)
This PR completes 40% of issue #538 (US-1 and US-2). Remaining user stories: