Skip to content

fix(kaitai): restrict sizes for Kaitai struct#194

Merged
kruton merged 2 commits into
connectbot:mainfrom
kruton:kaitai-struct-sizes
Jun 11, 2026
Merged

fix(kaitai): restrict sizes for Kaitai struct#194
kruton merged 2 commits into
connectbot:mainfrom
kruton:kaitai-struct-sizes

Conversation

@kruton

@kruton kruton commented Jun 11, 2026

Copy link
Copy Markdown
Member

Let Kaitai Struct handle more of the validation logic by supplying the expected sizes where possible.

Fixes GHSA-ch3q-cw5r-f4hg

kruton added 2 commits June 10, 2026 23:03
Some tests were not running because they were returning values other than
Unit. This can be easy to miss in Kotlin because the return types are
inferred.

Make this something that JUnit halts the build instead of just warning.
Let Kaitai Struct handle more of the validation logic by supplying the
expected sizes where possible.

Fixes GHSA-ch3q-cw5r-f4hg
Copilot AI review requested due to automatic review settings June 11, 2026 17:33
@kruton kruton force-pushed the kaitai-struct-sizes branch from 2084005 to 1db4fb4 Compare June 11, 2026 17:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR strengthens SSH/Kaitai parsing robustness to address GHSA-ch3q-cw5r-f4hg by adding explicit size/count validations in Kaitai .ksy specs and by standardizing how Kaitai validation/parse failures are surfaced to callers (e.g., as TransportException) with new regression tests.

Changes:

  • Add Kaitai valid expressions to ensure declared lengths/counts cannot exceed remaining bytes (and basic sanity constraints for packet framing).
  • Introduce a shared helper (kaitaiParseFailureOrNull) to detect Kaitai/underflow parse failures and wrap them into TransportException at key boundaries.
  • Add/extend unit tests covering malformed packet/agent frames and length validation failures.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
sshlib/src/main/kotlin/org/connectbot/sshlib/transport/PacketIO.kt Wrap Kaitai payload-parse failures as TransportException("Malformed SSH packet payload", …)
sshlib/src/main/kotlin/org/connectbot/sshlib/KaitaiParsing.kt Add helper to detect Kaitai/underflow parse failures in exception cause chains
sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt Wrap Kaitai parse failures from packet loop into a transport-level error (needs adjustment)
sshlib/src/main/kotlin/org/connectbot/sshlib/client/AgentProtocolHandler.kt Treat Kaitai parse failures as malformed agent requests and return SSH_AGENT_FAILURE
sshlib/src/test/kotlin/org/connectbot/sshlib/transport/PacketIOTest.kt Add regression test for wrapping Kaitai validation failures
sshlib/src/test/kotlin/org/connectbot/sshlib/SshClientTest.kt Add regression test for connect returning ProtocolError on malformed server packet
sshlib/src/test/kotlin/org/connectbot/sshlib/protocol/KaitaiLengthValidationTest.kt New tests asserting Kaitai valid length/count validations trip correctly
sshlib/src/test/kotlin/org/connectbot/sshlib/AgentProtocolTest.kt Add tests for rejecting malformed agent frames/payloads and not masking provider exceptions
protocol/src/main/resources/kaitai/ascii_string.ksy Add remaining-bytes validation to length field
protocol/src/main/resources/kaitai/byte_string.ksy Add remaining-bytes validation to length field
protocol/src/main/resources/kaitai/ecdsa_signature_blob.ksy Add remaining-bytes validation to blob length
protocol/src/main/resources/kaitai/encrypted_packet.ksy Validate encrypted payload length matches remaining bytes (excluding MAC)
protocol/src/main/resources/kaitai/etm_mac.ksy Validate encrypted packet length matches remaining bytes
protocol/src/main/resources/kaitai/mpint.ksy Add remaining-bytes validation to length field
protocol/src/main/resources/kaitai/name_list.ksy Add remaining-bytes validation to entries length
protocol/src/main/resources/kaitai/restrict_destination_constraint.ksy Bound keyspec counts by remaining bytes
protocol/src/main/resources/kaitai/ssh_agent_identities_answer.ksy Bound identity count by remaining bytes
protocol/src/main/resources/kaitai/ssh_agent_message.ksy Validate agent frame length matches remaining bytes
protocol/src/main/resources/kaitai/ssh_msg_ext_info.ksy Bound extension count by remaining bytes
protocol/src/main/resources/kaitai/ssh_msg_userauth_info_request.ksy Bound prompt count by remaining bytes
protocol/src/main/resources/kaitai/ssh_msg_userauth_info_response.ksy Bound response count by remaining bytes
protocol/src/main/resources/kaitai/ssh_public_key.ksy Add remaining-bytes validation to algorithm name length
protocol/src/main/resources/kaitai/ssh_signature.ksy Add remaining-bytes validation to algorithm name length
protocol/src/main/resources/kaitai/unencrypted_packet.ksy Validate packet framing fields (len_packet, padding length)
protocol/src/main/resources/kaitai/userauth_request_gssapi_with_mic.ksy Bound mechanism count by remaining bytes
protocol/src/main/resources/kaitai/utf8_string.ksy Add remaining-bytes validation to length field

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt Outdated
@kruton kruton force-pushed the kaitai-struct-sizes branch 2 times, most recently from 0d33a57 to 6c8311e Compare June 11, 2026 18:57
@kruton kruton requested a review from Copilot June 11, 2026 18:57
@kruton kruton force-pushed the kaitai-struct-sizes branch 2 times, most recently from b7f9a25 to 4d40972 Compare June 11, 2026 18:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 29 out of 30 changed files in this pull request and generated 2 comments.

Comment thread sshlib/src/test/kotlin/org/connectbot/sshlib/AgentProtocolTest.kt Outdated
Comment thread sshlib/src/test/kotlin/org/connectbot/sshlib/AgentProtocolTest.kt Outdated
@kruton kruton force-pushed the kaitai-struct-sizes branch from 4d40972 to 5b7aa81 Compare June 11, 2026 20:03
@kruton kruton merged commit a08421f into connectbot:main Jun 11, 2026
11 checks passed
@kruton kruton deleted the kaitai-struct-sizes branch June 11, 2026 21:29
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