Skip to content

Commit 1caa90c

Browse files
committed
(GH-1759) Preserve encoding of commands in powershell transport
When conveying data from the WinRM transport back to the powershell shell class, we use a set of pipes. Those pipes acquire the default external encoding of Ruby, even though the winrm gem ensures that the content will always be UTF-8. We now properly tag those pipes as UTF-8 to avoid the data being misinterpreted. In order to preserve \r\n line endings for Windows targets, the powershell shell class then puts those pipes into binmode. That causes the character encoding information to be stripped away entirely. We now save that encoding information before converting the pipe and we reassert the encoding on the string after reading it. This addresses an issue where systems with non-UTF-8 encodings would have valid UTF-8 data misinterpreted as invalid data in their native character encoding. !bug * **Improved support for non-UTF-8 character encodings** ([#1759](#1759)) Commands run from a target where the default character encoding is non-UTF-8 will now return proper results when using the WinRM transport.
1 parent 940d3ea commit 1caa90c

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/bolt/shell/powershell.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,18 @@ def execute(command)
267267

268268
result = Bolt::Node::Output.new
269269
inp.close
270-
out.binmode
271-
err.binmode
272-
stdout = Thread.new { result.stdout << out.read }
273-
stderr = Thread.new { result.stderr << err.read }
270+
stdout = Thread.new do
271+
# Set to binmode to preserve \r\n line endings, but save and restore
272+
# the proper encoding so the string isn't later misinterpreted
273+
encoding = out.external_encoding
274+
out.binmode
275+
result.stdout << out.read.force_encoding(encoding)
276+
end
277+
stderr = Thread.new do
278+
encoding = err.external_encoding
279+
err.binmode
280+
result.stderr << err.read.force_encoding(encoding)
281+
end
274282

275283
stdout.join
276284
stderr.join

lib/bolt/transport/winrm/connection.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ def execute(command)
108108
# it will fail if the shell attempts to provide stdin
109109
inp.close
110110

111-
out_rd, out_wr = IO.pipe
112-
err_rd, err_wr = IO.pipe
111+
out_rd, out_wr = IO.pipe('UTF-8')
112+
err_rd, err_wr = IO.pipe('UTF-8')
113113
th = Thread.new do
114114
result = @session.run(command)
115115
out_wr << result.stdout

0 commit comments

Comments
 (0)