过滤器
此页面由
docs/rules/filters.md自动同步生成。
本章介绍控制规则生效条件的过滤器。
includeFilter
Section titled “includeFilter”包含过滤器,只有满足条件的请求才会应用规则。
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/ | 匹配响应头;仅响应阶段有意义 |
| 客户端 IP | i:ip / i:cidr | 匹配客户端 IP 或 CIDR |
| 路径包含 | /path | 以 / 开头但不以 / 结尾时,按路径包含匹配 |
| 路径正则 | /regex/ | 以 / 开头并以 / 结尾时,按正则匹配路径 |
| URL host/path | example.com / example.com/api | 包含 . 的过滤值会按 host 与可选 path 匹配 |
当前实现会解析
b:/B:body 过滤器,但运行时 resolver 尚未读取请求/响应 body 参与过滤,匹配结果恒为不命中。不要把 body 过滤作为可用能力依赖;需要按内容筛选请使用bifrost search --req-body/--res-body查看流量证据。
# 只对 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⚠️ 注意:小括号内容会作为一个整体解析,可以包含空格;多行响应内容建议使用块变量。
# 只对 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-foundNot Found```# 匹配带有特定头的请求www.example.com host://debug.local includeFilter://h:X-Debug
# 匹配头部值www.example.com host://admin.local includeFilter://h:X-Role=admin
# 匹配 Content-Typewww.example.com resHeaders://(X-Json: true) includeFilter://h:content-type=application/json# 匹配响应头www.example.com resBody://(cached) includeFilter://resH:X-Cache=HIT注:
(cached)无空格,可使用行内值
# 匹配特定路径www.example.com resHeaders://(X-Api: true) includeFilter:///api
# 匹配路径模式www.example.com resDelay://1000 includeFilter:///slow# 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:POST | POST | 应用规则 |
| POST 方法 | test.com resHeaders://(X: 1) includeFilter://m:POST | GET | 不应用 |
| 状态码 500 | test.com replaceStatus://200 includeFilter://s:500 | 返回 500 | 状态码变 200 |
| 状态码 500 | test.com replaceStatus://200 includeFilter://s:500 | 返回 200 | 不变 |
| 头部匹配 | test.com host://debug includeFilter://h:X-Debug=true | 有头部 | 应用规则 |
excludeFilter
Section titled “excludeFilter”排除过滤器,满足条件的请求不会应用规则。
pattern rules... excludeFilter://condition与 includeFilter 相同的条件语法。
# 排除 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| 测试场景 | 规则 | 请求 | 预期 |
|---|---|---|---|
| 排除 GET | test.com resHeaders://(X: 1) excludeFilter://m:GET | GET | 不应用 |
| 排除 GET | test.com resHeaders://(X: 1) excludeFilter://m:GET | POST | 应用规则 |
passthrough
Section titled “passthrough”passthrough:// 用于忽略后续规则并直接透传请求。旧的 ignore:// 写法会在导入、同步或保存时自动转换为 passthrough://。
pattern passthrough://# 透传特定域名internal.example.com passthrough://
# 透传健康检查路径*.local/health passthrough://*.local/metrics passthrough://| 测试场景 | 规则 | 请求 | 预期 |
|---|---|---|---|
| 透传域名 | ignore-this.local passthrough:// | ignore-this.local | 请求直接透传 |
| 透传路径 | *.local/health passthrough:// | /health | 请求直接透传 |
delete
Section titled “delete”删除请求/响应中的头部、Cookie 或 URL 参数。
pattern delete://target.name| 目标 | 语法 | 说明 |
|---|---|---|
| 请求头 | reqHeaders.name | 删除请求头 |
| 响应头 | resHeaders.name | 删除响应头 |
| 请求 Cookie | reqCookies.name | 删除请求 Cookie |
| 响应 Cookie | resCookies.name | 删除响应 Set-Cookie |
| URL 参数 | urlParams.name | 删除 URL 参数 |
# 删除请求头www.example.com delete://reqHeaders.X-Custom
# 删除响应头www.example.com delete://resHeaders.X-Powered-By
# 删除请求 Cookiewww.example.com delete://reqCookies.session
# 删除响应 Cookiewww.example.com delete://resCookies.tracking
# 删除 URL 参数www.example.com delete://urlParams.debug# 删除多个头部www.example.com delete://reqHeaders.X-A delete://reqHeaders.X-B
# 删除多个 Cookiewww.example.com delete://reqCookies.a delete://reqCookies.b| 测试场景 | 规则 | 预期 |
|---|---|---|
| 删除请求头 | test.com delete://reqHeaders.X-Custom | 请求不含 X-Custom |
| 删除响应头 | test.com delete://resHeaders.Server | 响应不含 Server |
| 删除 Cookie | test.com delete://reqCookies.session | Cookie 不含 session |
| 删除参数 | test.com delete://urlParams.debug | URL 不含 debug 参数 |
跳过指定的已命中规则,并继续尝试匹配剩余规则。
pattern skip://pattern=patternStringpattern skip://operation=protocol://value# 跳过更具体的 pattern,回落到父级规则www.example.com/api/blocked skip://pattern=www.example.com/api/blocked
# 跳过某条已经命中的操作www.example.com/api skip://operation=resHeaders://X-Debug:firstpattern=...:按规则左侧的 pattern 跳过operation=...:按protocol://value跳过- 跳过后不会终止匹配;后续规则仍会继续尝试
| 测试场景 | 规则 | 预期 |
|---|---|---|
| 跳过 operation | test.com skip://operation=resHeaders://X-A:first | 后续同类规则仍可继续生效 |
| 跳过 pattern | test.com/api/blocked skip://pattern=test.com/api/blocked | 请求回落到更通用的规则 |
过滤器可以与其他规则组合使用:
# 多个过滤器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- 条件顺序:多个
includeFilter之间是 AND 关系 - 优先级:
excludeFilter优先于includeFilter - 状态码过滤:
s:过滤器用于响应阶段的规则 - 头部大小写:头部名称匹配不区分大小写
- Body 过滤边界:
b:/B:当前只被解析,不参与运行时匹配;不要在生产规则里依赖它