Skip to content

Commit 3a8b8a2

Browse files
committed
LDAP uses UTF-8 strings
1 parent 7dd6c3a commit 3a8b8a2

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/net/ber.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ class Net::BER::BerIdentifiedString < String
295295
attr_accessor :ber_identifier
296296
def initialize args
297297
super args
298+
# LDAP uses UTF-8 encoded strings
299+
force_encoding('UTF-8') if respond_to?(:encoding)
298300
end
299301
end
300302

lib/net/ber/core_ext/string.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,21 @@ module Net::BER::Extensions::String
1212
# User code should call either #to_ber_application_string or
1313
# #to_ber_contextspecific.
1414
def to_ber(code = 0x04)
15-
[code].pack('C') + length.to_ber_length_encoding + self
15+
raw_string = raw_utf8_encoded
16+
[code].pack('C') + raw_string.length.to_ber_length_encoding + raw_string
1617
end
1718

19+
def raw_utf8_encoded
20+
if self.respond_to?(:encode)
21+
# Strings should be UTF-8 encoded according to LDAP.
22+
# However, the BER code is not necessarily valid UTF-8
23+
self.encode('UTF-8').force_encoding('ASCII-8BIT')
24+
else
25+
self
26+
end
27+
end
28+
private :raw_utf8_encoded
29+
1830
##
1931
# Creates an application-specific BER string encoded value with the
2032
# provided syntax code value.

spec/unit/ber/ber_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@
7575
end
7676
end
7777
end
78+
if "Ruby 1.9".respond_to?(:encoding)
79+
context "strings" do
80+
it "should properly encode UTF-8 strings" do
81+
"\u00e5".force_encoding("UTF-8").to_ber.should ==
82+
"\x04\x02\xC3\xA5"
83+
end
84+
it "should properly encode strings encodable as UTF-8" do
85+
"teststring".encode("US-ASCII").to_ber.should == "\x04\nteststring"
86+
end
87+
it "should fail on strings that can not be converted to UTF-8" do
88+
error = Encoding::UndefinedConversionError
89+
lambda {"\x81".to_ber }.should raise_exception(error)
90+
end
91+
end
92+
end
7893
end
7994

8095
describe "BER decoding of" do
@@ -91,4 +106,4 @@
91106
[1, [3, "Administrator", "ad_is_bogus"]]
92107
end
93108
end
94-
end
109+
end

0 commit comments

Comments
 (0)