Skip to content

Commit 08a8962

Browse files
Merge pull request #408 from bgraves-lo/398-escape-raises-error-for-pound-sign
Fix escaping of # and space in attrs
2 parents 3f94287 + c5a115e commit 08a8962

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

lib/net/ldap/dn.rb

+4-12
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,19 @@ def to_s
192192
# http://tools.ietf.org/html/rfc2253 section 2.4 lists these exceptions
193193
# for dn values. All of the following must be escaped in any normal string
194194
# using a single backslash ('\') as escape.
195-
ESCAPES = {
196-
',' => ',',
197-
'+' => '+',
198-
'"' => '"',
199-
'\\' => '\\',
200-
'<' => '<',
201-
'>' => '>',
202-
';' => ';',
203-
}
195+
ESCAPES = %w[, + " \\ < > ;]
204196

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

212204
##
213205
# Escape a string for use in a DN value
214206
def self.escape(string)
215-
string.gsub(ESCAPE_RE) { |char| "\\" + ESCAPES[char] }
207+
string.gsub(ESCAPE_RE) { |char| "\\" + char }
216208
end
217209

218210
##

test/test_dn.rb

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ def test_escape
66
assert_equal '\\,\\+\\"\\\\\\<\\>\\;', Net::LDAP::DN.escape(',+"\\<>;')
77
end
88

9+
def test_escape_pound_sign
10+
assert_equal '\\#test', Net::LDAP::DN.escape('#test')
11+
end
12+
13+
def test_escape_space
14+
assert_equal '\\ before_after\\ ', Net::LDAP::DN.escape(' before_after ')
15+
end
16+
917
def test_escape_on_initialize
1018
dn = Net::LDAP::DN.new('cn', ',+"\\<>;', 'ou=company')
1119
assert_equal 'cn=\\,\\+\\"\\\\\\<\\>\\;,ou=company', dn.to_s

0 commit comments

Comments
 (0)