- 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>
107 lines
3.5 KiB
C#
107 lines
3.5 KiB
C#
using System.Threading.Channels;
|
|
using CommandForge.Application.Ports;
|
|
using CommandForge.Application.UseCases;
|
|
using CommandForge.Application.UserCommands;
|
|
using CommandForge.Domain;
|
|
using CommandForge.Infrastructure.UserCommands;
|
|
|
|
namespace CommandForge.Tests;
|
|
|
|
/// <summary>
|
|
/// Safety tests for user-defined commands (golden rule #1): they must never elevate, carry the
|
|
/// namespaced id (so the Elevator rejects them), and persist locally.
|
|
/// </summary>
|
|
public sealed class UserCommandsTests
|
|
{
|
|
private static UserCommand Sample(string id = "abc") => new()
|
|
{
|
|
Id = id,
|
|
Name = "My tool",
|
|
Executable = "cmd",
|
|
Arguments = "/c echo hi",
|
|
};
|
|
|
|
[Fact]
|
|
public void ToDefinition_NeverRequiresAdmin()
|
|
=> Assert.False(UserCommandFactory.ToDefinition(Sample()).RequiresAdmin);
|
|
|
|
[Fact]
|
|
public void ToDefinition_NamespacesId_SoElevatorRejectsIt()
|
|
{
|
|
var def = UserCommandFactory.ToDefinition(Sample("abc"));
|
|
|
|
Assert.StartsWith(UserCommandFactory.IdPrefix, def.Id, StringComparison.Ordinal);
|
|
Assert.Equal("user:abc", def.Id);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UserCommand_RoutesToExecutor_NotElevation()
|
|
{
|
|
var executor = new RecordingExecutor();
|
|
var elevation = new RecordingElevation();
|
|
var useCase = new RunCommandUseCase(executor, elevation);
|
|
|
|
await useCase.RunAsync(
|
|
UserCommandFactory.ToDefinition(Sample()),
|
|
Channel.CreateUnbounded<OutputLine>().Writer,
|
|
TestContext.Current.CancellationToken);
|
|
|
|
Assert.True(executor.Called);
|
|
Assert.False(elevation.Called); // never elevated
|
|
}
|
|
|
|
[Fact]
|
|
public void Store_Save_Get_Delete_Persists()
|
|
{
|
|
var path = Path.Combine(Path.GetTempPath(), $"cf-usercmd-{Guid.NewGuid():N}.json");
|
|
try
|
|
{
|
|
var store = new JsonUserCommandStore(path);
|
|
store.Save(Sample("a"));
|
|
store.Save(Sample("b") with { Name = "Second" });
|
|
|
|
Assert.Equal(2, store.GetAll().Count);
|
|
|
|
var reloaded = new JsonUserCommandStore(path);
|
|
Assert.Equal(2, reloaded.GetAll().Count);
|
|
Assert.Equal("cmd", reloaded.GetAll()[0].Executable);
|
|
|
|
reloaded.Delete("a");
|
|
Assert.Single(reloaded.GetAll());
|
|
Assert.DoesNotContain(new JsonUserCommandStore(path).GetAll(), c => c.Id == "a");
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(path);
|
|
}
|
|
}
|
|
|
|
private sealed class RecordingExecutor : 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(new ExecutionResult { ExitCode = 0, Success = true, Duration = TimeSpan.Zero });
|
|
}
|
|
}
|
|
|
|
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(new ExecutionResult { ExitCode = 0, Success = true, Duration = TimeSpan.Zero });
|
|
}
|
|
}
|
|
}
|