Skip to content

Commit 397f05d

Browse files
authored
Use UTF-8 encoding for UI helper standard output parsing (#1326)
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
2 parents 71a62ae + b8f4c28 commit 397f05d

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/shared/Core/Authentication/AuthenticationBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ protected internal virtual async Task<IDictionary<string, string>> InvokeHelperA
3535
RedirectStandardInput = true,
3636
RedirectStandardOutput = true,
3737
RedirectStandardError = false, // Do not redirect stderr as tracing might be enabled
38-
UseShellExecute = false
38+
UseShellExecute = false,
39+
StandardOutputEncoding = EncodingEx.UTF8NoBom,
3940
};
4041

4142
Context.Trace.WriteLine($"Starting helper process: {path} {args}");

src/shared/Core/EncodingEx.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Text;
2+
3+
namespace GitCredentialManager;
4+
5+
public static class EncodingEx
6+
{
7+
public static readonly Encoding UTF8NoBom = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
8+
}

src/shared/Core/StandardStreams.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public class StandardStreams : IStandardStreams
2929
{
3030
private const string LineFeed = "\n";
3131

32-
private static readonly Encoding Utf8NoBomEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
33-
3432
private TextReader _stdIn;
3533
private TextWriter _stdOut;
3634
private TextWriter _stdErr;
@@ -41,7 +39,7 @@ public TextReader In
4139
{
4240
if (_stdIn == null)
4341
{
44-
_stdIn = new StreamReader(Console.OpenStandardInput(), Utf8NoBomEncoding);
42+
_stdIn = new StreamReader(Console.OpenStandardInput(), EncodingEx.UTF8NoBom);
4543
}
4644

4745
return _stdIn;
@@ -54,7 +52,7 @@ public TextWriter Out
5452
{
5553
if (_stdOut == null)
5654
{
57-
_stdOut = new StreamWriter(Console.OpenStandardOutput(), Utf8NoBomEncoding)
55+
_stdOut = new StreamWriter(Console.OpenStandardOutput(), EncodingEx.UTF8NoBom)
5856
{
5957
AutoFlush = true,
6058
NewLine = LineFeed,
@@ -71,7 +69,7 @@ public TextWriter Error
7169
{
7270
if (_stdErr == null)
7371
{
74-
_stdErr = new StreamWriter(Console.OpenStandardError(), Utf8NoBomEncoding)
72+
_stdErr = new StreamWriter(Console.OpenStandardError(), EncodingEx.UTF8NoBom)
7573
{
7674
AutoFlush = true,
7775
NewLine = LineFeed,

0 commit comments

Comments
 (0)