Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de3ed23687 | ||
|
|
68e62f3f22 |
No files matched your search
@@ -28,11 +28,42 @@ public static class UserCommandFactory
|
||||
TitleKey = command.Name,
|
||||
DescriptionKey = command.Name,
|
||||
Executable = command.Executable,
|
||||
ArgsTemplate = command.Arguments,
|
||||
ArgsTemplate = NormalizeArguments(command.Executable, command.Arguments),
|
||||
ExecutionMode = ExecutionMode.Capture,
|
||||
RequiresAdmin = false, // INVARIANT — a user command must never elevate.
|
||||
DangerLevel = DangerLevel.Caution,
|
||||
Restart = RestartPolicy.No,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes arguments so a one-shot command actually runs and then exits. <c>cmd.exe</c>
|
||||
/// without <c>/c</c> (or <c>/k</c>) opens an interactive shell that blocks forever waiting on
|
||||
/// stdin, so for cmd we prepend <c>/c</c>. Other executables (e.g. powershell) are left as-is.
|
||||
/// </summary>
|
||||
public static string NormalizeArguments(string executable, string? arguments)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(executable);
|
||||
|
||||
var args = arguments ?? string.Empty;
|
||||
if (IsCmd(executable) && !HasCmdRunSwitch(args))
|
||||
{
|
||||
return args.Length == 0 ? "/c" : "/c " + args;
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
private static bool IsCmd(string executable)
|
||||
=> string.Equals(Path.GetFileNameWithoutExtension(executable.Trim()), "cmd", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
private static bool HasCmdRunSwitch(string arguments)
|
||||
{
|
||||
var trimmed = arguments.TrimStart();
|
||||
return StartsWithSwitch(trimmed, "/c") || StartsWithSwitch(trimmed, "/k");
|
||||
}
|
||||
|
||||
private static bool StartsWithSwitch(string text, string flag)
|
||||
=> text.StartsWith(flag, StringComparison.OrdinalIgnoreCase)
|
||||
&& (text.Length == flag.Length || char.IsWhiteSpace(text[flag.Length]));
|
||||
}
|
||||
@@ -25,6 +25,9 @@ internal sealed class SystemProcessRunner : IProcessRunner
|
||||
Arguments = arguments,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
// Redirect stdin so we can close it immediately: a process that reads input (e.g. an
|
||||
// interactive "cmd" with no /c) gets EOF and exits instead of hanging forever.
|
||||
RedirectStandardInput = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
// Pin the working directory to System32 so the CreateProcess search order can't
|
||||
@@ -52,6 +55,11 @@ internal sealed class SystemProcessRunner : IProcessRunner
|
||||
try
|
||||
{
|
||||
process.Start();
|
||||
|
||||
// Signal EOF on the child's stdin right away — nothing here ever feeds it input, and
|
||||
// leaving it open lets an interactive process block indefinitely.
|
||||
process.StandardInput.Close();
|
||||
|
||||
process.BeginOutputReadLine();
|
||||
process.BeginErrorReadLine();
|
||||
|
||||
|
||||
@@ -135,9 +135,10 @@ public sealed partial class RecipesViewModel : ObservableObject
|
||||
[RelayCommand]
|
||||
private void AddStep()
|
||||
{
|
||||
if (CommandToAdd is not null)
|
||||
// Avoid duplicate steps — a recipe runs each command once (don't run one command N times).
|
||||
if (CommandToAdd is { } command && !Steps.Contains(command))
|
||||
{
|
||||
Steps.Add(CommandToAdd);
|
||||
Steps.Add(command);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -155,10 +155,12 @@ public sealed partial class UserCommandsViewModel : ObservableObject
|
||||
Arguments = EditArguments.Trim(),
|
||||
};
|
||||
|
||||
// Transparency (Security.md §3): show the exact command line, and that it runs without admin.
|
||||
var commandLine = string.IsNullOrEmpty(userCommand.Arguments)
|
||||
// Transparency (Security.md §3): show the exact command line that will run (including the
|
||||
// cmd /c normalization applied by ToDefinition) and that it runs without admin.
|
||||
var effectiveArgs = UserCommandFactory.NormalizeArguments(executable, userCommand.Arguments);
|
||||
var commandLine = string.IsNullOrEmpty(effectiveArgs)
|
||||
? executable
|
||||
: executable + " " + userCommand.Arguments;
|
||||
: executable + " " + effectiveArgs;
|
||||
if (MessageBox.Show(
|
||||
string.Format(CultureInfo.CurrentCulture, Strings.Get("UserCmd_RunConfirm"), commandLine),
|
||||
Strings.Get("UserCmd_RunTitle"),
|
||||
|
||||
@@ -231,7 +231,8 @@
|
||||
<ListBox ItemsSource="{Binding Categories}" SelectedItem="{Binding SelectedCategory}"
|
||||
Background="Transparent" BorderThickness="0" Padding="4,4"
|
||||
HorizontalContentAlignment="Stretch"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
|
||||
ScrollViewer.VerticalScrollBarVisibility="Hidden">
|
||||
<ListBox.ItemContainerStyle>
|
||||
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
|
||||
<Setter Property="Padding" Value="12,8" />
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using CommandForge.Application.UserCommands;
|
||||
using CommandForge.Domain;
|
||||
|
||||
namespace CommandForge.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Tests the cmd argument normalization that fixes user commands hanging on an interactive
|
||||
/// cmd.exe (e.g. "cmd" + "ping google.com" must run as "cmd /c ping google.com").
|
||||
/// </summary>
|
||||
public sealed class UserCommandNormalizationTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("cmd", "ping google.com", "/c ping google.com")]
|
||||
[InlineData("cmd.exe", "dir", "/c dir")]
|
||||
[InlineData("CMD", "echo hi", "/c echo hi")]
|
||||
[InlineData("C:\\Windows\\System32\\cmd.exe", "whoami", "/c whoami")]
|
||||
[InlineData("cmd", "", "/c")]
|
||||
public void NormalizeArguments_PrependsSlashC_ForCmd(string exe, string args, string expected)
|
||||
=> Assert.Equal(expected, UserCommandFactory.NormalizeArguments(exe, args));
|
||||
|
||||
[Theory]
|
||||
[InlineData("cmd", "/c ping x")] // already has /c
|
||||
[InlineData("cmd", "/k keep open")] // explicit /k
|
||||
[InlineData("cmd", "/C UPPER")] // case-insensitive switch
|
||||
[InlineData("powershell", "ping x")] // not cmd — leave as-is (powershell runs it)
|
||||
[InlineData("ping", "google.com")] // direct executable
|
||||
public void NormalizeArguments_LeavesAsIs_WhenNotPlainCmd(string exe, string args)
|
||||
=> Assert.Equal(args, UserCommandFactory.NormalizeArguments(exe, args));
|
||||
|
||||
[Fact]
|
||||
public void ToDefinition_AppliesCmdNormalization()
|
||||
{
|
||||
var def = UserCommandFactory.ToDefinition(new UserCommand
|
||||
{
|
||||
Id = "x",
|
||||
Name = "n",
|
||||
Executable = "cmd",
|
||||
Arguments = "ping google.com",
|
||||
});
|
||||
|
||||
Assert.Equal("/c ping google.com", def.ArgsTemplate);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user