This repository was archived by the owner on Jul 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
[2.11.0-beta1] guard against malicious headers in quickpulse #1191
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1bce47a
guard against malicious headers in quickpulse
23f32ea
remove unused variable
251eef7
using not needed
ed40d8a
fixing compile issues
d07b4a9
Merge branch 'develop' into tilee/quickpulse_protect_headers
4372cdf
Merge branch 'develop' into tilee/quickpulse_protect_headers
86b0a86
update
3f51867
Merge branch 'tilee/quickpulse_protect_headers' of https://github.com…
e0b7854
Merge branch 'develop' into tilee/quickpulse_protect_headers
cijothomas ac9cd64
Merge branch 'develop' into tilee/quickpulse_protect_headers
cijothomas 981d1a7
Merge branch 'develop' into tilee/quickpulse_protect_headers
7a80d68
changelog
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
18 changes: 11 additions & 7 deletions
18
...ector/Perf.Shared.NetStandard/Implementation/QuickPulse/QuickPulseServiceClientHelpers.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -1,18 +1,22 @@ | ||
| namespace Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.Implementation.QuickPulse | ||
| { | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Net.Http.Headers; | ||
| using Microsoft.ApplicationInsights.Common; | ||
| using Microsoft.ApplicationInsights.Common.Internal; | ||
|
|
||
| internal static class QuickPulseServiceClientHelpers | ||
| { | ||
| private static readonly string[] emptyResult = ArrayExtensions.Empty<string>(); | ||
|
|
||
| public static IEnumerable<string> GetValuesSafe(this HttpResponseHeaders headers, string name) | ||
| public static string GetValueSafe(this HttpHeaders headers, string name) | ||
| { | ||
| IEnumerable<string> result = (headers?.Contains(name) ?? false) ? headers.GetValues(name) : emptyResult; | ||
| string value = default(string); | ||
|
|
||
| return result; | ||
| if (headers?.Contains(name) ?? false) | ||
| { | ||
| value = headers.GetValues(name).First(); | ||
| value = StringUtilities.EnforceMaxLength(value, InjectionGuardConstants.QuickPulseResponseHeaderHeaderMaxLength); | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
79 changes: 79 additions & 0 deletions
79
Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseServiceClientHelpersTests.cs
This file contains hidden or 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,79 @@ | ||
| namespace Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.Tests.QuickPulse | ||
| { | ||
| using System.Net.Http.Headers; | ||
| using Microsoft.ApplicationInsights.Common.Internal; | ||
| using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.Implementation.QuickPulse; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [TestClass] | ||
| public class QuickPulseServiceClientHelpersTests | ||
| { | ||
| private const int QuickPulseResponseHeaderHeaderMaxLength = InjectionGuardConstants.QuickPulseResponseHeaderHeaderMaxLength; | ||
| private const string SecretString = "12345abcd"; | ||
| private const string HeaderName = "myheader"; | ||
| private const string FakeHeaderName = "fake"; | ||
|
|
||
| [TestMethod] | ||
| public void VerifyHeaderGetValue() | ||
| { | ||
| // setup | ||
| var headers = new TestHttpHeaders(); | ||
| foreach (string headerName in QuickPulseConstants.XMsQpsAuthOpaqueHeaderNames) | ||
| { | ||
| headers.Add(headerName, headerName + SecretString); | ||
| } | ||
|
|
||
| // assert | ||
| foreach (string headerName in QuickPulseConstants.XMsQpsAuthOpaqueHeaderNames) | ||
| { | ||
| var value = headers.GetValueSafe(headerName); | ||
| Assert.AreEqual(headerName + SecretString, value); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void VerifyHeaderGetValues() | ||
| { | ||
| // setup | ||
| var headers = new TestHttpHeaders(); | ||
| headers.Add(HeaderName, "one"); | ||
| headers.Add(HeaderName, "two"); | ||
| headers.Add(HeaderName, "three"); | ||
|
|
||
| // assert | ||
| var result = headers.GetValueSafe(HeaderName); | ||
| Assert.AreEqual("one", result); | ||
|
|
||
| var result2 = headers.GetValueSafe(FakeHeaderName); | ||
| Assert.AreEqual(default(string), result2); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void VerifyHeadersLengthIsProtected() | ||
| { | ||
| // setup | ||
| var headers = new TestHttpHeaders(); | ||
| headers.Add(HeaderName, new string('*', QuickPulseResponseHeaderHeaderMaxLength)); | ||
|
|
||
| // assert | ||
| var result = headers.GetValueSafe(HeaderName); | ||
| Assert.AreEqual(QuickPulseResponseHeaderHeaderMaxLength, result.Length); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void VerifyInvalidHeadersLengthIsProtected() | ||
| { | ||
| // setup | ||
| var headers = new TestHttpHeaders(); | ||
| headers.Add(HeaderName, new string('*', QuickPulseResponseHeaderHeaderMaxLength + 1)); | ||
|
|
||
| // assert | ||
| var result = headers.GetValueSafe(HeaderName); | ||
| Assert.AreEqual(QuickPulseResponseHeaderHeaderMaxLength, result.Length); | ||
| } | ||
|
|
||
| private class TestHttpHeaders : HttpHeaders | ||
| { | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.