-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving HttpMock capabilities (#527)
* Improving HttpMock capabilities * Updating validatorbase and test file name
- Loading branch information
Showing
12 changed files
with
225 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Moq; | ||
using Moq.Protected; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.PatternMatcher.Plugins.Security.Helpers | ||
{ | ||
public class HttpMockHelper : DelegatingHandler | ||
{ | ||
private readonly List<Tuple<HttpRequestMessage, HttpStatusCode, HttpContent>> _fakeResponses = | ||
new List<Tuple<HttpRequestMessage, HttpStatusCode, HttpContent>>(); | ||
|
||
public void Mock(HttpRequestMessage httpRequestMessage, HttpStatusCode httpStatusCode, HttpContent httpContent) | ||
{ | ||
_fakeResponses.Add(new Tuple<HttpRequestMessage, HttpStatusCode, HttpContent>(httpRequestMessage, httpStatusCode, httpContent)); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
_fakeResponses.Clear(); | ||
} | ||
|
||
public static HttpMessageHandler Mock(HttpStatusCode httpStatusCode, HttpContent httpContent) | ||
{ | ||
var mockMessageHandler = new Mock<HttpMessageHandler>(); | ||
mockMessageHandler.Protected() | ||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) | ||
.ReturnsAsync(new HttpResponseMessage | ||
{ | ||
StatusCode = httpStatusCode, | ||
Content = httpContent | ||
}); | ||
|
||
return mockMessageHandler.Object; | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
Tuple<HttpRequestMessage, HttpStatusCode, HttpContent> fakeResponse = _fakeResponses.Find(fr => | ||
fr.Item1.RequestUri == request.RequestUri | ||
&& fr.Item1.Headers.IsEmptyEnumerable() == request.Headers.IsEmptyEnumerable()); | ||
|
||
if (fakeResponse == null) | ||
{ | ||
fakeResponse = _fakeResponses.Find(fr => | ||
fr.Item1.RequestUri == request.RequestUri | ||
&& fr.Item1.Headers.Authorization == request.Headers.Authorization); | ||
} | ||
|
||
return Task.FromResult( | ||
new HttpResponseMessage(fakeResponse.Item2) | ||
{ | ||
RequestMessage = request, | ||
Content = fakeResponse.Item3 | ||
}); | ||
} | ||
} | ||
} |
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
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
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
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
129 changes: 129 additions & 0 deletions
129
Src/Plugins/Tests.Security/Validators/SEC101_044.NpmCredentialsValidatorTests.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,129 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
|
||
using FluentAssertions; | ||
|
||
using Microsoft.CodeAnalysis.Sarif.PatternMatcher.Plugins.Security.Helpers; | ||
using Microsoft.CodeAnalysis.Sarif.PatternMatcher.Sdk; | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.PatternMatcher.Plugins.Security.Validators | ||
{ | ||
public class NpmCredentialsValidatorTests | ||
{ | ||
[Fact] | ||
public void NpmCredentialsValidator_MockHttp() | ||
{ | ||
const string id = "id"; | ||
const string host = "host"; | ||
const string secret = "secret"; | ||
string uri = $"https://{host}"; | ||
using var requestWithNoCredentials = new HttpRequestMessage(HttpMethod.Get, uri); | ||
|
||
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", id, secret))); | ||
|
||
using var requestWithCredentials = new HttpRequestMessage(HttpMethod.Get, uri); | ||
requestWithCredentials.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials); | ||
|
||
var mockHandler = new HttpMockHelper(); | ||
|
||
string fingerprintText = $"[host={host}][id={id}][secret={secret}]"; | ||
|
||
var testCases = new TestCase[] | ||
{ | ||
new TestCase | ||
{ | ||
HttpContents = new List<HttpContent>{ null }, | ||
HttpStatusCodes = new List<HttpStatusCode>{ HttpStatusCode.OK }, | ||
HttpRequestMessages = new List<HttpRequestMessage>{ requestWithNoCredentials }, | ||
ExpectedMessage = string.Empty, | ||
Title = "Endpoint does not require credential", | ||
ExpectedValidationState = ValidationState.NoMatch, | ||
}, | ||
new TestCase | ||
{ | ||
HttpContents = new List<HttpContent>{ null, null }, | ||
HttpStatusCodes = new List<HttpStatusCode>{ HttpStatusCode.Unauthorized, HttpStatusCode.OK }, | ||
HttpRequestMessages = new List<HttpRequestMessage>{ requestWithNoCredentials, requestWithCredentials }, | ||
ExpectedMessage = $"The '{id}' account is compromised for '{host}'.", | ||
Title = "Credential is valid", | ||
ExpectedValidationState = ValidationState.Authorized, | ||
}, | ||
new TestCase | ||
{ | ||
HttpContents = new List<HttpContent>{ null, null }, | ||
HttpStatusCodes = new List<HttpStatusCode>{ HttpStatusCode.Unauthorized, HttpStatusCode.Unauthorized }, | ||
HttpRequestMessages = new List<HttpRequestMessage>{ requestWithNoCredentials, requestWithCredentials }, | ||
ExpectedMessage = $"The provided '{id}' account secret is not authorized to access '{host}'.", | ||
Title = "Credential is invalid", | ||
ExpectedValidationState = ValidationState.Unauthorized, | ||
}, | ||
new TestCase | ||
{ | ||
HttpContents = new List<HttpContent>{ null, null }, | ||
HttpStatusCodes = new List<HttpStatusCode>{ HttpStatusCode.Unauthorized, HttpStatusCode.NotFound }, | ||
HttpRequestMessages = new List<HttpRequestMessage>{ requestWithNoCredentials, requestWithCredentials }, | ||
ExpectedMessage = $"An unexpected HTTP response code was received from '{id}' account on '{host}': NotFound.", | ||
Title = "Unexpected NotFound StatusCode", | ||
ExpectedValidationState = ValidationState.Unknown, | ||
}, | ||
}; | ||
|
||
var sb = new StringBuilder(); | ||
foreach (TestCase testCase in testCases) | ||
{ | ||
for (int i = 0; i < testCase.HttpStatusCodes.Count; i++) | ||
{ | ||
mockHandler.Mock(testCase.HttpRequestMessages[i], testCase.HttpStatusCodes[i], testCase.HttpContents[i]); | ||
} | ||
|
||
string message = string.Empty; | ||
ResultLevelKind resultLevelKind = default; | ||
var fingerprint = new Fingerprint(fingerprintText); | ||
var keyValuePairs = new Dictionary<string, string>(); | ||
|
||
ValidatorHelper.ResetStaticInstance<NpmCredentialsValidator>(); | ||
using var httpClient = new HttpClient(mockHandler); | ||
NpmCredentialsValidator.Instance.SetHttpClient(httpClient); | ||
|
||
ValidationState currentState = NpmCredentialsValidator.IsValidDynamic(ref fingerprint, | ||
ref message, | ||
keyValuePairs, | ||
ref resultLevelKind); | ||
|
||
if (currentState != testCase.ExpectedValidationState) | ||
{ | ||
sb.AppendLine($"The test '{testCase.Title}' was expecting '{testCase.ExpectedValidationState}' but found '{currentState}'."); | ||
} | ||
|
||
if (message != testCase.ExpectedMessage) | ||
{ | ||
sb.AppendLine($"The test '{testCase.Title}' was expecting '{testCase.ExpectedMessage}' but found '{message}'."); | ||
} | ||
|
||
mockHandler.Clear(); | ||
} | ||
|
||
sb.Length.Should().Be(0, sb.ToString()); | ||
} | ||
|
||
private struct TestCase | ||
{ | ||
public List<HttpContent> HttpContents { get; set; } | ||
public List<HttpStatusCode> HttpStatusCodes { get; set; } | ||
public List<HttpRequestMessage> HttpRequestMessages { get; set; } | ||
|
||
public string Title { get; set; } | ||
public string ExpectedMessage { get; set; } | ||
public ValidationState ExpectedValidationState { get; set; } | ||
} | ||
} | ||
} |
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
Oops, something went wrong.