-
Notifications
You must be signed in to change notification settings - Fork 863
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
13 changed files
with
897 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/ReverseProxy/Transforms/RequestHeadersAllowedTransform.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Yarp.ReverseProxy.Transforms | ||
{ | ||
/// <summary> | ||
/// Copies only allowed request headers. | ||
/// </summary> | ||
public class RequestHeadersAllowedTransform : RequestTransform | ||
{ | ||
public RequestHeadersAllowedTransform(string[] allowedHeaders) | ||
{ | ||
if (allowedHeaders is null) | ||
{ | ||
throw new ArgumentNullException(nameof(allowedHeaders)); | ||
} | ||
|
||
AllowedHeaders = allowedHeaders; | ||
AllowedHeadersSet = new HashSet<string>(allowedHeaders, StringComparer.OrdinalIgnoreCase); | ||
} | ||
|
||
internal string[] AllowedHeaders { get; } | ||
|
||
private HashSet<string> AllowedHeadersSet { get; } | ||
|
||
/// <inheritdoc/> | ||
public override ValueTask ApplyAsync(RequestTransformContext context) | ||
{ | ||
if (context is null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
Debug.Assert(!context.HeadersCopied); | ||
|
||
foreach (var header in context.HttpContext.Request.Headers) | ||
{ | ||
var headerName = header.Key; | ||
var headerValue = header.Value; | ||
if (!StringValues.IsNullOrEmpty(headerValue) | ||
&& AllowedHeadersSet.Contains(headerName)) | ||
{ | ||
AddHeader(context, headerName, headerValue); | ||
} | ||
} | ||
|
||
context.HeadersCopied = true; | ||
|
||
return default; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/ReverseProxy/Transforms/ResponseHeadersAllowedTransform.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Yarp.ReverseProxy.Transforms | ||
{ | ||
/// <summary> | ||
/// Copies only allowed response headers. | ||
/// </summary> | ||
public class ResponseHeadersAllowedTransform : ResponseTransform | ||
{ | ||
public ResponseHeadersAllowedTransform(string[] allowedHeaders) | ||
{ | ||
if (allowedHeaders is null) | ||
{ | ||
throw new ArgumentNullException(nameof(allowedHeaders)); | ||
} | ||
|
||
AllowedHeaders = allowedHeaders; | ||
AllowedHeadersSet = new HashSet<string>(allowedHeaders, StringComparer.OrdinalIgnoreCase); | ||
} | ||
|
||
internal string[] AllowedHeaders { get; } | ||
|
||
private HashSet<string> AllowedHeadersSet { get; } | ||
|
||
/// <inheritdoc/> | ||
public override ValueTask ApplyAsync(ResponseTransformContext context) | ||
{ | ||
if (context is null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
Debug.Assert(!context.HeadersCopied); | ||
|
||
// See https://github.com/microsoft/reverse-proxy/blob/51d797986b1fea03500a1ad173d13a1176fb5552/src/ReverseProxy/Forwarder/HttpTransformer.cs#L67-L77 | ||
var responseHeaders = context.HttpContext.Response.Headers; | ||
CopyResponseHeaders(context.ProxyResponse.Headers, responseHeaders); | ||
if (context.ProxyResponse.Content != null) | ||
{ | ||
CopyResponseHeaders(context.ProxyResponse.Content.Headers, responseHeaders); | ||
} | ||
|
||
context.HeadersCopied = true; | ||
|
||
return default; | ||
} | ||
|
||
// See https://github.com/microsoft/reverse-proxy/blob/51d797986b1fea03500a1ad173d13a1176fb5552/src/ReverseProxy/Forwarder/HttpTransformer.cs#L102-L115 | ||
private void CopyResponseHeaders(HttpHeaders source, IHeaderDictionary destination) | ||
{ | ||
foreach (var header in source) | ||
{ | ||
var headerName = header.Key; | ||
if (AllowedHeadersSet.Contains(headerName)) | ||
{ | ||
Debug.Assert(header.Value is string[]); | ||
destination.Append(headerName, header.Value as string[] ?? header.Value.ToArray()); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.