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 encoding corruptions #43

Merged
merged 4 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion lib/winrm-elevated/scripts/elevated_shell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ try {

function SlurpOutput($file, $cur_line, $out_type) {
if (Test-Path $file) {
get-content $file | Select-Object -skip $cur_line | ForEach-Object {
get-content -Encoding Oem $file | Select-Object -skip $cur_line | ForEach-Object {
fmang marked this conversation as resolved.
Show resolved Hide resolved
$cur_line += 1
if ($out_type -eq 'err') {
$host.ui.WriteErrorLine("$_")
Expand Down
4 changes: 3 additions & 1 deletion lib/winrm/shells/elevated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def close

def upload_elevated_shell_script(script_text)
elevated_shell_path = 'c:/windows/temp/winrm-elevated-shell-' + SecureRandom.uuid + '.ps1'
script_text_with_exit = "#{script_text}\r\n$Host.SetShouldExit($LASTEXITCODE)"
# Prepend the content of the file with an UTF-8 BOM for Windows to read it as such instead of the default
# Windows-XXXX encoding, and convert script_text accordingly if needed.
script_text_with_exit = "\uFEFF#{script_text.encode(Encoding::UTF_8)}\r\n$Host.SetShouldExit($LASTEXITCODE)"
@winrm_file_transporter.upload(StringIO.new(script_text_with_exit), elevated_shell_path)
elevated_shell_path
end
Expand Down
16 changes: 16 additions & 0 deletions spec/powershell_elevated_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@
it { should match(/Windows IP Configuration/) }
end

describe 'special characters' do
subject(:output) { elevated_shell.run("echo \"#{text}\"") }
# Sample text using more than ASCII, but still compatible with occidental OEM encodings.
let(:text) do
'Dès Noël, où un zéphyr haï me vêt de glaçons würmiens, je dîne d’exquis rôtis de bœuf au kir, ' \
'à l’aÿ d’âge mûr, &cætera.'
end

it { should have_exit_code 0 }
it 'outputs a transliterated version of the original string' do
expect(output.stdout).to eq "Dès Noël, où un zéphyr haï me vêt de glaçons würmiens, je dîne d'exquis " \
"rôtis de bouf au kir, à l'aÿ d'âge mûr, &cætera.\r\n"
end
it { should have_no_stderr }
end

describe 'capturing output from Write-Host and Write-Error' do
subject(:output) do
script = <<-COMMAND
Expand Down