Repository navigation
Expand file tree
/
Copy pathCommandLineArgumentsRedactor.cs
More file actions
113 lines (100 loc) · 3.82 KB
/
Copy pathCommandLineArgumentsRedactor.cs
File metadata and controls
113 lines (100 loc) · 3.82 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
// 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)
{
if (args.Length == 0)
{
return string.Empty;
}
string[] redacted = new string[args.Length];
bool redactNextValue = false;
bool sanitizeNextEndpoint = 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 = PlatformCommandLineProvider.DotNetTestHttpTokenOptionKey.Equals(name, StringComparison.OrdinalIgnoreCase);
bool isEndpoint = PlatformCommandLineProvider.DotNetTestHttpEndpointOptionKey.Equals(name, StringComparison.OrdinalIgnoreCase);
if (isToken)
{
redacted[i] = inlineValue is null
? arg
: $"{prefix}{name}{delimiter}{RedactedPlaceholder}";
redactNextValue = inlineValue is null;
sanitizeNextEndpoint = false;
}
else if (isEndpoint)
{
redacted[i] = inlineValue is null
? arg
: $"{prefix}{name}{delimiter}{SanitizeEndpoint(inlineValue)}";
sanitizeNextEndpoint = inlineValue is null;
redactNextValue = false;
}
else
{
redacted[i] = arg;
redactNextValue = false;
sanitizeNextEndpoint = false;
}
continue;
}
if (redactNextValue)
{
redacted[i] = RedactedPlaceholder;
redactNextValue = false;
}
else if (sanitizeNextEndpoint)
{
redacted[i] = SanitizeEndpoint(arg);
sanitizeNextEndpoint = false;
}
else
{
redacted[i] = arg;
}
}
return string.Join(" ", 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;
bool isDoubleDash = arg.Length > 2 && arg[0] == '-' && arg[1] == '-' && arg[2] != '-';
bool isSingleDash = !isDoubleDash && arg.Length > 1 && arg[0] == '-' && arg[1] != '-';
if (!isDoubleDash && !isSingleDash)
{
return false;
}
prefix = isDoubleDash ? "--" : "-";
string withoutPrefix = arg[prefix.Length..];
int delimiterIndex = withoutPrefix.IndexOfAny(['=', ':']);
if (delimiterIndex == -1)
{
name = withoutPrefix;
return true;
}
name = withoutPrefix[..delimiterIndex];
delimiter = withoutPrefix[delimiterIndex].ToString();
inlineValue = withoutPrefix[(delimiterIndex + 1)..];
return true;
}
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;
}