Repository navigation
Strictly validate MQTT UTF-8 Encoded String (#16939) - #16965
Merged
Merged
Conversation
Motivation:
Per MQTT 3.1.1 [MQTT-1.5.3-1] / MQTT 5.0 [MQTT-1.5.4-1], the character
data
in a UTF-8 Encoded String MUST be well-formed UTF-8 as defined by the
Unicode
specification and restated in RFC 3629. In particular it MUST NOT
contain
encodings of code points between U+D800 and U+DFFF, overlong sequences,
or
sequences longer than 4 bytes. If received, the Control Packet MUST be
treated as a Malformed Packet.
`MqttDecoder` currently decodes every UTF-8 Encoded String via
`ByteBuf#readString(size, UTF_8)`, which delegates to
`new String(bytes, UTF_8)`. That constructor uses replacement semantics:
malformed UTF-8 sequences are silently replaced with U+FFFD instead of
being
reported, and an embedded U+0000 byte is accepted. This affects every
UTF-8
Encoded String in MQTT, including ClientId, Will Topic, Topic Name
(PUBLISH /
SUBSCRIBE / UNSUBSCRIBE filter), User Name, and the MQTT 5 string
properties
(Content Type, Response Topic, Reason String, Authentication Method,
Server
Reference, User Property, …). Beyond spec compliance, silently rewriting
these fields can cause routing/ACL/identity mismatches between the wire
representation and what the application sees.
Modifications:
* `MqttDecoder` now performs strict UTF-8 validation by default:
* UTF-8 strings are decoded through a per-instance
`CharsetDecoder` configured with `CodingErrorAction.REPORT` for both
`onMalformedInput` and `onUnmappableCharacter`. Any
`CharacterCodingException` is converted to a `DecoderException`.
* After successful decoding, the resulting `String` is scanned for
`U+0000`; if present, a `DecoderException` is thrown.
* The exceptions propagate through the existing decode error path
(`MqttMessageFactory.newInvalidMessage`), so callers continue to receive
a single `MqttMessage` with `decoderResult().isFailure() == true`,
matching the existing behaviour for other malformed-packet conditions
(e.g. non-zero reserved flag).
* A new opt-out constructor
`MqttDecoder(int maxBytesInMessage, int maxClientIdLength, boolean
strictUtf8Validation)`
is provided for users that need to retain the historical
replacement-char
behaviour. The pre-existing constructors delegate to it with
`strictUtf8Validation = true`.
* The previously-static `decodeString` and `decodeProperties` helpers
are
converted to instance methods to access the new flag (the surrounding
variable-header / payload decoders were already instance methods that
call them; no signature changes for any other helpers).
* New tests in `MqttCodecTest` exercise:
* invalid 2-byte continuation (`0xC3 0x28`)
* truncated multi-byte sequence at end-of-string (lone `0xC3`)
* Modified-UTF-8 overlong NUL (`0xC0 0x80`)
* isolated UTF-16 high surrogate (`0xED 0xA0 0x80` → U+D800)
* 5-byte sequence forbidden by RFC 3629 (`0xF8 …`)
* embedded U+0000
* well-formed multi-byte UTF-8 ("hello") decodes successfully
* empty ClientId is still accepted under strict mode
* legacy mode (`strictUtf8Validation = false`) still accepts malformed
UTF-8 (replaced with U+FFFD) and embedded U+0000
Result:
* MQTT spec [MQTT-1.5.3-1/2] and [MQTT-1.5.4-1/2] are enforced for every
UTF-8 Encoded String parsed by `MqttDecoder` (ClientId, Will Topic,
Topic
Name, Topic Filter, User Name, MQTT 5 string properties, including User
Property key/value pairs).
* Behaviour change: packets that previously decoded into messages
containing U+FFFD or U+0000 will now be reported as malformed. Users
relying on the old behaviour can opt out via the new constructor flag.
* No API removed; the existing `MqttDecoder()`, `MqttDecoder(int)` and
`MqttDecoder(int, int)` constructors remain source- and binary-
compatible.
(cherry picked from commit ba8e9e6)
lltopk
pushed a commit
to lltopk/netty
that referenced
this pull request
Oct 24, 2026
Motivation:
Per MQTT 3.1.1 [MQTT-1.5.3-1] / MQTT 5.0 [MQTT-1.5.4-1], the character
data
in a UTF-8 Encoded String MUST be well-formed UTF-8 as defined by the
Unicode
specification and restated in RFC 3629. In particular it MUST NOT
contain
encodings of code points between U+D800 and U+DFFF, overlong sequences,
or
sequences longer than 4 bytes. If received, the Control Packet MUST be
treated as a Malformed Packet.
`MqttDecoder` currently decodes every UTF-8 Encoded String via
`ByteBuf#readString(size, UTF_8)`, which delegates to `new String(bytes,
UTF_8)`. That constructor uses replacement semantics: malformed UTF-8
sequences are silently replaced with U+FFFD instead of being
reported, and an embedded U+0000 byte is accepted. This affects every
UTF-8
Encoded String in MQTT, including ClientId, Will Topic, Topic Name
(PUBLISH /
SUBSCRIBE / UNSUBSCRIBE filter), User Name, and the MQTT 5 string
properties
(Content Type, Response Topic, Reason String, Authentication Method,
Server
Reference, User Property, …). Beyond spec compliance, silently rewriting
these fields can cause routing/ACL/identity mismatches between the wire
representation and what the application sees.
Modifications:
* `MqttDecoder` now performs strict UTF-8 validation by default:
* UTF-8 strings are decoded through a per-instance `CharsetDecoder`
configured with `CodingErrorAction.REPORT` for both `onMalformedInput`
and `onUnmappableCharacter`. Any `CharacterCodingException` is converted
to a `DecoderException`.
* After successful decoding, the resulting `String` is scanned for
`U+0000`; if present, a `DecoderException` is thrown.
* The exceptions propagate through the existing decode error path
(`MqttMessageFactory.newInvalidMessage`), so callers continue to receive
a single `MqttMessage` with `decoderResult().isFailure() == true`,
matching the existing behaviour for other malformed-packet conditions
(e.g. non-zero reserved flag).
* A new opt-out constructor `MqttDecoder(int maxBytesInMessage, int
maxClientIdLength, boolean strictUtf8Validation)`
is provided for users that need to retain the historical
replacement-char
behaviour. The pre-existing constructors delegate to it with
`strictUtf8Validation = true`.
* The previously-static `decodeString` and `decodeProperties` helpers
are
converted to instance methods to access the new flag (the surrounding
variable-header / payload decoders were already instance methods that
call them; no signature changes for any other helpers).
* New tests in `MqttCodecTest` exercise:
* invalid 2-byte continuation (`0xC3 0x28`)
* truncated multi-byte sequence at end-of-string (lone `0xC3`)
* Modified-UTF-8 overlong NUL (`0xC0 0x80`)
* isolated UTF-16 high surrogate (`0xED 0xA0 0x80` → U+D800)
* 5-byte sequence forbidden by RFC 3629 (`0xF8 …`)
* embedded U+0000
* well-formed multi-byte UTF-8 ("hello") decodes successfully
* empty ClientId is still accepted under strict mode
* legacy mode (`strictUtf8Validation = false`) still accepts malformed
UTF-8 (replaced with U+FFFD) and embedded U+0000
Result:
* MQTT spec [MQTT-1.5.3-1/2] and [MQTT-1.5.4-1/2] are enforced for every
UTF-8 Encoded String parsed by `MqttDecoder` (ClientId, Will Topic,
Topic
Name, Topic Filter, User Name, MQTT 5 string properties, including User
Property key/value pairs).
* Behaviour change: packets that previously decoded into messages
containing U+FFFD or U+0000 will now be reported as malformed. Users
relying on the old behaviour can opt out via the new constructor flag.
* No API removed; the existing `MqttDecoder()`, `MqttDecoder(int)` and
`MqttDecoder(int, int)` constructors remain source- and binary-
compatible.
(cherry picked from commit ba8e9e6)
Co-authored-by: skyguard1 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation:
Per MQTT 3.1.1 [MQTT-1.5.3-1] / MQTT 5.0 [MQTT-1.5.4-1], the character data
in a UTF-8 Encoded String MUST be well-formed UTF-8 as defined by the Unicode
specification and restated in RFC 3629. In particular it MUST NOT contain
encodings of code points between U+D800 and U+DFFF, overlong sequences, or
sequences longer than 4 bytes. If received, the Control Packet MUST be treated as a Malformed Packet.
MqttDecodercurrently decodes every UTF-8 Encoded String viaByteBuf#readString(size, UTF_8), which delegates tonew String(bytes, UTF_8). That constructor uses replacement semantics: malformed UTF-8 sequences are silently replaced with U+FFFD instead of beingreported, and an embedded U+0000 byte is accepted. This affects every UTF-8
Encoded String in MQTT, including ClientId, Will Topic, Topic Name (PUBLISH /
SUBSCRIBE / UNSUBSCRIBE filter), User Name, and the MQTT 5 string properties
(Content Type, Response Topic, Reason String, Authentication Method, Server
Reference, User Property, …). Beyond spec compliance, silently rewriting these fields can cause routing/ACL/identity mismatches between the wire representation and what the application sees.
Modifications:
MqttDecodernow performs strict UTF-8 validation by default:CharsetDecoderconfigured withCodingErrorAction.REPORTfor bothonMalformedInputandonUnmappableCharacter. AnyCharacterCodingExceptionis converted to aDecoderException.Stringis scanned forU+0000; if present, aDecoderExceptionis thrown.MqttMessageFactory.newInvalidMessage), so callers continue to receivea single
MqttMessagewithdecoderResult().isFailure() == true,matching the existing behaviour for other malformed-packet conditions
(e.g. non-zero reserved flag).
A new opt-out constructor
MqttDecoder(int maxBytesInMessage, int maxClientIdLength, boolean strictUtf8Validation)is provided for users that need to retain the historical replacement-char
behaviour. The pre-existing constructors delegate to it with
strictUtf8Validation = true.The previously-static
decodeStringanddecodePropertieshelpers areconverted to instance methods to access the new flag (the surrounding variable-header / payload decoders were already instance methods that call them; no signature changes for any other helpers).
New tests in
MqttCodecTestexercise:0xC3 0x28)0xC3)0xC0 0x80)0xED 0xA0 0x80→ U+D800)0xF8 …)strictUtf8Validation = false) still accepts malformed UTF-8 (replaced with U+FFFD) and embedded U+0000Result:
MqttDecoder(ClientId, Will Topic, TopicName, Topic Filter, User Name, MQTT 5 string properties, including User
Property key/value pairs).
MqttDecoder(),MqttDecoder(int)andMqttDecoder(int, int)constructors remain source- and binary- compatible.(cherry picked from commit ba8e9e6)