Path: redb-identity/tests/redb.Identity.Tests/Routes/MfaIdorTests.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/Routes/MfaIdorTests.cs ---- using AwesomeAssertions; using redb.Identity.Core.Routes.Processors; using redb.Route.Core; using Xunit; namespace redb.Identity.Tests.Routes; /// /// B8 — IDOR fix verification. Exercises /// directly with synthetic objects to validate the /// self-vs-admin authorization rule without spinning up the full HTTP stack. /// public sealed class MfaIdorTests { private const string AdminScope = "identity:manage"; private const string AccountScope = "identity:account"; private static RequireSelfOrAdminProcessor CreateSut() => new(AdminScope, AccountScope); private static Exchange BuildExchange(long? bodyUserId, long? callerUserId, string[]? scopes) { var body = bodyUserId.HasValue ? new Dictionary { ["userId"] = bodyUserId.Value } : new Dictionary(); var msg = new Message { Body = body }; var ex = new Exchange(msg); if (scopes is not null) ex.Properties["identity:management-scopes"] = scopes; if (callerUserId is not null) ex.Properties["identity:management-user-id"] = callerUserId.Value; return ex; } [Fact] public async Task SelfScope_WithMatchingSubject_AllowsRequest() { var sut = CreateSut(); var ex = BuildExchange(bodyUserId: 42, callerUserId: 42, scopes: new[] { AccountScope }); await sut.Process(ex); ex.HasOut.Should().BeFalse(); ex.IsStopped.Should().BeFalse(); } [Fact] public async Task SelfScope_TargetingOtherUser_IsForbidden_NoLeakage() { var sut = CreateSut(); var ex = BuildExchange(bodyUserId: 99, callerUserId: 42, scopes: new[] { AccountScope }); await sut.Process(ex); ex.HasOut.Should().BeTrue(); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(403); ex.IsStopped.Should().BeTrue(); // C4: application-level authorization denials use RFC 9457 Problem Details // (the RFC 6750 OAuth error envelope is produced upstream by ManagementBearerAuth // for scope/token failures; this processor enforces an application IDOR guard // which is not an OAuth concept and therefore uses problem+json). ex.Out!.ContentType.Should().Be("application/problem+json"); // Generic message — must not distinguish "no such user" from "not authorized". // The stable machine token is preserved as the RFC 9457 `code` extension member. var bodyText = System.Text.Encoding.UTF8.GetString((byte[])ex.Out!.Body!); bodyText.Should().Contain("not_authorized"); bodyText.Should().NotContain("99"); bodyText.Should().NotContain("42"); } [Fact] public async Task AdminScope_TargetingOtherUser_IsAllowed() { var sut = CreateSut(); var ex = BuildExchange(bodyUserId: 99, callerUserId: 42, scopes: new[] { AdminScope }); await sut.Process(ex); ex.HasOut.Should().BeFalse(); ex.IsStopped.Should().BeFalse(); } [Fact] public async Task BothScopes_AdminWins_AllowsAnyUser() { var sut = CreateSut(); var ex = BuildExchange(bodyUserId: 99, callerUserId: 42, scopes: new[] { AccountScope, AdminScope }); await sut.Process(ex); ex.HasOut.Should().BeFalse(); ex.IsStopped.Should().BeFalse(); } [Fact] public async Task SelfScope_BodyMissingUserId_IsForbidden() { var sut = CreateSut(); var ex = BuildExchange(bodyUserId: null, callerUserId: 42, scopes: new[] { AccountScope }); await sut.Process(ex); ex.HasOut.Should().BeTrue(); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(403); ex.IsStopped.Should().BeTrue(); } [Fact] public async Task SelfScope_MissingCallerUserId_IsForbidden() { // client_credentials tokens have no internal user-id mirrored. Self-service // requires an end-user grant — without a caller user-id we MUST deny. var sut = CreateSut(); var ex = BuildExchange(bodyUserId: 42, callerUserId: null, scopes: new[] { AccountScope }); await sut.Process(ex); ex.HasOut.Should().BeTrue(); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(403); ex.IsStopped.Should().BeTrue(); } [Fact] public async Task NoAuthContext_IsForbidden_DirectVmIsATransportNotATrustLevel() { // An exchange without identity:management-scopes is one nobody authenticated. This test used // to pin the opposite ("internal direct-vm callers bypass the check - direct-vm is not // network-reachable"): in a Tsak worker the direct-vm registry is shared by every module in // the process, so that bypass handed any module admin rights over users' MFA. The route gate // (RequireManagementContextProcessor) refuses first; this processor refuses on its own too. var sut = CreateSut(); var ex = BuildExchange(bodyUserId: 42, callerUserId: null, scopes: null); await sut.Process(ex); ex.HasOut.Should().BeTrue(); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(403); ex.IsStopped.Should().BeTrue(); } [Fact] public async Task UnknownScope_NeitherAdminNorSelf_IsForbidden() { // Defensive: even if upstream auth admitted some other scope (e.g. scim only), // self-or-admin must still deny. var sut = CreateSut(); var ex = BuildExchange(bodyUserId: 42, callerUserId: 42, scopes: new[] { "scim" }); await sut.Process(ex); ex.HasOut.Should().BeTrue(); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(403); ex.IsStopped.Should().BeTrue(); } // ── The granular scopes of the surface, as the MFA route wires them ── private static RequireSelfOrAdminProcessor CreateMfaSurfaceSut() => new(AdminScope, AccountScope, surface: new RequireSelfOrAdminProcessor.SurfaceScopes( "identity:mfa:write", "identity:mfa:read", new HashSet(StringComparer.Ordinal) { "status" })); [Theory] [InlineData("identity:mfa:write", "setup", true)] [InlineData("identity:mfa:write", "disable", true)] [InlineData("identity:mfa:write", "status", true)] [InlineData("identity:mfa:read", "status", true)] [InlineData("identity:mfa:read", "setup", false)] [InlineData("identity:mfa:read", "confirm", false)] [InlineData("identity:mfa:read", "disable", false)] [InlineData("identity:mfa:read", "regenerate-recovery", false)] [InlineData("identity:mfa:read", null, false)] [InlineData("identity:users:write", "status", false)] [InlineData("identity:users:write", "setup", false)] public async Task Surface_scopes_admit_another_user_by_operation(string scope, string? operation, bool admitted) { var sut = CreateMfaSurfaceSut(); var ex = BuildExchange(bodyUserId: 99, callerUserId: null, scopes: new[] { scope }); if (operation is not null) ex.In.Headers["operation"] = operation; await sut.Process(ex); ex.IsStopped.Should().Be(!admitted); if (!admitted) ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(403); } [Fact] public async Task Without_the_surface_the_granular_write_scope_is_refused() { var sut = CreateSut(); var ex = BuildExchange(bodyUserId: 99, callerUserId: null, scopes: new[] { "identity:mfa:write" }); ex.In.Headers["operation"] = "setup"; await sut.Process(ex); ex.IsStopped.Should().BeTrue("a guard built without surface scopes keeps admitting only the master scope"); } }