Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to escape distinguished names #393

Merged
merged 2 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"

ber "github.com/go-asn1-ber/asn1-ber"
)
Expand Down Expand Up @@ -345,3 +346,43 @@ func EscapeFilter(filter string) string {
}
return string(buf)
}

// EscapeDN escapes distinguished names as described in RFC4514. Characters in the
// set `"+,;<>\` are escaped by prepending a backslash, which is also done for trailing
// spaces or a leading `#`. Null bytes are replaced with `\00`.
func EscapeDN(dn string) string {
if dn == "" {
return ""
}

builder := strings.Builder{}

for i, r := range dn {
// Escape leading and trailing spaces
if (i == 0 || i == len(dn)-1) && r == ' ' {
builder.WriteRune('\\')
builder.WriteRune(r)
continue
}

// Escape leading '#'
if i == 0 && r == '#' {
builder.WriteRune('\\')
builder.WriteRune(r)
continue
}

// Escape characters as defined in RFC4514
switch r {
case '"', '+', ',', ';', '<', '>', '\\':
builder.WriteRune('\\')
builder.WriteRune(r)
case '\x00': // Null byte may not be escaped by a leading backslash
builder.WriteString("\\00")
default:
builder.WriteRune(r)
}
}

return builder.String()
}
24 changes: 24 additions & 0 deletions ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,27 @@ func Test_addControlDescriptions(t *testing.T) {
})
}
}

func TestEscapeDN(t *testing.T) {
tests := []struct {
name string
dn string
want string
}{
{name: "emptyString", dn: "", want: ""},
{name: "comma", dn: "test,user", want: "test\\,user"},
{name: "numberSign", dn: "#test#user#", want: "\\#test#user#"},
{name: "backslash", dn: "\\test\\user\\", want: "\\\\test\\\\user\\\\"},
{name: "whitespaces", dn: " test user ", want: "\\ test user \\ "},
{name: "nullByte", dn: "\u0000te\x00st\x00user" + string(rune(0)), want: "\\00te\\00st\\00user\\00"},
{name: "variousCharacters", dn: "test\"+,;<>\\-_user", want: "test\\\"\\+\\,\\;\\<\\>\\\\-_user"},
{name: "multiByteRunes", dn: "test\u0391user ", want: "test\u0391user\\ "},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := EscapeDN(tt.dn); got != tt.want {
t.Errorf("EscapeDN(%s) = %s, expected %s", tt.dn, got, tt.want)
}
})
}
}
41 changes: 41 additions & 0 deletions v3/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"

ber "github.com/go-asn1-ber/asn1-ber"
)
Expand Down Expand Up @@ -345,3 +346,43 @@ func EscapeFilter(filter string) string {
}
return string(buf)
}

// EscapeDN escapes distinguished names as described in RFC4514. Characters in the
// set `"+,;<>\` are escaped by prepending a backslash, which is also done for trailing
// spaces or a leading `#`. Null bytes are replaced with `\00`.
func EscapeDN(dn string) string {
if dn == "" {
return ""
}

builder := strings.Builder{}

for i, r := range dn {
// Escape leading and trailing spaces
if (i == 0 || i == len(dn)-1) && r == ' ' {
builder.WriteRune('\\')
builder.WriteRune(r)
continue
}

// Escape leading '#'
if i == 0 && r == '#' {
builder.WriteRune('\\')
builder.WriteRune(r)
continue
}

// Escape characters as defined in RFC4514
switch r {
case '"', '+', ',', ';', '<', '>', '\\':
builder.WriteRune('\\')
builder.WriteRune(r)
case '\x00': // Null byte may not be escaped by a leading backslash
builder.WriteString("\\00")
default:
builder.WriteRune(r)
}
}

return builder.String()
}
24 changes: 24 additions & 0 deletions v3/ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,27 @@ func Test_addControlDescriptions(t *testing.T) {
})
}
}

func TestEscapeDN(t *testing.T) {
tests := []struct {
name string
dn string
want string
}{
{name: "emptyString", dn: "", want: ""},
{name: "comma", dn: "test,user", want: "test\\,user"},
{name: "numberSign", dn: "#test#user#", want: "\\#test#user#"},
{name: "backslash", dn: "\\test\\user\\", want: "\\\\test\\\\user\\\\"},
{name: "whitespaces", dn: " test user ", want: "\\ test user \\ "},
{name: "nullByte", dn: "\u0000te\x00st\x00user" + string(rune(0)), want: "\\00te\\00st\\00user\\00"},
{name: "variousCharacters", dn: "test\"+,;<>\\-_user", want: "test\\\"\\+\\,\\;\\<\\>\\\\-_user"},
{name: "multiByteRunes", dn: "test\u0391user ", want: "test\u0391user\\ "},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := EscapeDN(tt.dn); got != tt.want {
t.Errorf("EscapeDN(%s) = %s, expected %s", tt.dn, got, tt.want)
}
})
}
}