-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update env expand logic * Upd process handler args validation * Remove test ex * Add copyright + cleanup imports * Simplify telemetry type * Simplify PH telemetry tests * Update debug logs * Exclude % from allowed symbols * Fix cmd expand env * remove useless telemetry * Add test traits * Fix env incorrect expanding * Add more test cases * Fix exception condition * Move validation args to public * Update validation logic * Add % to test * Code cleanup * Hide new logic under knob * Move constants to class * move const value back * Add PublishTelemetry overload * Update throw logic * Update invalid args exception * Update private const letter case --------- Co-authored-by: Kirill Ivlev <[email protected]>
- Loading branch information
1 parent
e5dca16
commit 34e2c30
Showing
10 changed files
with
548 additions
and
177 deletions.
There are no files selected for viewing
This file contains 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 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 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,96 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.VisualStudio.Services.Agent.Util; | ||
|
||
namespace Agent.Worker.Handlers.Helpers | ||
{ | ||
public static class CmdArgsSanitizer | ||
{ | ||
private const string _removedSymbolSign = "_#removed#_"; | ||
private const string _argsSplitSymbols = "^^"; | ||
private static readonly Regex _sanitizeRegExp = new("(?<!\\^)([^a-zA-Z0-9\\\\` _''\"\\-=\\/:\\.*,+~?^%])"); | ||
|
||
public static (string, CmdArgsSanitizingTelemetry) SanitizeArguments(string inputArgs) | ||
{ | ||
if (inputArgs == null) | ||
{ | ||
return (null, null); | ||
} | ||
|
||
var argsChunks = inputArgs.Split(_argsSplitSymbols); | ||
var matchesChunks = new List<MatchCollection>(); | ||
|
||
for (int i = 0; i < argsChunks.Length; i++) | ||
{ | ||
var matches = _sanitizeRegExp.Matches(argsChunks[i]); | ||
if (matches.Count > 0) | ||
{ | ||
matchesChunks.Add(matches); | ||
argsChunks[i] = _sanitizeRegExp.Replace(argsChunks[i], _removedSymbolSign); | ||
} | ||
} | ||
|
||
var resultArgs = string.Join(_argsSplitSymbols, argsChunks); | ||
|
||
CmdArgsSanitizingTelemetry telemetry = null; | ||
|
||
if (resultArgs != inputArgs) | ||
{ | ||
var symbolsCount = matchesChunks | ||
.Select(chunk => chunk.Count) | ||
.Aggregate(0, (acc, mc) => acc + mc); | ||
telemetry = new CmdArgsSanitizingTelemetry | ||
( | ||
RemovedSymbols: CmdArgsSanitizingTelemetry.ToSymbolsDictionary(matchesChunks), | ||
RemovedSymbolsCount: symbolsCount | ||
); | ||
} | ||
|
||
return (resultArgs, telemetry); | ||
} | ||
} | ||
|
||
public record CmdArgsSanitizingTelemetry | ||
( | ||
Dictionary<string, int> RemovedSymbols, | ||
int RemovedSymbolsCount | ||
) | ||
{ | ||
public static Dictionary<string, int> ToSymbolsDictionary(List<MatchCollection> matches) | ||
{ | ||
ArgUtil.NotNull(matches, nameof(matches)); | ||
|
||
var symbolsDict = new Dictionary<string, int>(); | ||
foreach (var mc in matches) | ||
{ | ||
foreach (var m in mc.Cast<Match>()) | ||
{ | ||
var symbol = m.Value; | ||
if (symbolsDict.TryGetValue(symbol, out _)) | ||
{ | ||
symbolsDict[symbol] += 1; | ||
} | ||
else | ||
{ | ||
symbolsDict[symbol] = 1; | ||
} | ||
} | ||
} | ||
|
||
return symbolsDict; | ||
} | ||
|
||
public Dictionary<string, object> ToDictionary() | ||
{ | ||
return new() | ||
{ | ||
["removedSymbols"] = RemovedSymbols, | ||
["removedSymbolsCount"] = RemovedSymbolsCount, | ||
}; | ||
} | ||
} | ||
} |
This file contains 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 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.