Skip to content

Commit

Permalink
protect against mutating frozen strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mwrock committed Sep 15, 2016
1 parent 4529105 commit 29515af
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/net/ntlm/encode_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.swap16(str)
# Decode a UTF16 string to a ASCII string
# @param [String] str The string to convert
def self.decode_utf16le(str)
str.force_encoding(Encoding::UTF_16LE)
str.dup.force_encoding(Encoding::UTF_16LE)
str.encode(Encoding::UTF_8, Encoding::UTF_16LE).force_encoding('UTF-8')
end

Expand All @@ -39,7 +39,6 @@ def self.decode_utf16le(str)
# the function will convert the string bytes to UTF-16LE and note the encoding as UTF-8 so that byte
# concatination works seamlessly.
def self.encode_utf16le(str)
str = str.force_encoding('UTF-8') if [::Encoding::ASCII_8BIT,::Encoding::US_ASCII].include?(str.encoding)
str.dup.force_encoding('UTF-8').encode(Encoding::UTF_16LE, Encoding::UTF_8).force_encoding('UTF-8')
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/net/ntlm/encode_util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

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

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

0 comments on commit 29515af

Please sign in to comment.