allthingssecurity opened a new pull request, #29894:
URL: https://github.com/apache/camel/pull/29894

   # Description
   
   [CAMEL-26903](https://issues.apache.org/jira/browse/CAMEL-26903)
   
   Named as a follow-up in #29043 (CAMEL-26433, maxFrameLength for the MINA 
codec), which also made the decoder compare the bytes unsigned.
   
   `HL7MLLPDecoder` (the MINA codec of camel-hl7) keeps the previous byte to 
find the end bytes (end byte 1, then end byte 2). A start byte, and end bytes 
that come before any start byte (which the decoder ignores with a WARN), did 
not reset it. So with an end byte 1 before the start byte (`\x1c\x0b\r...`, or 
an ignored end `\x1c\r` followed by `\x0b\r...`), an end byte 2 right after the 
start byte (the separator of an empty first segment) was taken as the end of a 
message of two bytes, whose length without the start and end bytes is negative. 
The decoder then failed with an `IllegalArgumentException` ("fieldSize cannot 
be negative: -1"), or a `NegativeArraySizeException` with 
`produceString=false`, and the MINA consumer closed the session of that sender. 
It needs both a stray end byte 1 before the start byte and a message whose 
first byte is the end byte 2, so it is rare.
   
   Change, in `HL7MLLPDecoder`: when a start byte starts a message, the 
previous byte is reset to `-1`, a value no byte has (the bytes are compared 
unsigned since CAMEL-26433). Its initial value and the two places that reset it 
to `0` use `-1` too, so a configured end byte 1 of `0x00` is not matched after 
a reset either. Nothing changes once a message has started, nor for ignored end 
bytes. With the default bytes, only an end byte 2 right after the start byte is 
read differently.
   
   Tests, new `HL7MLLPDecoderEndBytesBeforeStartTest` (decoder level, as 
`HL7MLLPDecoderMaxFrameLengthTest`):
   - `endByte1RightBeforeTheStartByte`: `\x1c` + start + `\rMSH|A` + end is 
decoded as `\rMSH|A`;
   - `endBytesWithoutStartBeforeTheStartByte`: an ignored end, then that 
message and a next one, both decoded;
   - `endBytesWithoutStartBeforeTheStartByteAsBytes`: the same with 
`produceString=false`;
   - `endByte1BeforeTheStartByteInAnotherRead`: the stray end byte 1 in one 
read, the message in the next;
   - `endByte1Zero`: with an end byte 1 of `0x00`, a message that starts with 
the end byte 2 is decoded;
   - `endBytesWithoutStartBeforeAMessage`: a control, an ignored end before a 
message that starts with `MSH`.
   
   Without the change the first five fail (`fieldSize cannot be negative: -1`, 
`NegativeArraySizeException`), the control passes. With it the camel-hl7 tests 
pass (106 run, 0 failures).
   
   The Netty decoder (`HL7MLLPNettyDecoder`) splits frames on the two end bytes 
and is not affected, nor is camel-mllp, which looks for the end bytes only 
after the start byte.
   
   Backport: 4.18.x and 4.14.x have the same bug, but their decoder compares 
signed bytes (the unsigned reads came with CAMEL-26433, on main only), so this 
commit does not cherry-pick there as is. Given how rare the input is, I would 
keep it on main.
   
   No documentation or upgrade guide change.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it): 
[CAMEL-26903](https://issues.apache.org/jira/browse/CAMEL-26903).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
     (I built the core modules, camel-mina, camel-netty and camel-hl7 with the 
formatter and import-sort plugins and ran the camel-hl7 tests. I did not run 
the full root build.)
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
     This PR was prepared with Claude Code (Claude Opus 5.5). The commit 
carries a `Co-Authored-By` trailer.
   
   _Claude Code on behalf of allthingssecurity_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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