From b8f4c28742268a7b5c783c1b302e12450ef7b8b1 Mon Sep 17 00:00:00 2001 From: Matthew John Cheetham Date: Tue, 11 Jul 2023 13:30:26 -0700 Subject: [PATCH] authbase: use UTF-8 encoding for helper stdout Ensure that we read helper UI standard output as UTF-8. Helper applications already write output with a UTF-8 encoding, so we must ensure we read using the same encoding in the core application or else non-ASCII characters may be mangled. --- src/shared/Core/Authentication/AuthenticationBase.cs | 3 ++- src/shared/Core/EncodingEx.cs | 8 ++++++++ src/shared/Core/StandardStreams.cs | 8 +++----- 3 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 src/shared/Core/EncodingEx.cs diff --git a/src/shared/Core/Authentication/AuthenticationBase.cs b/src/shared/Core/Authentication/AuthenticationBase.cs index f1edb00a0..03e4d8ca6 100644 --- a/src/shared/Core/Authentication/AuthenticationBase.cs +++ b/src/shared/Core/Authentication/AuthenticationBase.cs @@ -35,7 +35,8 @@ protected internal virtual async Task> InvokeHelperA RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = false, // Do not redirect stderr as tracing might be enabled - UseShellExecute = false + UseShellExecute = false, + StandardOutputEncoding = EncodingEx.UTF8NoBom, }; Context.Trace.WriteLine($"Starting helper process: {path} {args}"); diff --git a/src/shared/Core/EncodingEx.cs b/src/shared/Core/EncodingEx.cs new file mode 100644 index 000000000..11895cc09 --- /dev/null +++ b/src/shared/Core/EncodingEx.cs @@ -0,0 +1,8 @@ +using System.Text; + +namespace GitCredentialManager; + +public static class EncodingEx +{ + public static readonly Encoding UTF8NoBom = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); +} diff --git a/src/shared/Core/StandardStreams.cs b/src/shared/Core/StandardStreams.cs index 026ef481b..d0b3042b0 100644 --- a/src/shared/Core/StandardStreams.cs +++ b/src/shared/Core/StandardStreams.cs @@ -29,8 +29,6 @@ public class StandardStreams : IStandardStreams { private const string LineFeed = "\n"; - private static readonly Encoding Utf8NoBomEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); - private TextReader _stdIn; private TextWriter _stdOut; private TextWriter _stdErr; @@ -41,7 +39,7 @@ public TextReader In { if (_stdIn == null) { - _stdIn = new StreamReader(Console.OpenStandardInput(), Utf8NoBomEncoding); + _stdIn = new StreamReader(Console.OpenStandardInput(), EncodingEx.UTF8NoBom); } return _stdIn; @@ -54,7 +52,7 @@ public TextWriter Out { if (_stdOut == null) { - _stdOut = new StreamWriter(Console.OpenStandardOutput(), Utf8NoBomEncoding) + _stdOut = new StreamWriter(Console.OpenStandardOutput(), EncodingEx.UTF8NoBom) { AutoFlush = true, NewLine = LineFeed, @@ -71,7 +69,7 @@ public TextWriter Error { if (_stdErr == null) { - _stdErr = new StreamWriter(Console.OpenStandardError(), Utf8NoBomEncoding) + _stdErr = new StreamWriter(Console.OpenStandardError(), EncodingEx.UTF8NoBom) { AutoFlush = true, NewLine = LineFeed,