feature: Add http request filter for seata-server#7485
feature: Add http request filter for seata-server#7485funky-eyes merged 44 commits intoapache:2.xfrom
Conversation
…caused subsequent controllers to be unable to parse the request parameters
core/src/main/java/org/apache/seata/core/rpc/netty/http/filter/HttpRequestParamWrapper.java
Fixed
Show fixed
Hide fixed
core/src/main/java/org/apache/seata/core/rpc/netty/http/filter/impl/XSSHttpRequestFilter.java
Fixed
Show fixed
Hide fixed
…rnal FullHttpRequest construction for decoding form parameters.
|
@funky-eyes PTAL |
Please resolve the code conflicts and make this PR's functionality cover both HTTP and HTTP2. |
done |
core/src/main/java/org/apache/seata/core/rpc/netty/http/HttpDispatchHandler.java
Outdated
Show resolved
Hide resolved
core/src/main/java/org/apache/seata/core/rpc/netty/http/filter/impl/XSSHttpRequestFilter.java
Show resolved
Hide resolved
...he/seata/spring/boot/autoconfigure/properties/server/filter/ServerHttpFiltersProperties.java
Show resolved
Hide resolved
server/src/main/java/org/apache/seata/server/filter/XSSHttpRequestFilter.java
Outdated
Show resolved
Hide resolved
server/src/main/java/org/apache/seata/server/filter/XSSHttpRequestFilter.java
Show resolved
Hide resolved
# Conflicts: # changes/en-us/2.x.md # changes/zh-cn/2.x.md
funky-eyes
left a comment
There was a problem hiding this comment.
https://github.com/apache/incubator-seata/blob/2.x/script/config-center/config.txt
https://github.com/apache/incubator-seata/blob/2.x/server/src/main/resources/application.example.yml
https://github.com/apache/incubator-seata/blob/2.x/server/src/main/resources/application.raft.example.yml
Please add the newly added configuration to the above setup.
done |
Ⅰ. Describe what this PR did
新增针对来自netty的http请求的过滤器逻辑
不仅仅是支持了XSSFilter,同时预留了扩展点,以便未来轻松接入其他类型的过滤器
新增过滤器系统配置项,以便灵活启用相关过滤器的开启状态
Ⅱ. Does this pull request fix one issue?
fix #7409
Ⅲ. Why don't you add test cases (unit test/integration test)?
Ⅳ. Describe how to verify it
mvn clean test
Ⅴ. Special notes for reviews
Support the parsing logic for filtering four types of parameters: Query, Form, JSON, and Header: The original Seata Server only supported the POST parameter of the form type and did not support the request Body of the JSON type. This modification supplemented the parsing capability of the JSON Body parameter and enhanced the compatibility of the filter for multi-format request bodies to adapt to the parsing of the JSON type request body by the future SeATA-Server
Support the execution of filters in a custom order
Execution link

支持过滤 Query、Form、JSON、Header 四类参数的解析逻辑:原 Seata Server 仅支持表单类型 POST 参数,不支持 JSON 类型请求体,本次改动补充 JSON Body 参数解析能力,增强过滤器对多格式请求体的兼容性以适应未来seata-server对JSON类型请求体的解析
支持 filter 按照自定义顺序执行
其他:
关于XSSHttpRequestFilter判定非法字符的逻辑的一些简单补充
一、背景:
第一版的代码,对 HTTP 请求参数进行 XSS 防御时,使用了一个基于正则表达式的事件处理器检测逻辑,比如拦截
onload="..."、onclick="..."这样的 XSS 注入点。但CodeQL提示这种写法存在两个典型安全风险:
正则表达式灾难性回溯 (ReDoS)
攻击者可以通过精心构造如
"ononononononon..."这样的大量重复前缀,诱发正则引擎在回溯时消耗过多 CPU,导致服务 hang 死或响应超时,形成拒绝服务攻击。超长事件名绕过问题
为了解决 ReDoS,原正则表达式做了长度限制(如
{0,50}),但这样又带来了另一个问题:如果攻击者故意写一个超长非法事件名(比如
onXXXXXXXXXXXXXXXXXXX....=),就可能绕过正则匹配,不会被拦截。二、改造原因:
为了同时解决 性能风险 和 安全漏判,后来对 XSS 关键词检测逻辑进行两方面的改造:
在正则表达式层面加入最大长度约束
限制事件名最长不超过一定字符数,防止正则在超长文本上死回溯。
在正则匹配成功后,追加一层代码逻辑校验:
无论正则命中与否,都对提取到的事件名进行显式长度判断,只要长度超过预设阈值,也直接视为非法,强制拦截。
三、防护原理:
最终的防护方案如下:
第一层防护:正则长度限制
通过对事件名部分加
{0,50}限制,让正则引擎在遇到超长字符串时快速 fail-fast,防止回溯消耗。(这个长度暂定是50,后面根据业务需要可以调整)第二层防护:事件名长度二次校验
即便正则命中了较短片段,也通过
.group(1)获取事件名长度后,再次判断是否超出安全阈值(如 50 字符)。一旦超长,立即判定为高风险,抛异常拒绝。
第三层防护:快速短路 + 单次匹配
每次正则
matcher.find()命中后,逻辑上只处理第一个匹配项,不继续多次 find,避免多次匹配对大输入文本消耗资源。