目录

  1. TransportSecuritySettings 配置详解
  2. enable_dns_rebinding_protection 配置
  3. allowed_hosts 和 allowed_origins 白名单配置
  4. StreamableHTTPSessionManager 中的安全设置实例化
  5. 安全行为测试用例分析
  6. 总结

TransportSecuritySettings 配置详解

TransportSecuritySettings 类定义了 MCP 服务器传输安全功能的配置选项,主要用于防范 DNS 重绑定攻击,通过验证传入请求的头部信息来增强安全性。

本节来源

enable_dns_rebinding_protection 配置

enable_dns_rebinding_protection 是一个布尔类型的字段,用于控制是否启用 DNS 重绑定保护。

  • 默认值True
  • 生产环境建议值True

该字段在 TransportSecuritySettings 类中被定义为默认启用,文档中明确标注"recommended for production"(推荐用于生产环境)。当此选项启用时,系统将对请求的 Host 和 Origin 头部进行严格校验;当禁用时,系统将跳过这些安全检查,进入宽松模式。

True
False
Yes
No
请求到达
enable_dns_rebinding_protection
验证Host和Origin头部
跳过安全验证
验证通过?
处理请求
返回错误响应

图源

allowed_hosts 和 allowed_origins 白名单配置

allowed_hostsallowed_origins 是两个字符串列表类型的字段,用于定义允许的 Host 头部值和 Origin 头部值。

配置语法

这两个字段支持两种匹配模式:

  1. 精确匹配:直接指定完整的主机名或源地址
  2. 通配符端口模式:使用 :* 后缀来匹配任意端口

例如,localhost:* 可以匹配 localhost:8080localhost:3000 等所有端口的请求。

通配符端口模式使用方法

通配符端口模式的语法为 host:*,其中 host 是要匹配的主机名,* 表示匹配任意端口。系统通过检查请求头部是否以 host: 开头来判断是否匹配。

No
Yes
Yes
No
获取Host头部
以:*结尾?
精确匹配
提取基础主机名
检查请求Host是否以基础主机名+开头
匹配?
验证通过
验证失败

图源

本节来源

StreamableHTTPSessionManager 中的安全设置实例化

StreamableHTTPSessionManager 中,可以通过构造函数参数传递 TransportSecuritySettings 实例来配置安全选项。

# 创建安全设置实例
security_settings = TransportSecuritySettings(
    enable_dns_rebinding_protection=True,
    allowed_hosts=["localhost", "127.0.0.1", "custom.host"],
    allowed_origins=["http://localhost", "http://127.0.0.1", "http://custom.host"]
)

# 在StreamableHTTPSessionManager中使用安全设置
session_manager = StreamableHTTPSessionManager(
    app=app,
    json_response=False,
    stateless=False,
    security_settings=security_settings,
)

安全设置通过 StreamableHTTPSessionManager__init__ 方法接收,并在处理请求时传递给 StreamableHTTPServerTransport 实例。

创建实例
TransportSecuritySettings
+bool enable_dns_rebinding_protection
+list[str] allowed_hosts
+list[str] allowed_origins
StreamableHTTPSessionManager
-security_settings : TransportSecuritySettings
+__init__(app, event_store, json_response, stateless, security_settings)
+handle_request(scope, receive, send)
StreamableHTTPServerTransport
-security_settings : TransportSecuritySettings
+__init__(mcp_session_id, is_json_response_enabled, event_store, security_settings)

图源

本节来源

安全行为测试用例分析

通过测试用例可以清晰地看到不同配置组合下的安全行为差异。

禁用防护时的宽松模式

enable_dns_rebinding_protection 设置为 False 时,即使提供无效的 Host 头部,请求也能成功。

Client Server SecurityMiddleware POST / (Host : evil.com) validate_request() None (跳过验证) 200 OK Client Server SecurityMiddleware

本节来源

启用防护时的严格校验模式

enable_dns_rebinding_protection 设置为 True 时,系统会对 Host 和 Origin 头部进行严格校验。

Client Server SecurityMiddleware POST / (Host : evil.com) validate_request() _validate_host("evil.com") Response(421, "Invalid Host header") 421 Invalid Host header Client Server SecurityMiddleware

本节来源

自定义白名单配置

可以配置自定义的 allowed_hostsallowed_origins 白名单,允许特定的主机和源进行访问。

Client Server SecurityMiddleware POST / (Host : custom.host) validate_request() _validate_host("custom.host") 检查"custom.host"是否在allowed_hosts中 True _validate_origin("http : //custom.host") 检查"http : //custom.host"是否在allowed_origins中 True 200 OK Client Server SecurityMiddleware

本节来源

总结

TransportSecuritySettings 提供了一套完整的安全配置选项,用于保护 MCP 服务器免受 DNS 重绑定攻击。关键要点包括:

  1. enable_dns_rebinding_protection 默认值为 True,生产环境强烈建议保持启用状态
  2. allowed_hostsallowed_origins 支持精确匹配和通配符端口模式(如 host:*
  3. 安全设置通过 StreamableHTTPSessionManager 传递给底层传输层
  4. 启用防护时进行严格校验,禁用时进入宽松模式
  5. 可以通过自定义白名单来允许特定的主机和源进行访问

这些安全配置为 MCP 服务器提供了灵活而强大的安全保护机制,可以根据具体部署环境进行适当调整。

本节来源

Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