Skip to content

Commit

Permalink
Re fixed panic len out of range in rcon.Dial #5
Browse files Browse the repository at this point in the history
  • Loading branch information
outdead committed May 16, 2022
1 parent dac804a commit e74866a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ All notable changes to this project will be documented in this file.

**ATTN**: This project uses [semantic versioning](http://semver.org/).

## [Unreleased
## [Unreleased]

## [v1.3.3] - 2022-05-16
### Fixed
- Added "response from not rcon server" error on auth request (Re fixed panic: runtime error: makeslice: len out of range in rcon.Dial #5).

## [v1.3.2] - 2022-05-16
### Fixed
- Fixed panic: runtime error: makeslice: len out of range in rcon.Dial #5
- Fixed panic: runtime error: makeslice: len out of range in rcon.Dial

### Updated
- Updated golangci linter to 1.42.1 version

## [v1.3.1] - 2021-01-06
### Updated
Expand Down Expand Up @@ -78,7 +85,8 @@ changed.
### Added
- Initial implementation.

[Unreleased]: https://github.com/gorcon/rcon/compare/v1.3.2...HEAD
[Unreleased]: https://github.com/gorcon/rcon/compare/v1.3.3...HEAD
[v1.3.3]: https://github.com/gorcon/rcon/compare/v1.3.2...v1.3.3
[v1.3.2]: https://github.com/gorcon/rcon/compare/v1.3.1...v1.3.2
[v1.3.1]: https://github.com/gorcon/rcon/compare/v1.3.0...v1.3.1
[v1.3.0]: https://github.com/gorcon/rcon/compare/v1.2.4...v1.3.0
Expand Down
7 changes: 5 additions & 2 deletions rcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ const (
)

var (
// ErrAuthNotRCON is returned when got auth response with negative size.
ErrAuthNotRCON = errors.New("response from not rcon server")

// ErrInvalidAuthResponse is returned when we didn't get an auth packet
// back for second read try after discard empty SERVERDATA_RESPONSE_VALUE
// from authentication response.
Expand Down Expand Up @@ -184,8 +187,8 @@ func (c *Conn) auth(password string) error {
}

size := response.Size - PacketHeaderSize
if size <= 0 {
size = response.Size
if size < 0 {
return ErrAuthNotRCON
}

// When the server receives an auth request, it will respond with an empty
Expand Down
29 changes: 10 additions & 19 deletions rcon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@ func authHandler(c *rcontest.Context) {
case "another":
rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, 42, "").WriteTo(c.Conn())
case "makeslice":
size := int32(len([]byte("")) + int(rcon.PacketPaddingSize)) // Some minecraft servers does not add header size
size := int32(len([]byte("")))

buffer := bytes.NewBuffer(make([]byte, 0, size+4))

_ = binary.Write(buffer, binary.LittleEndian, size)
_ = binary.Write(buffer, binary.LittleEndian, c.Request().ID)
_ = binary.Write(buffer, binary.LittleEndian, rcon.SERVERDATA_RESPONSE_VALUE)

// Write command body, null terminated ASCII string and an empty ASCIIZ string.
buffer.Write(append([]byte(""), 0x00, 0x00))

buffer.WriteTo(c.Conn())

rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, c.Request().ID, "").WriteTo(c.Conn())
case c.Server().Settings.Password:
rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, "").WriteTo(c.Conn())
rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, c.Request().ID, "").WriteTo(c.Conn())
Expand Down Expand Up @@ -135,31 +130,27 @@ func TestDial(t *testing.T) {
}
})

t.Run("auth success", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password")
if err != nil {
t.Errorf("got err %q, want %v", err, nil)
return
}

conn.Close()
})

t.Run("makeslice", func(t *testing.T) {
server := rcontest.NewServer(
rcontest.SetSettings(rcontest.Settings{Password: "makeslice"}),
rcontest.SetAuthHandler(authHandler),
)
defer server.Close()

conn, err := rcon.Dial(server.Addr(), "makeslice")
_, err := rcon.Dial(server.Addr(), "makeslice")
if !errors.Is(err, rcon.ErrAuthNotRCON) {
t.Errorf("got err %q, want %q", err, rcon.ErrAuthNotRCON)
}
})

t.Run("auth success", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password")
if err != nil {
t.Errorf("got err %q, want %v", err, nil)
server.Close()
return
}

conn.Close()
server.Close()
})
}

Expand Down

0 comments on commit e74866a

Please sign in to comment.