backend: Enforce UTF-8 password byte limits
This commit is contained in:
1 parent
0215af0749
commit
9fe713fc10
6 files changed
+44
-25
No files matched your search
@@ -26,11 +26,15 @@ public sealed record AuthResponse(
|
||||
|
||||
internal static class PasswordRules
|
||||
{
|
||||
public const int MinLength = 8;
|
||||
public const int MaxLength = 64; // BCrypt zpracuje nejvýše 72 bajtů
|
||||
private const int MinLength = 8;
|
||||
private const int MaxLength = 64;
|
||||
private const int MaxUtf8Bytes = 72;
|
||||
|
||||
public static IRuleBuilderOptions<T, string> ValidPassword<T>(this IRuleBuilder<T, string> rule) =>
|
||||
rule.NotEmpty().MinimumLength(MinLength).MaximumLength(MaxLength);
|
||||
public static IRuleBuilderOptions<T, string?> ValidPassword<T>(this IRuleBuilderInitial<T, string?> rule) =>
|
||||
rule.Cascade(CascadeMode.Stop)
|
||||
.NotEmpty().MinimumLength(MinLength).MaximumLength(MaxLength)
|
||||
.Must(password => Encoding.UTF8.GetByteCount(password!) <= MaxUtf8Bytes)
|
||||
.WithMessage("Heslo může mít nejvýše 72 bajtů v UTF-8.");
|
||||
}
|
||||
|
||||
public sealed class RegisterRequestValidator : AbstractValidator<RegisterRequest>
|
||||
@@ -48,9 +52,7 @@ public sealed class ConfirmEmailRequestValidator : AbstractValidator<ConfirmEmai
|
||||
public ConfirmEmailRequestValidator()
|
||||
{
|
||||
RuleFor(x => x.Token).NotEmpty();
|
||||
RuleFor(x => x.NewPassword).Cascade(CascadeMode.Stop).ValidPassword()
|
||||
.Must(password => Encoding.UTF8.GetByteCount(password) <= 72)
|
||||
.WithMessage("Heslo může mít nejvýše 72 bajtů v UTF-8.");
|
||||
RuleFor(x => x.NewPassword).ValidPassword();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user