@@ -443,4 +443,57 @@ void testInvalidEscapeHeadersSequence() throws Exception { |
443 | 443 | assertEquals("received an invalid escape header sequence 'custom_invalid\\t'", |
444 | 444 | headersSubFrame.decoderResult().cause().getMessage()); |
445 | 445 | } |
| 446 | + |
| 447 | + @Test |
| 448 | + public void testCRResetsUtf8DecodeState() throws Exception { |
| 449 | + // When a CR byte appears during a multi-byte UTF-8 sequence, the parser's interim state |
| 450 | + // should be cleared so that subsequent bytes are decoded correctly. |
| 451 | + // Bug: CR is skipped without resetting interim/nextRead, so the next byte gets |
| 452 | + // incorrectly combined with dirty UTF-8 state, producing garbage characters. |
| 453 | + // |
| 454 | + // Craft a header value where: |
| 455 | + // - 0xC3 starts a 2-byte UTF-8 sequence (sets interim) |
| 456 | + // - 0x0D (CR) is skipped but should clear interim state |
| 457 | + // - 0x41 ('A') should be decoded as plain ASCII 'A', not combined with dirty interim |
| 458 | + channel = new EmbeddedChannel(new StompSubframeDecoder()); |
| 459 | + |
| 460 | + ByteBuf incoming = Unpooled.buffer(); |
| 461 | + // CONNECT command line |
| 462 | + incoming.writeBytes("CONNECT\r\n".getBytes(UTF_8)); |
| 463 | + // header with multi-byte UTF-8 start byte (0xC3), then CR, then ASCII 'A' |
| 464 | + incoming.writeByte((byte) 'h'); |
| 465 | + incoming.writeByte((byte) 'e'); |
| 466 | + incoming.writeByte((byte) 'a'); |
| 467 | + incoming.writeByte((byte) 'd'); |
| 468 | + incoming.writeByte((byte) 'e'); |
| 469 | + incoming.writeByte((byte) 'r'); |
| 470 | + incoming.writeByte((byte) ':'); |
| 471 | + // 0xC3 starts a 2-byte UTF-8 sequence - sets interim state |
| 472 | + incoming.writeByte((byte) 0xC3); |
| 473 | + // CR (0x0D) - should be skipped AND clear the interim UTF-8 state |
| 474 | + incoming.writeByte((byte) 0x0D); |
| 475 | + // 'A' - should be decoded as plain 'A' since CR should have reset state |
| 476 | + incoming.writeByte((byte) 'A'); |
| 477 | + // end of header line |
| 478 | + incoming.writeByte((byte) '\n'); |
| 479 | + // empty line to end headers |
| 480 | + incoming.writeByte((byte) '\n'); |
| 481 | + // null byte to end frame |
| 482 | + incoming.writeByte((byte) '\0'); |
| 483 | + |
| 484 | + assertTrue(channel.writeInbound(incoming)); |
| 485 | + |
| 486 | + StompHeadersSubframe frame = channel.readInbound(); |
| 487 | + assertNotNull(frame); |
| 488 | + assertEquals(StompCommand.CONNECT, frame.command()); |
| 489 | + // The header value should be just "A" (CR is skipped, UTF-8 state reset) |
| 490 | + // With the bug, it would contain corrupted UTF-8 combining 0xC3 with 'A' |
| 491 | + assertEquals("A", frame.headers().get("header")); |
| 492 | + |
| 493 | + StompContentSubframe content = channel.readInbound(); |
| 494 | + assertSame(LastStompContentSubframe.EMPTY_LAST_CONTENT, content); |
| 495 | + content.release(); |
| 496 | + |
| 497 | + assertNull(channel.readInbound()); |
| 498 | + } |
446 | 499 | } |