Repository navigation
Expand file tree
/
Copy pathCommandLineArgumentsRedactor.cs
More file actions
167 lines (147 loc) · 5.65 KB
/
Copy pathCommandLineArgumentsRedactor.cs
File metadata and controls
167 lines (147 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.Testing.Platform.CommandLine;
internal static class CommandLineArgumentsRedactor
{
private const string RedactedPlaceholder = "***REDACTED***";
public static string Redact(string[] args)
=> string.Join(" ", RedactArguments(args));
public static string RedactArgument(string[] args, int index)
=> RedactArguments(args)[index];
private static string[] RedactArguments(string[] args)
{
if (args.Length == 0)
{
return [];
}
string[] redacted = new string[args.Length];
bool redactNextValue = false;
bool sanitizeNextEndpoint = false;
bool sensitiveValueConsumed = false;
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if (TryParseOption(arg, out string? prefix, out string? name, out string? delimiter, out string? inlineValue))
{
bool isToken = IsTokenOption(name);
bool isEndpoint = IsEndpointOption(name);
if (isToken)
{
redacted[i] = inlineValue is null
? arg
: $"{prefix}{name}{delimiter}{RedactedPlaceholder}";
redactNextValue = true;
sanitizeNextEndpoint = false;
sensitiveValueConsumed = inlineValue is not null;
}
else if (isEndpoint)
{
redacted[i] = inlineValue is null
? arg
: $"{prefix}{name}{delimiter}{SanitizeEndpoint(inlineValue)}";
sanitizeNextEndpoint = true;
redactNextValue = false;
sensitiveValueConsumed = inlineValue is not null;
}
else if (redactNextValue && !sensitiveValueConsumed)
{
redacted[i] = RedactedPlaceholder;
redactNextValue = false;
sanitizeNextEndpoint = false;
sensitiveValueConsumed = false;
}
else if (sanitizeNextEndpoint && !sensitiveValueConsumed)
{
redacted[i] = SanitizeEndpoint(arg);
redactNextValue = false;
sanitizeNextEndpoint = false;
sensitiveValueConsumed = false;
}
else
{
redacted[i] = arg;
redactNextValue = false;
sanitizeNextEndpoint = false;
sensitiveValueConsumed = false;
}
continue;
}
if (redactNextValue)
{
redacted[i] = RedactedPlaceholder;
sensitiveValueConsumed = true;
}
else if (sanitizeNextEndpoint)
{
redacted[i] = SanitizeEndpoint(arg);
sensitiveValueConsumed = true;
}
else
{
redacted[i] = arg;
}
}
return redacted;
}
private static bool TryParseOption(
string arg,
[NotNullWhen(true)] out string? prefix,
[NotNullWhen(true)] out string? name,
out string? delimiter,
out string? inlineValue)
{
prefix = null;
name = null;
delimiter = null;
inlineValue = null;
int optionStart = 0;
while (optionStart < arg.Length && char.IsWhiteSpace(arg[optionStart]))
{
optionStart++;
}
int prefixLength = 0;
while (optionStart + prefixLength < arg.Length && arg[optionStart + prefixLength] == '-')
{
prefixLength++;
}
bool hasLeadingWhitespace = optionStart > 0;
bool isValidPrefix = !hasLeadingWhitespace && prefixLength is 1 or 2;
bool isMalformedPrefix = hasLeadingWhitespace || prefixLength >= 3;
if ((!isValidPrefix && !isMalformedPrefix) || optionStart + prefixLength == arg.Length)
{
return false;
}
prefix = arg[..(optionStart + prefixLength)];
string withoutPrefix = arg[(optionStart + prefixLength)..];
int delimiterIndex = withoutPrefix.IndexOfAny(['=', ':']);
if (delimiterIndex == -1)
{
name = withoutPrefix;
}
else
{
name = withoutPrefix[..delimiterIndex];
delimiter = withoutPrefix[delimiterIndex].ToString();
inlineValue = withoutPrefix[(delimiterIndex + 1)..];
}
if (!isMalformedPrefix || IsTokenOption(name) || IsEndpointOption(name))
{
return true;
}
prefix = null;
name = null;
delimiter = null;
inlineValue = null;
return false;
}
private static bool IsTokenOption(string name)
=> PlatformCommandLineProvider.DotNetTestHttpTokenOptionKey.Equals(name, StringComparison.OrdinalIgnoreCase);
private static bool IsEndpointOption(string name)
=> PlatformCommandLineProvider.DotNetTestHttpEndpointOptionKey.Equals(name, StringComparison.OrdinalIgnoreCase);
private static string SanitizeEndpoint(string endpoint)
=> Uri.TryCreate(endpoint, UriKind.Absolute, out Uri? uri)
&& uri.Scheme is "http" or "https"
&& RoslynString.IsNullOrEmpty(uri.UserInfo)
? uri.GetLeftPart(UriPartial.Authority)
: RedactedPlaceholder;
}