-
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
Draft
AmelBawa-msft
wants to merge
2
commits into
microsoft:master
Choose a base branch
from
AmelBawa-msft:user/amelbawa/pwsh-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−25
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
157 changes: 157 additions & 0 deletions
157
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,157 @@ | ||
| // ----------------------------------------------------------------------------- | ||
| // <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; | ||
| using System.Linq; | ||
| using System.Text; | ||
|
|
||
| /// <summary> | ||
| /// Represents a builder for WinGet CLI commands. | ||
| /// </summary> | ||
| public class WinGetCLICommandBuilder | ||
| { | ||
| private readonly StringBuilder builder; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="WinGetCLICommandBuilder"/> class. | ||
| /// </summary> | ||
| /// <param name="command">Optional main command (e.g., settings, source).</param> | ||
| public WinGetCLICommandBuilder(string? command = null) | ||
| { | ||
| this.builder = new StringBuilder(); | ||
| this.Command = command; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the main command (e.g., settings, source). | ||
| /// </summary> | ||
| public string? Command { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the constructed parameters string. | ||
| /// </summary> | ||
| public string Parameters => this.builder.ToString(); | ||
|
|
||
| /// <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.AppendToken($"--{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.AppendToken(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) | ||
| { | ||
| this.AppendToken($"--{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() | ||
| { | ||
| if (string.IsNullOrEmpty(this.Command)) | ||
| { | ||
| return this.Parameters; | ||
| } | ||
|
|
||
| return $"{this.Command} {this.Parameters}"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Escapes a command-line argument according to Windows command-line parsing rules. | ||
| /// </summary> | ||
| /// <param name="arg">The argument to escape.</param> | ||
| /// <returns>The escaped argument.</returns> | ||
| private string Escape(string arg) | ||
| { | ||
| if (string.IsNullOrEmpty(arg)) | ||
| { | ||
| return "\"\""; | ||
| } | ||
|
|
||
| // Determine if the argument needs quotes | ||
| // - Contains whitespace, or | ||
| // - Contains double quotes, or | ||
| // - Ends with a backslash | ||
| var needsQuotes = arg.Any(char.IsWhiteSpace) || arg.Contains('"') || arg.EndsWith("\\", StringComparison.Ordinal); | ||
| if (!needsQuotes) | ||
| { | ||
| return arg; | ||
| } | ||
|
|
||
| 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(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Appends a token to the command. | ||
| /// </summary> | ||
| /// <param name="token">The token to append.</param> | ||
| private void AppendToken(string token) | ||
| { | ||
| if (this.builder.Length > 0) | ||
| { | ||
| this.builder.Append(' '); | ||
| } | ||
|
|
||
| this.builder.Append(token); | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn’t find a good spot to add unit tests for this, so I’m thinking of creating a new csproj just for the tests