mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 13:11:10 +00:00
Renames project paths from Lumina to Nemera
This commit is contained in:
1 parent
ce6fddb4c9
commit
0f659a4877
6450 files changed
No files matched your search
+516
@@ -0,0 +1,516 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Contracts.Fixtures.Core.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.CompleteConsumption;
|
||||
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.MediaConsumption.CompleteConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <see cref="CompleteConsumptionEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CompleteConsumptionEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
{
|
||||
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
|
||||
private const long SEEDED_VERSION = 7;
|
||||
|
||||
private static readonly DateTime s_startedOnUtc = new(2026, 3, 1, 10, 0, 0, DateTimeKind.Utc);
|
||||
private static readonly DateTime s_lastActivityOnUtc = new(2026, 3, 1, 10, 5, 0, DateTimeKind.Utc);
|
||||
|
||||
private readonly LuminaApiFactory _apiFactory;
|
||||
private readonly HttpClient _client;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
||||
private readonly MediaConsumptionRecordEntityFixture _mediaConsumptionRecordEntityFixture = new();
|
||||
private readonly MediaPositionRequestFixture _mediaPositionRequestFixture = new();
|
||||
private readonly CompleteConsumptionRequestFixture _completeConsumptionRequestFixture = new();
|
||||
private readonly UserEntityFixture _userEntityFixture = new();
|
||||
private readonly List<string> _seededUsernames = [];
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CompleteConsumptionEndpointTests"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiFactory">Injected in-memory API factory.</param>
|
||||
public CompleteConsumptionEndpointTests(LuminaApiFactory apiFactory)
|
||||
{
|
||||
_apiFactory = apiFactory;
|
||||
_client = apiFactory.CreateClient();
|
||||
// A unique X-Forwarded-For isolates rate limiting state per test.
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompleteConsumption_WhenUnauthorized_ShouldReturnUnauthorizedResult()
|
||||
{
|
||||
// Arrange
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}/complete";
|
||||
CompleteConsumptionRequest request = _completeConsumptionRequestFixture.Create(includePosition: false, progressFraction: 1, consumedDurationInSeconds: 10);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
await AssertUnauthorizedAsync(response, url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompleteConsumption_WhenSuccessful_ShouldReturnNonCacheableResponseWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(_, Guid recordId) = await SeedRecordAsync(userId, MediaKind.MusicTrack, ConsumptionStatus.InProgress, 0, 0.1, MediaPositionKind.TimeOffset, 5_000, null);
|
||||
CompleteConsumptionRequest request = _completeConsumptionRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: nameof(MediaPositionKind.TimeOffset), timeOffsetSeconds: 60, includeLocation: false),
|
||||
progressFraction: 1,
|
||||
consumedDurationInSeconds: 120);
|
||||
string url = $"/api/v1/users/me/consumption-records/{recordId}/complete";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
AssertNonCacheable(response);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
AssertNoSensitiveData(content);
|
||||
AssertNoDatabaseOnlyFields(content);
|
||||
|
||||
ConsumptionRecordResponse? record = JsonSerializer.Deserialize<ConsumptionRecordResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(record);
|
||||
Assert.Equal(recordId, record!.Id);
|
||||
Assert.Equal(ConsumptionStatus.Completed, record.Status);
|
||||
Assert.NotNull(record.EndedOnUtc);
|
||||
Assert.Equal(120.0, record.ConsumedDurationInSeconds);
|
||||
Assert.Equal(1.0, record.LastProgressFraction);
|
||||
Assert.NotNull(record.LastPosition);
|
||||
Assert.Equal(MediaPositionKind.TimeOffset, record.LastPosition!.Kind);
|
||||
Assert.Equal(60.0, record.LastPosition.TimeOffsetSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompleteConsumption_WhenRecordDoesNotExist_ShouldReturnNonCacheableNotFoundWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
CompleteConsumptionRequest request = _completeConsumptionRequestFixture.Create(includePosition: false, progressFraction: 1, consumedDurationInSeconds: 10);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}/complete";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
string content = await AssertProblemDetailsAsync(
|
||||
response,
|
||||
HttpStatusCode.NotFound,
|
||||
"General.NotFound",
|
||||
"ConsumptionRecordNotFound",
|
||||
url,
|
||||
"https://tools.ietf.org/html/rfc9110#section-15.5.5");
|
||||
AssertNonCacheable(response);
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompleteConsumption_WhenAnotherUsersRecordIsTargeted_ShouldReturnNotFoundAndLeaveRecordUnchanged()
|
||||
{
|
||||
// Arrange
|
||||
(Guid otherUserId, _) = await SeedOtherUserAsync();
|
||||
(_, Guid otherRecordId) = await SeedRecordAsync(otherUserId, MediaKind.MusicTrack, ConsumptionStatus.InProgress, 15_000, 0.2, MediaPositionKind.TimeOffset, 15_000, null);
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
CompleteConsumptionRequest request = _completeConsumptionRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: nameof(MediaPositionKind.TimeOffset), timeOffsetSeconds: 90, includeLocation: false),
|
||||
progressFraction: 1,
|
||||
consumedDurationInSeconds: 90);
|
||||
string url = $"/api/v1/users/me/consumption-records/{otherRecordId}/complete";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
string content = await AssertProblemDetailsAsync(
|
||||
response,
|
||||
HttpStatusCode.NotFound,
|
||||
"General.NotFound",
|
||||
"ConsumptionRecordNotFound",
|
||||
url,
|
||||
"https://tools.ietf.org/html/rfc9110#section-15.5.5");
|
||||
AssertNoSensitiveData(content);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
MediaConsumptionRecordEntity? storedRecord = dbContext.MediaConsumptionRecords.FirstOrDefault(record => record.Id == otherRecordId);
|
||||
Assert.NotNull(storedRecord);
|
||||
Assert.Equal(ConsumptionStatus.InProgress, storedRecord!.Status);
|
||||
Assert.Null(storedRecord.EndedOnUtc);
|
||||
Assert.Equal(15_000, storedRecord.ConsumedDurationMilliseconds);
|
||||
Assert.Equal(0.2, storedRecord.LastProgressFraction);
|
||||
Assert.Equal(MediaPositionKind.TimeOffset, storedRecord.LastPositionKind);
|
||||
Assert.Equal(15_000, storedRecord.LastPositionTimeOffsetMilliseconds);
|
||||
Assert.Equal(SEEDED_VERSION, storedRecord.Version);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task CompleteConsumption_WithSqlInjectionInRecordId_ShouldRejectWithoutLeaking(string maliciousRecordId)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
CompleteConsumptionRequest request = _completeConsumptionRequestFixture.Create(includePosition: false, progressFraction: 1, consumedDurationInSeconds: 10);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Uri.EscapeDataString(maliciousRecordId)}/complete";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
await AssertRejectedInjectionAsync(response, maliciousRecordId, url, "ConsumptionRecordIdCannotBeEmpty");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task CompleteConsumption_WithUnknownPositionKind_ShouldRejectWithoutLeaking(string maliciousPositionKind)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
CompleteConsumptionRequest request = _completeConsumptionRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: maliciousPositionKind),
|
||||
progressFraction: 1,
|
||||
consumedDurationInSeconds: 10);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}/complete";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
string content = await AssertValidationProblemAsync(response, url, "InvalidMediaPosition");
|
||||
AssertNoSensitiveData(content);
|
||||
Assert.DoesNotContain(maliciousPositionKind, content, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task CompleteConsumption_WithOpaquePositionLocation_ShouldStoreItVerbatimWithoutLeaking(string location)
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(_, Guid recordId) = await SeedRecordAsync(userId, MediaKind.Book, ConsumptionStatus.InProgress, 0, 0.1, MediaPositionKind.Location, null, "chapter-1");
|
||||
CompleteConsumptionRequest request = _completeConsumptionRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: nameof(MediaPositionKind.Location), location: location, includeTimeOffsetSeconds: false),
|
||||
progressFraction: 1,
|
||||
consumedDurationInSeconds: 30);
|
||||
string url = $"/api/v1/users/me/consumption-records/{recordId}/complete";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
AssertNoSensitiveData(content);
|
||||
|
||||
ConsumptionRecordResponse? record = JsonSerializer.Deserialize<ConsumptionRecordResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(record);
|
||||
Assert.Equal(ConsumptionStatus.Completed, record!.Status);
|
||||
Assert.NotNull(record.LastPosition);
|
||||
Assert.Equal(MediaPositionKind.Location, record.LastPosition!.Kind);
|
||||
Assert.Equal(location, record.LastPosition.Location);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
MediaConsumptionRecordEntity? storedRecord = dbContext.MediaConsumptionRecords.FirstOrDefault(candidate => candidate.Id == recordId);
|
||||
Assert.NotNull(storedRecord);
|
||||
Assert.Equal(location, storedRecord!.LastPositionLocation);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a test user and authenticates it on <paramref name="client"/>.
|
||||
/// </summary>
|
||||
/// <param name="client">The HTTP client to authenticate.</param>
|
||||
/// <returns>The Id and username of the created user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync(HttpClient client)
|
||||
{
|
||||
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(client);
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
|
||||
/// </summary>
|
||||
/// <returns>The Id and username of the seeded user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid userId = Guid.NewGuid();
|
||||
string username = $"otheruser_{Guid.NewGuid()}";
|
||||
UserEntity user = _userEntityFixture.Create(id: userId, username: username, password: "TestPass123!");
|
||||
dbContext.Users.Add(user);
|
||||
await dbContext.SaveChangesAsync();
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a media library owned by <paramref name="userId"/> and a consumption record of a media item belonging to it.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user that owns the record.</param>
|
||||
/// <param name="mediaKind">The kind of media that the record consumes.</param>
|
||||
/// <param name="status">The state of the seeded session.</param>
|
||||
/// <param name="consumedDurationMilliseconds">The consumed duration of the record, in milliseconds.</param>
|
||||
/// <param name="lastProgressFraction">The last reported progress fraction of the record.</param>
|
||||
/// <param name="lastPositionKind">The kind of the last known position.</param>
|
||||
/// <param name="lastPositionTimeOffsetMilliseconds">The last known position, in milliseconds, when it is a time offset.</param>
|
||||
/// <param name="lastPositionLocation">The last known opaque locator, when it is a location.</param>
|
||||
/// <returns>The Ids of the seeded library and consumption record.</returns>
|
||||
private async Task<(Guid LibraryId, Guid RecordId)> SeedRecordAsync(
|
||||
Guid userId,
|
||||
MediaKind mediaKind,
|
||||
ConsumptionStatus status,
|
||||
long consumedDurationMilliseconds,
|
||||
double lastProgressFraction,
|
||||
MediaPositionKind lastPositionKind,
|
||||
long? lastPositionTimeOffsetMilliseconds,
|
||||
string? lastPositionLocation)
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid mediaItemId = Guid.NewGuid();
|
||||
Guid recordId = Guid.NewGuid();
|
||||
dbContext.Libraries.Add(_libraryEntityFixture.Create(
|
||||
id: libraryId,
|
||||
userId: userId,
|
||||
title: "Media Library",
|
||||
libraryType: mediaKind == MediaKind.Book ? LibraryType.EBook : LibraryType.Music,
|
||||
contentLocations: [],
|
||||
shouldTrackConsumptionProgress: false));
|
||||
dbContext.MediaConsumptionRecords.Add(_mediaConsumptionRecordEntityFixture.Create(
|
||||
id: recordId,
|
||||
userId: userId,
|
||||
libraryId: libraryId,
|
||||
mediaKind: mediaKind,
|
||||
mediaItemId: mediaItemId,
|
||||
status: status,
|
||||
startedOnUtc: s_startedOnUtc,
|
||||
lastActivityOnUtc: s_lastActivityOnUtc,
|
||||
consumedDurationMilliseconds: consumedDurationMilliseconds,
|
||||
lastPositionKind: lastPositionKind,
|
||||
lastPositionTimeOffsetMilliseconds: lastPositionTimeOffsetMilliseconds,
|
||||
lastPositionLocation: lastPositionLocation,
|
||||
lastProgressFraction: lastProgressFraction,
|
||||
consecutivePagesRead: 0,
|
||||
version: SEEDED_VERSION));
|
||||
await dbContext.SaveChangesAsync();
|
||||
return (libraryId, recordId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
||||
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
|
||||
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
|
||||
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the shape of a problem details response with the provided status, title, detail, instance and type.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="statusCode">The expected HTTP status code.</param>
|
||||
/// <param name="expectedTitle">The expected problem title.</param>
|
||||
/// <param name="expectedDetail">The expected problem detail.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedType">The expected problem type URI.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertProblemDetailsAsync(HttpResponseMessage response, HttpStatusCode statusCode, string expectedTitle, string expectedDetail, string expectedInstance, string expectedType)
|
||||
{
|
||||
Assert.Equal(statusCode, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal((int)statusCode, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal(expectedType, problemDetails["type"].GetString());
|
||||
Assert.Equal(expectedTitle, problemDetails["title"].GetString());
|
||||
Assert.Equal(expectedDetail, problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the standard validation problem of the complete consumption route.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedErrorDescription">The error description expected under the validation errors.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertValidationProblemAsync(HttpResponseMessage response, string expectedInstance, string expectedErrorDescription)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
Assert.Equal("OneOrMoreValidationErrorsOccurred", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorDescription, errors["General.Validation"]);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that a malicious route value is rejected without disclosing internals or reaching the data access layer.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="payload">The malicious value that was sent.</param>
|
||||
/// <param name="requestPath">The request path the value was sent in.</param>
|
||||
/// <param name="expectedErrorCode">The validation error code expected when the value reaches the validator.</param>
|
||||
private async Task AssertRejectedInjectionAsync(HttpResponseMessage response, string payload, string requestPath, string expectedErrorCode)
|
||||
{
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Assert.NotEqual(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.UnprocessableEntity)
|
||||
{
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
// The instance is the request path with the rejected payload still in place, independent of how the server escapes it.
|
||||
string escapedPayload = Uri.EscapeDataString(payload);
|
||||
int payloadIndex = requestPath.IndexOf(escapedPayload, StringComparison.Ordinal);
|
||||
string? instance = problemDetails["instance"].GetString();
|
||||
Assert.NotNull(instance);
|
||||
Assert.True(payloadIndex >= 0, "The escaped payload must appear in the request path.");
|
||||
Assert.StartsWith(requestPath[..payloadIndex], instance, StringComparison.Ordinal);
|
||||
Assert.EndsWith(requestPath[(payloadIndex + escapedPayload.Length)..], instance, StringComparison.Ordinal);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorCode, errors["General.Validation"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// A payload that carries encoded path separators is rejected by routing before it reaches the validator. That is still a safe rejection, so only it may take this branch.
|
||||
Assert.Contains("/", payload, StringComparison.Ordinal);
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
|
||||
AssertNoSensitiveData(content);
|
||||
Assert.DoesNotContain(payload, content, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the per-user responses are not storable by any intermediary and vary by the authorization header.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
private static void AssertNonCacheable(HttpResponseMessage response)
|
||||
{
|
||||
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
|
||||
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response body carries no credentials, secrets, persistence details or stack traces.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoSensitiveData(string content)
|
||||
{
|
||||
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the serialized consumption record exposes only the fields of its documented response contract.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoDatabaseOnlyFields(string content)
|
||||
{
|
||||
Assert.DoesNotContain("version", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("userId", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the test users created by the test.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
foreach (string username in _seededUsernames)
|
||||
_apiFactory.RemoveTestUser(username);
|
||||
}
|
||||
}
|
||||
+431
@@ -0,0 +1,431 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.GetConsumptionRecord;
|
||||
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.MediaConsumption.GetConsumptionRecord;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <see cref="GetConsumptionRecordEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetConsumptionRecordEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
{
|
||||
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
|
||||
|
||||
private static readonly DateTime s_startedOnUtc = new(2026, 3, 1, 10, 0, 0, DateTimeKind.Utc);
|
||||
private static readonly DateTime s_lastActivityOnUtc = new(2026, 3, 1, 10, 5, 0, DateTimeKind.Utc);
|
||||
|
||||
private readonly LuminaApiFactory _apiFactory;
|
||||
private readonly HttpClient _client;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
||||
private readonly MediaConsumptionRecordEntityFixture _mediaConsumptionRecordEntityFixture = new();
|
||||
private readonly UserEntityFixture _userEntityFixture = new();
|
||||
private readonly List<string> _seededUsernames = [];
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetConsumptionRecordEndpointTests"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiFactory">Injected in-memory API factory.</param>
|
||||
public GetConsumptionRecordEndpointTests(LuminaApiFactory apiFactory)
|
||||
{
|
||||
_apiFactory = apiFactory;
|
||||
_client = apiFactory.CreateClient();
|
||||
// A unique X-Forwarded-For isolates rate limiting state per test.
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumptionRecord_WhenUnauthorized_ShouldReturnUnauthorizedResult()
|
||||
{
|
||||
// Arrange
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
await AssertUnauthorizedAsync(response, url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumptionRecord_WhenSuccessful_ShouldReturnNonCacheableResponseWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
Guid recordId = await SeedRecordAsync(userId, ConsumptionStatus.InProgress, 15_000, 0.2, 15_000);
|
||||
string url = $"/api/v1/users/me/consumption-records/{recordId}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
AssertNonCacheable(response);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
AssertNoSensitiveData(content);
|
||||
AssertNoDatabaseOnlyFields(content);
|
||||
|
||||
ConsumptionRecordResponse? record = JsonSerializer.Deserialize<ConsumptionRecordResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(record);
|
||||
Assert.Equal(recordId, record!.Id);
|
||||
Assert.Equal(MediaKind.MusicTrack, record.MediaKind);
|
||||
Assert.Equal(ConsumptionStatus.InProgress, record.Status);
|
||||
Assert.Equal(15.0, record.ConsumedDurationInSeconds);
|
||||
Assert.Equal(0.2, record.LastProgressFraction);
|
||||
Assert.NotNull(record.LastPosition);
|
||||
Assert.Equal(MediaPositionKind.TimeOffset, record.LastPosition!.Kind);
|
||||
Assert.Equal(15.0, record.LastPosition.TimeOffsetSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumptionRecord_WhenRecordDoesNotExist_ShouldReturnNonCacheableNotFoundWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
string content = await AssertProblemDetailsAsync(
|
||||
response,
|
||||
HttpStatusCode.NotFound,
|
||||
"General.NotFound",
|
||||
"ConsumptionRecordNotFound",
|
||||
url,
|
||||
"https://tools.ietf.org/html/rfc9110#section-15.5.5");
|
||||
AssertNonCacheable(response);
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumptionRecord_WhenAnotherUsersRecordIsRequested_ShouldReturnNotFoundWithoutDisclosingExistence()
|
||||
{
|
||||
// Arrange
|
||||
(Guid otherUserId, _) = await SeedOtherUserAsync();
|
||||
Guid otherRecordId = await SeedRecordAsync(otherUserId, ConsumptionStatus.InProgress, 42_000, 0.4, 42_000);
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/users/me/consumption-records/{otherRecordId}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
Dictionary<string, JsonElement> foreignProblem = await ReadProblemDetailsAsync(response);
|
||||
HttpResponseMessage missingResponse = await _client.GetAsync($"/api/v1/users/me/consumption-records/{Guid.NewGuid()}");
|
||||
Dictionary<string, JsonElement> missingProblem = await ReadProblemDetailsAsync(missingResponse);
|
||||
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
Assert.Equal(HttpStatusCode.NotFound, missingResponse.StatusCode);
|
||||
Assert.Equal(missingProblem["status"].GetInt32(), foreignProblem["status"].GetInt32());
|
||||
Assert.Equal(missingProblem["title"].GetString(), foreignProblem["title"].GetString());
|
||||
Assert.Equal(missingProblem["detail"].GetString(), foreignProblem["detail"].GetString());
|
||||
Assert.Equal(missingProblem["type"].GetString(), foreignProblem["type"].GetString());
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
MediaConsumptionRecordEntity? storedRecord = dbContext.MediaConsumptionRecords.FirstOrDefault(record => record.Id == otherRecordId);
|
||||
Assert.NotNull(storedRecord);
|
||||
Assert.Equal(otherUserId, storedRecord!.UserId);
|
||||
Assert.Equal(ConsumptionStatus.InProgress, storedRecord.Status);
|
||||
Assert.Equal(42_000, storedRecord.ConsumedDurationMilliseconds);
|
||||
Assert.Equal(0.4, storedRecord.LastProgressFraction);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumptionRecord_WithSpoofedUserIdQueryParameter_ShouldIgnoreItAndReturnOwnRecord()
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
Guid ownRecordId = await SeedRecordAsync(userId, ConsumptionStatus.InProgress, 10_000, 0.1, 10_000);
|
||||
(Guid otherUserId, _) = await SeedOtherUserAsync();
|
||||
Guid otherRecordId = await SeedRecordAsync(otherUserId, ConsumptionStatus.Completed, 90_000, 1.0, 90_000);
|
||||
string url = $"/api/v1/users/me/consumption-records/{ownRecordId}?userId={otherUserId}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
ConsumptionRecordResponse? record = JsonSerializer.Deserialize<ConsumptionRecordResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(record);
|
||||
Assert.Equal(ownRecordId, record!.Id);
|
||||
Assert.NotEqual(otherRecordId, record.Id);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
MediaConsumptionRecordEntity? storedRecord = dbContext.MediaConsumptionRecords.FirstOrDefault(candidate => candidate.Id == otherRecordId);
|
||||
Assert.NotNull(storedRecord);
|
||||
Assert.Equal(otherUserId, storedRecord!.UserId);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task GetConsumptionRecord_WithSqlInjectionInRecordId_ShouldRejectWithoutLeaking(string maliciousRecordId)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Uri.EscapeDataString(maliciousRecordId)}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
await AssertRejectedInjectionAsync(response, maliciousRecordId, url, "ConsumptionRecordIdCannotBeEmpty");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a test user and authenticates it on <paramref name="client"/>.
|
||||
/// </summary>
|
||||
/// <param name="client">The HTTP client to authenticate.</param>
|
||||
/// <returns>The Id and username of the created user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync(HttpClient client)
|
||||
{
|
||||
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(client);
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
|
||||
/// </summary>
|
||||
/// <returns>The Id and username of the seeded user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid userId = Guid.NewGuid();
|
||||
string username = $"otheruser_{Guid.NewGuid()}";
|
||||
UserEntity user = _userEntityFixture.Create(id: userId, username: username, password: "TestPass123!");
|
||||
dbContext.Users.Add(user);
|
||||
await dbContext.SaveChangesAsync();
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a music library owned by <paramref name="userId"/> and a consumption record of a music track belonging to it.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user that owns the record.</param>
|
||||
/// <param name="status">The state of the seeded session.</param>
|
||||
/// <param name="consumedDurationMilliseconds">The consumed duration of the record, in milliseconds.</param>
|
||||
/// <param name="lastProgressFraction">The last reported progress fraction of the record.</param>
|
||||
/// <param name="lastPositionTimeOffsetMilliseconds">The last known position, in milliseconds.</param>
|
||||
/// <returns>The Id of the seeded consumption record.</returns>
|
||||
private async Task<Guid> SeedRecordAsync(
|
||||
Guid userId,
|
||||
ConsumptionStatus status,
|
||||
long consumedDurationMilliseconds,
|
||||
double lastProgressFraction,
|
||||
long lastPositionTimeOffsetMilliseconds)
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid mediaItemId = Guid.NewGuid();
|
||||
Guid recordId = Guid.NewGuid();
|
||||
dbContext.Libraries.Add(_libraryEntityFixture.Create(id: libraryId, userId: userId, title: "Music Library", libraryType: LibraryType.Music, contentLocations: []));
|
||||
dbContext.MediaConsumptionRecords.Add(_mediaConsumptionRecordEntityFixture.Create(
|
||||
id: recordId,
|
||||
userId: userId,
|
||||
libraryId: libraryId,
|
||||
mediaKind: MediaKind.MusicTrack,
|
||||
mediaItemId: mediaItemId,
|
||||
status: status,
|
||||
startedOnUtc: s_startedOnUtc,
|
||||
lastActivityOnUtc: s_lastActivityOnUtc,
|
||||
consumedDurationMilliseconds: consumedDurationMilliseconds,
|
||||
lastPositionKind: MediaPositionKind.TimeOffset,
|
||||
lastPositionTimeOffsetMilliseconds: lastPositionTimeOffsetMilliseconds,
|
||||
lastProgressFraction: lastProgressFraction,
|
||||
consecutivePagesRead: 0));
|
||||
await dbContext.SaveChangesAsync();
|
||||
return recordId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the problem details document of <paramref name="response"/>.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to read.</param>
|
||||
/// <returns>The parsed problem details fields.</returns>
|
||||
private async Task<Dictionary<string, JsonElement>> ReadProblemDetailsAsync(HttpResponseMessage response)
|
||||
{
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
return problemDetails!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
||||
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
|
||||
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
|
||||
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the shape of a problem details response with the provided status, title, detail, instance and type.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="statusCode">The expected HTTP status code.</param>
|
||||
/// <param name="expectedTitle">The expected problem title.</param>
|
||||
/// <param name="expectedDetail">The expected problem detail.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedType">The expected problem type URI.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertProblemDetailsAsync(HttpResponseMessage response, HttpStatusCode statusCode, string expectedTitle, string expectedDetail, string expectedInstance, string expectedType)
|
||||
{
|
||||
Assert.Equal(statusCode, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal((int)statusCode, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal(expectedType, problemDetails["type"].GetString());
|
||||
Assert.Equal(expectedTitle, problemDetails["title"].GetString());
|
||||
Assert.Equal(expectedDetail, problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that a malicious route value is rejected without disclosing internals or reaching the data access layer.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="payload">The malicious value that was sent.</param>
|
||||
/// <param name="requestPath">The request path the value was sent in.</param>
|
||||
/// <param name="expectedErrorCode">The validation error code expected when the value reaches the validator.</param>
|
||||
private async Task AssertRejectedInjectionAsync(HttpResponseMessage response, string payload, string requestPath, string expectedErrorCode)
|
||||
{
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Assert.NotEqual(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.UnprocessableEntity)
|
||||
{
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
// The instance is the request path with the rejected payload still in place, independent of how the server escapes it.
|
||||
string escapedPayload = Uri.EscapeDataString(payload);
|
||||
int payloadIndex = requestPath.IndexOf(escapedPayload, StringComparison.Ordinal);
|
||||
string? instance = problemDetails["instance"].GetString();
|
||||
Assert.NotNull(instance);
|
||||
Assert.True(payloadIndex >= 0, "The escaped payload must appear in the request path.");
|
||||
Assert.StartsWith(requestPath[..payloadIndex], instance, StringComparison.Ordinal);
|
||||
Assert.EndsWith(requestPath[(payloadIndex + escapedPayload.Length)..], instance, StringComparison.Ordinal);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorCode, errors["General.Validation"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// A payload that carries encoded path separators is rejected by routing before it reaches the validator. That is still a safe rejection, so only it may take this branch.
|
||||
Assert.Contains("/", payload, StringComparison.Ordinal);
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
|
||||
AssertNoSensitiveData(content);
|
||||
Assert.DoesNotContain(payload, content, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the per-user responses are not storable by any intermediary and vary by the authorization header.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
private static void AssertNonCacheable(HttpResponseMessage response)
|
||||
{
|
||||
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
|
||||
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response body carries no credentials, secrets, persistence details or stack traces.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoSensitiveData(string content)
|
||||
{
|
||||
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the serialized consumption record exposes only the fields of its documented response contract.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoDatabaseOnlyFields(string content)
|
||||
{
|
||||
Assert.DoesNotContain("version", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("userId", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the test users created by the test.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
foreach (string username in _seededUsernames)
|
||||
_apiFactory.RemoveTestUser(username);
|
||||
}
|
||||
}
|
||||
+543
@@ -0,0 +1,543 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.GetResumePoint;
|
||||
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.MediaConsumption.GetResumePoint;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <see cref="GetResumePointEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetResumePointEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
{
|
||||
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
|
||||
private const long SEEDED_VERSION = 7;
|
||||
|
||||
private static readonly DateTime s_lastConsumedOnUtc = new(2026, 2, 28, 18, 30, 0, DateTimeKind.Utc);
|
||||
|
||||
private readonly LuminaApiFactory _apiFactory;
|
||||
private readonly HttpClient _client;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
||||
private readonly ArtistEntityFixture _artistEntityFixture = new();
|
||||
private readonly AlbumEntityFixture _albumEntityFixture = new();
|
||||
private readonly TrackEntityFixture _trackEntityFixture = new();
|
||||
private readonly MediaResumePointEntityFixture _mediaResumePointEntityFixture = new();
|
||||
private readonly UserEntityFixture _userEntityFixture = new();
|
||||
private readonly List<string> _seededUsernames = [];
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetResumePointEndpointTests"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiFactory">Injected in-memory API factory.</param>
|
||||
public GetResumePointEndpointTests(LuminaApiFactory apiFactory)
|
||||
{
|
||||
_apiFactory = apiFactory;
|
||||
_client = apiFactory.CreateClient();
|
||||
// A unique X-Forwarded-For isolates rate limiting state per test.
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetResumePoint_WhenUnauthorized_ShouldReturnUnauthorizedResult()
|
||||
{
|
||||
// Arrange
|
||||
string url = $"/api/v1/users/me/media-resume-points/MusicTrack/{Guid.NewGuid()}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
await AssertUnauthorizedAsync(response, url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetResumePoint_WhenSuccessful_ShouldReturnNonCacheableResponseWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(Guid libraryId, Guid trackId) = await SeedLibraryArtistAlbumAndTrackAsync(userId, shouldTrackConsumptionProgress: true);
|
||||
Guid resumePointId = await SeedResumePointAsync(userId, libraryId, trackId, MediaPositionKind.TimeOffset, 125_500, null, 0.42);
|
||||
string url = $"/api/v1/users/me/media-resume-points/MusicTrack/{trackId}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
AssertNonCacheable(response);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
AssertNoSensitiveData(content);
|
||||
AssertNoDatabaseOnlyFields(content);
|
||||
|
||||
ResumePointResponse? resumePoint = JsonSerializer.Deserialize<ResumePointResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(resumePoint);
|
||||
Assert.Equal(resumePointId, resumePoint!.Id);
|
||||
Assert.Equal(libraryId, resumePoint.LibraryId);
|
||||
Assert.Equal(trackId, resumePoint.MediaItemId);
|
||||
Assert.Equal(MediaKind.MusicTrack, resumePoint.MediaKind);
|
||||
Assert.Equal(MediaPositionKind.TimeOffset, resumePoint.LastPosition.Kind);
|
||||
Assert.Equal(125.5, resumePoint.LastPosition.TimeOffsetSeconds);
|
||||
Assert.Null(resumePoint.LastPosition.Location);
|
||||
Assert.Equal(0.42, resumePoint.LastProgressFraction);
|
||||
Assert.Equal(s_lastConsumedOnUtc, resumePoint.LastConsumedOnUtc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetResumePoint_WhenResumePointDoesNotExist_ShouldReturnNonCacheableNotFoundWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/users/me/media-resume-points/MusicTrack/{Guid.NewGuid()}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
string content = await AssertProblemDetailsAsync(
|
||||
response,
|
||||
HttpStatusCode.NotFound,
|
||||
"General.NotFound",
|
||||
"MediaResumePointNotFound",
|
||||
url,
|
||||
"https://tools.ietf.org/html/rfc9110#section-15.5.5");
|
||||
AssertNonCacheable(response);
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetResumePoint_WhenAnotherUsersResumePointExistsForTheSameItem_ShouldReturnNotFoundAndKeepOtherRow()
|
||||
{
|
||||
// Arrange
|
||||
(Guid otherUserId, _) = await SeedOtherUserAsync();
|
||||
Guid mediaItemId = Guid.NewGuid();
|
||||
Guid otherResumePointId = await SeedResumePointAsync(otherUserId, Guid.NewGuid(), mediaItemId, MediaPositionKind.TimeOffset, 30_000, null, 0.3);
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/users/me/media-resume-points/MusicTrack/{mediaItemId}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
// The response must be indistinguishable from the one for a genuinely missing resume point.
|
||||
Dictionary<string, JsonElement> foreignProblem = await ReadProblemDetailsAsync(response);
|
||||
HttpResponseMessage missingResponse = await _client.GetAsync($"/api/v1/users/me/media-resume-points/MusicTrack/{Guid.NewGuid()}");
|
||||
Dictionary<string, JsonElement> missingProblem = await ReadProblemDetailsAsync(missingResponse);
|
||||
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
Assert.Equal(HttpStatusCode.NotFound, missingResponse.StatusCode);
|
||||
Assert.Equal(missingProblem["status"].GetInt32(), foreignProblem["status"].GetInt32());
|
||||
Assert.Equal(missingProblem["title"].GetString(), foreignProblem["title"].GetString());
|
||||
Assert.Equal(missingProblem["detail"].GetString(), foreignProblem["detail"].GetString());
|
||||
Assert.Equal(missingProblem["type"].GetString(), foreignProblem["type"].GetString());
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
MediaResumePointEntity? storedResumePoint = dbContext.MediaResumePoints.FirstOrDefault(candidate => candidate.Id == otherResumePointId);
|
||||
Assert.NotNull(storedResumePoint);
|
||||
Assert.Equal(otherUserId, storedResumePoint!.UserId);
|
||||
Assert.Equal(mediaItemId, storedResumePoint.MediaItemId);
|
||||
Assert.Equal(30_000, storedResumePoint.LastPositionTimeOffsetMilliseconds);
|
||||
Assert.Equal(0.3, storedResumePoint.LastProgressFraction);
|
||||
Assert.Equal(SEEDED_VERSION, storedResumePoint.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetResumePoint_WithSpoofedUserIdQueryParameter_ShouldIgnoreItAndReturnOwnResumePoint()
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(Guid libraryId, Guid trackId) = await SeedLibraryArtistAlbumAndTrackAsync(userId, shouldTrackConsumptionProgress: true);
|
||||
Guid ownResumePointId = await SeedResumePointAsync(userId, libraryId, trackId, MediaPositionKind.TimeOffset, 125_500, null, 0.42);
|
||||
(Guid otherUserId, _) = await SeedOtherUserAsync();
|
||||
Guid otherResumePointId = await SeedResumePointAsync(otherUserId, Guid.NewGuid(), Guid.NewGuid(), MediaPositionKind.TimeOffset, 60_000, null, 0.6);
|
||||
string url = $"/api/v1/users/me/media-resume-points/MusicTrack/{trackId}?userId={otherUserId}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
ResumePointResponse? resumePoint = JsonSerializer.Deserialize<ResumePointResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(resumePoint);
|
||||
Assert.Equal(ownResumePointId, resumePoint!.Id);
|
||||
Assert.NotEqual(otherResumePointId, resumePoint.Id);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
MediaResumePointEntity? storedResumePoint = dbContext.MediaResumePoints.FirstOrDefault(candidate => candidate.Id == otherResumePointId);
|
||||
Assert.NotNull(storedResumePoint);
|
||||
Assert.Equal(otherUserId, storedResumePoint!.UserId);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("0")] // MediaKind.None, a defined enumeration member.
|
||||
[InlineData("1")] // MediaKind.MusicTrack, a defined enumeration member.
|
||||
public async Task GetResumePoint_WhenMediaKindIsNumeric_ShouldReturnUnsupportedMediaKindAndServeNoData(string numericMediaKind)
|
||||
{
|
||||
// Arrange
|
||||
// A numeric route value that parses as a defined enumeration member must not be treated as a media kind, so the stored resume point is never served.
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(Guid libraryId, Guid trackId) = await SeedLibraryArtistAlbumAndTrackAsync(userId, shouldTrackConsumptionProgress: true);
|
||||
Guid resumePointId = await SeedResumePointAsync(userId, libraryId, trackId, MediaPositionKind.TimeOffset, 125_500, null, 0.42);
|
||||
string url = $"/api/v1/users/me/media-resume-points/{numericMediaKind}/{trackId}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
string content = await AssertValidationProblemAsync(response, url, "UnsupportedMediaKind");
|
||||
AssertNoSensitiveData(content);
|
||||
Assert.DoesNotContain(resumePointId.ToString(), content, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Assert.True(dbContext.MediaResumePoints.Any(candidate => candidate.Id == resumePointId));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaResumePoints--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaResumePoints")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task GetResumePoint_WithSqlInjectionInMediaKind_ShouldRejectWithoutLeaking(string maliciousMediaKind)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/users/me/media-resume-points/{Uri.EscapeDataString(maliciousMediaKind)}/{Guid.NewGuid()}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
await AssertRejectedInjectionAsync(response, maliciousMediaKind, url, "UnsupportedMediaKind");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaResumePoints--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaResumePoints")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task GetResumePoint_WithSqlInjectionInMediaItemId_ShouldRejectWithoutLeaking(string maliciousMediaItemId)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/users/me/media-resume-points/MusicTrack/{Uri.EscapeDataString(maliciousMediaItemId)}";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
await AssertRejectedInjectionAsync(response, maliciousMediaItemId, url, "MediaItemIdCannotBeEmpty");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a test user and authenticates it on <paramref name="client"/>.
|
||||
/// </summary>
|
||||
/// <param name="client">The HTTP client to authenticate.</param>
|
||||
/// <returns>The Id and username of the created user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync(HttpClient client)
|
||||
{
|
||||
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(client);
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
|
||||
/// </summary>
|
||||
/// <returns>The Id and username of the seeded user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid userId = Guid.NewGuid();
|
||||
string username = $"otheruser_{Guid.NewGuid()}";
|
||||
UserEntity user = _userEntityFixture.Create(id: userId, username: username, password: "TestPass123!");
|
||||
dbContext.Users.Add(user);
|
||||
await dbContext.SaveChangesAsync();
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a music library owned by <paramref name="userId"/>, an artist of that library, an album of that artist and a track of that album.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user that owns the library.</param>
|
||||
/// <param name="shouldTrackConsumptionProgress">Whether the library tracks the consumption progress of its items.</param>
|
||||
/// <returns>The Ids of the seeded library and track.</returns>
|
||||
private async Task<(Guid LibraryId, Guid TrackId)> SeedLibraryArtistAlbumAndTrackAsync(Guid userId, bool shouldTrackConsumptionProgress)
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid artistId = Guid.NewGuid();
|
||||
Guid albumId = Guid.NewGuid();
|
||||
Guid trackId = Guid.NewGuid();
|
||||
dbContext.Libraries.Add(_libraryEntityFixture.Create(
|
||||
id: libraryId,
|
||||
userId: userId,
|
||||
title: "Music Library",
|
||||
libraryType: LibraryType.Music,
|
||||
contentLocations: [],
|
||||
shouldTrackConsumptionProgress: shouldTrackConsumptionProgress,
|
||||
completionThresholdPercentage: shouldTrackConsumptionProgress ? 100 : null));
|
||||
dbContext.Artists.Add(_artistEntityFixture.Create(id: artistId, libraryId: libraryId, name: "Queen"));
|
||||
dbContext.Albums.Add(_albumEntityFixture.Create(id: albumId, artistId: artistId, libraryId: libraryId, title: "A Night at the Opera"));
|
||||
dbContext.Tracks.Add(_trackEntityFixture.Create(id: trackId, albumId: albumId, libraryId: libraryId, path: $"/music/queen/{trackId:N}.flac", title: "Bohemian Rhapsody", includeMetadata: false));
|
||||
await dbContext.SaveChangesAsync();
|
||||
return (libraryId, trackId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a resume point of a music track.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user that owns the resume point.</param>
|
||||
/// <param name="libraryId">The Id of the library that contains the media item.</param>
|
||||
/// <param name="mediaItemId">The Id of the media item to resume.</param>
|
||||
/// <param name="lastPositionKind">The kind of the position at which the item can be resumed.</param>
|
||||
/// <param name="lastPositionTimeOffsetMilliseconds">The position, in milliseconds, when it is a time offset.</param>
|
||||
/// <param name="lastPositionLocation">The opaque locator, when it is a location.</param>
|
||||
/// <param name="lastProgressFraction">The progress fraction at which the item can be resumed.</param>
|
||||
/// <returns>The Id of the seeded resume point.</returns>
|
||||
private async Task<Guid> SeedResumePointAsync(
|
||||
Guid userId,
|
||||
Guid libraryId,
|
||||
Guid mediaItemId,
|
||||
MediaPositionKind lastPositionKind,
|
||||
long? lastPositionTimeOffsetMilliseconds,
|
||||
string? lastPositionLocation,
|
||||
double lastProgressFraction)
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid resumePointId = Guid.NewGuid();
|
||||
dbContext.MediaResumePoints.Add(_mediaResumePointEntityFixture.Create(
|
||||
id: resumePointId,
|
||||
userId: userId,
|
||||
libraryId: libraryId,
|
||||
mediaKind: MediaKind.MusicTrack,
|
||||
mediaItemId: mediaItemId,
|
||||
lastPositionKind: lastPositionKind,
|
||||
lastPositionTimeOffsetMilliseconds: lastPositionTimeOffsetMilliseconds,
|
||||
lastPositionLocation: lastPositionLocation,
|
||||
lastProgressFraction: lastProgressFraction,
|
||||
lastConsumedOnUtc: s_lastConsumedOnUtc,
|
||||
version: SEEDED_VERSION));
|
||||
await dbContext.SaveChangesAsync();
|
||||
return resumePointId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the problem details document of <paramref name="response"/>.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to read.</param>
|
||||
/// <returns>The parsed problem details fields.</returns>
|
||||
private async Task<Dictionary<string, JsonElement>> ReadProblemDetailsAsync(HttpResponseMessage response)
|
||||
{
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
return problemDetails!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
||||
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
|
||||
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
|
||||
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the shape of a problem details response with the provided status, title, detail, instance and type.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="statusCode">The expected HTTP status code.</param>
|
||||
/// <param name="expectedTitle">The expected problem title.</param>
|
||||
/// <param name="expectedDetail">The expected problem detail.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedType">The expected problem type URI.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertProblemDetailsAsync(HttpResponseMessage response, HttpStatusCode statusCode, string expectedTitle, string expectedDetail, string expectedInstance, string expectedType)
|
||||
{
|
||||
Assert.Equal(statusCode, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal((int)statusCode, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal(expectedType, problemDetails["type"].GetString());
|
||||
Assert.Equal(expectedTitle, problemDetails["title"].GetString());
|
||||
Assert.Equal(expectedDetail, problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the standard validation problem of the get resume point route.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedErrorDescription">The error description expected under the validation errors.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertValidationProblemAsync(HttpResponseMessage response, string expectedInstance, string expectedErrorDescription)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
Assert.Equal("OneOrMoreValidationErrorsOccurred", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorDescription, errors["General.Validation"]);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that a malicious route value is rejected without disclosing internals or reaching the data access layer.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="payload">The malicious value that was sent.</param>
|
||||
/// <param name="requestPath">The request path the value was sent in.</param>
|
||||
/// <param name="expectedErrorCode">The validation error code expected when the value reaches the validator.</param>
|
||||
private async Task AssertRejectedInjectionAsync(HttpResponseMessage response, string payload, string requestPath, string expectedErrorCode)
|
||||
{
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Assert.NotEqual(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.UnprocessableEntity)
|
||||
{
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
// The instance is the request path with the rejected payload still in place, independent of how the server escapes it.
|
||||
string escapedPayload = Uri.EscapeDataString(payload);
|
||||
int payloadIndex = requestPath.IndexOf(escapedPayload, StringComparison.Ordinal);
|
||||
string? instance = problemDetails["instance"].GetString();
|
||||
Assert.NotNull(instance);
|
||||
Assert.True(payloadIndex >= 0, "The escaped payload must appear in the request path.");
|
||||
Assert.StartsWith(requestPath[..payloadIndex], instance, StringComparison.Ordinal);
|
||||
Assert.EndsWith(requestPath[(payloadIndex + escapedPayload.Length)..], instance, StringComparison.Ordinal);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorCode, errors["General.Validation"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// A payload that carries encoded path separators is rejected by routing before it reaches the validator. That is still a safe rejection, so only it may take this branch.
|
||||
Assert.Contains("/", payload, StringComparison.Ordinal);
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
|
||||
AssertNoSensitiveData(content);
|
||||
Assert.DoesNotContain(payload, content, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the per-user responses are not storable by any intermediary and vary by the authorization header.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
private static void AssertNonCacheable(HttpResponseMessage response)
|
||||
{
|
||||
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
|
||||
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response body carries no credentials, secrets, persistence details or stack traces.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoSensitiveData(string content)
|
||||
{
|
||||
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the serialized resume point exposes only the fields of its documented response contract.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoDatabaseOnlyFields(string content)
|
||||
{
|
||||
Assert.DoesNotContain("version", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("userId", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the test users created by the test.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
foreach (string username in _seededUsernames)
|
||||
_apiFactory.RemoveTestUser(username);
|
||||
}
|
||||
}
|
||||
+514
@@ -0,0 +1,514 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Contracts.Fixtures.Core.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.ReportConsumptionProgress;
|
||||
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.MediaConsumption.ReportConsumptionProgress;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <see cref="ReportConsumptionProgressEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ReportConsumptionProgressEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
{
|
||||
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
|
||||
private const long SEEDED_VERSION = 7;
|
||||
|
||||
private static readonly DateTime s_startedOnUtc = new(2026, 3, 1, 10, 0, 0, DateTimeKind.Utc);
|
||||
private static readonly DateTime s_lastActivityOnUtc = new(2026, 3, 1, 10, 5, 0, DateTimeKind.Utc);
|
||||
|
||||
private readonly LuminaApiFactory _apiFactory;
|
||||
private readonly HttpClient _client;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
||||
private readonly MediaConsumptionRecordEntityFixture _mediaConsumptionRecordEntityFixture = new();
|
||||
private readonly MediaPositionRequestFixture _mediaPositionRequestFixture = new();
|
||||
private readonly ReportConsumptionProgressRequestFixture _reportConsumptionProgressRequestFixture = new();
|
||||
private readonly UserEntityFixture _userEntityFixture = new();
|
||||
private readonly List<string> _seededUsernames = [];
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReportConsumptionProgressEndpointTests"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiFactory">Injected in-memory API factory.</param>
|
||||
public ReportConsumptionProgressEndpointTests(LuminaApiFactory apiFactory)
|
||||
{
|
||||
_apiFactory = apiFactory;
|
||||
_client = apiFactory.CreateClient();
|
||||
// A unique X-Forwarded-For isolates rate limiting state per test.
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReportConsumptionProgress_WhenUnauthorized_ShouldReturnUnauthorizedResult()
|
||||
{
|
||||
// Arrange
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}/progress";
|
||||
ReportConsumptionProgressRequest request = _reportConsumptionProgressRequestFixture.Create(includePosition: false, progressFraction: 0.5, consumedDurationInSeconds: 10);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PutAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
await AssertUnauthorizedAsync(response, url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReportConsumptionProgress_WhenSuccessful_ShouldReturnNonCacheableResponseWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(_, Guid recordId) = await SeedRecordAsync(userId, MediaKind.MusicTrack, ConsumptionStatus.InProgress, 0, 0.1, MediaPositionKind.TimeOffset, 5_000, null);
|
||||
ReportConsumptionProgressRequest request = _reportConsumptionProgressRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: nameof(MediaPositionKind.TimeOffset), timeOffsetSeconds: 12.5, includeLocation: false),
|
||||
progressFraction: 0.35,
|
||||
consumedDurationInSeconds: 45);
|
||||
string url = $"/api/v1/users/me/consumption-records/{recordId}/progress";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PutAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
AssertNonCacheable(response);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
AssertNoSensitiveData(content);
|
||||
AssertNoDatabaseOnlyFields(content);
|
||||
|
||||
ConsumptionRecordResponse? record = JsonSerializer.Deserialize<ConsumptionRecordResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(record);
|
||||
Assert.Equal(recordId, record!.Id);
|
||||
Assert.Equal(ConsumptionStatus.InProgress, record.Status);
|
||||
Assert.Equal(45.0, record.ConsumedDurationInSeconds);
|
||||
Assert.Equal(0.35, record.LastProgressFraction);
|
||||
Assert.NotNull(record.LastPosition);
|
||||
Assert.Equal(MediaPositionKind.TimeOffset, record.LastPosition!.Kind);
|
||||
Assert.Equal(12.5, record.LastPosition.TimeOffsetSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReportConsumptionProgress_WhenRecordDoesNotExist_ShouldReturnNonCacheableNotFoundWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
ReportConsumptionProgressRequest request = _reportConsumptionProgressRequestFixture.Create(includePosition: false, progressFraction: 0.5, consumedDurationInSeconds: 10);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}/progress";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PutAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
string content = await AssertProblemDetailsAsync(
|
||||
response,
|
||||
HttpStatusCode.NotFound,
|
||||
"General.NotFound",
|
||||
"ConsumptionRecordNotFound",
|
||||
url,
|
||||
"https://tools.ietf.org/html/rfc9110#section-15.5.5");
|
||||
AssertNonCacheable(response);
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReportConsumptionProgress_WhenAnotherUsersRecordIsTargeted_ShouldReturnNotFoundAndLeaveRecordUnchanged()
|
||||
{
|
||||
// Arrange
|
||||
(Guid otherUserId, _) = await SeedOtherUserAsync();
|
||||
(_, Guid otherRecordId) = await SeedRecordAsync(otherUserId, MediaKind.MusicTrack, ConsumptionStatus.InProgress, 15_000, 0.2, MediaPositionKind.TimeOffset, 15_000, null);
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
ReportConsumptionProgressRequest request = _reportConsumptionProgressRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: nameof(MediaPositionKind.TimeOffset), timeOffsetSeconds: 90, includeLocation: false),
|
||||
progressFraction: 0.9,
|
||||
consumedDurationInSeconds: 90);
|
||||
string url = $"/api/v1/users/me/consumption-records/{otherRecordId}/progress";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PutAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
string content = await AssertProblemDetailsAsync(
|
||||
response,
|
||||
HttpStatusCode.NotFound,
|
||||
"General.NotFound",
|
||||
"ConsumptionRecordNotFound",
|
||||
url,
|
||||
"https://tools.ietf.org/html/rfc9110#section-15.5.5");
|
||||
AssertNoSensitiveData(content);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
MediaConsumptionRecordEntity? storedRecord = dbContext.MediaConsumptionRecords.FirstOrDefault(record => record.Id == otherRecordId);
|
||||
Assert.NotNull(storedRecord);
|
||||
Assert.Equal(ConsumptionStatus.InProgress, storedRecord!.Status);
|
||||
Assert.Null(storedRecord.EndedOnUtc);
|
||||
Assert.Equal(15_000, storedRecord.ConsumedDurationMilliseconds);
|
||||
Assert.Equal(0.2, storedRecord.LastProgressFraction);
|
||||
Assert.Equal(MediaPositionKind.TimeOffset, storedRecord.LastPositionKind);
|
||||
Assert.Equal(15_000, storedRecord.LastPositionTimeOffsetMilliseconds);
|
||||
Assert.Equal(SEEDED_VERSION, storedRecord.Version);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task ReportConsumptionProgress_WithSqlInjectionInRecordId_ShouldRejectWithoutLeaking(string maliciousRecordId)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
ReportConsumptionProgressRequest request = _reportConsumptionProgressRequestFixture.Create(includePosition: false, progressFraction: 0.5, consumedDurationInSeconds: 10);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Uri.EscapeDataString(maliciousRecordId)}/progress";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PutAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
await AssertRejectedInjectionAsync(response, maliciousRecordId, url, "ConsumptionRecordIdCannotBeEmpty");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task ReportConsumptionProgress_WithUnknownPositionKind_ShouldRejectWithoutLeaking(string maliciousPositionKind)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
ReportConsumptionProgressRequest request = _reportConsumptionProgressRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: maliciousPositionKind),
|
||||
progressFraction: 0.5,
|
||||
consumedDurationInSeconds: 10);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}/progress";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PutAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
string content = await AssertValidationProblemAsync(response, url, "InvalidMediaPosition");
|
||||
AssertNoSensitiveData(content);
|
||||
Assert.DoesNotContain(maliciousPositionKind, content, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task ReportConsumptionProgress_WithOpaquePositionLocation_ShouldStoreItVerbatimWithoutLeaking(string location)
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(_, Guid recordId) = await SeedRecordAsync(userId, MediaKind.Book, ConsumptionStatus.InProgress, 0, 0.1, MediaPositionKind.Location, null, "chapter-1");
|
||||
ReportConsumptionProgressRequest request = _reportConsumptionProgressRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: nameof(MediaPositionKind.Location), location: location, includeTimeOffsetSeconds: false),
|
||||
progressFraction: 0.4,
|
||||
consumedDurationInSeconds: 30);
|
||||
string url = $"/api/v1/users/me/consumption-records/{recordId}/progress";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PutAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
AssertNoSensitiveData(content);
|
||||
|
||||
ConsumptionRecordResponse? record = JsonSerializer.Deserialize<ConsumptionRecordResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(record);
|
||||
Assert.NotNull(record!.LastPosition);
|
||||
Assert.Equal(MediaPositionKind.Location, record.LastPosition!.Kind);
|
||||
Assert.Equal(location, record.LastPosition.Location);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
MediaConsumptionRecordEntity? storedRecord = dbContext.MediaConsumptionRecords.FirstOrDefault(candidate => candidate.Id == recordId);
|
||||
Assert.NotNull(storedRecord);
|
||||
Assert.Equal(location, storedRecord!.LastPositionLocation);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a test user and authenticates it on <paramref name="client"/>.
|
||||
/// </summary>
|
||||
/// <param name="client">The HTTP client to authenticate.</param>
|
||||
/// <returns>The Id and username of the created user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync(HttpClient client)
|
||||
{
|
||||
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(client);
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
|
||||
/// </summary>
|
||||
/// <returns>The Id and username of the seeded user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid userId = Guid.NewGuid();
|
||||
string username = $"otheruser_{Guid.NewGuid()}";
|
||||
UserEntity user = _userEntityFixture.Create(id: userId, username: username, password: "TestPass123!");
|
||||
dbContext.Users.Add(user);
|
||||
await dbContext.SaveChangesAsync();
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a media library owned by <paramref name="userId"/> and a consumption record of a media item belonging to it.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user that owns the record.</param>
|
||||
/// <param name="mediaKind">The kind of media that the record consumes.</param>
|
||||
/// <param name="status">The state of the seeded session.</param>
|
||||
/// <param name="consumedDurationMilliseconds">The consumed duration of the record, in milliseconds.</param>
|
||||
/// <param name="lastProgressFraction">The last reported progress fraction of the record.</param>
|
||||
/// <param name="lastPositionKind">The kind of the last known position.</param>
|
||||
/// <param name="lastPositionTimeOffsetMilliseconds">The last known position, in milliseconds, when it is a time offset.</param>
|
||||
/// <param name="lastPositionLocation">The last known opaque locator, when it is a location.</param>
|
||||
/// <returns>The Ids of the seeded library and consumption record.</returns>
|
||||
private async Task<(Guid LibraryId, Guid RecordId)> SeedRecordAsync(
|
||||
Guid userId,
|
||||
MediaKind mediaKind,
|
||||
ConsumptionStatus status,
|
||||
long consumedDurationMilliseconds,
|
||||
double lastProgressFraction,
|
||||
MediaPositionKind lastPositionKind,
|
||||
long? lastPositionTimeOffsetMilliseconds,
|
||||
string? lastPositionLocation)
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid mediaItemId = Guid.NewGuid();
|
||||
Guid recordId = Guid.NewGuid();
|
||||
dbContext.Libraries.Add(_libraryEntityFixture.Create(
|
||||
id: libraryId,
|
||||
userId: userId,
|
||||
title: "Media Library",
|
||||
libraryType: mediaKind == MediaKind.Book ? LibraryType.EBook : LibraryType.Music,
|
||||
contentLocations: [],
|
||||
shouldTrackConsumptionProgress: false));
|
||||
dbContext.MediaConsumptionRecords.Add(_mediaConsumptionRecordEntityFixture.Create(
|
||||
id: recordId,
|
||||
userId: userId,
|
||||
libraryId: libraryId,
|
||||
mediaKind: mediaKind,
|
||||
mediaItemId: mediaItemId,
|
||||
status: status,
|
||||
startedOnUtc: s_startedOnUtc,
|
||||
lastActivityOnUtc: s_lastActivityOnUtc,
|
||||
consumedDurationMilliseconds: consumedDurationMilliseconds,
|
||||
lastPositionKind: lastPositionKind,
|
||||
lastPositionTimeOffsetMilliseconds: lastPositionTimeOffsetMilliseconds,
|
||||
lastPositionLocation: lastPositionLocation,
|
||||
lastProgressFraction: lastProgressFraction,
|
||||
consecutivePagesRead: 0,
|
||||
version: SEEDED_VERSION));
|
||||
await dbContext.SaveChangesAsync();
|
||||
return (libraryId, recordId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
||||
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
|
||||
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
|
||||
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the shape of a problem details response with the provided status, title, detail, instance and type.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="statusCode">The expected HTTP status code.</param>
|
||||
/// <param name="expectedTitle">The expected problem title.</param>
|
||||
/// <param name="expectedDetail">The expected problem detail.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedType">The expected problem type URI.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertProblemDetailsAsync(HttpResponseMessage response, HttpStatusCode statusCode, string expectedTitle, string expectedDetail, string expectedInstance, string expectedType)
|
||||
{
|
||||
Assert.Equal(statusCode, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal((int)statusCode, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal(expectedType, problemDetails["type"].GetString());
|
||||
Assert.Equal(expectedTitle, problemDetails["title"].GetString());
|
||||
Assert.Equal(expectedDetail, problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the standard validation problem of the report consumption progress route.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedErrorDescription">The error description expected under the validation errors.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertValidationProblemAsync(HttpResponseMessage response, string expectedInstance, string expectedErrorDescription)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
Assert.Equal("OneOrMoreValidationErrorsOccurred", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorDescription, errors["General.Validation"]);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that a malicious route value is rejected without disclosing internals or reaching the data access layer.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="payload">The malicious value that was sent.</param>
|
||||
/// <param name="requestPath">The request path the value was sent in.</param>
|
||||
/// <param name="expectedErrorCode">The validation error code expected when the value reaches the validator.</param>
|
||||
private async Task AssertRejectedInjectionAsync(HttpResponseMessage response, string payload, string requestPath, string expectedErrorCode)
|
||||
{
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Assert.NotEqual(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.UnprocessableEntity)
|
||||
{
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
// The instance is the request path with the rejected payload still in place, independent of how the server escapes it.
|
||||
string escapedPayload = Uri.EscapeDataString(payload);
|
||||
int payloadIndex = requestPath.IndexOf(escapedPayload, StringComparison.Ordinal);
|
||||
string? instance = problemDetails["instance"].GetString();
|
||||
Assert.NotNull(instance);
|
||||
Assert.True(payloadIndex >= 0, "The escaped payload must appear in the request path.");
|
||||
Assert.StartsWith(requestPath[..payloadIndex], instance, StringComparison.Ordinal);
|
||||
Assert.EndsWith(requestPath[(payloadIndex + escapedPayload.Length)..], instance, StringComparison.Ordinal);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorCode, errors["General.Validation"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// A payload that carries encoded path separators is rejected by routing before it reaches the validator. That is still a safe rejection, so only it may take this branch.
|
||||
Assert.Contains("/", payload, StringComparison.Ordinal);
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
|
||||
AssertNoSensitiveData(content);
|
||||
Assert.DoesNotContain(payload, content, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the per-user responses are not storable by any intermediary and vary by the authorization header.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
private static void AssertNonCacheable(HttpResponseMessage response)
|
||||
{
|
||||
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
|
||||
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response body carries no credentials, secrets, persistence details or stack traces.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoSensitiveData(string content)
|
||||
{
|
||||
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the serialized consumption record exposes only the fields of its documented response contract.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoDatabaseOnlyFields(string content)
|
||||
{
|
||||
Assert.DoesNotContain("version", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("userId", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the test users created by the test.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
foreach (string username in _seededUsernames)
|
||||
_apiFactory.RemoveTestUser(username);
|
||||
}
|
||||
}
|
||||
+467
@@ -0,0 +1,467 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.StartConsumption;
|
||||
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.MediaConsumption.StartConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <see cref="StartConsumptionEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StartConsumptionEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
{
|
||||
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
|
||||
|
||||
private readonly LuminaApiFactory _apiFactory;
|
||||
private readonly HttpClient _client;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
||||
private readonly ArtistEntityFixture _artistEntityFixture = new();
|
||||
private readonly AlbumEntityFixture _albumEntityFixture = new();
|
||||
private readonly TrackEntityFixture _trackEntityFixture = new();
|
||||
private readonly UserEntityFixture _userEntityFixture = new();
|
||||
private readonly List<string> _seededUsernames = [];
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StartConsumptionEndpointTests"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiFactory">Injected in-memory API factory.</param>
|
||||
public StartConsumptionEndpointTests(LuminaApiFactory apiFactory)
|
||||
{
|
||||
_apiFactory = apiFactory;
|
||||
_client = apiFactory.CreateClient();
|
||||
// A unique X-Forwarded-For isolates rate limiting state per test.
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StartConsumption_WhenUnauthorized_ShouldReturnUnauthorizedResult()
|
||||
{
|
||||
// Arrange
|
||||
string url = $"/api/v1/libraries/{Guid.NewGuid()}/media-items/MusicTrack/{Guid.NewGuid()}/consumption-records";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsync(url, null);
|
||||
|
||||
// Assert
|
||||
await AssertUnauthorizedAsync(response, url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StartConsumption_WhenSuccessful_ShouldReturnNonCacheableResponseWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(Guid libraryId, Guid trackId) = await SeedLibraryArtistAlbumAndTrackAsync(userId);
|
||||
string url = $"/api/v1/libraries/{libraryId}/media-items/MusicTrack/{trackId}/consumption-records";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsync(url, null);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
AssertNonCacheable(response);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
AssertNoSensitiveData(content);
|
||||
AssertNoDatabaseOnlyFields(content);
|
||||
|
||||
ConsumptionRecordResponse? record = JsonSerializer.Deserialize<ConsumptionRecordResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(record);
|
||||
Assert.NotEqual(Guid.Empty, record!.Id);
|
||||
Assert.Equal(libraryId, record.LibraryId);
|
||||
Assert.Equal(trackId, record.MediaItemId);
|
||||
Assert.Equal(MediaKind.MusicTrack, record.MediaKind);
|
||||
Assert.Equal(ConsumptionStatus.InProgress, record.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StartConsumption_WhenMediaItemDoesNotExist_ShouldReturnNonCacheableNotFoundWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
Guid libraryId = await SeedLibraryAsync(userId);
|
||||
string url = $"/api/v1/libraries/{libraryId}/media-items/MusicTrack/{Guid.NewGuid()}/consumption-records";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsync(url, null);
|
||||
|
||||
// Assert
|
||||
string content = await AssertProblemDetailsAsync(
|
||||
response,
|
||||
HttpStatusCode.NotFound,
|
||||
"General.NotFound",
|
||||
"TrackNotFound",
|
||||
url,
|
||||
"https://tools.ietf.org/html/rfc9110#section-15.5.5");
|
||||
AssertNonCacheable(response);
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StartConsumption_WhenLibraryBelongsToAnotherUser_ShouldReturnForbiddenAndCreateNoRecord()
|
||||
{
|
||||
// Arrange
|
||||
(Guid otherUserId, _) = await SeedOtherUserAsync();
|
||||
(Guid otherLibraryId, Guid otherTrackId) = await SeedLibraryArtistAlbumAndTrackAsync(otherUserId);
|
||||
(Guid attackerId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/libraries/{otherLibraryId}/media-items/MusicTrack/{otherTrackId}/consumption-records";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsync(url, null);
|
||||
|
||||
// Assert
|
||||
string content = await AssertProblemDetailsAsync(
|
||||
response,
|
||||
HttpStatusCode.Forbidden,
|
||||
"General.Unauthorized",
|
||||
"NotAuthorized",
|
||||
url,
|
||||
"https://tools.ietf.org/html/rfc9110#section-15.5.4");
|
||||
AssertNonCacheable(response);
|
||||
AssertNoSensitiveData(content);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Assert.False(dbContext.MediaConsumptionRecords.Any(record => record.UserId == attackerId));
|
||||
Assert.Equal(otherUserId, dbContext.Libraries.First(library => library.Id == otherLibraryId).UserId);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("0")] // MediaKind.None, a defined enumeration member.
|
||||
[InlineData("1")] // MediaKind.MusicTrack, a defined enumeration member.
|
||||
public async Task StartConsumption_WhenMediaKindIsNumeric_ShouldReturnUnsupportedMediaKindAndCreateNoRecord(string numericMediaKind)
|
||||
{
|
||||
// Arrange
|
||||
// Only the member names are accepted as a media kind, so a numeric route value that parses as a defined enumeration member must be rejected before any record is created.
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(Guid libraryId, Guid trackId) = await SeedLibraryArtistAlbumAndTrackAsync(userId);
|
||||
string url = $"/api/v1/libraries/{libraryId}/media-items/{numericMediaKind}/{trackId}/consumption-records";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsync(url, null);
|
||||
|
||||
// Assert
|
||||
string content = await AssertValidationProblemAsync(response, url, "UnsupportedMediaKind");
|
||||
AssertNoSensitiveData(content);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Assert.False(dbContext.MediaConsumptionRecords.Any(record => record.UserId == userId));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task StartConsumption_WithSqlInjectionInLibraryId_ShouldRejectWithoutLeaking(string maliciousLibraryId)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/libraries/{Uri.EscapeDataString(maliciousLibraryId)}/media-items/MusicTrack/{Guid.NewGuid()}/consumption-records";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsync(url, null);
|
||||
|
||||
// Assert
|
||||
await AssertRejectedInjectionAsync(response, maliciousLibraryId, url, "LibraryIdCannotBeEmpty");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task StartConsumption_WithSqlInjectionInMediaKind_ShouldRejectWithoutLeaking(string maliciousMediaKind)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/libraries/{Guid.NewGuid()}/media-items/{Uri.EscapeDataString(maliciousMediaKind)}/{Guid.NewGuid()}/consumption-records";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsync(url, null);
|
||||
|
||||
// Assert
|
||||
await AssertRejectedInjectionAsync(response, maliciousMediaKind, url, "UnsupportedMediaKind");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task StartConsumption_WithSqlInjectionInMediaItemId_ShouldRejectWithoutLeaking(string maliciousMediaItemId)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
string url = $"/api/v1/libraries/{Guid.NewGuid()}/media-items/MusicTrack/{Uri.EscapeDataString(maliciousMediaItemId)}/consumption-records";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsync(url, null);
|
||||
|
||||
// Assert
|
||||
await AssertRejectedInjectionAsync(response, maliciousMediaItemId, url, "MediaItemIdCannotBeEmpty");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a test user and authenticates it on <paramref name="client"/>.
|
||||
/// </summary>
|
||||
/// <param name="client">The HTTP client to authenticate.</param>
|
||||
/// <returns>The Id and username of the created user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync(HttpClient client)
|
||||
{
|
||||
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(client);
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
|
||||
/// </summary>
|
||||
/// <returns>The Id and username of the seeded user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid userId = Guid.NewGuid();
|
||||
string username = $"otheruser_{Guid.NewGuid()}";
|
||||
UserEntity user = _userEntityFixture.Create(id: userId, username: username, password: "TestPass123!");
|
||||
dbContext.Users.Add(user);
|
||||
await dbContext.SaveChangesAsync();
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a music library owned by <paramref name="userId"/>.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user that owns the library.</param>
|
||||
/// <returns>The Id of the seeded library.</returns>
|
||||
private async Task<Guid> SeedLibraryAsync(Guid userId)
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
dbContext.Libraries.Add(_libraryEntityFixture.Create(id: libraryId, userId: userId, title: "Music Library", libraryType: LibraryType.Music, contentLocations: []));
|
||||
await dbContext.SaveChangesAsync();
|
||||
return libraryId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a music library owned by <paramref name="userId"/>, an artist of that library, an album of that artist and a track of that album.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user that owns the library.</param>
|
||||
/// <returns>The Ids of the seeded library and track.</returns>
|
||||
private async Task<(Guid LibraryId, Guid TrackId)> SeedLibraryArtistAlbumAndTrackAsync(Guid userId)
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid artistId = Guid.NewGuid();
|
||||
Guid albumId = Guid.NewGuid();
|
||||
Guid trackId = Guid.NewGuid();
|
||||
dbContext.Libraries.Add(_libraryEntityFixture.Create(id: libraryId, userId: userId, title: "Music Library", libraryType: LibraryType.Music, contentLocations: []));
|
||||
dbContext.Artists.Add(_artistEntityFixture.Create(id: artistId, libraryId: libraryId, name: "Queen"));
|
||||
dbContext.Albums.Add(_albumEntityFixture.Create(id: albumId, artistId: artistId, libraryId: libraryId, title: "A Night at the Opera"));
|
||||
dbContext.Tracks.Add(_trackEntityFixture.Create(id: trackId, albumId: albumId, libraryId: libraryId, path: $"/music/queen/{trackId:N}.flac", title: "Bohemian Rhapsody", includeMetadata: false));
|
||||
await dbContext.SaveChangesAsync();
|
||||
return (libraryId, trackId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
||||
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
|
||||
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
|
||||
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the shape of a problem details response with the provided status, title, detail, instance and type.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="statusCode">The expected HTTP status code.</param>
|
||||
/// <param name="expectedTitle">The expected problem title.</param>
|
||||
/// <param name="expectedDetail">The expected problem detail.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedType">The expected problem type URI.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertProblemDetailsAsync(HttpResponseMessage response, HttpStatusCode statusCode, string expectedTitle, string expectedDetail, string expectedInstance, string expectedType)
|
||||
{
|
||||
Assert.Equal(statusCode, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal((int)statusCode, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal(expectedType, problemDetails["type"].GetString());
|
||||
Assert.Equal(expectedTitle, problemDetails["title"].GetString());
|
||||
Assert.Equal(expectedDetail, problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the standard validation problem of the start consumption route.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedErrorDescription">The error description expected under the validation errors.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertValidationProblemAsync(HttpResponseMessage response, string expectedInstance, string expectedErrorDescription)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
Assert.Equal("OneOrMoreValidationErrorsOccurred", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorDescription, errors["General.Validation"]);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that a malicious route value is rejected without disclosing internals or reaching the data access layer.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="payload">The malicious value that was sent.</param>
|
||||
/// <param name="requestPath">The request path the value was sent in.</param>
|
||||
/// <param name="expectedErrorCode">The validation error code expected when the value reaches the validator.</param>
|
||||
private async Task AssertRejectedInjectionAsync(HttpResponseMessage response, string payload, string requestPath, string expectedErrorCode)
|
||||
{
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Assert.NotEqual(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.UnprocessableEntity)
|
||||
{
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
// The instance is the request path with the rejected payload still in place, independent of how the server escapes it.
|
||||
string escapedPayload = Uri.EscapeDataString(payload);
|
||||
int payloadIndex = requestPath.IndexOf(escapedPayload, StringComparison.Ordinal);
|
||||
string? instance = problemDetails["instance"].GetString();
|
||||
Assert.NotNull(instance);
|
||||
Assert.True(payloadIndex >= 0, "The escaped payload must appear in the request path.");
|
||||
Assert.StartsWith(requestPath[..payloadIndex], instance, StringComparison.Ordinal);
|
||||
Assert.EndsWith(requestPath[(payloadIndex + escapedPayload.Length)..], instance, StringComparison.Ordinal);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorCode, errors["General.Validation"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// A payload that carries encoded path separators is rejected by routing before it reaches the validator. That is still a safe rejection, so only it may take this branch.
|
||||
Assert.Contains("/", payload, StringComparison.Ordinal);
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
|
||||
AssertNoSensitiveData(content);
|
||||
Assert.DoesNotContain(payload, content, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the per-user responses are not storable by any intermediary and vary by the authorization header.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
private static void AssertNonCacheable(HttpResponseMessage response)
|
||||
{
|
||||
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
|
||||
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response body carries no credentials, secrets, persistence details or stack traces.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoSensitiveData(string content)
|
||||
{
|
||||
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the serialized consumption record exposes only the fields of its documented response contract.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoDatabaseOnlyFields(string content)
|
||||
{
|
||||
Assert.DoesNotContain("version", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("userId", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the test users created by the test.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
foreach (string username in _seededUsernames)
|
||||
_apiFactory.RemoveTestUser(username);
|
||||
}
|
||||
}
|
||||
+516
@@ -0,0 +1,516 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Contracts.Fixtures.Core.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.StopConsumption;
|
||||
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.MediaConsumption.StopConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <see cref="StopConsumptionEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StopConsumptionEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
{
|
||||
private const string JWT_SECRET = "test-key-thats-at-least-32-chars-long-for-jwt";
|
||||
private const long SEEDED_VERSION = 7;
|
||||
|
||||
private static readonly DateTime s_startedOnUtc = new(2026, 3, 1, 10, 0, 0, DateTimeKind.Utc);
|
||||
private static readonly DateTime s_lastActivityOnUtc = new(2026, 3, 1, 10, 5, 0, DateTimeKind.Utc);
|
||||
|
||||
private readonly LuminaApiFactory _apiFactory;
|
||||
private readonly HttpClient _client;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
||||
private readonly MediaConsumptionRecordEntityFixture _mediaConsumptionRecordEntityFixture = new();
|
||||
private readonly MediaPositionRequestFixture _mediaPositionRequestFixture = new();
|
||||
private readonly StopConsumptionRequestFixture _stopConsumptionRequestFixture = new();
|
||||
private readonly UserEntityFixture _userEntityFixture = new();
|
||||
private readonly List<string> _seededUsernames = [];
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StopConsumptionEndpointTests"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiFactory">Injected in-memory API factory.</param>
|
||||
public StopConsumptionEndpointTests(LuminaApiFactory apiFactory)
|
||||
{
|
||||
_apiFactory = apiFactory;
|
||||
_client = apiFactory.CreateClient();
|
||||
// A unique X-Forwarded-For isolates rate limiting state per test.
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StopConsumption_WhenUnauthorized_ShouldReturnUnauthorizedResult()
|
||||
{
|
||||
// Arrange
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}/stop";
|
||||
StopConsumptionRequest request = _stopConsumptionRequestFixture.Create(includePosition: false, progressFraction: 0.5, consumedDurationInSeconds: 10);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
await AssertUnauthorizedAsync(response, url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StopConsumption_WhenSuccessful_ShouldReturnNonCacheableResponseWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(_, Guid recordId) = await SeedRecordAsync(userId, MediaKind.MusicTrack, ConsumptionStatus.InProgress, 0, 0.1, MediaPositionKind.TimeOffset, 5_000, null);
|
||||
StopConsumptionRequest request = _stopConsumptionRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: nameof(MediaPositionKind.TimeOffset), timeOffsetSeconds: 30, includeLocation: false),
|
||||
progressFraction: 0.4,
|
||||
consumedDurationInSeconds: 60);
|
||||
string url = $"/api/v1/users/me/consumption-records/{recordId}/stop";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
AssertNonCacheable(response);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
AssertNoSensitiveData(content);
|
||||
AssertNoDatabaseOnlyFields(content);
|
||||
|
||||
ConsumptionRecordResponse? record = JsonSerializer.Deserialize<ConsumptionRecordResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(record);
|
||||
Assert.Equal(recordId, record!.Id);
|
||||
Assert.Equal(ConsumptionStatus.Stopped, record.Status);
|
||||
Assert.NotNull(record.EndedOnUtc);
|
||||
Assert.Equal(60.0, record.ConsumedDurationInSeconds);
|
||||
Assert.Equal(0.4, record.LastProgressFraction);
|
||||
Assert.NotNull(record.LastPosition);
|
||||
Assert.Equal(MediaPositionKind.TimeOffset, record.LastPosition!.Kind);
|
||||
Assert.Equal(30.0, record.LastPosition.TimeOffsetSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StopConsumption_WhenRecordDoesNotExist_ShouldReturnNonCacheableNotFoundWithoutSensitiveData()
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
StopConsumptionRequest request = _stopConsumptionRequestFixture.Create(includePosition: false, progressFraction: 0.5, consumedDurationInSeconds: 10);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}/stop";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
string content = await AssertProblemDetailsAsync(
|
||||
response,
|
||||
HttpStatusCode.NotFound,
|
||||
"General.NotFound",
|
||||
"ConsumptionRecordNotFound",
|
||||
url,
|
||||
"https://tools.ietf.org/html/rfc9110#section-15.5.5");
|
||||
AssertNonCacheable(response);
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StopConsumption_WhenAnotherUsersRecordIsTargeted_ShouldReturnNotFoundAndLeaveRecordUnchanged()
|
||||
{
|
||||
// Arrange
|
||||
(Guid otherUserId, _) = await SeedOtherUserAsync();
|
||||
(_, Guid otherRecordId) = await SeedRecordAsync(otherUserId, MediaKind.MusicTrack, ConsumptionStatus.InProgress, 15_000, 0.2, MediaPositionKind.TimeOffset, 15_000, null);
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
StopConsumptionRequest request = _stopConsumptionRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: nameof(MediaPositionKind.TimeOffset), timeOffsetSeconds: 90, includeLocation: false),
|
||||
progressFraction: 0.5,
|
||||
consumedDurationInSeconds: 90);
|
||||
string url = $"/api/v1/users/me/consumption-records/{otherRecordId}/stop";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
string content = await AssertProblemDetailsAsync(
|
||||
response,
|
||||
HttpStatusCode.NotFound,
|
||||
"General.NotFound",
|
||||
"ConsumptionRecordNotFound",
|
||||
url,
|
||||
"https://tools.ietf.org/html/rfc9110#section-15.5.5");
|
||||
AssertNoSensitiveData(content);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
MediaConsumptionRecordEntity? storedRecord = dbContext.MediaConsumptionRecords.FirstOrDefault(record => record.Id == otherRecordId);
|
||||
Assert.NotNull(storedRecord);
|
||||
Assert.Equal(ConsumptionStatus.InProgress, storedRecord!.Status);
|
||||
Assert.Null(storedRecord.EndedOnUtc);
|
||||
Assert.Equal(15_000, storedRecord.ConsumedDurationMilliseconds);
|
||||
Assert.Equal(0.2, storedRecord.LastProgressFraction);
|
||||
Assert.Equal(MediaPositionKind.TimeOffset, storedRecord.LastPositionKind);
|
||||
Assert.Equal(15_000, storedRecord.LastPositionTimeOffsetMilliseconds);
|
||||
Assert.Equal(SEEDED_VERSION, storedRecord.Version);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task StopConsumption_WithSqlInjectionInRecordId_ShouldRejectWithoutLeaking(string maliciousRecordId)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
StopConsumptionRequest request = _stopConsumptionRequestFixture.Create(includePosition: false, progressFraction: 0.5, consumedDurationInSeconds: 10);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Uri.EscapeDataString(maliciousRecordId)}/stop";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
await AssertRejectedInjectionAsync(response, maliciousRecordId, url, "ConsumptionRecordIdCannotBeEmpty");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task StopConsumption_WithUnknownPositionKind_ShouldRejectWithoutLeaking(string maliciousPositionKind)
|
||||
{
|
||||
// Arrange
|
||||
await CreateAuthenticatedUserAsync(_client);
|
||||
StopConsumptionRequest request = _stopConsumptionRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: maliciousPositionKind),
|
||||
progressFraction: 0.5,
|
||||
consumedDurationInSeconds: 10);
|
||||
string url = $"/api/v1/users/me/consumption-records/{Guid.NewGuid()}/stop";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
string content = await AssertValidationProblemAsync(response, url, "InvalidMediaPosition");
|
||||
AssertNoSensitiveData(content);
|
||||
Assert.DoesNotContain(maliciousPositionKind, content, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("' OR '1'='1")] // Boolean-based injection.
|
||||
[InlineData("'; DROP TABLE MediaConsumptionRecords--")] // Destructive injection.
|
||||
[InlineData("1; DELETE FROM MediaConsumptionRecords")] // Stacked statement injection.
|
||||
[InlineData("../../../etc/passwd")] // Path traversal.
|
||||
[InlineData("<script>alert(1)</script>")] // Markup injection.
|
||||
public async Task StopConsumption_WithOpaquePositionLocation_ShouldStoreItVerbatimWithoutLeaking(string location)
|
||||
{
|
||||
// Arrange
|
||||
(Guid userId, _) = await CreateAuthenticatedUserAsync(_client);
|
||||
(_, Guid recordId) = await SeedRecordAsync(userId, MediaKind.Book, ConsumptionStatus.InProgress, 0, 0.1, MediaPositionKind.Location, null, "chapter-1");
|
||||
StopConsumptionRequest request = _stopConsumptionRequestFixture.Create(
|
||||
position: _mediaPositionRequestFixture.Create(kind: nameof(MediaPositionKind.Location), location: location, includeTimeOffsetSeconds: false),
|
||||
progressFraction: 0.4,
|
||||
consumedDurationInSeconds: 30);
|
||||
string url = $"/api/v1/users/me/consumption-records/{recordId}/stop";
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync(url, request);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
AssertNoSensitiveData(content);
|
||||
|
||||
ConsumptionRecordResponse? record = JsonSerializer.Deserialize<ConsumptionRecordResponse>(content, _jsonOptions);
|
||||
Assert.NotNull(record);
|
||||
Assert.Equal(ConsumptionStatus.Stopped, record!.Status);
|
||||
Assert.NotNull(record.LastPosition);
|
||||
Assert.Equal(MediaPositionKind.Location, record.LastPosition!.Kind);
|
||||
Assert.Equal(location, record.LastPosition.Location);
|
||||
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
MediaConsumptionRecordEntity? storedRecord = dbContext.MediaConsumptionRecords.FirstOrDefault(candidate => candidate.Id == recordId);
|
||||
Assert.NotNull(storedRecord);
|
||||
Assert.Equal(location, storedRecord!.LastPositionLocation);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a test user and authenticates it on <paramref name="client"/>.
|
||||
/// </summary>
|
||||
/// <param name="client">The HTTP client to authenticate.</param>
|
||||
/// <returns>The Id and username of the created user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> CreateAuthenticatedUserAsync(HttpClient client)
|
||||
{
|
||||
(Guid userId, string username) = await _apiFactory.CreateAndAuthenticateUserAsync(client);
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a user distinct from the authenticated test user and returns its Id and username.
|
||||
/// </summary>
|
||||
/// <returns>The Id and username of the seeded user.</returns>
|
||||
private async Task<(Guid UserId, string Username)> SeedOtherUserAsync()
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid userId = Guid.NewGuid();
|
||||
string username = $"otheruser_{Guid.NewGuid()}";
|
||||
UserEntity user = _userEntityFixture.Create(id: userId, username: username, password: "TestPass123!");
|
||||
dbContext.Users.Add(user);
|
||||
await dbContext.SaveChangesAsync();
|
||||
_seededUsernames.Add(username);
|
||||
return (userId, username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a media library owned by <paramref name="userId"/> and a consumption record of a media item belonging to it.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user that owns the record.</param>
|
||||
/// <param name="mediaKind">The kind of media that the record consumes.</param>
|
||||
/// <param name="status">The state of the seeded session.</param>
|
||||
/// <param name="consumedDurationMilliseconds">The consumed duration of the record, in milliseconds.</param>
|
||||
/// <param name="lastProgressFraction">The last reported progress fraction of the record.</param>
|
||||
/// <param name="lastPositionKind">The kind of the last known position.</param>
|
||||
/// <param name="lastPositionTimeOffsetMilliseconds">The last known position, in milliseconds, when it is a time offset.</param>
|
||||
/// <param name="lastPositionLocation">The last known opaque locator, when it is a location.</param>
|
||||
/// <returns>The Ids of the seeded library and consumption record.</returns>
|
||||
private async Task<(Guid LibraryId, Guid RecordId)> SeedRecordAsync(
|
||||
Guid userId,
|
||||
MediaKind mediaKind,
|
||||
ConsumptionStatus status,
|
||||
long consumedDurationMilliseconds,
|
||||
double lastProgressFraction,
|
||||
MediaPositionKind lastPositionKind,
|
||||
long? lastPositionTimeOffsetMilliseconds,
|
||||
string? lastPositionLocation)
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid mediaItemId = Guid.NewGuid();
|
||||
Guid recordId = Guid.NewGuid();
|
||||
dbContext.Libraries.Add(_libraryEntityFixture.Create(
|
||||
id: libraryId,
|
||||
userId: userId,
|
||||
title: "Media Library",
|
||||
libraryType: mediaKind == MediaKind.Book ? LibraryType.EBook : LibraryType.Music,
|
||||
contentLocations: [],
|
||||
shouldTrackConsumptionProgress: false));
|
||||
dbContext.MediaConsumptionRecords.Add(_mediaConsumptionRecordEntityFixture.Create(
|
||||
id: recordId,
|
||||
userId: userId,
|
||||
libraryId: libraryId,
|
||||
mediaKind: mediaKind,
|
||||
mediaItemId: mediaItemId,
|
||||
status: status,
|
||||
startedOnUtc: s_startedOnUtc,
|
||||
lastActivityOnUtc: s_lastActivityOnUtc,
|
||||
consumedDurationMilliseconds: consumedDurationMilliseconds,
|
||||
lastPositionKind: lastPositionKind,
|
||||
lastPositionTimeOffsetMilliseconds: lastPositionTimeOffsetMilliseconds,
|
||||
lastPositionLocation: lastPositionLocation,
|
||||
lastProgressFraction: lastProgressFraction,
|
||||
consecutivePagesRead: 0,
|
||||
version: SEEDED_VERSION));
|
||||
await dbContext.SaveChangesAsync();
|
||||
return (libraryId, recordId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the unauthenticated problem document produced by the authentication middleware.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
private async Task AssertUnauthorizedAsync(HttpResponseMessage response, string expectedInstance)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
||||
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status401Unauthorized, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc7235#section-3.1", problemDetails["type"].GetString());
|
||||
Assert.Equal("Unauthorized", problemDetails["title"].GetString());
|
||||
Assert.Equal("Authentication failed", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetProperty("value").GetString());
|
||||
AssertNoSensitiveData(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the shape of a problem details response with the provided status, title, detail, instance and type.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="statusCode">The expected HTTP status code.</param>
|
||||
/// <param name="expectedTitle">The expected problem title.</param>
|
||||
/// <param name="expectedDetail">The expected problem detail.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedType">The expected problem type URI.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertProblemDetailsAsync(HttpResponseMessage response, HttpStatusCode statusCode, string expectedTitle, string expectedDetail, string expectedInstance, string expectedType)
|
||||
{
|
||||
Assert.Equal(statusCode, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal((int)statusCode, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal(expectedType, problemDetails["type"].GetString());
|
||||
Assert.Equal(expectedTitle, problemDetails["title"].GetString());
|
||||
Assert.Equal(expectedDetail, problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response is the standard validation problem of the stop consumption route.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="expectedInstance">The expected request instance path.</param>
|
||||
/// <param name="expectedErrorDescription">The error description expected under the validation errors.</param>
|
||||
/// <returns>The raw response body, so that leak assertions can be applied to it.</returns>
|
||||
private async Task<string> AssertValidationProblemAsync(HttpResponseMessage response, string expectedInstance, string expectedErrorDescription)
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
Assert.Equal("OneOrMoreValidationErrorsOccurred", problemDetails["detail"].GetString());
|
||||
Assert.Equal(expectedInstance, problemDetails["instance"].GetString());
|
||||
Assert.NotNull(problemDetails["traceId"].GetString());
|
||||
Assert.NotEmpty(problemDetails["traceId"].GetString()!);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorDescription, errors["General.Validation"]);
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that a malicious route value is rejected without disclosing internals or reaching the data access layer.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
/// <param name="payload">The malicious value that was sent.</param>
|
||||
/// <param name="requestPath">The request path the value was sent in.</param>
|
||||
/// <param name="expectedErrorCode">The validation error code expected when the value reaches the validator.</param>
|
||||
private async Task AssertRejectedInjectionAsync(HttpResponseMessage response, string payload, string requestPath, string expectedErrorCode)
|
||||
{
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Assert.NotEqual(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.UnprocessableEntity)
|
||||
{
|
||||
Dictionary<string, JsonElement>? problemDetails = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(content, _jsonOptions);
|
||||
Assert.NotNull(problemDetails);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails!["status"].GetInt32());
|
||||
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails["type"].GetString());
|
||||
Assert.Equal("General.Validation", problemDetails["title"].GetString());
|
||||
// The instance is the request path with the rejected payload still in place, independent of how the server escapes it.
|
||||
string escapedPayload = Uri.EscapeDataString(payload);
|
||||
int payloadIndex = requestPath.IndexOf(escapedPayload, StringComparison.Ordinal);
|
||||
string? instance = problemDetails["instance"].GetString();
|
||||
Assert.NotNull(instance);
|
||||
Assert.True(payloadIndex >= 0, "The escaped payload must appear in the request path.");
|
||||
Assert.StartsWith(requestPath[..payloadIndex], instance, StringComparison.Ordinal);
|
||||
Assert.EndsWith(requestPath[(payloadIndex + escapedPayload.Length)..], instance, StringComparison.Ordinal);
|
||||
|
||||
Dictionary<string, string[]>? errors = problemDetails["errors"].Deserialize<Dictionary<string, string[]>>(_jsonOptions);
|
||||
Assert.NotNull(errors);
|
||||
Assert.Contains("General.Validation", errors!.Keys);
|
||||
Assert.Contains(expectedErrorCode, errors["General.Validation"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// A payload that carries encoded path separators is rejected by routing before it reaches the validator. That is still a safe rejection, so only it may take this branch.
|
||||
Assert.Contains("/", payload, StringComparison.Ordinal);
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
|
||||
AssertNoSensitiveData(content);
|
||||
Assert.DoesNotContain(payload, content, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the per-user responses are not storable by any intermediary and vary by the authorization header.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to assert on.</param>
|
||||
private static void AssertNonCacheable(HttpResponseMessage response)
|
||||
{
|
||||
Assert.Equal("private, no-store", response.Headers.NonValidated["Cache-Control"].ToString());
|
||||
Assert.Equal("Authorization", response.Headers.NonValidated["Vary"].ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response body carries no credentials, secrets, persistence details or stack traces.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoSensitiveData(string content)
|
||||
{
|
||||
Assert.DoesNotContain("password", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("hash", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("salt", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(" at Lumina.", content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("SQL", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(JWT_SECRET, content, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("Data Source", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the serialized consumption record exposes only the fields of its documented response contract.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to assert on.</param>
|
||||
private static void AssertNoDatabaseOnlyFields(string content)
|
||||
{
|
||||
Assert.DoesNotContain("version", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("userId", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedBy", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("createdOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("updatedOnUtc", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the test users created by the test.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
foreach (string username in _seededUsernames)
|
||||
_apiFactory.RemoveTestUser(username);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user