Fix malformed PUBLISH parsing and handle protocol errors safely#659
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a remote crash vulnerability where malformed MQTT PUBLISH packets with zero-length topics could trigger a Swift runtime trap (Range requires lowerBound <= upperBound) during topic slicing. The fix adds proper bounds validation in FramePublish parsing and converts all parse/protocol errors in CocoaMQTTReader into safe disconnections rather than silent continuation.
Changes:
- Added strict bounds checks and zero-length topic rejection in
FramePublishinitialization, with MQTT 5.0 allowing empty topics only when a Topic Alias is present - Changed
CocoaMQTTReader.frameReady()to disconnect the socket on any parse or protocol error instead of logging and continuing - Added comprehensive test coverage for both the frame-level rejection and reader-level disconnect behavior
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| Source/FramePublish.swift | Added bounds checks, zero-length topic rejection for MQTT 3.1.1, and empty-topic-without-alias rejection for MQTT 5.0 |
| Source/CocoaMQTTReader.swift | Replaced printError+break with protocolError+return across all frame parse failures; added protocolError() helper that logs and disconnects |
| CocoaMQTTTests/FrameTests.swift | Added three tests for zero-length topic handling across MQTT 3.1.1 and 5.0 |
| CocoaMQTTTests/CocoaMQTTReaderProtocolErrorTests.swift | New test file with spy-based tests verifying socket disconnect on malformed publish and unknown frame type |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Thanks tsuki <t5uki@.> to report this issue via email.
Summary
PUBLISHtopic bytesPUBLISHpackets with zero-length topic instead of trappingWhy
Malformed packets could trigger a runtime trap during topic slicing (
Range requires lowerBound <= upperBound), causing a remote crash. This change converts malformed input handling into safe parse failure plus connection close.Tests
swift test --filter FrameTests/testFramePublishRejectsZeroLengthTopic --filter FrameTests/testFramePublishRejectsZeroLengthTopicInMQTT5WithoutAlias --filter FrameTests/testFramePublishAllowsZeroLengthTopicInMQTT5WithAlias --filter CocoaMQTTReaderProtocolErrorTestsAdded tests:
testFramePublishRejectsZeroLengthTopictestFramePublishRejectsZeroLengthTopicInMQTT5WithoutAliastestFramePublishAllowsZeroLengthTopicInMQTT5WithAliastestMalformedPublishDisconnectsSockettestUnknownFrameTypeDisconnectsSocket