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