Skip to content

Commit

Permalink
fix (ldap): improve EscapeValue(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambrt committed Jul 14, 2023
1 parent 822334c commit dadacf7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion ldap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,10 +830,14 @@ func EscapeValue(input string) string {
// - trailing space
// - special characters '"', '+', ',', ';', '<', '>', '\\'
// - null
for i := 0; i < len(input); i++ {
inputLen := len(input)
for i := 0; i < inputLen; i++ {
escaped := false
if input[i] == '\\' {
i++
if i > inputLen-1 {
break
}
escaped = true
}
switch input[i] {
Expand All @@ -843,6 +847,9 @@ func EscapeValue(input string) string {
i++
}
continue
case '\000':
input = input[0:i] + `\00` + input[i+1:]
continue
}
if escaped {
input = input[0:i] + "\\" + input[i:]
Expand Down
6 changes: 5 additions & 1 deletion ldap/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ func Test_EscapeValue(t *testing.T) {
"test\\hello": "test\\\\hello",
" test ": "\\ test \\ ",
"": "",
`\`: `\`,
"golang\000": `golang\00`,
"go\000lang": `go\00lang`,
"\000": `\00`,
}

for test, answer := range testcases {
res := EscapeValue(test)
if res != answer {
t.Errorf("Failed to escape %s: %s != %s\n", test, res, answer)
t.Errorf("Failed to escape %q: %q != %q\n", test, res, answer)
}
}
}

0 comments on commit dadacf7

Please sign in to comment.