mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +00:00
Hardens test assertions and adds missing test coverage.
This commit is contained in:
1 parent
6b86fd5805
commit
1189bbaf36
489 files changed
+39982
-512
No files matched your search
+20
-26
@@ -7,7 +7,6 @@ using Lumina.Infrastructure.Core.Security;
|
||||
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
@@ -74,41 +73,36 @@ public class ChangePasswordEndpointTests : IClassFixture<LuminaApiFactory>, IDis
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ChangePassword_WithValidRequest_ShouldNotLeakTimingInformation()
|
||||
public async Task ChangePassword_WithWrongCurrentPassword_ShouldReturnUniformForbiddenResponse()
|
||||
{
|
||||
// Arrange
|
||||
UserEntity user = await CreateAndAuthenticateUser();
|
||||
Stopwatch stopwatch = new();
|
||||
List<long> timings = [];
|
||||
ChangePasswordRequest request = new(
|
||||
Username: user.Username,
|
||||
CurrentPassword: "WrongPass123!",
|
||||
NewPassword: "NewPass123!",
|
||||
NewPasswordConfirm: "NewPass123!"
|
||||
);
|
||||
|
||||
// Act
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
await Task.Delay(100);
|
||||
stopwatch.Restart();
|
||||
await _client.PostAsJsonAsync("/api/v1/auth/change-password", new ChangePasswordRequest(
|
||||
Username: user.Username,
|
||||
CurrentPassword: "WrongPass123!",
|
||||
NewPassword: "NewPass123!",
|
||||
NewPasswordConfirm: "NewPass123!"
|
||||
));
|
||||
stopwatch.Stop();
|
||||
timings.Add(stopwatch.ElapsedMilliseconds);
|
||||
}
|
||||
|
||||
timings.Sort();
|
||||
timings = timings.Skip(1).SkipLast(1).ToList(); // remove outliers
|
||||
HttpResponseMessage firstResponse = await _client.PostAsJsonAsync("/api/v1/auth/change-password", request);
|
||||
string firstContent = await firstResponse.Content.ReadAsStringAsync();
|
||||
HttpResponseMessage secondResponse = await _client.PostAsJsonAsync("/api/v1/auth/change-password", request);
|
||||
string secondContent = await secondResponse.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
double stdDev = CalculateStandardDeviation(timings);
|
||||
Assert.True(stdDev < 200);
|
||||
Assert.Equal(HttpStatusCode.Forbidden, firstResponse.StatusCode);
|
||||
Assert.Equal(HttpStatusCode.Forbidden, secondResponse.StatusCode);
|
||||
AssertProblemDetail(firstContent, "General.Failure", "InvalidCurrentPassword");
|
||||
AssertProblemDetail(secondContent, "General.Failure", "InvalidCurrentPassword");
|
||||
}
|
||||
|
||||
private static double CalculateStandardDeviation(List<long> values)
|
||||
private void AssertProblemDetail(string content, string expectedTitle, string expectedDetail)
|
||||
{
|
||||
double average = values.Average();
|
||||
double sumOfSquaresOfDifferences = values.Select(value => (value - average) * (value - average)).Sum();
|
||||
return Math.Sqrt(sumOfSquaresOfDifferences / values.Count);
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(expectedTitle, problemDetails!["title"].GetString());
|
||||
Assert.Equal(expectedDetail, problemDetails["detail"].GetString());
|
||||
}
|
||||
|
||||
private async Task<UserEntity> CreateAndAuthenticateUser()
|
||||
|
||||
+22
-26
@@ -9,7 +9,6 @@ using Lumina.Infrastructure.Core.Security;
|
||||
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Net;
|
||||
@@ -110,36 +109,32 @@ public class LoginEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Login_WithValidCredentials_ShouldNotLeakTimingInformation()
|
||||
public async Task Login_WithUnknownUsernameAndWrongPassword_ShouldReturnIndistinguishableResponses()
|
||||
{
|
||||
// Arrange
|
||||
UserEntity user = await CreateTestUser();
|
||||
Stopwatch stopwatch = new();
|
||||
List<long> timings = [];
|
||||
// since this test is rigged to trigger the rate limiting, assign a different IP to it
|
||||
LoginRequest unknownUserRequest = new(
|
||||
Username: "nonexistent_user",
|
||||
Password: "TestPass123!"
|
||||
);
|
||||
LoginRequest wrongPasswordRequest = new(
|
||||
Username: user.Username,
|
||||
Password: "WrongPass123!"
|
||||
);
|
||||
_client.DefaultRequestHeaders.Clear();
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", $"192.168.1.2");
|
||||
|
||||
// Act
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
await Task.Delay(100); // add small delay between requests
|
||||
stopwatch.Restart();
|
||||
await _client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest(
|
||||
Username: user.Username,
|
||||
Password: "TestPass123!"
|
||||
));
|
||||
stopwatch.Stop();
|
||||
timings.Add(stopwatch.ElapsedMilliseconds);
|
||||
}
|
||||
// remove the highest and lowest values to account for outliers
|
||||
timings.Sort();
|
||||
timings = timings.Skip(1).SkipLast(1).ToList();
|
||||
HttpResponseMessage unknownUserResponse = await _client.PostAsJsonAsync("/api/v1/auth/login", unknownUserRequest);
|
||||
string unknownUserContent = await unknownUserResponse.Content.ReadAsStringAsync();
|
||||
HttpResponseMessage wrongPasswordResponse = await _client.PostAsJsonAsync("/api/v1/auth/login", wrongPasswordRequest);
|
||||
string wrongPasswordContent = await wrongPasswordResponse.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
// check for timing consistency to prevent timing attacks
|
||||
double stdDev = CalculateStandardDeviation(timings);
|
||||
Assert.True(stdDev < 200);
|
||||
Assert.Equal(HttpStatusCode.Forbidden, unknownUserResponse.StatusCode);
|
||||
Assert.Equal(HttpStatusCode.Forbidden, wrongPasswordResponse.StatusCode);
|
||||
AssertProblemDetail(unknownUserContent, "General.Failure", "InvalidUsernameOrPassword");
|
||||
AssertProblemDetail(wrongPasswordContent, "General.Failure", "InvalidUsernameOrPassword");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -247,11 +242,12 @@ public class LoginEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
Assert.DoesNotContain("Exception", content);
|
||||
}
|
||||
|
||||
private double CalculateStandardDeviation(List<long> values)
|
||||
private void AssertProblemDetail(string content, string expectedTitle, string expectedDetail)
|
||||
{
|
||||
double average = values.Average();
|
||||
double sumOfSquaresOfDifferences = values.Select(value => (value - average) * (value - average)).Sum();
|
||||
return Math.Sqrt(sumOfSquaresOfDifferences / values.Count);
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(expectedTitle, problemDetails!["title"].GetString());
|
||||
Assert.Equal(expectedDetail, problemDetails["detail"].GetString());
|
||||
}
|
||||
|
||||
private async Task<UserEntity> CreateTestUser()
|
||||
|
||||
+17
-22
@@ -6,7 +6,6 @@ using Lumina.Infrastructure.Core.Security;
|
||||
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
@@ -42,34 +41,29 @@ public class RegisterEndpointTests : IClassFixture<LuminaApiFactory>, IDisposabl
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Register_WithValidData_ShouldNotLeakTimingInformation()
|
||||
public async Task Register_WhenUsernameTaken_ShouldReturnUniformConflictResponse()
|
||||
{
|
||||
// Arrange
|
||||
Stopwatch stopwatch = new();
|
||||
List<long> timings = [];
|
||||
_client.DefaultRequestHeaders.Clear();
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", $"192.168.1.6");
|
||||
RegistrationRequest request = new(
|
||||
Username: _testUsername,
|
||||
Password: "TestPass123!",
|
||||
PasswordConfirm: "TestPass123!"
|
||||
);
|
||||
_client.DefaultRequestHeaders.Clear();
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", $"192.168.1.6");
|
||||
|
||||
// Act
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
await Task.Delay(100);
|
||||
stopwatch.Restart();
|
||||
await _client.PostAsJsonAsync("/api/v1/auth/register", request);
|
||||
stopwatch.Stop();
|
||||
timings.Add(stopwatch.ElapsedMilliseconds);
|
||||
}
|
||||
timings.Sort();
|
||||
timings = timings.Skip(1).SkipLast(1).ToList();
|
||||
HttpResponseMessage firstResponse = await _client.PostAsJsonAsync("/api/v1/auth/register", request);
|
||||
HttpResponseMessage secondResponse = await _client.PostAsJsonAsync("/api/v1/auth/register", request);
|
||||
string secondContent = await secondResponse.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
double stdDev = CalculateStandardDeviation(timings);
|
||||
Assert.True(stdDev < 200);
|
||||
Assert.Equal(HttpStatusCode.Created, firstResponse.StatusCode);
|
||||
Assert.Equal(HttpStatusCode.Conflict, secondResponse.StatusCode);
|
||||
AssertProblemDetail(secondContent, "General.Conflict", "UsernameAlreadyExists");
|
||||
Assert.DoesNotContain("password", secondContent);
|
||||
Assert.DoesNotContain("hash", secondContent);
|
||||
Assert.DoesNotContain("salt", secondContent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -203,11 +197,12 @@ public class RegisterEndpointTests : IClassFixture<LuminaApiFactory>, IDisposabl
|
||||
Assert.DoesNotContain("salt", content);
|
||||
}
|
||||
|
||||
private double CalculateStandardDeviation(List<long> values)
|
||||
private void AssertProblemDetail(string content, string expectedTitle, string expectedDetail)
|
||||
{
|
||||
double average = values.Average();
|
||||
double sumOfSquaresOfDifferences = values.Select(value => (value - average) * (value - average)).Sum();
|
||||
return Math.Sqrt(sumOfSquaresOfDifferences / values.Count);
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(expectedTitle, problemDetails!["title"].GetString());
|
||||
Assert.Equal(expectedDetail, problemDetails["detail"].GetString());
|
||||
}
|
||||
|
||||
private async Task<UserEntity> CreateTestUser()
|
||||
|
||||
Reference in new issue
Block a user