Skip to content

Commit 6e39d67

Browse files
committed
client: Have SetCapability() return an error for unsupported caps
1 parent 8fd2c2a commit 6e39d67

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

client/auth.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ import (
1414

1515
const defaultAuthPluginName = mysql.AUTH_NATIVE_PASSWORD
1616

17+
var optionalCapabilities = []uint32{
18+
mysql.CLIENT_FOUND_ROWS,
19+
mysql.CLIENT_IGNORE_SPACE,
20+
mysql.CLIENT_MULTI_STATEMENTS,
21+
mysql.CLIENT_MULTI_RESULTS,
22+
mysql.CLIENT_PS_MULTI_RESULTS,
23+
mysql.CLIENT_CONNECT_ATTRS,
24+
mysql.CLIENT_COMPRESS,
25+
mysql.CLIENT_ZSTD_COMPRESSION_ALGORITHM,
26+
mysql.CLIENT_LOCAL_FILES,
27+
}
28+
1729
// defines the supported auth plugins
1830
var supportedAuthPlugins = []string{mysql.AUTH_NATIVE_PASSWORD, mysql.AUTH_SHA256_PASSWORD, mysql.AUTH_CACHING_SHA2_PASSWORD, mysql.AUTH_MARIADB_ED25519}
1931

@@ -214,11 +226,9 @@ func (c *Conn) writeAuthHandshake() error {
214226
// Adjust client capability flags on specific client requests
215227
// Only flags that would make any sense setting and aren't handled elsewhere
216228
// in the library are supported here
217-
capability |= c.ccaps&mysql.CLIENT_FOUND_ROWS | c.ccaps&mysql.CLIENT_IGNORE_SPACE |
218-
c.ccaps&mysql.CLIENT_MULTI_STATEMENTS | c.ccaps&mysql.CLIENT_MULTI_RESULTS |
219-
c.ccaps&mysql.CLIENT_PS_MULTI_RESULTS | c.ccaps&mysql.CLIENT_CONNECT_ATTRS |
220-
c.ccaps&mysql.CLIENT_COMPRESS | c.ccaps&mysql.CLIENT_ZSTD_COMPRESSION_ALGORITHM |
221-
c.ccaps&mysql.CLIENT_LOCAL_FILES
229+
for _, optionalCap := range optionalCapabilities {
230+
capability |= c.ccaps & optionalCap
231+
}
222232

223233
capability &^= c.clientExplicitOffCaps
224234

client/conn.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net"
1010
"runtime"
1111
"runtime/debug"
12+
"slices"
1213
"strings"
1314
"time"
1415

@@ -238,9 +239,13 @@ func (c *Conn) Ping() error {
238239
}
239240

240241
// SetCapability marks the specified flag as explicitly enabled by the client.
241-
func (c *Conn) SetCapability(cap uint32) {
242+
func (c *Conn) SetCapability(cap uint32) error {
243+
if !slices.Contains(optionalCapabilities, cap) {
244+
return errors.New("unsupported or unknown capability")
245+
}
242246
c.ccaps |= cap
243247
c.clientExplicitOffCaps &^= cap
248+
return nil
244249
}
245250

246251
// UnsetCapability marks the specified flag as explicitly disabled by the client.

0 commit comments

Comments
 (0)