Files
poli0981andClaude Opus 5.5 81657aa058 fix(elevation): harden the broker and the Elevator
- Elevated children get an allow-listed environment rebuilt from trusted sources: per-user
  variables (COR_PROFILER/CORECLR_* DLL injection, PSModulePath module hijacking, windir/
  SystemRoot retargeting of recursive deletes) no longer reach commands run as administrator.
  PSModulePath is $PSHOME\Modules only, the one value Windows PowerShell 5.1 keeps as-is.
- Both ends verify the peer: the UI accepts only the Elevator process it launched
  (GetNamedPipeClientProcessId); the Elevator serves only CommandForge.Wpf.exe from its own folder
  (GetNamedPipeServerProcessId) and connects with Identification impersonation.
- The Elevator writes no files any more (an elevated append to the user-writable profile could be
  redirected with a junction); diagnostics go back to the UI's log as pipe Log messages.
- StartupHookSupport=false on the Elevator: DOTNET_STARTUP_HOOKS is ignored.
- Broker: cancelling while UAC/connecting returns Cancelled instead of throwing; a command that
  ignores a cancel for 10 s ends the session; pipe/JSON errors fail the run cleanly; the output
  channel is always completed.
- IElevationService: IsSessionActive and EndSessionAsync (needed before an update replaces the
  files the elevated process runs from) replace the unused IsElevationAvailable.

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

39 lines
1.6 KiB
C#

// CommandForge.Elevator — privileged broker helper (runs elevated, requireAdministrator).
//
// Connects to the UI's named pipe and serves ONLY admin commands from its OWN embedded catalog
// (golden rules #1/#2). It never builds a command line from pipe input — it accepts a commandId,
// looks it up, validates requiresAdmin, and runs it. It writes no files: diagnostics go back to
// the UI over the pipe (a file in the user-writable profile could be redirected by a junction).
using System.IO.Pipes;
using System.Security.Principal;
using CommandForge.Infrastructure.Catalog;
using CommandForge.Infrastructure.Elevation;
using CommandForge.Infrastructure.Execution;
if (args.Length < 1 || string.IsNullOrWhiteSpace(args[0]))
{
return 2;
}
try
{
// Identification level: the (non-elevated) pipe server may learn who we are but cannot act as us.
using var client = new NamedPipeClientStream(".", args[0], PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.Identification);
await client.ConnectAsync(30_000);
if (!PeerVerifier.IsExpectedServer(client))
{
return 3; // someone else owns the pipe — serve nothing
}
// Elevated children get a clean environment (see ChildEnvironment for why).
var executor = new ProcessCommandExecutor(new SystemProcessRunner(ChildEnvironment.Apply));
await new ElevationServer(new JsonCatalogProvider(), executor).RunAsync(client, CancellationToken.None);
return 0;
}
catch (Exception ex) when (ex is IOException or TimeoutException or UnauthorizedAccessException or OperationCanceledException)
{
return 1;
}