安全指南#
本文档描述 Cloud Native MCP Server 的安全特性和最佳实践。
Cloud Native MCP Server 提供多层安全保护,包括:
- 认证: API Key、Bearer Token、Basic Auth
- 密钥管理: 安全存储、密钥轮换、密钥生成
- 输入清理: 防止注入攻击
- 审计日志: 跟踪所有操作
- 安全头部: 过滤敏感信息
快速开始#
启用认证#
1
2
3
4
| auth:
enabled: true
mode: "apikey"
apiKey: "Abc123!@#Xyz789!@#Abc123!@#"
|
启用审计日志#
1
2
3
4
5
6
7
8
9
10
| audit:
enabled: true
storage: "database"
database:
type: "sqlite"
sqlitePath: "/var/lib/cloud-native-mcp-server/audit.db"
format: "json"
masking:
enabled: true
maskValue: "***REDACTED***"
|
启用输入清理#
1
2
3
4
5
6
| sanitization:
enabled: true
max_length: 1000
allowed_protocols:
- http
- https
|
输入清理#
所有用户输入都经过清理,以防止注入攻击。
清理特性#
- 过滤值: 移除危险字符(SQL 注入、XSS、命令注入)
- URL 验证: 只允许 http/https 协议
- 长度限制: 最大字符串长度强制(1000 个字符)
- 特殊字符移除: 移除分号、引号和其他注入向量
清理规则#
以下字符会从用户输入中移除:
- SQL 注入:
;, ', ", --, /*, */ - 命令注入:
|, &, $, (, ), <, >, \``, ` - XSS:
<script>, javascript:, onload=, onerror=
审计日志#
审计日志跟踪所有操作,有助于安全监控和合规性。
启用审计日志#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| audit:
enabled: true
level: "info"
storage: "database"
format: "json"
# 敏感数据掩码
masking:
enabled: true
fields:
- password
- token
- apiKey
- secret
- authorization
maskValue: "***REDACTED***"
|
审计事件#
以下事件会被记录:
查询审计日志#
1
2
3
4
5
6
7
8
9
10
11
| # 查询最近 100 条审计日志
curl -H "X-API-Key: your-key" \
"http://localhost:8080/api/audit/logs?limit=100"
# 查询特定用户的审计日志
curl -H "X-API-Key: your-key" \
"http://localhost:8080/api/audit/logs?user=admin&limit=50"
# 查询失败的认证尝试
curl -H "X-API-Key: your-key" \
"http://localhost:8080/api/audit/logs?tool=auth_login&status=failed"
|
安全头部#
服务器会自动过滤调试日志中的敏感头部:
AuthorizationCookieX-API-KeyX-Api-Keyx-api-key
这些头部永远不会以明文形式记录。
自定义安全头部#
1
2
3
4
5
6
7
| security:
headers:
X-Frame-Options: "DENY"
X-Content-Type-Options: "nosniff"
X-XSS-Protection: "1; mode=block"
Strict-Transport-Security: "max-age=31536000; includeSubDomains"
Content-Security-Policy: "default-src 'self'"
|
TLS/SSL 配置#
在生产环境中使用 TLS/SSL 加密通信:
基本 TLS 配置#
1
2
3
4
5
6
7
8
| server:
mode: "sse"
addr: "0.0.0.0:8443"
tls:
certFile: "/path/to/cert.pem"
keyFile: "/path/to/key.pem"
minVersion: "TLS1.2"
maxVersion: "TLS1.3"
|
mTLS 配置#
1
2
3
4
5
6
7
8
| server:
mode: "sse"
addr: "0.0.0.0:8443"
tls:
certFile: "/path/to/server-cert.pem"
keyFile: "/path/to/server-key.pem"
clientAuth: "RequireAndVerifyClientCert"
caFile: "/path/to/ca-cert.pem"
|
速率限制#
防止暴力攻击和滥用:
1
2
3
4
5
| ratelimit:
enabled: true
requests_per_second: 100
burst: 200
cleanup_interval: 60
|
相关文档#