Skip to content
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
16 changes: 4 additions & 12 deletions lib/net/ldap/dn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,27 +192,19 @@ def to_s
# http://tools.ietf.org/html/rfc2253 section 2.4 lists these exceptions
# for dn values. All of the following must be escaped in any normal string
# using a single backslash ('\') as escape.
ESCAPES = {
',' => ',',
'+' => '+',
'"' => '"',
'\\' => '\\',
'<' => '<',
'>' => '>',
';' => ';',
}
ESCAPES = %w[, + " \\ < > ;]

# Compiled character class regexp using the keys from the above hash, and
# Compiled character class regexp using the values from the above list, and
# checking for a space or # at the start, or space at the end, of the
# string.
ESCAPE_RE = Regexp.new("(^ |^#| $|[" +
ESCAPES.keys.map { |e| Regexp.escape(e) }.join +
ESCAPES.map { |e| Regexp.escape(e) }.join +
"])")

##
# Escape a string for use in a DN value
def self.escape(string)
string.gsub(ESCAPE_RE) { |char| "\\" + ESCAPES[char] }
string.gsub(ESCAPE_RE) { |char| "\\" + char }
end

##
Expand Down
8 changes: 8 additions & 0 deletions test/test_dn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ def test_escape
assert_equal '\\,\\+\\"\\\\\\<\\>\\;', Net::LDAP::DN.escape(',+"\\<>;')
end

def test_escape_pound_sign
assert_equal '\\#test', Net::LDAP::DN.escape('#test')
end

def test_escape_space
assert_equal '\\ before_after\\ ', Net::LDAP::DN.escape(' before_after ')
end

def test_escape_on_initialize
dn = Net::LDAP::DN.new('cn', ',+"\\<>;', 'ou=company')
assert_equal 'cn=\\,\\+\\"\\\\\\<\\>\\;,ou=company', dn.to_s
Expand Down