Skip to content

Commit

Permalink
Fix NOAUTH Error: Ensure AUTH is Sent Before HELLO (#791)
Browse files Browse the repository at this point in the history
* Fix: Ensure AUTH is sent before HELLO 2

* Ensure AUTH is sent before HELLO 2 in tests

* Dynamically track the correct position of the HELLO command
  • Loading branch information
arbha1erao authored Mar 2, 2025
1 parent 2f1a6bc commit a18aadd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
5 changes: 3 additions & 2 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,13 @@ func _newPipe(connFn func() (net.Conn, error), option *ClientOption, r2ps, nobg
return nil, ErrNoCache
}
init = init[:0]
init = append(init, []string{"HELLO", "2"})
if password != "" && username == "" {
init = append(init, []string{"AUTH", password})
} else if username != "" {
init = append(init, []string{"AUTH", username, password})
}
helloIndex := len(init)
init = append(init, []string{"HELLO", "2"})
if option.ClientName != "" {
init = append(init, []string{"CLIENT", "SETNAME", option.ClientName})
}
Expand Down Expand Up @@ -312,7 +313,7 @@ func _newPipe(connFn func() (net.Conn, error), option *ClientOption, r2ps, nobg
p.Close()
return nil, err
}
if i == 0 {
if i == helloIndex {
p.info, err = r.AsMap()
}
}
Expand Down
20 changes: 10 additions & 10 deletions pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ func TestNewPipe(t *testing.T) {
n1, n2 := net.Pipe()
mock := &redisMock{buf: bufio.NewReader(n2), conn: n2, t: t}
go func() {
mock.Expect("AUTH", "pa").
ReplyString("OK")
mock.Expect("HELLO", "2").
Reply(RedisMessage{
typ: '*',
Expand All @@ -262,8 +264,6 @@ func TestNewPipe(t *testing.T) {
{typ: '+', string: "us-west-1a"},
},
})
mock.Expect("AUTH", "pa").
ReplyString("OK")
mock.Expect("CLIENT", "SETNAME", "cn").
ReplyString("OK")
mock.Expect("SELECT", "1").
Expand Down Expand Up @@ -650,10 +650,10 @@ func TestNewRESP2Pipe(t *testing.T) {
ReplyError("UNKNOWN COMMAND")
mock.Expect("CLIENT", "SETINFO", "LIB-VER", LibVer).
ReplyError("UNKNOWN COMMAND")
mock.Expect("HELLO", "2").
ReplyError("ERR unknown command `HELLO`")
mock.Expect("AUTH", "pa").
ReplyString("OK")
mock.Expect("HELLO", "2").
ReplyError("ERR unknown command `HELLO`")
mock.Expect("CLIENT", "SETNAME", "cn").
ReplyString("OK")
mock.Expect("SELECT", "1").
Expand Down Expand Up @@ -693,10 +693,10 @@ func TestNewRESP2Pipe(t *testing.T) {
ReplyError("UNKNOWN COMMAND")
mock.Expect("CLIENT", "SETINFO", "LIB-VER", LibVer).
ReplyError("UNKNOWN COMMAND")
mock.Expect("HELLO", "2").
ReplyError("ERR unknown command `HELLO`")
mock.Expect("AUTH", "ua", "pa").
ReplyString("OK")
mock.Expect("HELLO", "2").
ReplyError("ERR unknown command `HELLO`")
mock.Expect("CLIENT", "SETNAME", "cn").
ReplyString("OK")
mock.Expect("SELECT", "1").
Expand Down Expand Up @@ -739,10 +739,10 @@ func TestNewRESP2Pipe(t *testing.T) {
ReplyError("UNKNOWN COMMAND")
mock.Expect("CLIENT", "SETINFO", "LIB-VER", LibVer).
ReplyError("UNKNOWN COMMAND")
mock.Expect("HELLO", "2").
ReplyError("ERR unknown command `HELLO`")
mock.Expect("AUTH", "pa").
ReplyString("OK")
mock.Expect("HELLO", "2").
ReplyError("ERR unknown command `HELLO`")
mock.Expect("CLIENT", "SETNAME", "cn").
ReplyString("OK")
mock.Expect("SELECT", "1").
Expand Down Expand Up @@ -787,10 +787,10 @@ func TestNewRESP2Pipe(t *testing.T) {
ReplyError("UNKNOWN COMMAND")
mock.Expect("CLIENT", "SETINFO", "LIB-VER", LibVer).
ReplyError("UNKNOWN COMMAND")
mock.Expect("HELLO", "2").
ReplyError("ERR unknown command `HELLO`")
mock.Expect("AUTH", "pa").
ReplyString("OK")
mock.Expect("HELLO", "2").
ReplyError("ERR unknown command `HELLO`")
mock.Expect("CLIENT", "SETNAME", "cn").
ReplyString("OK")
mock.Expect("SELECT", "1").
Expand Down

0 comments on commit a18aadd

Please sign in to comment.