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

fix: Update logging to coerce ASCII-8BIT into UTF-8. #1076

Merged
merged 1 commit into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,15 @@ def self.wrap_logfmt_value(val)
# Hopefully val is a string, but protect in case it's not.
val = val.to_s

# Some values returned by the server are encoded in ASCII-8BIT before
# being parsed as UTF-8 by Marshal. If we don't transform these here, then
# puts will fail as it tries to render UTF-8 characters as ASCII-8BIT
# which is not valid.
if val && val.encoding == Encoding::ASCII_8BIT
# Dup the string as it is a frozen literal.
val = val.dup.force_encoding("UTF-8")
end

if %r{[^\w\-/]} =~ val
# If the string contains any special characters, escape any double
# quotes it has, remove newlines, and wrap the whole thing in quotes.
Expand Down
7 changes: 7 additions & 0 deletions test/stripe/util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@ def isatty
should "not error if given a non-string" do
assert_equal "true", Util.send(:wrap_logfmt_value, true)
end

should "handle UTF-8 characters encoded in ASCII-8BIT" do
expected_utf8_str = "\"é\"".dup.force_encoding(Encoding::UTF_8.name)
ascii_8bit_str = "é".dup.force_encoding(Encoding::ASCII_8BIT.name)

assert_equal expected_utf8_str, Util.send(:wrap_logfmt_value, ascii_8bit_str)
end
end
end
end