-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathHttpMessageSanitizer.cs
More file actions
198 lines (164 loc) · 6.16 KB
/
HttpMessageSanitizer.cs
File metadata and controls
198 lines (164 loc) · 6.16 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Azure.Core;
internal class HttpMessageSanitizer
{
private const string LogAllValue = "*";
private readonly bool _logAllHeaders;
private readonly bool _logFullQueries;
private readonly string[] _allowedQueryParameters;
private readonly string _redactedPlaceholder;
private readonly HashSet<string> _allowedHeaders;
[ThreadStatic]
private static StringBuilder? s_cachedStringBuilder;
private const int MaxCachedStringBuilderCapacity = 1024;
internal static HttpMessageSanitizer Default = new HttpMessageSanitizer(Array.Empty<string>(), Array.Empty<string>());
public HttpMessageSanitizer(string[] allowedQueryParameters, string[] allowedHeaders, string redactedPlaceholder = "REDACTED")
{
_logAllHeaders = allowedHeaders.Contains(LogAllValue);
_logFullQueries = allowedQueryParameters.Contains(LogAllValue);
_allowedQueryParameters = allowedQueryParameters;
_redactedPlaceholder = redactedPlaceholder;
_allowedHeaders = new HashSet<string>(allowedHeaders, StringComparer.InvariantCultureIgnoreCase);
}
public string SanitizeHeader(string name, string value)
{
if (_logAllHeaders || _allowedHeaders.Contains(name))
{
return value;
}
return _redactedPlaceholder;
}
public string SanitizeUrl(string url)
{
if (_logFullQueries)
{
return url;
}
#if NET5_0_OR_GREATER
int indexOfQuerySeparator = url.IndexOf('?', StringComparison.Ordinal);
#else
int indexOfQuerySeparator = url.IndexOf('?');
#endif
if (indexOfQuerySeparator == -1)
{
return url;
}
// PERF: Avoid allocations in this heavily-used method:
// 1. Use ReadOnlySpan<char> to avoid creating substrings.
// 2. Defer creating a StringBuilder until absolutely necessary.
// 3. Use a rented StringBuilder to avoid allocating a new one
// each time.
// Create the StringBuilder only when necessary (when we encounter
// a query parameter that needs to be redacted)
StringBuilder? stringBuilder = null;
// Keeps track of the number of characters we've processed so far
// so that, if we need to create a StringBuilder, we know how many
// characters to copy over from the original URL.
int lengthSoFar = indexOfQuerySeparator + 1;
ReadOnlySpan<char> query = url.AsSpan(indexOfQuerySeparator + 1); // +1 to skip the '?'
while (query.Length > 0)
{
int endOfParameterValue = query.IndexOf('&');
int endOfParameterName = query.IndexOf('=');
bool noValue = false;
// Check if we have parameter without value
if ((endOfParameterValue == -1 && endOfParameterName == -1) ||
(endOfParameterValue != -1 && (endOfParameterName == -1 || endOfParameterName > endOfParameterValue)))
{
endOfParameterName = endOfParameterValue;
noValue = true;
}
if (endOfParameterName == -1)
{
endOfParameterName = query.Length;
}
if (endOfParameterValue == -1)
{
endOfParameterValue = query.Length;
}
else
{
// include the separator
endOfParameterValue++;
}
ReadOnlySpan<char> parameterName = query.Slice(0, endOfParameterName);
bool isAllowed = false;
foreach (string name in _allowedQueryParameters)
{
if (parameterName.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase))
{
isAllowed = true;
break;
}
}
int valueLength = endOfParameterValue;
int nameLength = endOfParameterName;
if (isAllowed || noValue)
{
if (stringBuilder is null)
{
lengthSoFar += valueLength;
}
else
{
AppendReadOnlySpan(stringBuilder, query.Slice(0, valueLength));
}
}
else
{
// Encountered a query value that needs to be redacted.
// Create the StringBuilder if we haven't already.
stringBuilder ??= RentStringBuilder(url.Length).Append(url, 0, lengthSoFar);
AppendReadOnlySpan(stringBuilder, query.Slice(0, nameLength))
.Append('=')
.Append(_redactedPlaceholder);
if (query[endOfParameterValue - 1] == '&')
{
stringBuilder.Append('&');
}
}
query = query.Slice(valueLength);
}
return stringBuilder is null ? url : ToStringAndReturnStringBuilder(stringBuilder);
static StringBuilder AppendReadOnlySpan(StringBuilder builder, ReadOnlySpan<char> span)
{
#if NET6_0_OR_GREATER
return builder.Append(span);
#else
foreach (char c in span)
{
builder.Append(c);
}
return builder;
#endif
}
}
private static StringBuilder RentStringBuilder(int capacity)
{
if (capacity <= MaxCachedStringBuilderCapacity)
{
StringBuilder? builder = s_cachedStringBuilder;
if (builder is not null && builder.Capacity >= capacity)
{
s_cachedStringBuilder = null;
return builder;
}
}
return new StringBuilder(capacity);
}
private static string ToStringAndReturnStringBuilder(StringBuilder builder)
{
string result = builder.ToString();
if (builder.Capacity <= MaxCachedStringBuilderCapacity)
{
s_cachedStringBuilder = builder.Clear();
}
return result;
}
}