diff --git a/src/java/nginx/unit/websocket/WsFrameBase.java b/src/java/nginx/unit/websocket/WsFrameBase.java index 06d20bf48..f07a89624 100644 --- a/src/java/nginx/unit/websocket/WsFrameBase.java +++ b/src/java/nginx/unit/websocket/WsFrameBase.java @@ -260,6 +260,13 @@ private boolean processRemainingHeader() throws IOException { } else if (payloadLength == 127) { payloadLength = byteArrayToLong(inputBuffer.array(), inputBuffer.arrayOffset() + inputBuffer.position(), 8); + // The most significant bit of those 8 bytes is required to be zero + // (see RFC 6455, section 5.2). If the most significant bit is set, + // the resulting payload length will be negative so test for that. + if (payloadLength < 0) { + throw new WsIOException( + new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.payloadMsbInvalid"))); + } inputBuffer.position(inputBuffer.position() + 8); } if (Util.isControl(opCode)) { @@ -670,7 +677,7 @@ protected static long byteArrayToLong(byte[] b, int start, int len) throws IOExc int shift = 0; long result = 0; for (int i = start + len - 1; i >= start; i--) { - result = result + ((b[i] & 0xFF) << shift); + result = result + ((b[i] & 0xFFL) << shift); shift += 8; } return result;