From a546027db8ea138b2cc9f21b4c4505b828931c34 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 6 Dec 2022 15:29:05 +0100 Subject: [PATCH] Fixes #9006 - WebSocket MessageInputStream.read() returns signed byte Now properly coverting to `int`. Added test. Also fixed MultiPartInputStreamParser.Base64InputStream for the same issue. Signed-off-by: Simone Bordet --- .../server/MultiPartInputStreamParser.java | 2 +- .../internal/messages/MessageInputStream.java | 2 +- .../common/MessageInputStreamTest.java | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/MultiPartInputStreamParser.java b/jetty-server/src/main/java/org/eclipse/jetty/server/MultiPartInputStreamParser.java index acbd940ce2c..a8fe2bf15c3 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/MultiPartInputStreamParser.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/MultiPartInputStreamParser.java @@ -941,7 +941,7 @@ public class MultiPartInputStreamParser _pos = 0; } - return _buffer[_pos++]; + return _buffer[_pos++] & 0xFF; } } } diff --git a/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageInputStream.java b/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageInputStream.java index 12b20ce350f..d670440c87f 100644 --- a/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageInputStream.java +++ b/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageInputStream.java @@ -87,7 +87,7 @@ public class MessageInputStream extends InputStream implements MessageSink if (len < 0) // EOF return -1; if (len > 0) // did read something - return buf[0]; + return buf[0] & 0xFF; // reading nothing (len == 0) tries again } } diff --git a/jetty-websocket/websocket-jetty-common/src/test/java/org/eclipse/jetty/websocket/common/MessageInputStreamTest.java b/jetty-websocket/websocket-jetty-common/src/test/java/org/eclipse/jetty/websocket/common/MessageInputStreamTest.java index 74929abe8b5..e22965db5ed 100644 --- a/jetty-websocket/websocket-jetty-common/src/test/java/org/eclipse/jetty/websocket/common/MessageInputStreamTest.java +++ b/jetty-websocket/websocket-jetty-common/src/test/java/org/eclipse/jetty/websocket/common/MessageInputStreamTest.java @@ -33,6 +33,7 @@ import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTimeout; @@ -279,4 +280,23 @@ public class MessageInputStreamTest } }); } + + @Test + public void testReadSingleByteIsNotSigned() throws Exception + { + try (MessageInputStream stream = new MessageInputStream()) + { + // Byte must be greater than 127. + int theByte = 200; + // Append a single message (simple, short) + Frame frame = new Frame(OpCode.BINARY); + frame.setPayload(new byte[]{(byte)theByte}); + frame.setFin(true); + stream.accept(frame, Callback.NOOP); + + // Single byte read must not return a signed byte. + int read = stream.read(); + assertEquals(theByte, read); + } + } }