Skip to content

Request Path

Request Path

通过清理和过滤保护免受基于路径的攻击

Traefik 在处理传入请求路径时实现多层安全。 这包括路径清理以规范化潜在的危险序列,以及编码字符过滤以防止使用 URL 编码的攻击向量。 了解 Traefik 如何处理请求路径对于维护安全的路由基础设施至关重要。

Traefik 如何处理请求路径

当 Traefik 收到 HTTP 请求时,它会通过几个以安全为重点的阶段处理请求路径:

1. 编码字符过滤

Traefik 检查路径中潜在的危险编码字符,并拒绝包含它们的请求,除非明确允许。

以下是默认允许的编码字符列表:

编码字符字符
%2f 或 %2F/ (正斜杠)
%5c 或 %5C\ (反斜杠)
%00NULL (空字符)
%3b 或 %3B; (分号)
%25% (百分号)
%3f 或 %3F? (问号)
%23# (井号)

2. 路径规范化

Traefik 通过解码未保留的百分号编码字符来规范化请求路径,因为它们等效于其未编码的形式(根据 rfc3986#section-2.3),并将百分号编码字符大写(根据 rfc3986#section-6.2.2.1)。

3. 路径清理

Traefik 清理请求路径以防止常见的攻击向量,方法是从 URL 中删除 ... 和重复的斜杠段(根据 rfc3986#section-6.2.2.3)。

路径安全配置

Traefik 提供两种主要的路径安全机制,它们协同工作以保护你的应用程序。

路径清理

路径清理默认启用,可通过规范化请求路径帮助防止目录遍历攻击。在 EntryPoints HTTP 部分配置它:

YAML

yaml
entryPoints:
  websecure:
    address: ":443"
    http:
      sanitizePath: true  # 默认:true(推荐)

TOML

toml
[entryPoints.websecure]
  address = ":443"

  [entryPoints.websecure.http]
    sanitizePath = true

CLI

bash
--entryPoints.websecure.address=:443
--entryPoints.websecure.http.sanitizePath=true

清理行为:

  • ./foo/bar/foo/bar(删除相对当前目录)
  • /foo/../bar/bar(解析父目录遍历)
  • /foo/bar///foo/bar/(删除重复斜杠)
  • /./foo/../bar///bar/(组合所有规范化)

编码字符过滤

编码字符过滤通过拒绝潜在的危险 URL 编码字符提供额外的安全层。在 EntryPoints HTTP 部分配置它。

此过滤发生在路径清理之前,可捕获使用编码绕过其它安全控制的攻击尝试。

默认情况下,所有编码字符过滤都处于禁用状态(true 表示允许编码字符)。

安全注意事项

当你的后端不完全符合 RFC 3986 并且特别是在请求路径中解码编码的保留字符时,建议将这些选项设置为 false 以避免分视图情况,并有助于防止路径遍历攻击或绕过安全控制的其它恶意尝试。

YAML

yaml
entryPoints:
  websecure:
    address: ":443"
    http:
      encodedCharacters:
        allowEncodedSlash: false          # %2F - 默认:true
        allowEncodedBackSlash: false      # %5C - 默认:true
        allowEncodedNullCharacter: false  # %00 - 默认:true
        allowEncodedSemicolon: false      # %3B - 默认:true
        allowEncodedPercent: false        # %25 - 默认:true
        allowEncodedQuestionMark: false   # %3F - 默认:true
        allowEncodedHash: false           # %23 - 默认:true

TOML

toml
[entryPoints.websecure]
  address = ":443"

  [entryPoints.websecure.http.encodedCharacters]
    allowEncodedSlash = false
    allowEncodedBackSlash = false
    allowEncodedNullCharacter = false
    allowEncodedSemicolon = false
    allowEncodedPercent = false
    allowEncodedQuestionMark = false
    allowEncodedHash = false

CLI

bash
--entryPoints.websecure.address=:443
--entryPoints.websecure.http.encodedCharacters.allowEncodedSlash=false
--entryPoints.websecure.http.encodedCharacters.allowEncodedBackSlash=false
--entryPoints.websecure.http.encodedCharacters.allowEncodedNullCharacter=false
--entryPoints.websecure.http.encodedCharacters.allowEncodedSemicolon=false
--entryPoints.websecure.http.encodedCharacters.allowEncodedPercent=false
--entryPoints.websecure.http.encodedCharacters.allowEncodedQuestionMark=false
--entryPoints.websecure.http.encodedCharacters.allowEncodedHash=false

按路由的编码字符过滤

如果你需要按路由配置编码字符过滤,可以使用 EncodedCharacters 中间件。 有关详细的实现说明和配置选项,请参阅 EncodedCharacter 中间件 文档。

基于 MIT 协议发布