Skip to content

Commit 90cb6c3

Browse files
committed
Use blacklist to avoid vulnerability with interpolation
1 parent 2a634df commit 90cb6c3

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

collations.go

+14
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,17 @@ var collations = map[string]byte{
234234
"utf8mb4_unicode_520_ci": 246,
235235
"utf8mb4_vietnamese_ci": 247,
236236
}
237+
238+
// A blacklist of collations which is unsafe to interpolate parameters.
239+
// These multibyte encodings may contains 0x5c (`\`) in their trailing bytes.
240+
var unsafeCollations = map[byte]bool{
241+
1: true, // big5_chinese_ci
242+
13: true, // sjis_japanese_ci
243+
28: true, // gbk_chinese_ci
244+
84: true, // big5_bin
245+
86: true, // gb2312_bin
246+
87: true, // gbk_bin
247+
88: true, // sjis_bin
248+
95: true, // cp932_japanese_ci
249+
96: true, // cp932_bin
250+
}

utils.go

+2-24
Original file line numberDiff line numberDiff line change
@@ -148,30 +148,8 @@ func parseDSN(dsn string) (cfg *config, err error) {
148148
return nil, errInvalidDSNNoSlash
149149
}
150150

151-
if cfg.interpolateParams && cfg.collation != defaultCollation {
152-
// A whitelist of collations which safe to interpolate parameters.
153-
// ASCII and latin-1 are safe since they are single byte encoding.
154-
// utf-8 is safe since it doesn't conatins ASCII characters in trailing bytes.
155-
safeCollations := []string{"ascii_", "latin1_", "utf8_", "utf8mb4_"}
156-
157-
var collationName string
158-
for name, collation := range collations {
159-
if collation == cfg.collation {
160-
collationName = name
161-
break
162-
}
163-
}
164-
165-
safe := false
166-
for _, p := range safeCollations {
167-
if strings.HasPrefix(collationName, p) {
168-
safe = true
169-
break
170-
}
171-
}
172-
if !safe {
173-
return nil, errInvalidDSNUnsafeCollation
174-
}
151+
if cfg.interpolateParams && unsafeCollations[cfg.collation] {
152+
return nil, errInvalidDSNUnsafeCollation
175153
}
176154

177155
// Set default network if empty

0 commit comments

Comments
 (0)