Path: redb-identity/tests/redb.Identity.Tests/Authorization/PerUserEndpointAuthMatrixTests.cs Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/Authorization/PerUserEndpointAuthMatrixTests.cs ---- using System.Net; using System.Net.Http.Headers; using System.Text; using System.Text.Json; using AwesomeAssertions; using redb.Identity.Tests.Infrastructure; using Xunit; using redb.Identity.Core.Routes; namespace redb.Identity.Tests.Authorization; /// /// G14 — endpoint authorization matrix on per-user management endpoints. /// /// Extends with the three additional /// surface areas called out in the STATUS gap: MFA per-user endpoints, consents, /// and applications. For each, asserts the three-token matrix: /// /// no token → 401 (anonymous rejected); /// wrong-scope token (SCIM-only) → 401/403; /// management-scope token → 2xx. /// /// This is the HTTP-surface bookend of the unit-level /// — a regression that drops /// ManagementBearerAuthProcessor from one of these controllers would let /// anonymous callers mutate MFA / consents / applications. /// /// [Collection("ProductionHttp")] public sealed class PerUserEndpointAuthMatrixTests { private readonly ProductionHttpFixture _fx; public PerUserEndpointAuthMatrixTests(ProductionHttpFixture fx) => _fx = fx; // Endpoints that read/mutate per-user state and MUST be behind // ManagementBearerAuthProcessor (admin or account scope). public static IEnumerable PerUserEndpoints() { // Path + method + request body (null → no body) yield return new object?[] { "GET", "/api/v1/identity/mfa/status/1", null }; yield return new object?[] { "GET", "/api/v1/identity/consents?userId=1", null }; yield return new object?[] { "GET", "/api/v1/identity/applications", null }; yield return new object?[] { "GET", "/api/v1/identity/scopes", null }; yield return new object?[] { "GET", "/api/v1/identity/tokens?userId=1", null }; } [Theory] [MemberData(nameof(PerUserEndpoints))] public async Task Endpoint_WithoutBearerToken_Returns401(string method, string path, string? body) { using var req = BuildRequest(method, path, body, token: null); var resp = await _fx.Http.SendAsync(req); ((int)resp.StatusCode).Should().Be(401, "{0} {1}: per-user management endpoint MUST reject anonymous calls. " + "A regression here lets the internet read/mutate user state without credentials.", method, path); } [Theory] [MemberData(nameof(PerUserEndpoints))] public async Task Endpoint_WithScimOnlyToken_IsRejected(string method, string path, string? body) { using var req = BuildRequest(method, path, body, token: _fx.ScimToken); var resp = await _fx.Http.SendAsync(req); var code = (int)resp.StatusCode; (code == 401 || code == 403).Should().BeTrue( "{0} {1}: a SCIM-only token must NOT pass the management scope gate (got {2}). " + "Cross-scope admission is a privilege boundary violation.", method, path, code); } [Theory] [MemberData(nameof(PerUserEndpoints))] public async Task Endpoint_WithManagementToken_IsAdmitted(string method, string path, string? body) { using var req = BuildRequest(method, path, body, token: _fx.ManagementToken); var resp = await _fx.Http.SendAsync(req); // Admission ≠ business-layer success. We only assert the token passed the auth // gate: anything other than 401/403 is acceptable here (endpoint may return 404 // for a user with no MFA state, 400 for a malformed userId, 200 for an empty // list). The invariant under test is that the admin scope MUST be admitted. var code = (int)resp.StatusCode; code.Should().NotBe(401, "{0} {1}: admin-scoped token rejected as unauthenticated (401).", method, path); code.Should().NotBe(403, "{0} {1}: admin-scoped token rejected as forbidden (403).", method, path); } /// /// Pins the B8 IDOR contract at the HTTP layer: a caller holding only /// identity:account (self-scope) cannot mutate MFA for a user whose id /// differs from the token's sub claim. The unit-level coverage lives in /// ; this test exists to /// detect the regression where RequireSelfOrAdminProcessor is dropped /// from the direct-vm route wiring at /// (MfaManage block). /// [Fact] public async Task MfaSetup_WithScimToken_CannotCrossScopeMutate() { // We use ScimToken here as the stand-in for "a token that is NOT identity:manage // and NOT identity:account" — so this covers the same-scope defence as // AdminEndpointAuthMatrix plus the extra guarantee that the /mfa/totp/setup // surface specifically is refused. var body = JsonSerializer.Serialize(new { userId = 9999, username = "victim" }); using var req = new HttpRequestMessage(HttpMethod.Post, "/api/v1/identity/mfa/totp/setup") { Content = new StringContent(body, Encoding.UTF8, "application/json"), }; req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _fx.ScimToken); var resp = await _fx.Http.SendAsync(req); var code = (int)resp.StatusCode; (code == 401 || code == 403).Should().BeTrue( "MFA setup MUST NOT be reachable with a token that carries neither identity:manage " + "nor identity:account (got {0}). Dropping the auth processor from MfaManage is a " + "B8 IDOR regression.", code); } private static HttpRequestMessage BuildRequest(string method, string path, string? body, string? token) { var req = new HttpRequestMessage(new HttpMethod(method), path); if (body is not null) req.Content = new StringContent(body, Encoding.UTF8, "application/json"); if (token is not null) req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); return req; } }