Skip to content

Strictly validate MQTT UTF-8 Encoded String (#16939) - #16965

Merged
chrisvest merged 1 commit into
netty:4.1from
chrisvest:4.1-mqtt-validation
Jun 17, 2026
Merged

chrisvest merged 1 commit into
netty:4.1from
chrisvest:4.1-mqtt-validation

Conversation

@chrisvest

Copy link
Copy Markdown
Member

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)

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)
@chrisvest chrisvest added this to the 4.1.136.Final milestone Jun 16, 2026
@chrisvest
chrisvest merged commit 82e3dc1 into netty:4.1 Jun 17, 2026
16 of 18 checks passed
@chrisvest
chrisvest deleted the 4.1-mqtt-validation branch June 17, 2026 21:13
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants