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

Handle console output that is improperly encoded #121

Merged
merged 2 commits into from
Jun 14, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. This projec
## [Unreleased]

* Your contribution here!
* [#121](https://github.com/mattbrictson/airbrussh/pull/121): Gracefully handle SSH output that has invalid UTF-8 encoding instead of raising an exception - [@mattbrictson](https://github.com/mattbrictson)

## [1.3.1][] (2018-11-04)

Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ environment:

install:
- SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
- gem install bundler --conservative --no-document -v "~>1.17"
- gem uninstall bundler --all --executables
- gem install bundler --no-document -v "~>1.17"
- bundle install --retry=3

test_script:
Expand Down
10 changes: 9 additions & 1 deletion lib/airbrussh/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def truncate_to_console_width(string)
end

def strip_ascii_color(string)
(string || "").gsub(/\033\[[0-9;]*m/, "")
string ||= ""
string = to_utf8(string) unless string.valid_encoding?

string.gsub(/\033\[[0-9;]*m/, "")
end

def console_width
Expand Down Expand Up @@ -85,5 +88,10 @@ def utf8_supported?(string)
rescue Encoding::UndefinedConversionError
false
end

def to_utf8(string)
string.force_encoding("ASCII-8BIT")
.encode("UTF-8", :invalid => :replace, :undef => :replace)
end
end
end
11 changes: 11 additions & 0 deletions test/airbrussh/console_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ def test_truncates_improperly_encoded_ascii_string
assert_equal(ascii_8bit("The ‘...\n"), ascii_8bit(output))
end

def test_print_line_handles_invalid_utf8
console = configured_console(:tty => false)

invalid_utf8 = "The ‘quick’ brown fox"
.encode("Windows-1255")
.force_encoding("UTF-8")

console.print_line(invalid_utf8)
assert_equal("The �quick� brown fox\n", output)
end

def test_doesnt_truncates_to_zero_width
console = configured_console(:tty => true) do |config|
config.color = false
Expand Down