- 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>
77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
using System.Threading.Channels;
|
|
using CommandForge.Application.Ports;
|
|
using CommandForge.Application.UseCases;
|
|
using CommandForge.Domain;
|
|
|
|
namespace CommandForge.Tests;
|
|
|
|
/// <summary>Tests that commands route to the right executor based on <c>RequiresAdmin</c>.</summary>
|
|
public sealed class RunCommandUseCaseTests
|
|
{
|
|
[Fact]
|
|
public async Task AdminCommand_RoutesToElevation()
|
|
{
|
|
var executor = new Recording();
|
|
var elevation = new RecordingElevation();
|
|
var useCase = new RunCommandUseCase(executor, elevation);
|
|
|
|
await useCase.RunAsync(Command(requiresAdmin: true), Channel.CreateUnbounded<OutputLine>().Writer, TestContext.Current.CancellationToken);
|
|
|
|
Assert.True(elevation.Called);
|
|
Assert.False(executor.Called);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task NonAdminCommand_RoutesToExecutor()
|
|
{
|
|
var executor = new Recording();
|
|
var elevation = new RecordingElevation();
|
|
var useCase = new RunCommandUseCase(executor, elevation);
|
|
|
|
await useCase.RunAsync(Command(requiresAdmin: false), Channel.CreateUnbounded<OutputLine>().Writer, TestContext.Current.CancellationToken);
|
|
|
|
Assert.True(executor.Called);
|
|
Assert.False(elevation.Called);
|
|
}
|
|
|
|
private static CommandDefinition Command(bool requiresAdmin) => new()
|
|
{
|
|
Id = "x",
|
|
CategoryId = "c",
|
|
TitleKey = "t",
|
|
DescriptionKey = "d",
|
|
Executable = "x",
|
|
RequiresAdmin = requiresAdmin,
|
|
};
|
|
|
|
private static ExecutionResult Ok() => new() { ExitCode = 0, Success = true, Duration = TimeSpan.Zero };
|
|
|
|
private sealed class Recording : ICommandExecutor
|
|
{
|
|
public bool Called { get; private set; }
|
|
|
|
public Task<ExecutionResult> ExecuteAsync(CommandDefinition command, ChannelWriter<OutputLine> output, CancellationToken cancellationToken = default)
|
|
{
|
|
Called = true;
|
|
output.TryComplete();
|
|
return Task.FromResult(Ok());
|
|
}
|
|
}
|
|
|
|
private sealed class RecordingElevation : IElevationService
|
|
{
|
|
public bool Called { get; private set; }
|
|
|
|
public bool IsSessionActive => false;
|
|
|
|
public Task EndSessionAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
|
|
|
|
public Task<ExecutionResult> RunElevatedAsync(CommandDefinition command, ChannelWriter<OutputLine> output, CancellationToken cancellationToken = default)
|
|
{
|
|
Called = true;
|
|
output.TryComplete();
|
|
return Task.FromResult(Ok());
|
|
}
|
|
}
|
|
}
|