backend: Enforce UTF-8 password byte limits

This commit is contained in:
GonnaHetzMe committed 2026-12-05 01:00:00 +01:00
1 parent 0215af0749
commit 9fe713fc10
6 files changed
+44 -25

No files matched your search

@@ -1,6 +1,7 @@
using System.Net;
using System.Net.Http.Json;
using Helpdesk.Api.Controllers;
using Helpdesk.Application.Abstractions;
using Helpdesk.Application.Auth;
using Helpdesk.Domain.Auth;
using Helpdesk.Infrastructure.Persistence;
@@ -41,28 +42,39 @@ public sealed class AuthTests(BackendFixture fixture) : IClassFixture<BackendFix
}
[Fact]
public async Task ChangingPasswordThroughAnEmailLinkRevokesTheOldSessionAndRemovesItsDevice()
public async Task ResetReplacesALegacyPasswordAndRevokesTheOldSessionAndDevice()
{
using var anonymous = fixture.CreateClient();
var user = await fixture.RegisterAsync(anonymous);
using var oldSession = fixture.CreateClient((await LoginAsync(anonymous, user.Email)).AccessToken);
await using var scope = fixture.Services.CreateAsyncScope();
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
var hasher = scope.ServiceProvider.GetRequiredService<IPasswordHasher>();
var legacyPassword = new string('ž', 37);
var account = await db.Users.SingleAsync(u => u.Id == user.Id, Cancellation);
account.ChangePasswordHash(hasher.Hash(legacyPassword));
await db.SaveChangesAsync(Cancellation);
var oldLogin = await ReadAsync<AuthResponse>(anonymous.PostAsJsonAsync("/api/v1/auth/login",
new LoginRequest(user.Email, legacyPassword), Cancellation));
using var oldSession = fixture.CreateClient(oldLogin.AccessToken);
await ExpectAsync(oldSession.PostAsJsonAsync("/api/v1/devices",
new RegisterDeviceRequest("password-reset-device"), Cancellation), HttpStatusCode.NoContent);
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/forgot-password",
new ForgotPasswordRequest(user.Email), Cancellation), HttpStatusCode.NoContent);
const string newPassword = "New-integration-password";
var newPassword = new string('č', 36);
var reset = new ResetPasswordRequest(await fixture.TakeResetTokenAsync(user.Email), newPassword);
var validation = await ReadAsync<ValidationProblemDetails>(anonymous.PostAsJsonAsync("/api/v1/auth/reset-password",
reset with { NewPassword = newPassword + "a" }, Cancellation), HttpStatusCode.BadRequest);
Assert.Contains("72 bajtů", Assert.Single(validation.Errors["newPassword"]));
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/reset-password", reset, Cancellation), HttpStatusCode.NoContent);
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/reset-password", reset, Cancellation), HttpStatusCode.BadRequest);
await ExpectAsync(oldSession.GetAsync("/api/v1/users/me", Cancellation), HttpStatusCode.Unauthorized);
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/login",
new LoginRequest(user.Email, Password), Cancellation), HttpStatusCode.Unauthorized);
new LoginRequest(user.Email, legacyPassword), Cancellation), HttpStatusCode.Unauthorized);
var session = await ReadAsync<AuthResponse>(anonymous.PostAsJsonAsync("/api/v1/auth/login",
new LoginRequest(user.Email, newPassword), Cancellation));
Assert.Equal(user.Id, session.User.Id);
await using var scope = fixture.Services.CreateAsyncScope();
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
Assert.False(await db.PushDevices.AnyAsync(device => device.UserId == user.Id, Cancellation));
}