-
Notifications
You must be signed in to change notification settings - Fork 11k
Expand file tree
/
Copy pathForwardedHeadersMiddleware.cs
More file actions
504 lines (455 loc) · 19.8 KB
/
Copy pathForwardedHeadersMiddleware.cs
File metadata and controls
504 lines (455 loc) · 19.8 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.HttpOverrides;
/// <summary>
/// A middleware for forwarding proxied headers onto the current request.
/// </summary>
public class ForwardedHeadersMiddleware
{
private readonly ForwardedHeadersOptions _options;
private readonly RequestDelegate _next;
private readonly ILogger _logger;
private bool _allowAllHosts;
private IList<StringSegment>? _allowedHosts;
// RFC 3986 scheme = ALPHA * (ALPHA / DIGIT / "+" / "-" / ".")
private static readonly SearchValues<char> SchemeChars =
SearchValues.Create("+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
// Host Matches Http.Sys and Kestrel
// Host Matches RFC 3986 except "*" / "+" / "," / ";" / "=" and "%" HEXDIG HEXDIG which are not allowed by Http.Sys
private static readonly SearchValues<char> HostChars =
SearchValues.Create("!$&'()-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~");
// 0-9 / A-F / a-f / ":" / "."
private static readonly SearchValues<char> Ipv6HostChars =
SearchValues.Create(".0123456789:ABCDEFabcdef");
/// <summary>
/// Create a new <see cref="ForwardedHeadersMiddleware"/>.
/// </summary>
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> used for logging.</param>
/// <param name="options">The <see cref="ForwardedHeadersOptions"/> for configuring the middleware.</param>
public ForwardedHeadersMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions<ForwardedHeadersOptions> options)
{
ArgumentNullException.ThrowIfNull(next);
ArgumentNullException.ThrowIfNull(loggerFactory);
ArgumentNullException.ThrowIfNull(options);
// Make sure required options is not null or whitespace
ArgumentException.ThrowIfNullOrWhiteSpace(options.Value.ForwardedForHeaderName);
ArgumentException.ThrowIfNullOrWhiteSpace(options.Value.ForwardedHostHeaderName);
ArgumentException.ThrowIfNullOrWhiteSpace(options.Value.ForwardedProtoHeaderName);
ArgumentException.ThrowIfNullOrWhiteSpace(options.Value.ForwardedPrefixHeaderName);
ArgumentException.ThrowIfNullOrWhiteSpace(options.Value.OriginalForHeaderName);
ArgumentException.ThrowIfNullOrWhiteSpace(options.Value.OriginalHostHeaderName);
ArgumentException.ThrowIfNullOrWhiteSpace(options.Value.OriginalProtoHeaderName);
ArgumentException.ThrowIfNullOrWhiteSpace(options.Value.OriginalPrefixHeaderName);
_options = options.Value;
_logger = loggerFactory.CreateLogger<ForwardedHeadersMiddleware>();
_next = next;
PreProcessHosts();
}
private void PreProcessHosts()
{
if (_options.AllowedHosts == null || _options.AllowedHosts.Count == 0)
{
_allowAllHosts = true;
return;
}
var allowedHosts = new List<StringSegment>();
foreach (var entry in _options.AllowedHosts)
{
// Punycode. Http.Sys requires you to register Unicode hosts, but the headers contain punycode.
var host = new HostString(entry).ToUriComponent();
if (IsTopLevelWildcard(host))
{
// Disable filtering
_allowAllHosts = true;
return;
}
if (!allowedHosts.Contains(host, StringSegmentComparer.OrdinalIgnoreCase))
{
allowedHosts.Add(host);
}
}
_allowedHosts = allowedHosts;
}
private static bool IsTopLevelWildcard(string host)
{
return (string.Equals("*", host, StringComparison.Ordinal) // HttpSys wildcard
|| string.Equals("[::]", host, StringComparison.Ordinal) // Kestrel wildcard, IPv6 Any
|| string.Equals("0.0.0.0", host, StringComparison.Ordinal)); // IPv4 Any
}
/// <summary>
/// Executes the middleware.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
public Task Invoke(HttpContext context)
{
ApplyForwarders(context);
return _next(context);
}
/// <summary>
/// Forward the proxied headers to the given <see cref="HttpContext"/>.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/>.</param>
public void ApplyForwarders(HttpContext context)
{
// Gather expected headers.
string[]? forwardedFor = null, forwardedProto = null, forwardedHost = null, forwardedPrefix = null;
bool checkFor = false, checkProto = false, checkHost = false, checkPrefix = false;
int entryCount = 0;
var request = context.Request;
var requestHeaders = context.Request.Headers;
if (_options.ForwardedHeaders.HasFlag(ForwardedHeaders.XForwardedFor))
{
checkFor = true;
forwardedFor = requestHeaders.GetCommaSeparatedValues(_options.ForwardedForHeaderName);
entryCount = Math.Max(forwardedFor.Length, entryCount);
}
if (_options.ForwardedHeaders.HasFlag(ForwardedHeaders.XForwardedProto))
{
checkProto = true;
forwardedProto = requestHeaders.GetCommaSeparatedValues(_options.ForwardedProtoHeaderName);
if (_options.RequireHeaderSymmetry && checkFor && forwardedFor!.Length != forwardedProto.Length)
{
_logger.LogWarning(1, "Parameter count mismatch between X-Forwarded-For and X-Forwarded-Proto.");
return;
}
entryCount = Math.Max(forwardedProto.Length, entryCount);
}
if (_options.ForwardedHeaders.HasFlag(ForwardedHeaders.XForwardedHost))
{
checkHost = true;
forwardedHost = requestHeaders.GetCommaSeparatedValues(_options.ForwardedHostHeaderName);
if (_options.RequireHeaderSymmetry
&& ((checkFor && forwardedFor!.Length != forwardedHost.Length)
|| (checkProto && forwardedProto!.Length != forwardedHost.Length)))
{
_logger.LogWarning(1, "Parameter count mismatch between X-Forwarded-Host and X-Forwarded-For or X-Forwarded-Proto.");
return;
}
entryCount = Math.Max(forwardedHost.Length, entryCount);
}
if (_options.ForwardedHeaders.HasFlag(ForwardedHeaders.XForwardedPrefix))
{
checkPrefix = true;
forwardedPrefix = requestHeaders.GetCommaSeparatedValues(_options.ForwardedPrefixHeaderName);
if (_options.RequireHeaderSymmetry
&& ((checkFor && forwardedFor!.Length != forwardedPrefix.Length)
|| (checkProto && forwardedProto!.Length != forwardedPrefix.Length)
|| (checkHost && forwardedHost!.Length != forwardedPrefix.Length)))
{
_logger.LogWarning(1, "Parameter count mismatch between X-Forwarded-Prefix and X-Forwarded-Host and X-Forwarded-For or X-Forwarded-Proto.");
return;
}
entryCount = Math.Max(forwardedPrefix.Length, entryCount);
}
// Apply ForwardLimit, if any
if (_options.ForwardLimit.HasValue && entryCount > _options.ForwardLimit)
{
entryCount = _options.ForwardLimit.Value;
}
// Group the data together.
var sets = new SetOfForwarders[entryCount];
for (int i = 0; i < sets.Length; i++)
{
// They get processed in reverse order, right to left.
var set = new SetOfForwarders();
if (checkFor && i < forwardedFor!.Length)
{
set.IpAndPortText = forwardedFor[forwardedFor.Length - i - 1];
}
if (checkProto && i < forwardedProto!.Length)
{
set.Scheme = forwardedProto[forwardedProto.Length - i - 1];
}
if (checkHost && i < forwardedHost!.Length)
{
set.Host = forwardedHost[forwardedHost.Length - i - 1];
}
if (checkPrefix && i < forwardedPrefix!.Length)
{
set.Prefix = forwardedPrefix[forwardedPrefix.Length - i - 1];
}
sets[i] = set;
}
// Gather initial values
var connection = context.Connection;
var currentValues = new SetOfForwarders()
{
RemoteIpAndPort = connection.RemoteIpAddress != null ? new IPEndPoint(connection.RemoteIpAddress, connection.RemotePort) : null,
// Host and Scheme initial values are never inspected, no need to set them here.
};
var checkKnownIps = _options.KnownIPNetworks.Count > 0 || _options.KnownProxies.Count > 0;
bool applyChanges = false;
int entriesConsumed = 0;
for (; entriesConsumed < sets.Length; entriesConsumed++)
{
var set = sets[entriesConsumed];
if (checkKnownIps)
{
// When trusted-proxy enforcement (KnownProxies/KnownNetworks) is configured, forwarders are
// only applied when the immediate peer can be attested as a known proxy.
if (currentValues.RemoteIpAndPort is null)
{
// A request that arrives without a peer IP (e.g. over a Unix socket or named pipe) cannot
// be attested as a known proxy, so fail closed and stop applying forwarders rather than
// trusting the headers implicitly.
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug(1, "Unknown proxy: no remote IP address available.");
}
break;
}
if (!CheckKnownAddress(currentValues.RemoteIpAndPort.Address))
{
// Stop at the first unknown remote IP, but still apply changes processed so far.
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug(1, "Unknown proxy: {RemoteIpAndPort}", currentValues.RemoteIpAndPort);
}
break;
}
}
if (checkFor)
{
if (IPEndPoint.TryParse(set.IpAndPortText, out var parsedEndPoint))
{
applyChanges = true;
set.RemoteIpAndPort = parsedEndPoint;
currentValues.IpAndPortText = set.IpAndPortText;
currentValues.RemoteIpAndPort = set.RemoteIpAndPort;
}
else if (!string.IsNullOrEmpty(set.IpAndPortText))
{
// Stop at the first unparsable IP, but still apply changes processed so far.
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug(1, "Unparsable IP: {IpAndPortText}", set.IpAndPortText);
}
break;
}
else if (_options.RequireHeaderSymmetry)
{
_logger.LogWarning(2, "Missing forwarded IPAddress.");
return;
}
}
if (checkProto)
{
if (!string.IsNullOrEmpty(set.Scheme) && !set.Scheme.ContainsAnyExcept(SchemeChars))
{
applyChanges = true;
currentValues.Scheme = set.Scheme;
}
else if (_options.RequireHeaderSymmetry)
{
_logger.LogWarning(3, $"Forwarded scheme is not present, this is required by {nameof(_options.RequireHeaderSymmetry)}");
return;
}
}
if (checkHost)
{
if (!string.IsNullOrEmpty(set.Host) && TryValidateHost(set.Host)
&& (_allowAllHosts || HostString.MatchesAny(set.Host, _allowedHosts!)))
{
applyChanges = true;
currentValues.Host = set.Host;
}
else if (_options.RequireHeaderSymmetry)
{
_logger.LogWarning(4, $"Incorrect number of x-forwarded-host header values, see {nameof(_options.RequireHeaderSymmetry)}.");
return;
}
}
if (checkPrefix)
{
if (!string.IsNullOrEmpty(set.Prefix) && set.Prefix[0] == '/')
{
applyChanges = true;
currentValues.Prefix = set.Prefix;
}
else if (_options.RequireHeaderSymmetry)
{
_logger.LogWarning(5, $"Incorrect number of x-forwarded-prefix header values, see {nameof(_options.RequireHeaderSymmetry)}");
return;
}
}
}
if (applyChanges)
{
if (checkFor && currentValues.RemoteIpAndPort != null)
{
if (connection.RemoteIpAddress != null)
{
// Save the original
requestHeaders[_options.OriginalForHeaderName] = new IPEndPoint(connection.RemoteIpAddress, connection.RemotePort).ToString();
}
if (forwardedFor!.Length > entriesConsumed)
{
// Truncate the consumed header values
requestHeaders[_options.ForwardedForHeaderName] =
TruncateConsumedHeaderValues(forwardedFor, entriesConsumed);
}
else
{
// All values were consumed
requestHeaders.Remove(_options.ForwardedForHeaderName);
}
connection.RemoteIpAddress = currentValues.RemoteIpAndPort.Address;
connection.RemotePort = currentValues.RemoteIpAndPort.Port;
}
if (checkProto && currentValues.Scheme != null)
{
// Save the original
requestHeaders[_options.OriginalProtoHeaderName] = request.Scheme;
if (forwardedProto!.Length > entriesConsumed)
{
// Truncate the consumed header values
requestHeaders[_options.ForwardedProtoHeaderName] =
TruncateConsumedHeaderValues(forwardedProto, entriesConsumed);
}
else
{
// All values were consumed
requestHeaders.Remove(_options.ForwardedProtoHeaderName);
}
request.Scheme = currentValues.Scheme;
}
if (checkHost && currentValues.Host != null)
{
// Save the original
requestHeaders[_options.OriginalHostHeaderName] = request.Host.ToString();
if (forwardedHost!.Length > entriesConsumed)
{
// Truncate the consumed header values
requestHeaders[_options.ForwardedHostHeaderName] =
TruncateConsumedHeaderValues(forwardedHost, entriesConsumed);
}
else
{
// All values were consumed
requestHeaders.Remove(_options.ForwardedHostHeaderName);
}
request.Host = HostString.FromUriComponent(currentValues.Host);
}
if (checkPrefix && currentValues.Prefix != null)
{
if (request.PathBase.HasValue)
{
// Save the original
requestHeaders[_options.OriginalPrefixHeaderName] = request.PathBase.ToString();
}
if (forwardedPrefix!.Length > entriesConsumed)
{
// Truncate the consumed header values
requestHeaders[_options.ForwardedPrefixHeaderName] =
TruncateConsumedHeaderValues(forwardedPrefix, entriesConsumed);
}
else
{
// All values were consumed
requestHeaders.Remove(_options.ForwardedPrefixHeaderName);
}
request.PathBase = PathString.FromUriComponent(currentValues.Prefix);
}
}
}
private bool CheckKnownAddress(IPAddress address)
{
if (address.IsIPv4MappedToIPv6)
{
var ipv4Address = address.MapToIPv4();
if (CheckKnownAddress(ipv4Address))
{
return true;
}
}
if (_options.KnownProxies.Contains(address))
{
return true;
}
foreach (var network in _options.KnownIPNetworks)
{
if (network.Contains(address))
{
return true;
}
}
return false;
}
private struct SetOfForwarders
{
public string IpAndPortText;
public IPEndPoint? RemoteIpAndPort;
public string Host;
public string Scheme;
public string Prefix;
}
// Empty was checked for by the caller
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryValidateHost(string host)
{
if (host[0] == '[')
{
return TryValidateIPv6Host(host);
}
if (host[0] == ':')
{
// Only a port
return false;
}
var firstNonHostCharIdx = host.AsSpan().IndexOfAnyExcept(HostChars);
if (firstNonHostCharIdx == -1)
{
// no port
return true;
}
else
{
return TryValidateHostPort(host, firstNonHostCharIdx);
}
}
// The lead '[' was already checked
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryValidateIPv6Host(string hostText)
{
var host = hostText.AsSpan(1);
var hostEndIdx = host.IndexOfAnyExcept(Ipv6HostChars);
if ((uint)hostEndIdx >= (uint)host.Length || // No ']'. The uint cast is there to eliminate the
// bounds check on the 'host[hostEndIdx]' access below.
host[hostEndIdx] != ']' || // We found an invalid host character
hostEndIdx < 3) // [::1] is the shortest valid IPv6 host
{
return false;
}
// If there's nothing left, we're good. If there's more, validate it as a port.
// +2 to skip the '[' and ']' (the '[' wasn't included in hostEndIdx because we
// cut it off in the AsSpan above).
return (hostEndIdx + 2 == hostText.Length) || TryValidateHostPort(hostText, hostEndIdx + 2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryValidateHostPort(string hostText, int offset)
{
if (hostText[offset] != ':' || hostText.Length == offset + 1)
{
// Must have at least one number after the colon if present.
return false;
}
return !hostText.AsSpan(offset + 1).ContainsAnyExceptInRange('0', '9');
}
private static string[] TruncateConsumedHeaderValues(string[] forwarded, int entriesConsumed)
{
var newLength = forwarded.Length - entriesConsumed;
var remaining = new string[newLength];
Array.Copy(forwarded, remaining, newLength);
return remaining;
}
}