From 537447821909c7debb33099e5af842989a28ac73 Mon Sep 17 00:00:00 2001 From: Masahide YAMASAKI Date: Sun, 1 Jun 2025 00:38:24 +0900 Subject: [PATCH 1/3] feat: Update client capability flags Adjust client capability flags based on server support, and account for explicitly disabled capabilities. - client/auth.go: Update client capability flags - client/conn.go: Add field to manage disabled capabilities --- client/auth.go | 13 +++++++++++-- client/conn.go | 5 ++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/client/auth.go b/client/auth.go index 952accec5..34dc38bae 100644 --- a/client/auth.go +++ b/client/auth.go @@ -211,8 +211,15 @@ func (c *Conn) writeAuthHandshake() error { capability := mysql.CLIENT_PROTOCOL_41 | mysql.CLIENT_SECURE_CONNECTION | mysql.CLIENT_LONG_PASSWORD | mysql.CLIENT_TRANSACTIONS | mysql.CLIENT_PLUGIN_AUTH // Adjust client capability flags based on server support - capability |= c.capability & mysql.CLIENT_LONG_FLAG - capability |= c.capability & mysql.CLIENT_QUERY_ATTRIBUTES + // ---- Inherit capabilities that the server has and the user has NOT explicitly denied ---- + inherit := c.capability & ^c.dcaps // Server-side capabilities minus explicit denies + capability |= inherit & mysql.CLIENT_LONG_FLAG // Existing + // ---- Handling of CLIENT_QUERY_ATTRIBUTES ---- + if c.ccaps&mysql.CLIENT_QUERY_ATTRIBUTES != 0 { // Explicitly ON + capability |= mysql.CLIENT_QUERY_ATTRIBUTES + } else if inherit&mysql.CLIENT_QUERY_ATTRIBUTES != 0 { // Server has it and no denial + capability |= mysql.CLIENT_QUERY_ATTRIBUTES + } // Adjust client capability flags on specific client requests // Only flags that would make any sense setting and aren't handled elsewhere // in the library are supported here @@ -361,5 +368,7 @@ func (c *Conn) writeAuthHandshake() error { data[pos] = 0x03 } + c.capability = capability // update capability to the one we sent + return c.WritePacket(data) } diff --git a/client/conn.go b/client/conn.go index cf56399ad..79504e9f8 100644 --- a/client/conn.go +++ b/client/conn.go @@ -45,6 +45,7 @@ type Conn struct { capability uint32 // client-set capabilities only ccaps uint32 + dcaps uint32 // disabled capabilities attributes map[string]string @@ -236,12 +237,14 @@ func (c *Conn) Ping() error { // SetCapability enables the use of a specific capability func (c *Conn) SetCapability(cap uint32) { + c.dcaps &^= cap c.ccaps |= cap } // UnsetCapability disables the use of a specific capability func (c *Conn) UnsetCapability(cap uint32) { - c.ccaps &= ^cap + c.ccaps &^= cap + c.dcaps |= cap } // HasCapability returns true if the connection has the specific capability From 08ffcfe1bec41092a2fc6becaaf1b4a2a3ff6275 Mon Sep 17 00:00:00 2001 From: Masahide YAMASAKI Date: Sun, 8 Jun 2025 00:41:06 +0900 Subject: [PATCH 2/3] feat: Update client capability flags Adjust client capability flags based on server support. Add a new field to manage explicitly disabled flags. - client/auth.go: Improve logic for adjusting client capability flags - client/conn.go: Add field to manage explicitly disabled flags --- client/auth.go | 15 ++++----------- client/conn.go | 7 ++++--- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/client/auth.go b/client/auth.go index 34dc38bae..2035e5c21 100644 --- a/client/auth.go +++ b/client/auth.go @@ -211,15 +211,8 @@ func (c *Conn) writeAuthHandshake() error { capability := mysql.CLIENT_PROTOCOL_41 | mysql.CLIENT_SECURE_CONNECTION | mysql.CLIENT_LONG_PASSWORD | mysql.CLIENT_TRANSACTIONS | mysql.CLIENT_PLUGIN_AUTH // Adjust client capability flags based on server support - // ---- Inherit capabilities that the server has and the user has NOT explicitly denied ---- - inherit := c.capability & ^c.dcaps // Server-side capabilities minus explicit denies - capability |= inherit & mysql.CLIENT_LONG_FLAG // Existing - // ---- Handling of CLIENT_QUERY_ATTRIBUTES ---- - if c.ccaps&mysql.CLIENT_QUERY_ATTRIBUTES != 0 { // Explicitly ON - capability |= mysql.CLIENT_QUERY_ATTRIBUTES - } else if inherit&mysql.CLIENT_QUERY_ATTRIBUTES != 0 { // Server has it and no denial - capability |= mysql.CLIENT_QUERY_ATTRIBUTES - } + capability |= c.capability & mysql.CLIENT_LONG_FLAG + capability |= c.capability & mysql.CLIENT_QUERY_ATTRIBUTES // Adjust client capability flags on specific client requests // Only flags that would make any sense setting and aren't handled elsewhere // in the library are supported here @@ -229,6 +222,8 @@ func (c *Conn) writeAuthHandshake() error { c.ccaps&mysql.CLIENT_COMPRESS | c.ccaps&mysql.CLIENT_ZSTD_COMPRESSION_ALGORITHM | c.ccaps&mysql.CLIENT_LOCAL_FILES + capability &^= c.clientExplicitOffCaps + // To enable TLS / SSL if c.tlsConfig != nil { capability |= mysql.CLIENT_SSL @@ -368,7 +363,5 @@ func (c *Conn) writeAuthHandshake() error { data[pos] = 0x03 } - c.capability = capability // update capability to the one we sent - return c.WritePacket(data) } diff --git a/client/conn.go b/client/conn.go index 79504e9f8..22351b84d 100644 --- a/client/conn.go +++ b/client/conn.go @@ -45,7 +45,8 @@ type Conn struct { capability uint32 // client-set capabilities only ccaps uint32 - dcaps uint32 // disabled capabilities + // Flags explicitly disabled via SetCapability/UnsetCapability + clientExplicitOffCaps uint32 attributes map[string]string @@ -237,14 +238,14 @@ func (c *Conn) Ping() error { // SetCapability enables the use of a specific capability func (c *Conn) SetCapability(cap uint32) { - c.dcaps &^= cap c.ccaps |= cap + c.clientExplicitOffCaps &^= cap } // UnsetCapability disables the use of a specific capability func (c *Conn) UnsetCapability(cap uint32) { c.ccaps &^= cap - c.dcaps |= cap + c.clientExplicitOffCaps |= cap } // HasCapability returns true if the connection has the specific capability From abad2bf89456fb04ab46e378b5d7b83f15d15b0d Mon Sep 17 00:00:00 2001 From: Masahide YAMASAKI Date: Sun, 8 Jun 2025 01:30:41 +0900 Subject: [PATCH 3/3] fix comment --- client/conn.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/client/conn.go b/client/conn.go index 22351b84d..572fe2b09 100644 --- a/client/conn.go +++ b/client/conn.go @@ -45,7 +45,8 @@ type Conn struct { capability uint32 // client-set capabilities only ccaps uint32 - // Flags explicitly disabled via SetCapability/UnsetCapability + // Capability flags explicitly disabled by the client via UnsetCapability() + // These flags are removed from the final advertised capability set during handshake. clientExplicitOffCaps uint32 attributes map[string]string @@ -236,13 +237,14 @@ func (c *Conn) Ping() error { return nil } -// SetCapability enables the use of a specific capability +// SetCapability marks the specified flag as explicitly enabled by the client. func (c *Conn) SetCapability(cap uint32) { c.ccaps |= cap c.clientExplicitOffCaps &^= cap } -// UnsetCapability disables the use of a specific capability +// UnsetCapability marks the specified flag as explicitly disabled by the client. +// This disables the flag even if the server supports it. func (c *Conn) UnsetCapability(cap uint32) { c.ccaps &^= cap c.clientExplicitOffCaps |= cap