AlexStocks opened a new issue, #8: URL: https://github.com/apache/dubbo-net/issues/8
## 概述 `DubboHessian2Reader` 在**解析期**按载荷里声明的长度直接分配内存,分配量完全由几个字节决定。 这跟 PR #1 里已经修好的「展开期」预算是两条不同的路径:`ToJsonCompatible` 的节点/体积预算在 parse **之后**才跑,拦不住 parse 期间的分配。响应体 8 MiB 的上限(`DubboService/Native/NativeDubboConnection.cs:215`)也拦不住——触发只需要 6 个字节。 状态:本 issue 记录的问题在 PR #1 的 head `6a9b023` 上仍然存在。 来源:PR #1 的两条 review 评论 [discussion_r4434759962](https://github.com/apache/dubbo-net/pull/1#discussion_r4434759962) / [discussion_r4434759967](https://github.com/apache/dubbo-net/pull/1#discussion_r4434759967) ## 1. 列表声明长度 → `new List<object>(length)` `DubboService/Native/DubboHessian2Reader.cs:238` ```csharp List<object> list = length > 0 ? new List<object>(length) : new List<object>(); ``` `length` 来自 `0x58` 之后的 `ReadInt()`,当前只校验 `length < -1`。`List<object>(capacity)` 会在读到任何元素**之前**就按容量分配引用数组: | 载荷 | 声明长度 | 分配 | |---|---|---| | `58 49 7F FF FF FF`(6 字节) | 2,147,483,647 | `new object[2^31]` ≈ 16 GB | 单次请求通常先拿到 `OutOfMemoryException`(16 GB 连续分配在任何现实机器上都会失败)。真正的风险在并发下的中等值:20 个并发请求各声明 1 亿元素,每个约 800 MB,足以把进程打进 OOM,而每个请求只花了 6 字节。 ## 2. class definition 字段数 → `new string[fieldCount]` `DubboService/Native/DubboHessian2Reader.cs:131` 与 `:137` ```csharp int fieldCount = ReadInt(); ... string[] fields = new string[fieldCount]; ``` 只校验 `fieldCount < 0`。载荷 `43 00 49 7F FF FF FF`(`'C'` + 空类型名 + `int.MaxValue`,7 字节)同样走到 `new string[2,147,483,647]`。 只修列表那条,攻击面只是从 `0x58` 换成了 `'C'`,**两条必须一起修**。 ## 修复建议 每个元素/字段名在 wire 上至少占 1 字节(`ReadValue` 至少读一个 tag,字段名至少 1 字节 tag),所以「声明长度 > 剩余字节数」的输入一定是坏的。这个上界精确、零成本,不需要新增可配置常量。 ```csharp private void EnsurePlausibleCount(int count, string kind) { int remaining = _buffer.Length - _position; if (count > remaining) { throw new InvalidDataException( $"Hessian2 {kind} {count} exceeds the {remaining} remaining payload bytes."); } } ``` ```csharp // ReadList EnsurePlausibleCount(length, "list length"); List<object> list = length > 0 ? new List<object>(length) : new List<object>(); // ReadClassDefinitionAndValue EnsurePlausibleCount(fieldCount, "class field count"); string[] fields = new string[fieldCount]; ``` 这样声明的上限天然被限制在 ≤ 8 MiB 之内。 ## 验收标准 - [ ] `58 49 7F FF FF FF` → `InvalidDataException`,不触发大块分配 - [ ] `43 00 49 7F FF FF FF` → `InvalidDataException` - [ ] 现有 388 项单元测试全绿,特别是 `HessianResponseSafetyTest` 的深度/预算边界用例 - [ ] 截断输入(声明长度 > 实际可用字节)抛可捕获异常(`InvalidDataException` 或 `EndOfStreamException` 均可),不出现未捕获崩溃 - [ ] 正常列表与正常 class definition(声明的长度 ≈ 实际长度)行为不变 ## 备注 `ReadAttachments`、`ReadMapOrObject` 都由上面两条路径驱动,修好这两处即覆盖。`ReadUtf8Chars(int charCount)` 的入参来自 `UInt16`,不需要额外上界。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
