-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clientruntime/sanitize request header (#28169)
* fix(ClientRuntime): sanitize request headers 1. add `HttpRequestSanitizer` to sanitize headers 2. update `HttpRequestMessageWrapper` to sanitive headers during headers copy 3. add test cases * prepare release notes of 2.3.24
- Loading branch information
Showing
5 changed files
with
167 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
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
65 changes: 65 additions & 0 deletions
65
sdk/mgmtcommon/ClientRuntime/ClientRuntime/Utilities/HttpRequestSanitizer.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,65 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Rest.Utilities | ||
{ | ||
/// <summary> | ||
/// Sanitizer used internall by <see cref="HttpRequestMessageWrapper"/>. | ||
/// </summary> | ||
internal class HttpRequestSanitizer | ||
{ | ||
private readonly static string _redactedPlaceholder = "REDACTED"; | ||
private readonly static HashSet<string> _allowedHeaders = new HashSet<string>(new string[] | ||
{ | ||
"x-ms-request-id", | ||
"x-ms-client-request-id", | ||
"x-ms-return-client-request-id", | ||
"traceparent", | ||
"MS-CV", | ||
|
||
"Accept", | ||
"Cache-Control", | ||
"Connection", | ||
"Content-Length", | ||
"Content-Type", | ||
"Date", | ||
"ETag", | ||
"Expires", | ||
"If-Match", | ||
"If-Modified-Since", | ||
"If-None-Match", | ||
"If-Unmodified-Since", | ||
"Last-Modified", | ||
"Pragma", | ||
"Request-Id", | ||
"Retry-After", | ||
"Server", | ||
"Transfer-Encoding", | ||
"User-Agent", | ||
"WWW-Authenticate" // OAuth Challenge header. | ||
}, StringComparer.OrdinalIgnoreCase); | ||
|
||
/// <summary> | ||
/// Sanitize value of sensitive headers in the given <paramref name="headers"/>. | ||
/// </summary> | ||
/// <param name="headers">A collection of headers to sanitize.</param> | ||
public static void SanitizerHeaders(IDictionary<string, IEnumerable<string>> headers) | ||
{ | ||
if (headers == null) | ||
{ | ||
return; | ||
} | ||
|
||
var namesOfHeaderToSanitize = headers.Keys.Except(_allowedHeaders, StringComparer.OrdinalIgnoreCase).ToList(); | ||
|
||
foreach (string name in namesOfHeaderToSanitize) | ||
{ | ||
headers[name] = new string[] { _redactedPlaceholder }; | ||
} | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
sdk/mgmtcommon/ClientRuntime/Tests/ClientRuntime.NetCore.Tests/HttpRequestSanitizerTest.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,96 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Rest.ClientRuntime.Tests | ||
{ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
using System.Linq; | ||
|
||
public class HttpRequestSanitizerTest | ||
{ | ||
[Theory] | ||
[InlineData("authorization")] | ||
[InlineData("Authorization")] | ||
[InlineData("AUTHORIZATION")] | ||
public void SanitizeAuthorizationHeader(string headerName) | ||
{ | ||
var request = CreateRequestWrapper(headerName, "test"); | ||
|
||
Assert.True(request.Headers.TryGetValue(HttpRequestHeader.Authorization.ToString(), out IEnumerable<string> sanitizedValues)); | ||
Assert.Single(sanitizedValues); | ||
Assert.NotEqual("test", sanitizedValues.First()); | ||
Assert.Equal("REDACTED", sanitizedValues.First()); | ||
} | ||
|
||
[Theory] | ||
[InlineData("custom")] | ||
[InlineData("foo")] | ||
[InlineData("x-ms-secret")] | ||
public void SanitizeCustomHeader(string headerName) | ||
{ | ||
var request = CreateRequestWrapper(headerName, "test"); | ||
|
||
Assert.True(request.Headers.TryGetValue(headerName, out IEnumerable<string> sanitizedValues)); | ||
Assert.Single(sanitizedValues); | ||
Assert.NotEqual("test", sanitizedValues.First()); | ||
Assert.Equal("REDACTED", sanitizedValues.First()); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Accept", "application/json")] | ||
[InlineData("User-Agent", "azure-sdk")] | ||
[InlineData("Pragma", "foo")] | ||
public void KeepAllowedHeaders(string headerName, string headerValue) | ||
{ | ||
var request = CreateRequestWrapper(headerName, headerValue); | ||
|
||
Assert.True(request.Headers.TryGetValue(headerName, out IEnumerable<string> sanitizedValues)); | ||
Assert.Single(sanitizedValues); | ||
Assert.Equal(headerValue, sanitizedValues.First()); | ||
} | ||
|
||
[Theory] | ||
[InlineData("accept", "Accept", "application/json")] | ||
[InlineData("user-agent", "User-Agent", "azure-sdk")] | ||
[InlineData("pragma", "Pragma", "foo")] | ||
public void AllowedHeaderNamesAreCaseInsensitive(string headerName, string standardName, string headerValue) | ||
{ | ||
var request = CreateRequestWrapper(headerName, headerValue); | ||
|
||
Assert.True(request.Headers.TryGetValue(standardName, out IEnumerable<string> sanitizedValues)); | ||
Assert.Single(sanitizedValues); | ||
Assert.Equal(headerValue, sanitizedValues.First()); | ||
} | ||
|
||
[Fact] | ||
public void OnlySanitizeNotAllowedHeader() | ||
{ | ||
var request = CreateRequestWrapper("Authorization", "test"); | ||
request.Headers.Add("Pragma", new string[] { "foo" }); | ||
request.Headers.Add("User-Agent", new string[] { "azure-sdk" }); | ||
|
||
Assert.True(request.Headers.TryGetValue(HttpRequestHeader.Authorization.ToString(), out IEnumerable<string> sanitizedValues)); | ||
Assert.Single(sanitizedValues); | ||
Assert.NotEqual("test", sanitizedValues.First()); | ||
Assert.Equal("REDACTED", sanitizedValues.First()); | ||
|
||
Assert.True(request.Headers.TryGetValue("Pragma", out sanitizedValues)); | ||
Assert.Single(sanitizedValues); | ||
Assert.Equal("foo", sanitizedValues.First()); | ||
|
||
Assert.True(request.Headers.TryGetValue("User-Agent", out sanitizedValues)); | ||
Assert.Single(sanitizedValues); | ||
Assert.Equal("azure-sdk", sanitizedValues.First()); | ||
} | ||
|
||
private HttpRequestMessageWrapper CreateRequestWrapper(string headerName, string headerValue) | ||
{ | ||
var request = new HttpRequestMessage(); | ||
request.Headers.Add(headerName, headerValue); | ||
return new HttpRequestMessageWrapper(request, ""); | ||
} | ||
} | ||
} |
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