oscerd commented on code in PR #28846:
URL: https://github.com/apache/camel/pull/28846#discussion_r4441197351


##########
components/camel-syslog/src/generated/java/org/apache/camel/component/syslog/SyslogDataFormatConfigurer.java:
##########
@@ -2,7 +2,6 @@
 package org.apache.camel.component.syslog;
 
 import javax.annotation.processing.Generated;
-import java.util.Locale;

Review Comment:
   The "Regen" commit that followed restored the `java.util.Locale` import, so 
this file is no longer part of the PR (the diff is back to the decoder and its 
test).
   
   _Claude Code on behalf of oscerd_
   



##########
components/camel-syslog/src/main/java/org/apache/camel/component/syslog/netty/Rfc5425FrameDecoder.java:
##########
@@ -33,33 +32,29 @@ public class Rfc5425FrameDecoder extends 
ByteToMessageDecoder implements Channel
     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> 
out) throws Exception {
         if (currentFramelength == null) {
             // find index of the first space, it should be after the length 
field
-            int index = indexOf(in, Unpooled.wrappedBuffer(new byte[] { ' ' 
}));
-
-            // Read part until the first space, if we have found one
-            StringBuilder lengthbuffer = new StringBuilder();
-            if (index > -1) {
-                ByteBuf byteBuf = in.readBytes(index);
-                byte[] dest = new byte[byteBuf.readableBytes()];
-                byteBuf.readBytes(dest);
-                lengthbuffer.append(new String(dest));
+            int spaceIndex = in.indexOf(in.readerIndex(), in.writerIndex(), 
(byte) ' ');
+            if (spaceIndex < 0) {
+                // The length delimiter has not arrived yet, wait for more 
data without modifying reader index
+                return;
             }
 
-            int length;
-            try {
-                // add one because we have to take in account the space after
-                // the length field
-                length = Integer.parseInt(lengthbuffer.toString()) + 1;
-            } catch (NumberFormatException e) {
-                length = -1;
+            int lengthDigits = spaceIndex - in.readerIndex();
+            if (lengthDigits == 0) {
+                throw new IllegalArgumentException("Invalid RFC 5425 frame: 
empty length field");
             }
 
-            // We have not found the length field, reset the buffer so we can
-            // retry next time
-            if (length < 0) {
-                in.resetReaderIndex();
-                return;
+            int length = 0;
+            for (int i = 0; i < lengthDigits; i++) {
+                byte b = in.getByte(in.readerIndex() + i);
+                if (b < '0' || b > '9') {
+                    throw new IllegalArgumentException("Invalid RFC 5425 
frame: non-digit character in length field");
+                }
+                length = length * 10 + (b - '0');

Review Comment:
   Fixed in the commit "CAMEL-26481: Address review feedback - reject a length 
field that does not fit an int, test the rejected frames": the loop now throws 
`Invalid RFC 5425 frame: length field does not fit an int` as soon as the next 
digit would take the value past `Integer.MAX_VALUE - 1` (the frame is read as 
`length + 1` bytes), so `4294967296` and `2147483647` are rejected instead of 
wrapping. The rejection also drops the buffered bytes, so the connection closed 
on the exception does not decode them again on the way out. Tests: 
`testLengthFieldWrappingAnIntIsRejected`, 
`testLengthFieldAtIntegerMaxIsRejected`, and 
`testTenDigitLengthFieldThatFitsIsAccepted` for the largest accepted field.
   
   _Claude Code on behalf of oscerd_
   



##########
components/camel-syslog/src/main/java/org/apache/camel/component/syslog/netty/Rfc5425FrameDecoder.java:
##########
@@ -33,33 +32,29 @@ public class Rfc5425FrameDecoder extends 
ByteToMessageDecoder implements Channel
     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> 
out) throws Exception {
         if (currentFramelength == null) {
             // find index of the first space, it should be after the length 
field
-            int index = indexOf(in, Unpooled.wrappedBuffer(new byte[] { ' ' 
}));
-
-            // Read part until the first space, if we have found one
-            StringBuilder lengthbuffer = new StringBuilder();
-            if (index > -1) {
-                ByteBuf byteBuf = in.readBytes(index);
-                byte[] dest = new byte[byteBuf.readableBytes()];
-                byteBuf.readBytes(dest);
-                lengthbuffer.append(new String(dest));
+            int spaceIndex = in.indexOf(in.readerIndex(), in.writerIndex(), 
(byte) ' ');
+            if (spaceIndex < 0) {
+                // The length delimiter has not arrived yet, wait for more 
data without modifying reader index
+                return;
             }
 
-            int length;
-            try {
-                // add one because we have to take in account the space after
-                // the length field
-                length = Integer.parseInt(lengthbuffer.toString()) + 1;
-            } catch (NumberFormatException e) {
-                length = -1;
+            int lengthDigits = spaceIndex - in.readerIndex();
+            if (lengthDigits == 0) {
+                throw new IllegalArgumentException("Invalid RFC 5425 frame: 
empty length field");
             }
 
-            // We have not found the length field, reset the buffer so we can
-            // retry next time
-            if (length < 0) {
-                in.resetReaderIndex();
-                return;
+            int length = 0;
+            for (int i = 0; i < lengthDigits; i++) {
+                byte b = in.getByte(in.readerIndex() + i);
+                if (b < '0' || b > '9') {
+                    throw new IllegalArgumentException("Invalid RFC 5425 
frame: non-digit character in length field");
+                }
+                length = length * 10 + (b - '0');

Review Comment:
   Fixed in the commit "CAMEL-26481: Address review feedback - reject a length 
field that does not fit an int, test the rejected frames": the loop now throws 
`Invalid RFC 5425 frame: length field does not fit an int` as soon as the next 
digit would take the value past `Integer.MAX_VALUE - 1` (the frame is read as 
`length + 1` bytes), so `4294967296` and `2147483647` are rejected instead of 
wrapping. The rejection also drops the buffered bytes, so the connection closed 
on the exception does not decode them again on the way out. Tests: 
`testLengthFieldWrappingAnIntIsRejected`, 
`testLengthFieldAtIntegerMaxIsRejected`, and 
`testTenDigitLengthFieldThatFitsIsAccepted` for the largest accepted field.
   
   _Claude Code on behalf of oscerd_
   



##########
components/camel-syslog/src/test/java/org/apache/camel/component/syslog/netty/Rfc5425FrameDecoderTest.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.
+ */
+package org.apache.camel.component.syslog.netty;
+
+import java.nio.charset.StandardCharsets;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.embedded.EmbeddedChannel;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class Rfc5425FrameDecoderTest {
+
+    @Test
+    void testFragmentedLengthFieldDoesNotDuplicateFrames() {
+        EmbeddedChannel channel = new EmbeddedChannel(new 
Rfc5425FrameDecoder());
+
+        // Write first message and partial length of next message: "3 one5"
+        assertTrue(channel.writeInbound(Unpooled.copiedBuffer("3 one5", 
StandardCharsets.UTF_8)));
+
+        // Exactly one frame (" one") should be produced
+        ByteBuf frame1 = channel.readInbound();
+        assertNotNull(frame1);
+        assertEquals(" one", frame1.toString(StandardCharsets.UTF_8));
+        frame1.release();
+
+        // No duplicate frame should be produced
+        assertNull(channel.readInbound());
+
+        // Write remainder of second message: " three"
+        assertTrue(channel.writeInbound(Unpooled.copiedBuffer(" three", 
StandardCharsets.UTF_8)));
+
+        // Second frame (" three") should be produced
+        ByteBuf frame2 = channel.readInbound();
+        assertNotNull(frame2);
+        assertEquals(" three", frame2.toString(StandardCharsets.UTF_8));
+        frame2.release();
+
+        assertNull(channel.readInbound());
+        assertFalse(channel.finish());
+    }
+
+    @Test
+    void testFragmentedLengthDigitsAcrossPackets() {
+        EmbeddedChannel channel = new EmbeddedChannel(new 
Rfc5425FrameDecoder());
+
+        // First packet contains only partial length digit: "1"
+        assertFalse(channel.writeInbound(Unpooled.copiedBuffer("1", 
StandardCharsets.UTF_8)));
+        assertNull(channel.readInbound());
+
+        // Second packet contains remaining length digit, space, and body: "1 
hello world"
+        assertTrue(channel.writeInbound(Unpooled.copiedBuffer("1 hello world", 
StandardCharsets.UTF_8)));
+
+        ByteBuf frame = channel.readInbound();
+        assertNotNull(frame);
+        assertEquals(" hello world", frame.toString(StandardCharsets.UTF_8));
+        frame.release();
+
+        assertNull(channel.readInbound());
+        assertFalse(channel.finish());
+    }

Review Comment:
   Added in the commit "CAMEL-26481: Address review feedback - reject a length 
field that does not fit an int, test the rejected frames": 
`testEmptyLengthFieldIsRejected`, `testNonDigitLengthFieldIsRejected` and the 
two overflow cases, each asserting the `DecoderException` that 
`EmbeddedChannel.writeInbound` rethrows and its message.
   
   _Claude Code on behalf of oscerd_
   



##########
components/camel-syslog/src/main/java/org/apache/camel/component/syslog/netty/Rfc5425FrameDecoder.java:
##########
@@ -33,33 +32,29 @@ public class Rfc5425FrameDecoder extends 
ByteToMessageDecoder implements Channel
     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> 
out) throws Exception {
         if (currentFramelength == null) {
             // find index of the first space, it should be after the length 
field
-            int index = indexOf(in, Unpooled.wrappedBuffer(new byte[] { ' ' 
}));
-
-            // Read part until the first space, if we have found one
-            StringBuilder lengthbuffer = new StringBuilder();
-            if (index > -1) {
-                ByteBuf byteBuf = in.readBytes(index);
-                byte[] dest = new byte[byteBuf.readableBytes()];
-                byteBuf.readBytes(dest);
-                lengthbuffer.append(new String(dest));
+            int spaceIndex = in.indexOf(in.readerIndex(), in.writerIndex(), 
(byte) ' ');
+            if (spaceIndex < 0) {
+                // The length delimiter has not arrived yet, wait for more 
data without modifying reader index
+                return;
             }
 
-            int length;
-            try {
-                // add one because we have to take in account the space after
-                // the length field
-                length = Integer.parseInt(lengthbuffer.toString()) + 1;
-            } catch (NumberFormatException e) {
-                length = -1;
+            int lengthDigits = spaceIndex - in.readerIndex();
+            if (lengthDigits == 0) {
+                throw new IllegalArgumentException("Invalid RFC 5425 frame: 
empty length field");
             }
 
-            // We have not found the length field, reset the buffer so we can
-            // retry next time
-            if (length < 0) {
-                in.resetReaderIndex();
-                return;
+            int length = 0;
+            for (int i = 0; i < lengthDigits; i++) {
+                byte b = in.getByte(in.readerIndex() + i);
+                if (b < '0' || b > '9') {
+                    throw new IllegalArgumentException("Invalid RFC 5425 
frame: non-digit character in length field");
+                }
+                length = length * 10 + (b - '0');
             }
-            currentFramelength = length;
+
+            // add one because we have to take in account the space after the 
length field
+            currentFramelength = length + 1;
+            in.skipBytes(lengthDigits);

Review Comment:
   Fixed in the commit "CAMEL-26481: Address review feedback - reject a length 
field that does not fit an int, test the rejected frames": the loop now throws 
`Invalid RFC 5425 frame: length field does not fit an int` as soon as the next 
digit would take the value past `Integer.MAX_VALUE - 1` (the frame is read as 
`length + 1` bytes), so `4294967296` and `2147483647` are rejected instead of 
wrapping. The rejection also drops the buffered bytes, so the connection closed 
on the exception does not decode them again on the way out. Tests: 
`testLengthFieldWrappingAnIntIsRejected`, 
`testLengthFieldAtIntegerMaxIsRejected`, and 
`testTenDigitLengthFieldThatFitsIsAccepted` for the largest accepted field.
   
   _Claude Code on behalf of oscerd_
   



-- 
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]

Reply via email to