Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect against mutating frozen strings #30

Merged
merged 1 commit into from
Sep 15, 2016
Merged

Protect against mutating frozen strings #30

merged 1 commit into from
Sep 15, 2016

Conversation

mwrock
Copy link
Member

@mwrock mwrock commented Sep 15, 2016

I have seen a few occurence where passing in credential strings that are frozen, usually because they are from environment variables, a can't modify frozen String error is raised from rubyntlm.

In this PR, the altered tests demonstrate this:

Failures:

  1) Net::NTLM::EncodeUtil#encode_utf16le should convert an ASCII string to UTF
     Failure/Error: str = str.force_encoding('UTF-8') if [::Encoding::ASCII_8BIT,::Encoding::US_ASCII].include?(str.encoding)

     RuntimeError:
       can't modify frozen String
     # ./lib/net/ntlm/encode_util.rb:42:in `force_encoding'
     # ./lib/net/ntlm/encode_util.rb:42:in `encode_utf16le'
     # ./spec/lib/net/ntlm/encode_util_spec.rb:7:in `block (3 levels) in <top (required)>'

This PR duplicates the string in decode to ensure the encoding changes occur on a different instance. For encode_utf16le, I simply removed:

str = str.force_encoding('UTF-8') if [::Encoding::ASCII_8BIT,::Encoding::US_ASCII].include?(str.encoding)

which seemed unnecesary given that the next line, which already duplicates the value, begins with the same forced encoding.

@mwrock
Copy link
Member Author

mwrock commented Sep 15, 2016

Also see WinRb/WinRM#221

@mwrock
Copy link
Member Author

mwrock commented Sep 15, 2016

rebased with #31 to fix travis. Looking to release this and include with a winrm release later today. cc @zenchild and @sneal

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to do an assignment here? Otherwise the next line will be against the original object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh. yes of course

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added reassignment @zenchild

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to find a way to make this blow up if that first encoding was omitted to better catch this in the unit tests but ruby seems very forgiving at least on the strings and encoding I was using

@zenchild
Copy link
Member

:shipit:

@mwrock mwrock merged commit 8bdd327 into master Sep 15, 2016
@mwrock mwrock deleted the freeze branch September 15, 2016 14:49
@mwrock
Copy link
Member Author

mwrock commented Sep 15, 2016

Thanks @zenchild ! Will do a release later on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants