Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 45 additions & 1 deletion msdsn/conn_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ var adoSynonyms = map[string]string{

func splitConnectionString(dsn string) (res map[string]string) {
res = map[string]string{}
parts := strings.Split(dsn, ";")
parts := splitAdoConnectionStringParts(dsn)
for _, part := range parts {
if len(part) == 0 {
continue
Expand All @@ -674,6 +674,12 @@ func splitConnectionString(dsn string) (res map[string]string) {
var value string = ""
if len(lst) > 1 {
value = strings.TrimSpace(lst[1])
// Remove surrounding double quotes if present
if len(value) >= 2 && strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
value = value[1 : len(value)-1]
// Unescape double quotes
value = strings.ReplaceAll(value, "\"\"", "\"")
}
}
synonym, hasSynonym := adoSynonyms[name]
if hasSynonym {
Expand All @@ -699,6 +705,44 @@ func splitConnectionString(dsn string) (res map[string]string) {
return res
}

// splitAdoConnectionStringParts splits an ADO connection string into parts,
// properly handling double-quoted values that may contain semicolons
func splitAdoConnectionStringParts(dsn string) []string {
var parts []string
var current strings.Builder
inQuotes := false

for i := 0; i < len(dsn); i++ {
char := dsn[i]

if char == '"' {
if inQuotes && i+1 < len(dsn) && dsn[i+1] == '"' {
// Double quote escape sequence - add both quotes to current part
current.WriteByte(char)
Copy link
Collaborator

@shueybubbles shueybubbles Aug 13, 2025

Choose a reason for hiding this comment

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

WriteByte

shouldn't this code use WriteRune instead of WriteByte?
Go strings are UTF-8 encoded and may have multi-byte characters. Include a test with multi-byte UTF8 character values. @copilot #Closed

Copy link
Author

Choose a reason for hiding this comment

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

Fixed in commit 4794128. Changed the function to properly handle UTF-8 runes by using WriteRune instead of WriteByte and iterating over runes instead of bytes. Added comprehensive test cases with multibyte UTF-8 characters including Cyrillic, Chinese, emoji, and accented characters.

current.WriteByte(dsn[i+1])
i++ // Skip the next quote
} else {
// Start or end of quoted section
inQuotes = !inQuotes
current.WriteByte(char)
}
} else if char == ';' && !inQuotes {
// Semicolon outside of quotes - end current part
parts = append(parts, current.String())
current.Reset()
} else {
current.WriteByte(char)
}
}

// Add the last part if it's not empty
if current.Len() > 0 {
parts = append(parts, current.String())
}

return parts
}

// Splits a URL of the form sqlserver://username:password@host/instance?param1=value&param2=value
func splitConnectionStringURL(dsn string) (map[string]string, error) {
res := map[string]string{}
Expand Down
19 changes: 19 additions & 0 deletions msdsn/conn_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ func TestValidConnectionString(t *testing.T) {
{"MultiSubnetFailover=false", func(p Config) bool { return !p.MultiSubnetFailover }},
{"timezone=Asia/Shanghai", func(p Config) bool { return p.Encoding.Timezone.String() == "Asia/Shanghai" }},
{"Pwd=placeholder", func(p Config) bool { return p.Password == "placeholder" }},

// ADO connection string tests with double-quoted values containing semicolons
{"server=test;password=\"pass;word\"", func(p Config) bool { return p.Host == "test" && p.Password == "pass;word" }},
{"password=\"[2+R2B6O:fF/[;]cJsr\"", func(p Config) bool { return p.Password == "[2+R2B6O:fF/[;]cJsr" }},
{"server=host;user id=user;password=\"complex;pass=word\"", func(p Config) bool {
return p.Host == "host" && p.User == "user" && p.Password == "complex;pass=word"
}},
{"password=\"value with \"\"quotes\"\" inside\"", func(p Config) bool { return p.Password == "value with \"quotes\" inside" }},
{"server=test;password=\"simple\"", func(p Config) bool { return p.Host == "test" && p.Password == "simple" }},
// Test case specifically for the reported issue #282
{"Server=tcp:sql.database.windows.net,1433;Initial Catalog=MyDatabase;User [email protected];Password=\"[2+R2B6O:fF/[;]cJsr\"", func(p Config) bool {
return p.Host == "sql.database.windows.net" && p.Database == "MyDatabase" && p.User == "[email protected]" && p.Password == "[2+R2B6O:fF/[;]cJsr"
}},
// Additional edge cases for double-quoted values
{"password=\"\"", func(p Config) bool { return p.Password == "" }}, // Empty quoted password
{"password=\";\"", func(p Config) bool { return p.Password == ";" }}, // Just a semicolon
{"password=\";;\"", func(p Config) bool { return p.Password == ";;" }}, // Multiple semicolons
{"server=\"host;name\";password=\"pass;word\"", func(p Config) bool { return p.Host == "host;name" && p.Password == "pass;word" }}, // Multiple quoted values

// those are supported currently, but maybe should not be
{"someparam", func(p Config) bool { return true }},
{";;=;", func(p Config) bool { return true }},
Expand Down