|
| 1 | +using System.Text.Encodings.Web; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Text.Json.Nodes; |
| 4 | +using DebugProbe.AspNetCore.Options; |
| 5 | + |
| 6 | +namespace DebugProbe.AspNetCore.Internal.Utils; |
| 7 | + |
| 8 | +internal static class RedactionUtils |
| 9 | +{ |
| 10 | + public static string RedactHeader(string name, string value, DebugProbeOptions options) |
| 11 | + { |
| 12 | + return IsMatch(name, options.RedactedHeaders) ? options.RedactionText : value; |
| 13 | + } |
| 14 | + |
| 15 | + public static string RedactQueryString(string? queryString, DebugProbeOptions options) |
| 16 | + { |
| 17 | + if (string.IsNullOrEmpty(queryString) || options.RedactedQueryParameters.Length == 0) |
| 18 | + { |
| 19 | + return queryString ?? string.Empty; |
| 20 | + } |
| 21 | + |
| 22 | + var prefix = queryString.StartsWith('?') ? "?" : string.Empty; |
| 23 | + var query = prefix.Length == 0 ? queryString : queryString[1..]; |
| 24 | + |
| 25 | + return prefix + RedactQuery(query, options); |
| 26 | + } |
| 27 | + |
| 28 | + public static string RedactUrl(string? url, DebugProbeOptions options) |
| 29 | + { |
| 30 | + if (string.IsNullOrEmpty(url) || options.RedactedQueryParameters.Length == 0) |
| 31 | + { |
| 32 | + return url ?? string.Empty; |
| 33 | + } |
| 34 | + |
| 35 | + var fragmentIndex = url.IndexOf('#'); |
| 36 | + var fragment = fragmentIndex >= 0 ? url[fragmentIndex..] : string.Empty; |
| 37 | + var withoutFragment = fragmentIndex >= 0 ? url[..fragmentIndex] : url; |
| 38 | + |
| 39 | + var queryIndex = withoutFragment.IndexOf('?'); |
| 40 | + if (queryIndex < 0) |
| 41 | + { |
| 42 | + return url; |
| 43 | + } |
| 44 | + |
| 45 | + var beforeQuery = withoutFragment[..queryIndex]; |
| 46 | + var query = withoutFragment[(queryIndex + 1)..]; |
| 47 | + |
| 48 | + return $"{beforeQuery}?{RedactQuery(query, options)}{fragment}"; |
| 49 | + } |
| 50 | + |
| 51 | + public static string RedactJsonFields(string? body, DebugProbeOptions options) |
| 52 | + { |
| 53 | + if (string.IsNullOrWhiteSpace(body) || options.RedactedJsonFields.Length == 0) |
| 54 | + { |
| 55 | + return body ?? string.Empty; |
| 56 | + } |
| 57 | + |
| 58 | + try |
| 59 | + { |
| 60 | + var node = JsonNode.Parse(body); |
| 61 | + if (node is null) |
| 62 | + { |
| 63 | + return body; |
| 64 | + } |
| 65 | + |
| 66 | + RedactNode(node, options); |
| 67 | + |
| 68 | + return JsonSerializer.Serialize( |
| 69 | + node, |
| 70 | + new JsonSerializerOptions |
| 71 | + { |
| 72 | + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 73 | + }); |
| 74 | + } |
| 75 | + catch |
| 76 | + { |
| 77 | + return body; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static string RedactQuery(string query, DebugProbeOptions options) |
| 82 | + { |
| 83 | + if (query.Length == 0) |
| 84 | + { |
| 85 | + return query; |
| 86 | + } |
| 87 | + |
| 88 | + var parts = query.Split('&'); |
| 89 | + |
| 90 | + for (var i = 0; i < parts.Length; i++) |
| 91 | + { |
| 92 | + var part = parts[i]; |
| 93 | + var equalsIndex = part.IndexOf('='); |
| 94 | + var name = equalsIndex >= 0 ? part[..equalsIndex] : part; |
| 95 | + |
| 96 | + if (!IsMatch(DecodeQueryValue(name), options.RedactedQueryParameters)) |
| 97 | + { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + parts[i] = equalsIndex >= 0? $"{name}={options.RedactionText}" : $"{name}={options.RedactionText}"; |
| 102 | + } |
| 103 | + |
| 104 | + return string.Join("&", parts); |
| 105 | + } |
| 106 | + |
| 107 | + private static void RedactNode(JsonNode node, DebugProbeOptions options) |
| 108 | + { |
| 109 | + if (node is JsonObject jsonObject) |
| 110 | + { |
| 111 | + foreach (var property in jsonObject.ToList()) |
| 112 | + { |
| 113 | + if (IsMatch(property.Key, options.RedactedJsonFields)) |
| 114 | + { |
| 115 | + jsonObject[property.Key] = options.RedactionText; |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + if (property.Value is not null) |
| 120 | + { |
| 121 | + RedactNode(property.Value, options); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + if (node is JsonArray jsonArray) |
| 129 | + { |
| 130 | + foreach (var item in jsonArray) |
| 131 | + { |
| 132 | + if (item is not null) |
| 133 | + { |
| 134 | + RedactNode(item, options); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private static string DecodeQueryValue(string value) |
| 141 | + { |
| 142 | + return Uri.UnescapeDataString(value.Replace("+", " ")); |
| 143 | + } |
| 144 | + |
| 145 | + private static bool IsMatch(string value, string[] candidates) |
| 146 | + { |
| 147 | + return candidates.Any(candidate => |
| 148 | + string.Equals(value, candidate, StringComparison.OrdinalIgnoreCase)); |
| 149 | + } |
| 150 | +} |
0 commit comments