Skip to content

过滤器

此页面由 docs/rules/filters.md 自动同步生成。

本章介绍控制规则生效条件的过滤器。


包含过滤器,只有满足条件的请求才会应用规则。

pattern rules... includeFilter://condition
条件类型语法说明
方法m:METHOD / m:GET,POST匹配请求方法
状态码s:CODE / s:200-299 / s:200,404,500匹配响应状态码;请求阶段没有状态码时不会命中
请求头存在h:name判断请求头是否存在
请求头匹配h:name=value / reqH:name=/regex/h:reqH: 都匹配请求头;value 可为普通文本或 /regex/
响应头匹配resH:name=value / resH:name=/regex/匹配响应头;仅响应阶段有意义
客户端 IPi:ip / i:cidr匹配客户端 IP 或 CIDR
路径包含/path/ 开头但不以 / 结尾时,按路径包含匹配
路径正则/regex// 开头并以 / 结尾时,按正则匹配路径
URL host/pathexample.com / example.com/api包含 . 的过滤值会按 host 与可选 path 匹配

当前实现会解析 b: / B: body 过滤器,但运行时 resolver 尚未读取请求/响应 body 参与过滤,匹配结果恒为不命中。不要把 body 过滤作为可用能力依赖;需要按内容筛选请使用 bifrost search --req-body/--res-body 查看流量证据。

Terminal window
# 只对 POST 请求生效
www.example.com resHeaders://(X-Method:POST) includeFilter://m:POST
# 只对 GET 请求生效
www.example.com resHeaders://(X-Method:GET) includeFilter://m:GET
# 只对 PUT 请求生效
www.example.com resHeaders://(X-Method:PUT) includeFilter://m:PUT

⚠️ 注意:小括号内容会作为一个整体解析,可以包含空格;多行响应内容建议使用块变量。

Terminal window
# 只对 500 响应生效
www.example.com replaceStatus://200 includeFilter://s:500
# 只对 404 响应生效(使用块变量)
www.example.com resBody://{not-found} includeFilter://s:404
# 对 4xx 响应生效
www.example.com resHeaders://(X-Error: true) includeFilter://s:400-499

块变量定义:

``` not-found
Not Found
```
Terminal window
# 匹配带有特定头的请求
www.example.com host://debug.local includeFilter://h:X-Debug
# 匹配头部值
www.example.com host://admin.local includeFilter://h:X-Role=admin
# 匹配 Content-Type
www.example.com resHeaders://(X-Json: true) includeFilter://h:content-type=application/json
Terminal window
# 匹配响应头
www.example.com resBody://(cached) includeFilter://resH:X-Cache=HIT

注:(cached) 无空格,可使用行内值

Terminal window
# 匹配特定路径
www.example.com resHeaders://(X-Api: true) includeFilter:///api
# 匹配路径模式
www.example.com resDelay://1000 includeFilter:///slow
Terminal window
# AND 条件(同时满足)
www.example.com replaceStatus://200 includeFilter://m:POST includeFilter://s:500
# 方法 + 头部
www.example.com host://special.local includeFilter://m:POST includeFilter://h:X-Special
测试场景规则请求预期
POST 方法test.com resHeaders://(X: 1) includeFilter://m:POSTPOST应用规则
POST 方法test.com resHeaders://(X: 1) includeFilter://m:POSTGET不应用
状态码 500test.com replaceStatus://200 includeFilter://s:500返回 500状态码变 200
状态码 500test.com replaceStatus://200 includeFilter://s:500返回 200不变
头部匹配test.com host://debug includeFilter://h:X-Debug=true有头部应用规则

排除过滤器,满足条件的请求不会应用规则。

pattern rules... excludeFilter://condition

includeFilter 相同的条件语法。

