-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add command builder with escaped user input #5982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AmelBawa-msft
merged 9 commits into
microsoft:master
from
AmelBawa-msft:user/amelbawa/pwsh-fix
Jan 23, 2026
+429
−27
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0d9a477
Add command builder with escaped input
AmelBawa-msft 6277d1b
CE
AmelBawa-msft 0539c6d
Init UT
AmelBawa-msft 94da643
CE
AmelBawa-msft a7b77e6
Update expect.txt
AmelBawa-msft 60cc0ab
Fix warning msgs
AmelBawa-msft 62b3a61
Addressed comments
AmelBawa-msft d8ef06b
Include UT in pipeline
AmelBawa-msft 5b0d883
Update pipeline
AmelBawa-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/WinGetCLICommandBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // ----------------------------------------------------------------------------- | ||
| // <copyright file="WinGetCLICommandBuilder.cs" company="Microsoft Corporation"> | ||
| // Copyright (c) Microsoft Corporation. Licensed under the MIT License. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| namespace Microsoft.WinGet.Client.Engine.Helpers | ||
| { | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| /// <summary> | ||
| /// Represents a builder for WinGet CLI commands. | ||
| /// </summary> | ||
| public class WinGetCLICommandBuilder | ||
| { | ||
| private readonly List<string> commands; | ||
| private readonly List<string> parameters; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="WinGetCLICommandBuilder"/> class. | ||
| /// </summary> | ||
| /// <param name="commands">The commands to initialize the builder with.</param> | ||
| public WinGetCLICommandBuilder(params string[] commands) | ||
| { | ||
| this.commands = new (commands); | ||
| this.parameters = new (); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the main command (e.g. [empty], settings, source). | ||
| /// </summary> | ||
| public string Command => string.Join(" ", this.commands); | ||
|
|
||
| /// <summary> | ||
| /// Gets the constructed parameters string. | ||
| /// </summary> | ||
| public string Parameters => string.Join(" ", this.parameters); | ||
|
|
||
| /// <summary> | ||
| /// Appends a switch to the command. | ||
| /// </summary> | ||
| /// <param name="switchName">The name of the switch to append.</param> | ||
| /// <returns>The current instance of <see cref="WinGetCLICommandBuilder"/>.</returns> | ||
| public WinGetCLICommandBuilder AppendSwitch(string switchName) | ||
| { | ||
| this.parameters.Add($"--{switchName}"); | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Appends a sub-command to the command. | ||
| /// </summary> | ||
| /// <param name="subCommand">The sub-command to append.</param> | ||
| /// <returns>The current instance of <see cref="WinGetCLICommandBuilder"/>.</returns> | ||
| public WinGetCLICommandBuilder AppendSubCommand(string subCommand) | ||
| { | ||
| this.commands.Add(subCommand); | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Appends an option with its value to the command. | ||
| /// </summary> | ||
| /// <param name="option">The name of the option to append.</param> | ||
| /// <param name="value">The value of the option to append.</param> | ||
| /// <returns>The current instance of <see cref="WinGetCLICommandBuilder"/>.</returns> | ||
| public WinGetCLICommandBuilder AppendOption(string option, string? value) | ||
| { | ||
| if (value == null) | ||
| { | ||
| return this; | ||
| } | ||
|
|
||
| this.parameters.Add($"--{option} {this.Escape(value)}"); | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts the command builder to its string representation. | ||
| /// </summary> | ||
| /// <returns>The string representation of the command.</returns> | ||
| public override string ToString() | ||
| { | ||
| var parametersString = this.Parameters; | ||
| var commandString = this.Command; | ||
| if (string.IsNullOrEmpty(commandString)) | ||
| { | ||
| return parametersString; | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(parametersString)) | ||
| { | ||
| return commandString; | ||
| } | ||
|
|
||
| return $"{commandString} {parametersString}"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Escapes a command-line argument according to Windows command-line parsing rules. | ||
| /// References: | ||
| /// - https://devblogs.microsoft.com/oldnewthing/20100917-00/?p=12833 | ||
| /// - https://learn.microsoft.com/en-us/cpp/c-language/parsing-c-command-line-arguments. | ||
| /// </summary> | ||
| /// <param name="arg">The argument to escape.</param> | ||
| /// <returns>The escaped argument.</returns> | ||
| private string Escape(string arg) | ||
AmelBawa-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (string.IsNullOrEmpty(arg)) | ||
| { | ||
| return "\"\""; | ||
| } | ||
|
|
||
| var sb = new StringBuilder(arg.Length + 2); | ||
| sb.Append('"'); | ||
|
|
||
| int bs = 0; | ||
| foreach (char c in arg) | ||
| { | ||
| if (c == '\\') | ||
| { | ||
| bs++; | ||
| } | ||
| else if (c == '"') | ||
| { | ||
| sb.Append('\\', (bs * 2) + 1); | ||
| sb.Append('"'); | ||
| bs = 0; | ||
| } | ||
| else | ||
| { | ||
| sb.Append('\\', bs); | ||
| sb.Append(c); | ||
| bs = 0; | ||
| } | ||
| } | ||
|
|
||
| if (bs > 0) | ||
| { | ||
| sb.Append('\\', bs * 2); | ||
| } | ||
|
|
||
| sb.Append('"'); | ||
| return sb.ToString(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.