Files
CommandForge/src/CommandForge.Infrastructure/Execution/ExecutableResolver.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

57 lines
2.3 KiB
C#

namespace CommandForge.Infrastructure.Execution;
/// <summary>
/// Turns a catalog executable name ("dism", "powershell", "services.msc") into an absolute path in
/// the Windows system directories. CreateProcess otherwise searches the application's own folder
/// first — for a per-user install that folder is writable by the user, so a planted "reg.exe" next
/// to the (elevated) Elevator would run with administrator rights.
/// </summary>
internal static class ExecutableResolver
{
private static readonly string[] ImplicitExtensions = [".exe", ".com", ".msc", ".cpl"];
/// <summary>Returns the absolute path for <paramref name="executable"/>, or the input unchanged when it can't be resolved.</summary>
public static string Resolve(string executable)
{
if (string.IsNullOrWhiteSpace(executable) || Path.IsPathRooted(executable)
|| executable.IndexOfAny([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]) >= 0)
{
return executable;
}
if (IsName(executable, "powershell"))
{
return Path.Combine(SystemPaths.System32, "WindowsPowerShell", "v1.0", "powershell.exe");
}
if (IsName(executable, "winget"))
{
// winget is an App Execution Alias in the user's WindowsApps folder, not a system binary.
var alias = Path.Combine(SystemPaths.LocalAppData, "Microsoft", "WindowsApps", "winget.exe");
return File.Exists(alias) ? alias : executable;
}
var candidates = Path.HasExtension(executable)
? [executable]
: ImplicitExtensions.Select(extension => executable + extension).ToArray();
foreach (var directory in new[] { SystemPaths.System32, SystemPaths.WindowsDirectory })
{
foreach (var candidate in candidates)
{
var path = Path.Combine(directory, candidate);
if (File.Exists(path))
{
return path;
}
}
}
return executable;
}
private static bool IsName(string executable, string name)
=> string.Equals(executable, name, StringComparison.OrdinalIgnoreCase)
|| string.Equals(executable, name + ".exe", StringComparison.OrdinalIgnoreCase);
}