Terminal window
# 排除 GET 请求
www.example.com resDelay://1000 excludeFilter://m:GET
# 排除静态资源
www.example.com resHeaders://(X-Dynamic: true) excludeFilter:///\.js$/ excludeFilter:///\.css$/
# 排除成功响应
www.example.com resHeaders://(X-Error: true) excludeFilter://s:200
# 排除特定头部
www.example.com host://default.local excludeFilter://h:X-Special
测试场景规则请求预期
排除 GETtest.com resHeaders://(X: 1) excludeFilter://m:GETGET不应用
排除 GETtest.com resHeaders://(X: 1) excludeFilter://m:GETPOST应用规则

passthrough:// 用于忽略后续规则并直接透传请求。旧的 ignore:// 写法会在导入、同步或保存时自动转换为 passthrough://

pattern passthrough://
Terminal window
# 透传特定域名
internal.example.com passthrough://
# 透传健康检查路径
*.local/health passthrough://
*.local/metrics passthrough://
测试场景规则请求预期
透传域名ignore-this.local passthrough://ignore-this.local请求直接透传
透传路径*.local/health passthrough:///health请求直接透传

删除请求/响应中的头部、Cookie 或 URL 参数。

pattern delete://target.name
目标语法说明
请求头reqHeaders.name删除请求头
响应头resHeaders.name删除响应头
请求 CookiereqCookies.name删除请求 Cookie
响应 CookieresCookies.name删除响应 Set-Cookie
URL 参数urlParams.name删除 URL 参数
Terminal window
# 删除请求头
www.example.com delete://reqHeaders.X-Custom
# 删除响应头
www.example.com delete://resHeaders.X-Powered-By
# 删除请求 Cookie
www.example.com delete://reqCookies.session
# 删除响应 Cookie
www.example.com delete://resCookies.tracking
# 删除 URL 参数
www.example.com delete://urlParams.debug
Terminal window
# 删除多个头部
www.example.com delete://reqHeaders.X-A delete://reqHeaders.X-B
# 删除多个 Cookie
www.example.com delete://reqCookies.a delete://reqCookies.b
测试场景规则预期
删除请求头test.com delete://reqHeaders.X-Custom请求不含 X-Custom
删除响应头test.com delete://resHeaders.Server响应不含 Server
删除 Cookietest.com delete://reqCookies.sessionCookie 不含 session
删除参数test.com delete://urlParams.debugURL 不含 debug 参数

跳过指定的已命中规则,并继续尝试匹配剩余规则。

pattern skip://pattern=patternString
pattern skip://operation=protocol://value
Terminal window
# 跳过更具体的 pattern,回落到父级规则
www.example.com/api/blocked skip://pattern=www.example.com/api/blocked
# 跳过某条已经命中的操作
www.example.com/api skip://operation=resHeaders://X-Debug:first
  • pattern=...:按规则左侧的 pattern 跳过
  • operation=...:按 protocol://value 跳过
  • 跳过后不会终止匹配;后续规则仍会继续尝试
测试场景规则预期
跳过 operationtest.com skip://operation=resHeaders://X-A:first后续同类规则仍可继续生效
跳过 patterntest.com/api/blocked skip://pattern=test.com/api/blocked请求回落到更通用的规则

过滤器可以与其他规则组合使用:

Terminal window
# 多个过滤器
www.example.com host://backend.local includeFilter://m:POST excludeFilter:///health
# 过滤器 + 修改规则
www.example.com resHeaders://(X-Debug: true) includeFilter://h:X-Debug
# 删除 + 过滤器
www.example.com delete://reqHeaders.X-Internal includeFilter://m:GET
# 条件透传
www.example.com passthrough:// includeFilter:///static

  1. 条件顺序:多个 includeFilter 之间是 AND 关系
  2. 优先级excludeFilter 优先于 includeFilter
  3. 状态码过滤s: 过滤器用于响应阶段的规则
  4. 头部大小写:头部名称匹配不区分大小写
  5. Body 过滤边界b:/B: 当前只被解析,不参与运行时匹配;不要在生产规则里依赖它