fix: enhance token server request handling and add max frame length validation to prevent memory issues #3572
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

中文版本
请描述这个PR的作用以及为什么需要它
本PR增强了token服务器的请求处理能力,并增加了最大帧长度验证,以防止潜在的安全风险和性能问题。
当公司安全团队使用nmap进行端口扫描时,可能会向token服务器发送畸形数据包。
这些数据包可能包含异常大的长度字段,导致服务器创建极大的字节数组,从而引发过多的内存消耗和Full GC问题。
现象
在maxFrameLength最大为1024时,解码ping报文时,会创建16M的临时数组,带来内存压力。
复现
com.alibaba.csp.sentinel.demo.cluster.ClusterServerDemobrew install nmapnmap -oX - 127.0.0.1 -p 11111 -T4 -sT -sV -Pn -n --host-timeout 300000ms --max-retries 1 --min-parallelism 16 --max-scan-delay 5scom.alibaba.csp.sentinel.cluster.server.codec.data.PingRequestDataDecoder.decode断点,可以看到解码出超大的length原理
根据namp端口特征 规则库可以看到,DNSVersionBindReqTCP 类型的探测报文会被token server误解码为ping包。
https://raw.githubusercontent.com/nmap/nmap/refs/heads/master/nmap-service-probes
这个PR是否修复了某个问题?
修复了畸形数据包中异常大的长度字段可能导致token服务器过度内存分配和Full GC的问题。
请描述您是如何解决的
ServerConstants.java中添加了一个值为1024的常量NETTY_MAX_FRAME_LENGTH,用于定义允许的最大帧长度。NettyTransportServer.java,在LengthFieldBasedFrameDecoder中使用NETTY_MAX_FRAME_LENGTH常量替代硬编码值。ParamFlowRequestDataDecoder.java,对字符串参数长度进行验证,如果超过最大帧长度则抛出异常。PingRequestDataDecoder.java,检测并记录可能是端口扫描尝试的异常数据包。请描述如何验证这个PR
特别说明(给评审人员)
此修复解决了畸形数据包可能导致过度内存分配的潜在安全和性能问题。该解决方案引入了适当的数据包大小验证和限制,以防止拒绝服务场景的发生。
English Version
Describe what this PR does / why we need it
This PR enhances the token server's request handling and adds max frame length validation to prevent potential security risks and performance issues. When the company's security team performs port scanning using nmap, malformed packets may be sent to the token server. These packets may contain abnormally large length fields, which could cause the server to create extremely large byte arrays, leading to excessive memory consumption and Full GC issues.
Phenomenon
When maxFrameLength is set to a maximum of 1024, decoding ping packets creates a 16M temporary array, causing memory pressure.
Reproduction Steps
com.alibaba.csp.sentinel.demo.cluster.ClusterServerDemobrew install nmapnmap -oX - 127.0.0.1 -p 11111 -T4 -sT -sV -Pn -n --host-timeout 300000ms --max-retries 1 --min-parallelism 16 --max-scan-delay 5scom.alibaba.csp.sentinel.cluster.server.codec.data.PingRequestDataDecoder.decodeto see the decoded oversizedlengthPrinciple
According to the nmap port characteristic rule base, DNSVersionBindReqTCP type probe packets are misdecoded by the token server as ping packets.
https://raw.githubusercontent.com/nmap/nmap/refs/heads/master/nmap-service-probes
Does this pull request fix one issue?
Fixes the issue where malformed packets with abnormally large length fields could cause excessive memory allocation and Full GC in the token server.
Describe how you did it
NETTY_MAX_FRAME_LENGTHwith a value of 1024 inServerConstants.javato define the maximum frame length allowed.NettyTransportServer.javato use theNETTY_MAX_FRAME_LENGTHconstant in theLengthFieldBasedFrameDecoderinstead of a hardcoded value.ParamFlowRequestDataDecoder.javato validate the string parameter length against the maximum frame length and throw an exception if it exceeds the limit.PingRequestDataDecoder.javato detect and log abnormal packets that may be port scanning attempts.Describe how to verify it
Special notes for reviews
This fix addresses a potential security and performance issue where malformed packets could cause excessive memory allocation. The solution introduces proper validation and limits on packet sizes to prevent denial-of-service scenarios.