Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ var (
ErrAccessDeniedNoPassword = fmt.Errorf("%w without password", ErrAccessDenied)
)

// isEmptyPassword returns true if the auth data represents an empty password.
// Some clients send an empty packet (len == 0), while others (e.g. MySQL's libmysql)
// send a single null byte. This matches MySQL server's own handling:
// if (!pkt_len || (pkt_len == 1 && *pkt == 0))
// https://github.com/mysql/mysql-server/blob/8.0/sql/auth/sha2_password.cc
// https://github.com/mysql/mysql-server/blob/8.0/sql/auth/sql_authentication.cc
func isEmptyPassword(authData []byte) bool {
return len(authData) == 0 || (len(authData) == 1 && authData[0] == 0)
}

func (c *Conn) compareAuthData(authPluginName string, clientAuthData []byte) error {
if authPluginName != c.credential.AuthPluginName {
err := c.writeAuthSwitchRequest(c.credential.AuthPluginName)
Expand Down Expand Up @@ -66,7 +76,7 @@ func scrambleValidation(cached, nonce, scramble []byte) bool {
}

func (c *Conn) compareNativePasswordAuthData(clientAuthData []byte, credential Credential) error {
if len(clientAuthData) == 0 {
if isEmptyPassword(clientAuthData) {
if credential.hasEmptyPassword() {
return nil
}
Expand All @@ -90,8 +100,7 @@ func (c *Conn) compareNativePasswordAuthData(clientAuthData []byte, credential C
}

func (c *Conn) compareSha256PasswordAuthData(clientAuthData []byte, credential Credential) error {
// Empty passwords are not hashed, but sent as empty string
if len(clientAuthData) == 0 {
if isEmptyPassword(clientAuthData) {
if credential.hasEmptyPassword() {
return nil
}
Expand Down Expand Up @@ -135,8 +144,7 @@ func (c *Conn) compareSha256PasswordAuthData(clientAuthData []byte, credential C
}

func (c *Conn) compareCacheSha2PasswordAuthData(clientAuthData []byte) error {
// Empty passwords are not hashed, but sent as empty string
if len(clientAuthData) == 0 {
if isEmptyPassword(clientAuthData) {
if c.credential.hasEmptyPassword() {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion server/auth_switch_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Conn) handleCachingSha2PasswordFullAuth(authData []byte) error {
}

func (c *Conn) checkSha2CacheCredentials(clientAuthData []byte, credential Credential) error {
if len(clientAuthData) == 0 {
if isEmptyPassword(clientAuthData) {
if credential.hasEmptyPassword() {
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions server/auth_switch_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ func TestCheckSha2CacheCredentials_EmptyPassword(t *testing.T) {
serverPassword: "secret",
wantErr: ErrAccessDeniedNoPassword,
},
{
name: "null byte client auth, empty server password",
clientAuthData: []byte{0x00},
serverPassword: "",
wantErr: nil,
},
{
name: "null byte client auth, non-empty server password",
clientAuthData: []byte{0x00},
serverPassword: "secret",
wantErr: ErrAccessDeniedNoPassword,
},
}

for _, tt := range tests {
Expand Down
36 changes: 36 additions & 0 deletions server/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ func TestCompareNativePasswordAuthData_EmptyPassword(t *testing.T) {
serverPassword: "secret",
wantErr: ErrAccessDeniedNoPassword,
},
{
name: "null byte client auth, empty server password",
clientAuthData: []byte{0x00},
serverPassword: "",
wantErr: nil,
},
{
name: "null byte client auth, non-empty server password",
clientAuthData: []byte{0x00},
serverPassword: "secret",
wantErr: ErrAccessDeniedNoPassword,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -68,6 +80,18 @@ func TestCompareSha256PasswordAuthData_EmptyPassword(t *testing.T) {
serverPassword: "secret",
wantErr: ErrAccessDeniedNoPassword,
},
{
name: "null byte client auth, empty server password",
clientAuthData: []byte{0x00},
serverPassword: "",
wantErr: nil,
},
{
name: "null byte client auth, non-empty server password",
clientAuthData: []byte{0x00},
serverPassword: "secret",
wantErr: ErrAccessDeniedNoPassword,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -104,6 +128,18 @@ func TestCompareCacheSha2PasswordAuthData_EmptyPassword(t *testing.T) {
serverPassword: "secret",
wantErr: ErrAccessDeniedNoPassword,
},
{
name: "null byte client auth, empty server password",
clientAuthData: []byte{0x00},
serverPassword: "",
wantErr: nil,
},
{
name: "null byte client auth, non-empty server password",
clientAuthData: []byte{0x00},
serverPassword: "secret",
wantErr: ErrAccessDeniedNoPassword,
},
}

for _, tt := range tests {
Expand Down
Loading