Skip to content

Commit 78f73a1

Browse files
committed
protect against mutating frozen strings
1 parent 27d5288 commit 78f73a1

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

Diff for: lib/net/ntlm/encode_util.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def self.swap16(str)
2727
# Decode a UTF16 string to a ASCII string
2828
# @param [String] str The string to convert
2929
def self.decode_utf16le(str)
30-
str.force_encoding(Encoding::UTF_16LE)
30+
str.dup.force_encoding(Encoding::UTF_16LE)
3131
str.encode(Encoding::UTF_8, Encoding::UTF_16LE).force_encoding('UTF-8')
3232
end
3333

@@ -39,7 +39,6 @@ def self.decode_utf16le(str)
3939
# the function will convert the string bytes to UTF-16LE and note the encoding as UTF-8 so that byte
4040
# concatination works seamlessly.
4141
def self.encode_utf16le(str)
42-
str = str.force_encoding('UTF-8') if [::Encoding::ASCII_8BIT,::Encoding::US_ASCII].include?(str.encoding)
4342
str.dup.force_encoding('UTF-8').encode(Encoding::UTF_16LE, Encoding::UTF_8).force_encoding('UTF-8')
4443
end
4544
end

Diff for: spec/lib/net/ntlm/encode_util_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
context '#encode_utf16le' do
66
it 'should convert an ASCII string to UTF' do
7-
expect(Net::NTLM::EncodeUtil.encode_utf16le('Test')).to eq("T\x00e\x00s\x00t\x00")
7+
expect(Net::NTLM::EncodeUtil.encode_utf16le('Test'.encode(::Encoding::ASCII_8BIT).freeze)).to eq("T\x00e\x00s\x00t\x00")
88
end
99
end
1010

1111
context '#decode_utf16le' do
1212
it 'should convert a UTF string to ASCII' do
13-
expect(Net::NTLM::EncodeUtil.decode_utf16le("T\x00e\x00s\x00t\x00")).to eq('Test')
13+
expect(Net::NTLM::EncodeUtil.decode_utf16le("T\x00e\x00s\x00t\x00".freeze)).to eq('Test')
1414
end
1515
end
1616
end

0 commit comments

Comments
 (0)