GeezerVison is showing github.com — Merge changes from forks by chrisvest · Pull Request #17213 · netty/netty
Merge changes from forks
#17213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1a896eb
CORS: Don't override vary header if it already exists
normanmaurer Jul 1, 2026
9975553
Encoding-side validation of MQTT fields
chrisvest Jul 1, 2026
c07b97c
Bound SCTP fragmented message buffering
yawkat Jul 3, 2026
6b159a9
Apply suggestion from @yawkat
yawkat Jul 3, 2026
a8f53f2
Correctly handle ClientHello with a handshake header split across TLS…
violetagg Jul 16, 2026
b39650a
Fix NPE risk
chrisvest Jul 23, 2026
5dd41ea
Remove invalid user data from exception messages
chrisvest Jul 23, 2026
4625406
Harden the SOCKS4/5 input validation at encoding time
chrisvest Jul 25, 2026
bb74154
Do not re-aggregate the ClientHello on every received TLS record
violetagg Jul 28, 2026
5f1888e
Validate trust manager configuration
violetagg Aug 3, 2026
9a18e8d
Merge branch '4.2-83wf-socks-nul-byte-sec' into 4.2-sec-merge
chrisvest Aug 4, 2026
2f1ebab
Merge branch 'pr-ghsa-c4c3-7fpv-j4q5-2' into 4.2-sec-merge
chrisvest Aug 4, 2026
74d3007
Merge branch 'pr-ghsa-2qj4-mmr9-4v2f-1' into 4.2-sec-merge
chrisvest Aug 4, 2026
d997aab
Merge branch 'pr-ghsa-8c42-7qj2-3j46-1' into 4.2-sec-merge
chrisvest Aug 4, 2026
93340a5
Merge branch 'hf3j-mqtt-sec' into 4.2-sec-merge
chrisvest Aug 4, 2026
2b45fb0
Merge branch 'pr-ghsa-p85m-gvr3-788c-1' into 4.2-sec-merge
chrisvest Aug 4, 2026
File filter
Filter by extension
.java
(18)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -193,7 +193,9 @@ private void echoRequestOrigin(final HttpResponse response) {
}
private static void setVaryHeader (final HttpResponse response ) {
response .headers ().set (HttpHeaderNames .VARY , HttpHeaderNames .ORIGIN );
if (!response .headers ().containsValue (HttpHeaderNames .VARY , HttpHeaderNames .ORIGIN , true )) {
response .headers ().add (HttpHeaderNames .VARY , HttpHeaderNames .ORIGIN );
}
}
private static void setAnyOrigin (final HttpResponse response ) {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -25,6 +25,7 @@
import io .netty .handler .codec .http .FullHttpRequest ;
import io .netty .handler .codec .http .DefaultHttpHeadersFactory ;
import io .netty .handler .codec .http .DefaultHttpContent ;
import io .netty .handler .codec .http .FullHttpResponse ;
import io .netty .handler .codec .http .HttpContent ;
import io .netty .handler .codec .http .LastHttpContent ;
import io .netty .handler .codec .http .HttpMethod ;
Expand Down
Expand Up
@@ -683,6 +684,32 @@ public void shortCircuitWithNullOriginAllowedShouldSucceed() {
assertTrue (ReferenceCountUtil .release (response ));
}
@ Test
public void varyHeaderIsAppended () {
CorsConfig config = forAnyOrigin ().allowCredentials ().build ();
EmbeddedChannel channel = new EmbeddedChannel (new CorsHandler (config ),
new SimpleChannelInboundHandler <Object >() {
@ Override
protected void channelRead0 (ChannelHandlerContext ctx , Object msg ) {
HttpResponse response = new DefaultFullHttpResponse (HTTP_1_1 , OK , Unpooled .buffer (0 ));
response .headers ().set (VARY , ACCEPT_ENCODING );
ctx .writeAndFlush (response );
}
});
FullHttpRequest request = createHttpRequest (GET );
request .headers ().set (ORIGIN , "http://localhost:7777" );
assertFalse (channel .writeInbound (request ));
FullHttpResponse response = channel .readOutbound ();
List <String > varyHeaders = response .headers ().getAll (VARY );
assertThat (varyHeaders ).contains (ACCEPT_ENCODING .toString ());
assertThat (varyHeaders ).contains (ORIGIN .toString ());
response .release ();
assertFalse (channel .finish ());
}
private static HttpResponse simpleRequest (final CorsConfig config , final String origin ) {
return simpleRequest (config , origin , null );
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -42,10 +42,13 @@ static void setMqttVersion(ChannelHandlerContext ctx, MqttVersion version) {
}
static boolean isValidPublishTopicName (String topicName ) {
if (topicName == null ) {
return false ;
}
// publish topic name must not contain any wildcard
for (int i = 0 ; i < topicName .length (); i ++) {
char c = topicName .charAt (i );
if (c == '#' || c == '+' ) {
if (c == '#' || c == '+' || c == '\0' ) {
return false ;
}
}
Expand All
@@ -56,15 +59,31 @@ static boolean isValidMessageId(int messageId) {
return messageId != 0 ;
}
static boolean isValidClientId (MqttVersion mqttVersion , int maxClientIdLength , String clientId ) {
static boolean isValidUserName (String userName ) {
return userName == null || userName .indexOf ('\0' ) == -1 ;
}
/**
* Determine if a client identifier is valid.
* @param mqttVersion The MQTT version semantics to use.
* @param maxClientIdLength The max client id length.
* @param clientId The client id value.
* @param acceptNulBytes MQTT normally does not allow NUL bytes in client identifiers.
* Set this to {@code true} to enable "legacy"/"lenient" mode, otherwise {@code false} for strict spec compliance.
* @return {@code true} if the client id is valid, otherwise {@code false}.
*/
static boolean isValidClientId (MqttVersion mqttVersion , int maxClientIdLength , String clientId ,
boolean acceptNulBytes ) {
if (clientId == null || (!acceptNulBytes && clientId .indexOf ('\0' ) != -1 )) {
return false ;
}
if (mqttVersion == MqttVersion .MQTT_3_1 ) {
return clientId != null && clientId .length () >= MIN_CLIENT_ID_LENGTH &&
clientId .length () <= maxClientIdLength ;
return clientId .length () >= MIN_CLIENT_ID_LENGTH && clientId .length () <= maxClientIdLength ;
}
if (mqttVersion == MqttVersion .MQTT_3_1_1 || mqttVersion == MqttVersion .MQTT_5 ) {
// In 3.1.3.1 Client Identifier of MQTT 3.1.1 and 5.0 specifications, The Server MAY allow ClientId’s
// that contain more than 23 encoded bytes. And, The Server MAY allow zero-length ClientId.
return clientId != null ;
return true ;
}
throw new IllegalArgumentException (mqttVersion + " is unknown mqtt version" );
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -584,8 +584,8 @@ private MqttConnectPayload decodeConnectionPayload(
final String decodedClientIdValue = decodedClientId .value ;
final MqttVersion mqttVersion = MqttVersion .fromProtocolNameAndLevel (mqttConnectVariableHeader .name (),
(byte ) mqttConnectVariableHeader .version ());
if (!isValidClientId (mqttVersion , maxClientIdLength , decodedClientIdValue )) {
throw new MqttIdentifierRejectedException ("invalid clientIdentifier: " + decodedClientIdValue );
if (!isValidClientId (mqttVersion , maxClientIdLength , decodedClientIdValue , ! strictUtf8Validation )) {
throw new MqttIdentifierRejectedException ("invalid clientIdentifier" );
}
int numberOfBytesConsumed = decodedClientId .numberOfBytesConsumed ;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -30,6 +30,8 @@
import static io .netty .buffer .ByteBufUtil .*;
import static io .netty .handler .codec .mqtt .MqttCodecUtil .getMqttVersion ;
import static io .netty .handler .codec .mqtt .MqttCodecUtil .isValidClientId ;
import static io .netty .handler .codec .mqtt .MqttCodecUtil .isValidPublishTopicName ;
import static io .netty .handler .codec .mqtt .MqttCodecUtil .isValidUserName ;
import static io .netty .handler .codec .mqtt .MqttCodecUtil .setMqttVersion ;
import static io .netty .handler .codec .mqtt .MqttConstant .DEFAULT_MAX_CLIENT_ID_LENGTH ;
import static io .netty .handler .codec .mqtt .MqttProperties .ASSIGNED_CLIENT_IDENTIFIER ;
Expand Down
Expand Up
@@ -155,8 +157,8 @@ private static ByteBuf encodeConnectMessage(
// Client id
String clientIdentifier = payload .clientIdentifier ();
if (!isValidClientId (mqttVersion , DEFAULT_MAX_CLIENT_ID_LENGTH , clientIdentifier )) {
throw new MqttIdentifierRejectedException ("invalid clientIdentifier: " + clientIdentifier );
if (!isValidClientId (mqttVersion , DEFAULT_MAX_CLIENT_ID_LENGTH , clientIdentifier , false )) {
throw new MqttIdentifierRejectedException ("invalid clientIdentifier" );
}
int clientIdentifierBytes = utf8Bytes (clientIdentifier );
payloadBufferSize += 2 + clientIdentifierBytes ;
Expand All
@@ -167,13 +169,19 @@ private static ByteBuf encodeConnectMessage(
byte [] willMessage = payload .willMessageInBytes ();
byte [] willMessageBytes = willMessage != null ? willMessage : EmptyArrays .EMPTY_BYTES ;
if (variableHeader .isWillFlag ()) {
if (!isValidPublishTopicName (willTopic )) {
throw new MqttIdentifierRejectedException ("invalid willTopic" );
}
payloadBufferSize += 2 + willTopicBytes ;
payloadBufferSize += 2 + willMessageBytes .length ;
}
String userName = payload .userName ();
int userNameBytes = nullableUtf8Bytes (userName );
if (variableHeader .hasUserName ()) {
if (!isValidUserName (userName )) {
throw new MqttIdentifierRejectedException ("invalid userName" );
}
payloadBufferSize += 2 + userNameBytes ;
}
Expand Down
Expand Up
@@ -462,6 +470,9 @@ private static ByteBuf encodePublishMessage(
ByteBuf payload = message .payload ().duplicate ();
String topicName = variableHeader .topicName ();
if (!isValidPublishTopicName (topicName )) {
throw new MqttIdentifierRejectedException ("invalid topicName" );
}
int topicNameBytes = utf8Bytes (topicName );
ByteBuf propertiesBuf = encodePropertiesIfNeeded (mqttVersion ,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -18,7 +18,8 @@
import io .netty .handler .codec .DecoderException ;
/**
* A {@link MqttIdentifierRejectedException} which is thrown when a CONNECT request contains invalid client identifier.
* A {@link MqttIdentifierRejectedException} which is thrown when a CONNECT request contains invalid client identifier,
* will topic name, or username, or when a PUBLISH message contains an invalid topic name.
*/
public final class MqttIdentifierRejectedException extends DecoderException {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -33,6 +33,8 @@
import org .junit .jupiter .api .AfterEach ;
import org .junit .jupiter .api .BeforeEach ;
import org .junit .jupiter .api .Test ;
import org .junit .jupiter .params .ParameterizedTest ;
import org .junit .jupiter .params .provider .EnumSource ;
import org .mockito .Mock ;
import org .mockito .MockitoAnnotations ;
import org .mockito .stubbing .Answer ;
Expand Down
Expand Up
@@ -1500,4 +1502,61 @@ public void legacyModeAcceptsEmbeddedNullCharacter() {
ReferenceCountUtil .release (msg );
}
}
@ ParameterizedTest
@ EnumSource (MqttVersion .class )
public void encodeConnectMessageWithNulInClientIdIsRejected (MqttVersion version ) {
final MqttConnectMessage message = MqttMessageBuilders .connect ()
.clientId ("client\u0000 id" )
.protocolVersion (version )
.cleanSession (true )
.keepAlive (KEEP_ALIVE_SECONDS )
.build ();
assertThrows (MqttIdentifierRejectedException .class , () -> MqttEncoder .doEncode (ctx , message ));
}
@ ParameterizedTest
@ EnumSource (MqttVersion .class )
public void encodeConnectMessageWithNulInWillTopicIsRejected (MqttVersion version ) {
final MqttConnectMessage message = MqttMessageBuilders .connect ()
.clientId (CLIENT_ID )
.protocolVersion (version )
.willFlag (true )
.willQoS (MqttQoS .AT_LEAST_ONCE )
.willTopic ("will\u0000 topic" )
.willMessage (WILL_MESSAGE .getBytes (CharsetUtil .UTF_8 ))
.cleanSession (true )
.keepAlive (KEEP_ALIVE_SECONDS )
.build ();
assertThrows (MqttIdentifierRejectedException .class , () -> MqttEncoder .doEncode (ctx , message ));
}
@ ParameterizedTest
@ EnumSource (MqttVersion .class )
public void encodeConnectMessageWithNulInUserNameIsRejected (MqttVersion version ) {
final MqttConnectMessage message = MqttMessageBuilders .connect ()
.clientId (CLIENT_ID )
.protocolVersion (version )
.username ("user\u0000 name" )
.password (PASSWORD_BYTES )
.cleanSession (true )
.keepAlive (KEEP_ALIVE_SECONDS )
.build ();
assertThrows (MqttIdentifierRejectedException .class , () -> MqttEncoder .doEncode (ctx , message ));
}
@ Test
public void encodePublishMessageWithNulInTopicIsRejected () {
MqttFixedHeader fixedHeader =
new MqttFixedHeader (MqttMessageType .PUBLISH , false , MqttQoS .AT_LEAST_ONCE , false , 0 );
MqttPublishVariableHeader variableHeader = new MqttPublishVariableHeader ("home/\u0000 /sensor" , 1 );
ByteBuf payload = ALLOCATOR .buffer ();
payload .writeBytes ("data" .getBytes (CharsetUtil .UTF_8 ));
MqttPublishMessage message = new MqttPublishMessage (fixedHeader , variableHeader , payload );
try {
assertThrows (MqttIdentifierRejectedException .class , () -> MqttEncoder .doEncode (ctx , message ));
} finally {
payload .release ();
}
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -20,7 +20,9 @@
import io .netty .buffer .ByteBufUtil ;
import io .netty .channel .ChannelHandler .Sharable ;
import io .netty .channel .ChannelHandlerContext ;
import io .netty .handler .codec .EncoderException ;
import io .netty .handler .codec .MessageToByteEncoder ;
import io .netty .util .AsciiString ;
import io .netty .util .NetUtil ;
/**
Expand All
@@ -47,14 +49,25 @@ protected void encode(ChannelHandlerContext ctx, Socks4CommandRequest msg, ByteB
ByteBufUtil .writeShortBE (out , msg .dstPort ());
if (NetUtil .isValidIpV4Address (msg .dstAddr ())) {
out .writeBytes (NetUtil .createByteArrayFromIpAddressString (msg .dstAddr ()));
ByteBufUtil .writeAscii (out , msg .userId ());
ByteBufUtil .writeAscii (out , sanitize ( "userId" , msg .userId () ));
out .writeByte (0 );
} else {
out .writeBytes (IPv4_DOMAIN_MARKER );
ByteBufUtil .writeAscii (out , msg .userId ());
ByteBufUtil .writeAscii (out , sanitize ( "userId" , msg .userId () ));
out .writeByte (0 );
ByteBufUtil .writeAscii (out , msg .dstAddr ());
ByteBufUtil .writeAscii (out , sanitize ( "dstAddr" , msg .dstAddr () ));
out .writeByte (0 );
}
}
private CharSequence sanitize (String fieldName , String strValue ) {
for (int i = 0 , len = strValue .length (); i < len ; i ++) {
char c = strValue .charAt (i );
// SOCKS4 uses NUL-bytes as field delimiters.
if (AsciiString .c2b (c ) == 0 ) {
throw new EncoderException ("Illegal character in " + fieldName + " field." );
}
}
return strValue ;
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -80,7 +80,7 @@ private static void encodeAuthMethodRequest(Socks5InitialRequest msg, ByteBuf ou
final List <Socks5AuthMethod > authMethods = msg .authMethods ();
final int numAuthMethods = authMethods .size ();
out . writeByte ( numAuthMethods );
writeFieldLength ( out , numAuthMethods );
if (authMethods instanceof RandomAccess ) {
for (int i = 0 ; i < numAuthMethods ; i ++) {
Expand All
@@ -97,18 +97,18 @@ private static void encodePasswordAuthRequest(Socks5PasswordAuthRequest msg, Byt
out .writeByte (0x01 );
final String username = msg .username ();
out . writeByte ( username .length ());
writeFieldLength ( out , username .length ());
ByteBufUtil .writeAscii (out , username );
final String password = msg .password ();
out . writeByte ( password .length ());
writeFieldLength ( out , password .length ());
ByteBufUtil .writeAscii (out , password );
}
private static void encodePrivateAuthRequest (Socks5PrivateAuthRequest msg , ByteBuf out ) {
byte [] bytes = msg .privateToken ();
out .writeByte (0x01 );
out . writeByte ( bytes .length );
writeFieldLength ( out , bytes .length );
out .writeBytes (bytes );
}
Expand All
@@ -119,7 +119,22 @@ private void encodeCommandRequest(Socks5CommandRequest msg, ByteBuf out) throws
final Socks5AddressType dstAddrType = msg .dstAddrType ();
out .writeByte (dstAddrType .byteValue ());
addressEncoder .encodeAddress (dstAddrType , msg .dstAddr (), out );
String addrValue = msg .dstAddr ();
if (addrValue != null && dstAddrType == Socks5AddressType .DOMAIN ) {
checkFieldLength (addrValue .length ());
}
addressEncoder .encodeAddress (dstAddrType , addrValue , out );
ByteBufUtil .writeShortBE (out , msg .dstPort ());
}
private static void writeFieldLength (ByteBuf out , int length ) {
checkFieldLength (length );
out .writeByte (length );
}
private static void checkFieldLength (int length ) {
if (length > 255 || length < 0 ) {
throw new EncoderException ("Invalid field length value: " + length );
}
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -94,8 +94,18 @@ private void encodeCommandResponse(Socks5CommandResponse msg, ByteBuf out) throw
final Socks5AddressType bndAddrType = msg .bndAddrType ();
out .writeByte (bndAddrType .byteValue ());
addressEncoder .encodeAddress (bndAddrType , msg .bndAddr (), out );
String addrValue = msg .bndAddr ();
if (addrValue != null && bndAddrType == Socks5AddressType .DOMAIN ) {
checkFieldLength (addrValue .length ());
}
addressEncoder .encodeAddress (bndAddrType , addrValue , out );
ByteBufUtil .writeShortBE (out , msg .bndPort ());
}
private static void checkFieldLength (int length ) {
if (length > 255 || length < 0 ) {
throw new EncoderException ("Invalid field length value: " + length );
}
}
}
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.