Skip to content
Open
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
17 changes: 12 additions & 5 deletions server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ 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))
// See: https://github.com/mysql/mysql-server/blob/8.0/sql/auth/sha2_password.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 +75,7 @@ func scrambleValidation(cached, nonce, scramble []byte) bool {
}

func (c *Conn) compareNativePasswordAuthData(clientAuthData []byte, credential Credential) error {
if len(clientAuthData) == 0 {
if isEmptyPassword(clientAuthData) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see your MySQL reference is only for caching_sha2_password_authenticate. Will it also apply for compareNativePasswordAuthData and compareSha256PasswordAuthData?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SHA256 authentication does have the exact same check here: https://github.com/mysql/mysql-server/blob/056a391cdc1af9b17b5415aee243483d1bac532d/sql/auth/sql_authentication.cc#L4763

For mysql_native_password, I'm less sure but my understanding is that both cases were supported as well by setting a default \0, and then trimming with get_length_encoded_string: https://github.com/mysql/mysql-server/blob/8.0/sql/auth/sql_authentication.cc#L3003-L3012

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please also add the second link to function comment of isEmptyPassword. Rest lgtm

if credential.hasEmptyPassword() {
return nil
}
Expand All @@ -90,8 +99,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 +143,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