AlexStocks commented on code in PR #18: URL: https://github.com/apache/dubbo-net/pull/18#discussion_r4463444510
########## DubboService/Native/Hessian2/Hessian2ByteCursor.cs: ########## @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Buffers.Binary; +using System.IO; + +namespace DubboNet.DubboService.Native.Hessian2 +{ + // Owns the input buffer and cursor; Hessian2 tags and decoded values belong to the caller. + internal sealed class Hessian2ByteCursor + { + private readonly byte[] _buffer; + private int _position; + + internal Hessian2ByteCursor(byte[] buffer) + { + _buffer = buffer ?? throw new ArgumentNullException(nameof(buffer)); + } + + internal int Remaining => _buffer.Length - _position; + + internal byte PeekByte() + { + EnsureAvailable(1); + return _buffer[_position]; + } + + internal byte ReadByte() + { + EnsureAvailable(1); + return _buffer[_position++]; + } + + internal int ReadUInt16() + { + EnsureAvailable(2); + int value = BinaryPrimitives.ReadUInt16BigEndian(_buffer.AsSpan(_position, 2)); + _position += 2; + return value; + } + + internal int ReadInt32() + { + EnsureAvailable(4); + int value = BinaryPrimitives.ReadInt32BigEndian(_buffer.AsSpan(_position, 4)); + _position += 4; + return value; + } + + internal long ReadInt64() + { + EnsureAvailable(8); + long value = BinaryPrimitives.ReadInt64BigEndian(_buffer.AsSpan(_position, 8)); + _position += 8; + return value; + } + + internal byte[] ReadBytes(int count) + { + EnsureAvailable(count); + byte[] result = new byte[count]; + Buffer.BlockCopy(_buffer, _position, result, 0, count); + _position += count; + return result; + } + + internal void CopyTo(Stream destination, int count) Review Comment: 💭 **游标开始依赖 `System.IO.Stream` 了,这一点可以不要** 现在的分工是「`Hessian2ByteCursor` 独占缓冲区、位置和边界检查」。但 `CopyTo(Stream destination, int count)` 让它同时负责「往哪儿搬数据」,于是这个类型必须 `using System.IO`,而它唯一的生产调用方只是把分块二进制塞进 `MemoryStream`。 真正想消除的是「调用方自己算偏移、自己 `_position += n`」,不是「调用方不能碰字节」。所以下面这个签名同样能达到目的,而且把搬运策略留在 reader 里: ```csharp internal ReadOnlySpan<byte> ReadSpan(int count) { EnsureAvailable(count); ReadOnlySpan<byte> span = _buffer.AsSpan(_position, count); _position += count; return span; } ``` reader 侧 `chunks.Write(_byteCursor.ReadSpan(length))` 就行。附带好处:`MemoryStream.Write(ReadOnlySpan<byte>)` 少一次 `byte[] + offset` 的间接寻址,`ReadBytes` 也可以用 `ReadSpan(count).ToArray()` 实现,边界检查只剩一处。 **注意**这不是要你改 `ReadBytes` 的语义——返回 `Span` 时底层仍是同一个缓冲区,`Hessian2ByteCursorTest.ReadBytesCopiesWithoutChangingTheSourceBuffer` 那个「拷贝后改源数组不影响结果」的断言在 `ReadSpan` 下不成立,两者要并存或分开命名。如果觉得当前 `CopyTo` 够用,也可以只在注释里写明「刻意让游标负责搬运,避免 Span 生命周期外泄」,那我就把这条当已决。 ########## DubboService/Native/Hessian2/Hessian2Tags.cs: ########## @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace DubboNet.DubboService.Native.Hessian2 +{ + // Wire-format values used by the dynamic Hessian2 response reader. + internal static class Hessian2Tags + { + internal const byte Null = (byte)'N'; + internal const byte True = (byte)'T'; + internal const byte False = (byte)'F'; + internal const byte End = (byte)'Z'; + internal const byte Reference = 0x51; + + internal const byte Int = (byte)'I'; + internal const byte CompactIntFirstTag = 0x80; + internal const byte CompactIntLastTag = 0xbf; + internal const byte CompactIntZeroTag = 0x90; + internal const byte CompactIntTwoByteFirstTag = 0xc0; + internal const byte CompactIntTwoByteLastTag = 0xcf; + internal const byte CompactIntTwoByteZeroTag = 0xc8; + internal const byte CompactIntThreeByteFirstTag = 0xd0; + internal const byte CompactIntThreeByteLastTag = 0xd7; + internal const byte CompactIntThreeByteZeroTag = 0xd4; + + internal const byte Long = (byte)'L'; + internal const byte LongAsInt = 0x59; + internal const byte CompactLongFirstTag = 0xd8; + internal const byte CompactLongLastTag = 0xef; + internal const byte CompactLongZeroTag = 0xe0; + internal const byte CompactLongTwoByteFirstTag = 0xf0; + internal const byte CompactLongTwoByteLastTag = 0xff; + internal const byte CompactLongTwoByteZeroTag = 0xf8; + internal const byte CompactLongThreeByteFirstTag = 0x38; + internal const byte CompactLongThreeByteLastTag = 0x3f; + internal const byte CompactLongThreeByteZeroTag = 0x3c; + + internal const byte Double = (byte)'D'; // 0x44 + internal const byte DoubleZero = 0x5b; + internal const byte DoubleOne = 0x5c; + internal const byte DoubleAsByte = 0x5d; + internal const byte DoubleAsShort = 0x5e; + internal const byte DoubleAsMillisecond = 0x5f; Review Comment: 💭 **这个 tag 的名字对不对,取决于你信哪个规范——建议把结论写进注释** `0x5f` 在 Caucho 官方 Hessian 2.0 bytecode map 里是 *double 以 float 表示*(4 字节 IEEE-754 单精度)。但本实现是「32 位整数 × 0.001」,并且这个名字 `DoubleAsMillisecond` 也是按后者取的。 我核对了 Dubbo 实际用的 `apache/dubbo-hessian-lite`,`Hessian2Output.writeDouble` 是: ```java int mills = (int) (value * 1000); if (0.001 * mills == value) { buffer[0] = (byte) BC_DOUBLE_MILL; // 0x5f // 写入 4 字节 mills } ``` `Hessian2Input` 对应分支也是 `return 0.001 * mills;`。所以 **实现和命名都对,与规范不一致的是 Dubbo 生态本身**。 **为什么值得加注释而不是只留名字:** `0x5f` 不是冷门分支。`2.5`、`0.25`、`100.5` 这类能精确表示为 float 的 double 都会走它(`intValue == value` 不成立时才继续往下判 mills)。一个拿着公开规范读这段代码的评审者,最可能的动作就是「这里应该是 float,改成 `BitConverter.Int32BitsToSingle`」——改完 `2.5` 会读成 `1.07e9`,而现有用例里只有 `[InlineData("5F000003E8", 1d)]` 一条能挡住,很容易被当成「预期变了」顺手改掉。 **Suggestion:** ```csharp // 与 Caucho 规范不同:Dubbo 的 hessian-lite 把 0x5f 用作 BC_DOUBLE_MILL, // 即「毫秒精度的整数」。任何可精确表示为 float 的 double(2.5、0.25)都会走这里。 // 不要按公开规范改成 4 字节 float,否则会读出错值。 internal const byte DoubleAsMillisecond = 0x5f; ``` ########## Tests/UnitTestForDubboNet/Hessian2ByteCursorTest.cs: ########## @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using DubboNet.DubboService.Native.Hessian2; + +namespace UnitTestForDubboNet; + +public class Hessian2ByteCursorTest +{ + [Fact] + public void PeekDoesNotAdvanceAndSequentialReadsConsumeExactlyOneByte() + { + var input = new Hessian2ByteCursor(new byte[] { 0x80, 0x01 }); + Assert.Equal(2, input.Remaining); + Assert.Equal(0x80, input.PeekByte()); + Assert.Equal(0x80, input.PeekByte()); + Assert.Equal(2, input.Remaining); + Assert.Equal(0x80, input.ReadByte()); + Assert.Equal(1, input.Remaining); + Assert.Equal(0x01, input.ReadByte()); + Assert.Equal(0, input.Remaining); + } + + [Fact] + public void FixedWidthIntegersUseBigEndianOrderAndAdvanceOnce() + { + var input = new Hessian2ByteCursor(Convert.FromHexString( + "1234010203040102030405060708FF")); + Assert.Equal(0x1234, input.ReadUInt16()); + Assert.Equal(0x01020304, input.ReadInt32()); + Assert.Equal(0x0102030405060708L, input.ReadInt64()); + Assert.Equal(0xff, input.PeekByte()); + Assert.Equal(1, input.Remaining); + } + + [Fact] + public void ReadBytesCopiesWithoutChangingTheSourceBuffer() + { + byte[] source = { 0x11, 0x22, 0x33 }; + var input = new Hessian2ByteCursor(source); + byte[] copy = input.ReadBytes(2); + source[0] = 0xff; + Assert.Equal(new byte[] { 0x11, 0x22 }, copy); + Assert.Equal(0x33, input.ReadByte()); + Assert.Equal(0, input.Remaining); + } + + [Fact] + public void CopyToWritesOnlyRequestedBytesAndLeavesNextTagUnread() + { + var input = new Hessian2ByteCursor(new byte[] { 0xab, 0xcd, 0x54 }); + using var destination = new MemoryStream(); + input.CopyTo(destination, 2); + Assert.Equal(new byte[] { 0xab, 0xcd }, destination.ToArray()); + Assert.Equal(0x54, input.PeekByte()); + Assert.Equal(1, input.Remaining); + input.CopyTo(destination, 0); + Assert.Equal(1, input.Remaining); + } + + [Fact] + public void ConstructorPreservesTheExistingNullArgumentName() + { + var error = Assert.Throws<ArgumentNullException>(() => new Hessian2ByteCursor(null!)); + Assert.Equal("buffer", error.ParamName); + } + + [Theory] + [InlineData(0)] // Peek + [InlineData(1)] // Single byte + [InlineData(2)] // Two-byte integer + [InlineData(4)] // Four-byte integer + [InlineData(8)] // Eight-byte integer + public void TruncatedReadsDoNotConsumeInput(int width) + { + var input = new Hessian2ByteCursor(new byte[] { 0x12 }); + Assert.Equal(0x12, input.PeekByte()); + Assert.Equal(0x12, input.ReadByte()); + Assert.Equal(0, input.Remaining); + Action read = width switch + { + 0 => () => input.PeekByte(), + 1 => () => input.ReadByte(), + 2 => () => input.ReadUInt16(), + 4 => () => input.ReadInt32(), + _ => () => input.ReadInt64() + }; + var error = Assert.Throws<EndOfStreamException>(read); + Assert.Equal("Unexpected end of Hessian2 payload.", error.Message); + Assert.Equal(0, input.Remaining); + } + + [Theory] + [InlineData(2)] + [InlineData(4)] + [InlineData(8)] + public void PartialFixedWidthReadLeavesExistingBytesUnread(int width) + { + // Consume one byte first so a failed read cannot silently reset the cursor. + byte[] wire = new byte[] { 0xaa } + .Concat(Enumerable.Range(1, width - 1).Select(value => (byte)value)) + .ToArray(); + var input = new Hessian2ByteCursor(wire); + Assert.Equal(0xaa, input.ReadByte()); + int remaining = width - 1; + Action read = width switch + { + 2 => () => input.ReadUInt16(), + 4 => () => input.ReadInt32(), + _ => () => input.ReadInt64() + }; + + var error = Assert.Throws<EndOfStreamException>(read); + Assert.Equal("Unexpected end of Hessian2 payload.", error.Message); + Assert.Equal(remaining, input.Remaining); + Assert.Equal(0x01, input.PeekByte()); + Assert.Equal(0x01, input.ReadByte()); + Assert.Equal(remaining - 1, input.Remaining); + } + + [Theory] + [InlineData(-1)] + [InlineData(2)] + public void InvalidCopyOrReadLengthDoesNotConsumeInput(int length) Review Comment: 💭 **这个用例缺了最该有的那个值:`int.MaxValue`** 现在覆盖 `-1` 和 `2`,正好跳过了一个真实的对抗性输入:`#8` 里那条「6 字节载荷要求 16 GB」的攻击,声明的长度就是 `0x7FFFFFFF`。而 `EnsureAvailable` 的判断是 ```csharp if (count < 0 || _position > _buffer.Length - count) ``` `_buffer.Length - count` 在 `count = int.MaxValue` 时是**负数**——靠「`_position` 不可能小于负数」偶然成立。后来的人只要把它「优化」成 `_position + count > _buffer.Length`,`int.MaxValue + 1` 就溢出成 `int.MinValue`,判断恒为 false,边界检查整条失效,而 `ReadBytes` 紧接着就 `new byte[count]`。 **Suggestion:** 加两行,同时钉住溢出: ```csharp [Theory] [InlineData(int.MinValue)] [InlineData(-1)] [InlineData(2)] [InlineData(int.MaxValue)] // 声明长度溢出边界:必须走同一个判定 public void InvalidCopyOrReadLengthDoesNotConsumeInput(int length) ``` 这条对一个「唯一职责就是挡住越界」的类型来说,成本几乎为零。 -- 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]
