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

Stop ascii_safe_hex() from crashing on UTF-8 strings #3

Merged
merged 2 commits into from
Oct 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/rex/text/hex.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: binary -*-
module Rex
module Text
# We are re-opening the module to add these module methods.
Expand Down Expand Up @@ -112,6 +111,11 @@ def self.hex_to_raw(str)
# @see to_hex Converts all the chars
#
def self.ascii_safe_hex(str, whitespace=false)
# This sanitization is terrible and breaks everything if it finds unicode.
# ~4 Billion can't be wrong; long-term, this should be removed.
if str.encoding == (::Encoding::UTF_8)
return str
end
if whitespace
str.gsub(/([\x00-\x20\x80-\xFF])/n){ |x| "\\x%.2x" % x.unpack("C*")[0] }
else
Expand Down