Validate packet inputs and fail closed on malformed responses#263
Merged
Conversation
Owner
|
Thank you! |
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.
Summary
PacketBuilder.sendRawDatato current firmware path and payload limitshasConnection,getContactByKey, andgetAdvertPathsetPathHashModeRationale
This change tightens validation at the correct protocol boundaries.
At the low-level packet-construction layer, callers should not be able to build malformed command frames that firmware will reject or misinterpret. That is why the builder now enforces the current raw-data limits and always emits fixed-width public-key fields for commands whose wire format requires 32 bytes.
At the public session API layer, methods that already document a full 32-byte public key now reject invalid input up front with
MeshCoreError.invalidInputinstead of silently padding or truncating user input and sending a request anyway.setPathHashMode(_:)similarly rejects reserved mode values before sending.On the receive side, malformed responses should fail closed rather than being partially parsed into invented or defaulted state. This PR makes that explicit for:
Firmware Validation
I validated these client-side constraints against current
meshcorefirmwaremainbehavior:MAX_PATH_SIZEis64andMAX_PACKET_PAYLOADis184insrc/MeshCore.hdocs/packet_format.mddocuments path as up to64bytes and payload as up to184bytesdocs/packet_format.mdandsrc/Packet.cpptreat path-length mode0b11/3as reserved or unsupportedPUB_KEY_SIZE == 32byte public-key fieldsThat means the builder clamping and fixed-width encoding are matching the current firmware contract, not inventing a client-only policy.
Why these layers
PacketBuilderis the shared construction boundary for protocol-shaped packetsMeshCoreSessionis the right place for caller-facing input validation and clean errorsPacketParser/Parsersare the right place to reject malformed incoming wire dataThat split avoids pushing protocol-specific validation into the transport or send layer while still giving higher-level callers better errors.
Validation
swift test --package-path MeshCoreResult:
280tests passedTest coverage added