|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http.Json; |
| 3 | +using AwesomeAssertions; |
| 4 | +using CodeBeam.UltimateAuth.Core.Contracts; |
| 5 | +using CodeBeam.UltimateAuth.Tests.Integration.Infrastructure; |
| 6 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 7 | + |
| 8 | +namespace CodeBeam.UltimateAuth.Tests.Integration.Security; |
| 9 | + |
| 10 | +public sealed class LoginCsrfExploitProofTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public async Task CrossOriginLogin_WithoutCsrfProof_MustNotAuthenticateVictimAsAttacker() |
| 14 | + { |
| 15 | + await using var factory = new AuthServerFactory(); |
| 16 | + |
| 17 | + var attacker = await factory.CreateLoginUserAsync(); |
| 18 | + var deviceId = $"csrf-victim-{Guid.NewGuid():N}"; |
| 19 | + |
| 20 | + using var victim = CreateBrowser(factory, deviceId); |
| 21 | + |
| 22 | + // The victim starts unauthenticated. |
| 23 | + await AssertUnauthenticatedAsync(victim); |
| 24 | + |
| 25 | + // The attacker submits valid credentials without CSRF proof. |
| 26 | + using var request = new HttpRequestMessage( |
| 27 | + HttpMethod.Post, |
| 28 | + "/auth/login") |
| 29 | + { |
| 30 | + Content = JsonContent.Create(new |
| 31 | + { |
| 32 | + identifier = attacker.Identifier, |
| 33 | + secret = attacker.Secret |
| 34 | + }) |
| 35 | + }; |
| 36 | + |
| 37 | + request.Headers.TryAddWithoutValidation( |
| 38 | + "Origin", |
| 39 | + "https://localhost:6130"); |
| 40 | + |
| 41 | + using var response = await victim.SendAsync(request); |
| 42 | + |
| 43 | + var body = await response.Content.ReadAsStringAsync(); |
| 44 | + |
| 45 | + // Prove that the rejection came from CSRF validation. |
| 46 | + response.StatusCode.Should().Be( |
| 47 | + HttpStatusCode.BadRequest, |
| 48 | + $"unexpected login response: {body}"); |
| 49 | + |
| 50 | + body.Should().Contain( |
| 51 | + "invalid_antiforgery_token"); |
| 52 | + |
| 53 | + // The rejected login must not issue a session cookie. |
| 54 | + var sessionCookie = GetSessionCookie(response); |
| 55 | + |
| 56 | + sessionCookie.Should().BeNull( |
| 57 | + "a rejected CSRF login must not establish a session"); |
| 58 | + |
| 59 | + // The victim must remain unauthenticated. |
| 60 | + await AssertUnauthenticatedAsync(victim); |
| 61 | + } |
| 62 | + |
| 63 | + private static async Task AssertUnauthenticatedAsync( |
| 64 | + HttpClient client) |
| 65 | + { |
| 66 | + using var response = await client.PostAsJsonAsync( |
| 67 | + "/auth/me/sessions/chains", |
| 68 | + new PageRequest |
| 69 | + { |
| 70 | + PageNumber = 1, |
| 71 | + PageSize = 10 |
| 72 | + }); |
| 73 | + |
| 74 | + response.StatusCode.Should().Be( |
| 75 | + HttpStatusCode.Unauthorized); |
| 76 | + } |
| 77 | + |
| 78 | + private static HttpClient CreateBrowser( |
| 79 | + AuthServerFactory factory, |
| 80 | + string deviceId) |
| 81 | + { |
| 82 | + var client = factory.CreateClient( |
| 83 | + new WebApplicationFactoryClientOptions |
| 84 | + { |
| 85 | + AllowAutoRedirect = false, |
| 86 | + HandleCookies = false |
| 87 | + }); |
| 88 | + |
| 89 | + client.DefaultRequestHeaders.TryAddWithoutValidation( |
| 90 | + "X-UDID", |
| 91 | + deviceId); |
| 92 | + |
| 93 | + return client; |
| 94 | + } |
| 95 | + |
| 96 | + private static string? GetSessionCookie( |
| 97 | + HttpResponseMessage response) |
| 98 | + { |
| 99 | + if (!response.Headers.TryGetValues( |
| 100 | + "Set-Cookie", |
| 101 | + out var cookies)) |
| 102 | + { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + return cookies |
| 107 | + .Select(x => x.Split(';', 2)[0]) |
| 108 | + .FirstOrDefault(x => x.StartsWith( |
| 109 | + "uas=", |
| 110 | + StringComparison.OrdinalIgnoreCase)); |
| 111 | + } |
| 112 | +} |
0 commit comments