|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
| 3 | +#nullable enable |
3 | 4 |
|
4 | 5 | using System; |
5 | 6 | using System.Collections.Generic; |
6 | 7 | using System.Text; |
7 | 8 | using System.Linq; |
8 | 9 |
|
9 | | -namespace Azure.Core |
| 10 | +namespace Azure.Core; |
| 11 | + |
| 12 | +internal class HttpMessageSanitizer |
10 | 13 | { |
11 | | - internal class HttpMessageSanitizer |
12 | | - { |
13 | | - private const string LogAllValue = "*"; |
14 | | - private readonly bool _logAllHeaders; |
15 | | - private readonly bool _logFullQueries; |
16 | | - private readonly string[] _allowedQueryParameters; |
17 | | - private readonly string _redactedPlaceholder; |
18 | | - private readonly HashSet<string> _allowedHeaders; |
| 14 | + private const string LogAllValue = "*"; |
| 15 | + private readonly bool _logAllHeaders; |
| 16 | + private readonly bool _logFullQueries; |
| 17 | + private readonly string[] _allowedQueryParameters; |
| 18 | + private readonly string _redactedPlaceholder; |
| 19 | + private readonly HashSet<string> _allowedHeaders; |
19 | 20 |
|
20 | | - internal static HttpMessageSanitizer Default = new HttpMessageSanitizer(Array.Empty<string>(), Array.Empty<string>()); |
| 21 | + [ThreadStatic] |
| 22 | + private static StringBuilder? s_cachedStringBuilder; |
| 23 | + private const int MaxCachedStringBuilderCapacity = 1024; |
21 | 24 |
|
22 | | - public HttpMessageSanitizer(string[] allowedQueryParameters, string[] allowedHeaders, string redactedPlaceholder = "REDACTED") |
23 | | - { |
24 | | - _logAllHeaders = allowedHeaders.Contains(LogAllValue); |
25 | | - _logFullQueries = allowedQueryParameters.Contains(LogAllValue); |
| 25 | + internal static HttpMessageSanitizer Default = new HttpMessageSanitizer(Array.Empty<string>(), Array.Empty<string>()); |
26 | 26 |
|
27 | | - _allowedQueryParameters = allowedQueryParameters; |
28 | | - _redactedPlaceholder = redactedPlaceholder; |
29 | | - _allowedHeaders = new HashSet<string>(allowedHeaders, StringComparer.InvariantCultureIgnoreCase); |
30 | | - } |
| 27 | + public HttpMessageSanitizer(string[] allowedQueryParameters, string[] allowedHeaders, string redactedPlaceholder = "REDACTED") |
| 28 | + { |
| 29 | + _logAllHeaders = allowedHeaders.Contains(LogAllValue); |
| 30 | + _logFullQueries = allowedQueryParameters.Contains(LogAllValue); |
31 | 31 |
|
32 | | - public string SanitizeHeader(string name, string value) |
33 | | - { |
34 | | - if (_logAllHeaders || _allowedHeaders.Contains(name)) |
35 | | - { |
36 | | - return value; |
37 | | - } |
| 32 | + _allowedQueryParameters = allowedQueryParameters; |
| 33 | + _redactedPlaceholder = redactedPlaceholder; |
| 34 | + _allowedHeaders = new HashSet<string>(allowedHeaders, StringComparer.InvariantCultureIgnoreCase); |
| 35 | + } |
38 | 36 |
|
39 | | - return _redactedPlaceholder; |
| 37 | + public string SanitizeHeader(string name, string value) |
| 38 | + { |
| 39 | + if (_logAllHeaders || _allowedHeaders.Contains(name)) |
| 40 | + { |
| 41 | + return value; |
40 | 42 | } |
41 | 43 |
|
42 | | - public string SanitizeUrl(string url) |
| 44 | + return _redactedPlaceholder; |
| 45 | + } |
| 46 | + |
| 47 | + public string SanitizeUrl(string url) |
| 48 | + { |
| 49 | + if (_logFullQueries) |
43 | 50 | { |
44 | | - if (_logFullQueries) |
45 | | - { |
46 | | - return url; |
47 | | - } |
| 51 | + return url; |
| 52 | + } |
48 | 53 |
|
49 | 54 | #if NET5_0_OR_GREATER |
50 | | - int indexOfQuerySeparator = url.IndexOf('?', StringComparison.Ordinal); |
| 55 | + int indexOfQuerySeparator = url.IndexOf('?', StringComparison.Ordinal); |
51 | 56 | #else |
52 | | - int indexOfQuerySeparator = url.IndexOf('?'); |
| 57 | + int indexOfQuerySeparator = url.IndexOf('?'); |
53 | 58 | #endif |
54 | 59 |
|
55 | | - if (indexOfQuerySeparator == -1) |
| 60 | + if (indexOfQuerySeparator == -1) |
| 61 | + { |
| 62 | + return url; |
| 63 | + } |
| 64 | + |
| 65 | + // PERF: Avoid allocations in this heavily-used method: |
| 66 | + // 1. Use ReadOnlySpan<char> to avoid creating substrings. |
| 67 | + // 2. Defer creating a StringBuilder until absolutely necessary. |
| 68 | + // 3. Use a rented StringBuilder to avoid allocating a new one |
| 69 | + // each time. |
| 70 | + |
| 71 | + // Create the StringBuilder only when necessary (when we encounter |
| 72 | + // a query parameter that needs to be redacted) |
| 73 | + StringBuilder? stringBuilder = null; |
| 74 | + |
| 75 | + // Keeps track of the number of characters we've processed so far |
| 76 | + // so that, if we need to create a StringBuilder, we know how many |
| 77 | + // characters to copy over from the original URL. |
| 78 | + int lengthSoFar = indexOfQuerySeparator + 1; |
| 79 | + |
| 80 | + ReadOnlySpan<char> query = url.AsSpan(indexOfQuerySeparator + 1); // +1 to skip the '?' |
| 81 | + |
| 82 | + while (query.Length > 0) |
| 83 | + { |
| 84 | + int endOfParameterValue = query.IndexOf('&'); |
| 85 | + int endOfParameterName = query.IndexOf('='); |
| 86 | + bool noValue = false; |
| 87 | + |
| 88 | + // Check if we have parameter without value |
| 89 | + if ((endOfParameterValue == -1 && endOfParameterName == -1) || |
| 90 | + (endOfParameterValue != -1 && (endOfParameterName == -1 || endOfParameterName > endOfParameterValue))) |
56 | 91 | { |
57 | | - return url; |
| 92 | + endOfParameterName = endOfParameterValue; |
| 93 | + noValue = true; |
58 | 94 | } |
59 | 95 |
|
60 | | - StringBuilder stringBuilder = new StringBuilder(url.Length); |
61 | | - stringBuilder.Append(url, 0, indexOfQuerySeparator); |
| 96 | + if (endOfParameterName == -1) |
| 97 | + { |
| 98 | + endOfParameterName = query.Length; |
| 99 | + } |
62 | 100 |
|
63 | | - string query = url.Substring(indexOfQuerySeparator); |
| 101 | + if (endOfParameterValue == -1) |
| 102 | + { |
| 103 | + endOfParameterValue = query.Length; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + // include the separator |
| 108 | + endOfParameterValue++; |
| 109 | + } |
64 | 110 |
|
65 | | - int queryIndex = 1; |
66 | | - stringBuilder.Append('?'); |
| 111 | + ReadOnlySpan<char> parameterName = query.Slice(0, endOfParameterName); |
67 | 112 |
|
68 | | - do |
| 113 | + bool isAllowed = false; |
| 114 | + foreach (string name in _allowedQueryParameters) |
69 | 115 | { |
70 | | - int endOfParameterValue = query.IndexOf('&', queryIndex); |
71 | | - int endOfParameterName = query.IndexOf('=', queryIndex); |
72 | | - bool noValue = false; |
73 | | - |
74 | | - // Check if we have parameter without value |
75 | | - if ((endOfParameterValue == -1 && endOfParameterName == -1) || |
76 | | - (endOfParameterValue != -1 && (endOfParameterName == -1 || endOfParameterName > endOfParameterValue))) |
| 116 | + if (parameterName.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase)) |
77 | 117 | { |
78 | | - endOfParameterName = endOfParameterValue; |
79 | | - noValue = true; |
| 118 | + isAllowed = true; |
| 119 | + break; |
80 | 120 | } |
| 121 | + } |
81 | 122 |
|
82 | | - if (endOfParameterName == -1) |
83 | | - { |
84 | | - endOfParameterName = query.Length; |
85 | | - } |
| 123 | + int valueLength = endOfParameterValue; |
| 124 | + int nameLength = endOfParameterName; |
86 | 125 |
|
87 | | - if (endOfParameterValue == -1) |
| 126 | + if (isAllowed || noValue) |
| 127 | + { |
| 128 | + if (stringBuilder is null) |
88 | 129 | { |
89 | | - endOfParameterValue = query.Length; |
| 130 | + lengthSoFar += valueLength; |
90 | 131 | } |
91 | 132 | else |
92 | 133 | { |
93 | | - // include the separator |
94 | | - endOfParameterValue++; |
| 134 | + AppendReadOnlySpan(stringBuilder, query.Slice(0, valueLength)); |
95 | 135 | } |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + // Encountered a query value that needs to be redacted. |
| 140 | + // Create the StringBuilder if we haven't already. |
| 141 | + stringBuilder ??= RentStringBuilder(url.Length).Append(url, 0, lengthSoFar); |
96 | 142 |
|
97 | | - ReadOnlySpan<char> parameterName = query.AsSpan(queryIndex, endOfParameterName - queryIndex); |
| 143 | + AppendReadOnlySpan(stringBuilder, query.Slice(0, nameLength)) |
| 144 | + .Append('=') |
| 145 | + .Append(_redactedPlaceholder); |
98 | 146 |
|
99 | | - bool isAllowed = false; |
100 | | - foreach (string name in _allowedQueryParameters) |
| 147 | + if (query[endOfParameterValue - 1] == '&') |
101 | 148 | { |
102 | | - if (parameterName.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase)) |
103 | | - { |
104 | | - isAllowed = true; |
105 | | - break; |
106 | | - } |
| 149 | + stringBuilder.Append('&'); |
107 | 150 | } |
| 151 | + } |
| 152 | + |
| 153 | + query = query.Slice(valueLength); |
| 154 | + } |
108 | 155 |
|
109 | | - int valueLength = endOfParameterValue - queryIndex; |
110 | | - int nameLength = endOfParameterName - queryIndex; |
| 156 | + return stringBuilder is null ? url : ToStringAndReturnStringBuilder(stringBuilder); |
111 | 157 |
|
112 | | - if (isAllowed) |
113 | | - { |
114 | | - stringBuilder.Append(query, queryIndex, valueLength); |
115 | | - } |
116 | | - else |
117 | | - { |
118 | | - if (noValue) |
119 | | - { |
120 | | - stringBuilder.Append(query, queryIndex, valueLength); |
121 | | - } |
122 | | - else |
123 | | - { |
124 | | - stringBuilder.Append(query, queryIndex, nameLength); |
125 | | - stringBuilder.Append('='); |
126 | | - stringBuilder.Append(_redactedPlaceholder); |
127 | | - if (query[endOfParameterValue - 1] == '&') |
128 | | - { |
129 | | - stringBuilder.Append('&'); |
130 | | - } |
131 | | - } |
132 | | - } |
| 158 | + static StringBuilder AppendReadOnlySpan(StringBuilder builder, ReadOnlySpan<char> span) |
| 159 | + { |
| 160 | +#if NET6_0_OR_GREATER |
| 161 | + return builder.Append(span); |
| 162 | +#else |
| 163 | + foreach (char c in span) |
| 164 | + { |
| 165 | + builder.Append(c); |
| 166 | + } |
| 167 | + |
| 168 | + return builder; |
| 169 | +#endif |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private static StringBuilder RentStringBuilder(int capacity) |
| 174 | + { |
| 175 | + if (capacity <= MaxCachedStringBuilderCapacity) |
| 176 | + { |
| 177 | + StringBuilder? builder = s_cachedStringBuilder; |
| 178 | + if (builder is not null && builder.Capacity >= capacity) |
| 179 | + { |
| 180 | + s_cachedStringBuilder = null; |
| 181 | + return builder; |
| 182 | + } |
| 183 | + } |
133 | 184 |
|
134 | | - queryIndex += valueLength; |
135 | | - } while (queryIndex < query.Length); |
| 185 | + return new StringBuilder(capacity); |
| 186 | + } |
136 | 187 |
|
137 | | - return stringBuilder.ToString(); |
| 188 | + private static string ToStringAndReturnStringBuilder(StringBuilder builder) |
| 189 | + { |
| 190 | + string result = builder.ToString(); |
| 191 | + if (builder.Capacity <= MaxCachedStringBuilderCapacity) |
| 192 | + { |
| 193 | + s_cachedStringBuilder = builder.Clear(); |
138 | 194 | } |
| 195 | + |
| 196 | + return result; |
139 | 197 | } |
140 | 198 | } |
0 commit comments