Files
CommandForge/src/CommandForge.Infrastructure/Execution/OutputDecoder.cs
T
poli0981andClaude Opus 5.5 37950a05ff feat(execution): trusted executable paths, encoding-aware output, kill-on-close job
- ExecutableResolver: catalog executables resolve to absolute paths in System32/Windows (and
  the winget alias), so CreateProcess never searches the per-user, user-writable app folder —
  a planted reg.exe/powershell.exe there would otherwise run elevated. Launch-mode commands now
  pin their working directory too.
- OutputDecoder: stdout/stderr are read as raw bytes and decoded per line (UTF-16LE when the
  stream is UTF-16, else strict UTF-8 with an OEM-code-page fallback). sfc/wsl output and every
  native tool on Vietnamese/Japanese/Chinese Windows used to show as mojibake. Lone \r (progress
  updates) still breaks lines.
- ProcessJob: captured commands join a kill-on-close job object, so closing or crashing the app
  can no longer leave a half-finished command running.
- ArgumentTokens ({SystemDrive}, {SystemRoot}, {UserProfile}) filled from SystemPaths, which reads
  the kernel-reported system directory, known folders and HKLM — never %SystemRoot%-style
  variables a standard user can redefine. The command preview shows the expanded values.
- Exit code 3010 always flags a restart; Launch commands honour RestartPolicy.Always; output is
  only captured (bounded) when a restart regex needs it. ExecutionResult.OutputLines is gone
  (nothing read it, and it held the entire output in memory).
- RunCommandUseCase always completes the output channel and maps cancellation, so a failing run
  can no longer leave the console waiting forever.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 20:19:03 +07:00

166 lines
4.8 KiB
C#

using System.Text;
namespace CommandForge.Infrastructure.Execution;
/// <summary>
/// Splits a child process's raw stdout/stderr bytes into text lines. Windows tools disagree on the
/// encoding of redirected output: most write the console's OEM code page (localized text on
/// Vietnamese/Japanese/Chinese Windows), newer tools write UTF-8, and sfc/wsl write UTF-16LE. So the
/// stream is sniffed once for UTF-16LE, and otherwise every line is decoded as strict UTF-8 with an
/// OEM-code-page fallback. Line breaks are \n, \r\n or a lone \r (progress updates).
/// </summary>
internal sealed class OutputDecoder
{
private static readonly UTF8Encoding StrictUtf8 = new(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
private readonly Encoding _fallback;
private readonly List<byte> _pending = [];
private bool? _utf16;
private bool _bomChecked;
public OutputDecoder(Encoding fallback)
{
ArgumentNullException.ThrowIfNull(fallback);
_fallback = fallback;
}
/// <summary>Appends <paramref name="chunk"/> and adds every completed line to <paramref name="lines"/>.</summary>
public void Push(ReadOnlySpan<byte> chunk, List<string> lines)
{
ArgumentNullException.ThrowIfNull(lines);
foreach (var b in chunk)
{
_pending.Add(b);
}
_utf16 ??= DetectUtf16(_pending);
if (_utf16 is null)
{
return; // not enough bytes yet to tell UTF-16LE from a byte encoding
}
if (_utf16 == true && !_bomChecked && _pending.Count >= 2)
{
if (_pending[0] == 0xFF && _pending[1] == 0xFE)
{
_pending.RemoveRange(0, 2);
}
_bomChecked = true;
}
ExtractLines(lines, endOfStream: false);
}
/// <summary>Returns the trailing text after the last line break (call once at end of stream).</summary>
public string? Flush()
{
_utf16 ??= false;
var lines = new List<string>();
ExtractLines(lines, endOfStream: true);
return lines.Count == 0 ? null : string.Join(Environment.NewLine, lines);
}
private void ExtractLines(List<string> lines, bool endOfStream)
{
var unit = _utf16 == true ? 2 : 1;
var start = 0;
var i = 0;
while (i + unit <= _pending.Count)
{
var code = CodeAt(i, unit);
if (code == '\n')
{
lines.Add(Decode(start, i));
i += unit;
start = i;
}
else if (code == '\r')
{
var next = i + unit;
if (next + unit > _pending.Count && !endOfStream)
{
break; // can't tell yet whether a \n follows
}
lines.Add(Decode(start, i));
var nextCode = next + unit <= _pending.Count ? CodeAt(next, unit) : -1;
i = nextCode == '\n' ? next + unit : next;
start = i;
}
else
{
i += unit;
}
}
if (endOfStream && start < _pending.Count)
{
lines.Add(Decode(start, _pending.Count));
start = _pending.Count;
}
_pending.RemoveRange(0, start);
}
/// <summary>The code unit at <paramref name="index"/>: one byte, or a little-endian UTF-16 unit.</summary>
private int CodeAt(int index, int unit)
=> unit == 2 ? _pending[index] | (_pending[index + 1] << 8) : _pending[index];
private string Decode(int start, int end)
{
if (end <= start)
{
return string.Empty;
}
var bytes = _pending.GetRange(start, end - start).ToArray();
if (_utf16 == true)
{
return Encoding.Unicode.GetString(bytes);
}
try
{
return StrictUtf8.GetString(bytes);
}
catch (DecoderFallbackException)
{
return _fallback.GetString(bytes);
}
}
/// <summary>UTF-16LE when a BOM is present or most odd bytes of plain-ASCII-range text are zero.</summary>
private static bool? DetectUtf16(List<byte> data)
{
if (data.Count >= 2 && data[0] == 0xFF && data[1] == 0xFE)
{
return true;
}
if (data.Count < 4)
{
return null; // decide once there is enough to look at
}
var sample = Math.Min(data.Count, 512) & ~1;
var zeroOdd = 0;
var zeroEven = 0;
for (var i = 0; i < sample; i += 2)
{
if (data[i] == 0)
{
zeroEven++;
}
if (data[i + 1] == 0)
{
zeroOdd++;
}
}
var pairs = sample / 2;
return zeroOdd * 2 >= pairs && zeroEven * 4 < pairs;
}
}