Skip to content

Commit

Permalink
Use UTF-8 encoding for UI helper standard output parsing (#1326)
Browse files Browse the repository at this point in the history
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.

Fixes #1287
  • Loading branch information
mjcheetham committed Jul 11, 2023
2 parents 71a62ae + b8f4c28 commit 397f05d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/shared/Core/Authentication/AuthenticationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ protected internal virtual async Task<IDictionary<string, string>> 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}");
Expand Down
8 changes: 8 additions & 0 deletions src/shared/Core/EncodingEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Text;

namespace GitCredentialManager;

public static class EncodingEx
{
public static readonly Encoding UTF8NoBom = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
}
8 changes: 3 additions & 5 deletions src/shared/Core/StandardStreams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 397f05d

Please sign in to comment.