Skip to content

Return ErrMalformPkt for a truncated initial handshake packet - #1857

Closed
aibeshkov wants to merge 1 commit into
go-sql-driver:masterfrom
aibeshkov:fix/short-handshake-packet
Closed

aibeshkov wants to merge 1 commit into
go-sql-driver:masterfrom
aibeshkov:fix/short-handshake-packet

Conversation

@aibeshkov

Copy link
Copy Markdown

Description

readHandshakePacket slices the initial handshake packet at fixed offsets without checking its length, so a short packet from the server (e.g. the two bytes 0x0a 0x00) panics with slice bounds out of range in the caller's goroutine. This checks the length before each fixed-size read and returns ErrMalformPkt, as discussed in #1848. A packet that has the lower capability bytes but nothing after them is still accepted as before.

The new test TestReadHandshakePacketTruncated feeds every prefix of a valid handshake (the one from TestRegression801) to readHandshakePacket and requires either success or ErrMalformPkt/ErrOldProtocol. Without the change it panics.

This only covers the initial handshake. The other fixed-offset reads from #1854 are left for a separate PR.

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

readHandshakePacket sliced the packet at fixed offsets without checking
its length, so a short handshake from the server (for example the two
bytes 0x0a 0x00) panicked with "slice bounds out of range" instead of
returning an error. Check the length before each fixed-size read and
return ErrMalformPkt.

Fixes go-sql-driver#1848

Signed-off-by: Andrei Beshkov <andrey.beshkov@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 16, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

readHandshakePacket checks handshake packet lengths before reading fixed fields and bounds the second authentication-data offset. A regression test checks every nonempty prefix of a sample handshake payload. The AUTHORS list adds Andrei Beshkov.

Changes

Handshake packet validation

Layer / File(s) Summary
Validate handshake fields and truncated packets
packets.go, packets_test.go
readHandshakePacket checks that required handshake fields are present before reading them. It bounds the second authentication-data offset to the packet length. A regression test exercises every nonempty prefix of a sample payload.

Author list

Layer / File(s) Summary
Add author entry
AUTHORS
The author list adds Andrei Beshkov.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

Possibly related PRs

Suggested reviewers: methane

Merge Risk: Minimal · up to c11c7

A truncated server handshake now returns ErrMalformPkt instead of causing a slice-bounds panic. The change is narrow and is covered by a prefix-based regression test. No merge-blocking risk is apparent.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: readHandshakePacket now returns ErrMalformPkt for a truncated initial handshake packet instead of panicking. The raw summary and objectives confirm the added length ch…
Description check Passed The description states the problem (fixed-offset slicing without length checks panics on short packets), the fix (length checks returning ErrMalformPkt), the new test, and the scope limit. It matches …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: readHandshakePacket now returns ErrMalformPkt for a truncated initial handshake packet instead of panicking. The raw summary and objectives confirm the added length checks before fixed-offset reads and the new truncation test.

Full details: Description check

Explanation

The description states the problem (fixed-offset slicing without length checks panics on short packets), the fix (length checks returning ErrMalformPkt), the new test, and the scope limit. It matches the changeset in packets.go, packets_test.go, and AUTHORS.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e88a7bc7-5f05-4fb1-b9c1-c1ea138b59a3
Commits

Reviewing files that changed from the base of the PR and between f32af30 and c11c752.

Files selected for processing: 3

  • AUTHORS
  • packets.go
  • packets_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@methane

methane commented Nov 17, 2026

Copy link
Copy Markdown
Member

i merged #1850

@methane methane closed this Nov 17, 2026
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