Path: redb/redb.Tests.Integration/Tests/Base/PasswordHasherDefaultTestsBase.cs
Snapshot: redb, 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
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Base/PasswordHasherDefaultTestsBase.cs
----
using redb.Core;
using redb.Core.Models.Users;
using redb.Core.Security;
namespace redb.Tests.Integration.Tests.Base;
///
/// Pins the default password hasher of a bare redb deployment (external security report,
/// 2026-09-08): every provider's stock CreateUserProvider factory used the short UserProvider
/// constructor, which hard-wired SimplePasswordHasher (salted SHA256) - BcryptPasswordHasher
/// existed in the tree but nothing reached it. The default is bcrypt now; legacy SHA256+salt
/// hashes keep validating (BcryptPasswordHasher recognizes both formats), so existing
/// databases migrate naturally on the next password change.
///
public abstract class PasswordHasherDefaultTestsBase
{
protected readonly IRedbService Redb;
protected PasswordHasherDefaultTestsBase(IRedbService redb) => Redb = redb;
private static string FreshLogin() => "hash_" + Guid.NewGuid().ToString("N")[..12];
[Fact]
public async Task DefaultHasher_WritesBcrypt()
{
var login = FreshLogin();
var user = await Redb.UserProvider.CreateUserAsync(
new CreateUserRequest { Login = login, Name = login, Password = "correct-horse" });
var stored = await Redb.Context.ExecuteScalarAsync(
$"SELECT _password FROM _users WHERE _id = {user.Id}");
stored.Should().StartWith("$2",
"a bare redb deployment must hash passwords with bcrypt by default, not salted SHA256");
}
[Fact]
public async Task LegacySha256Hash_StillValidates()
{
// An existing database carries SimplePasswordHasher-era hashes ("base64(salt):base64(hash)").
// The bcrypt default must keep accepting them - login survives the upgrade, and the hash
// migrates to bcrypt whenever the user changes the password.
var login = FreshLogin();
var user = await Redb.UserProvider.CreateUserAsync(
new CreateUserRequest { Login = login, Name = login, Password = "placeholder" });
var legacy = new SimplePasswordHasher().HashPassword("old-password");
await Redb.Context.ExecuteAsync(
$"UPDATE _users SET _password = '{legacy}' WHERE _id = {user.Id}");
var validated = await Redb.UserProvider.ValidateUserAsync(login, "old-password");
validated.Should().NotBeNull("a legacy SHA256+salt hash must still authenticate");
var rejected = await Redb.UserProvider.ValidateUserAsync(login, "wrong-password");
rejected.Should().BeNull("the legacy path must still reject a wrong password");
}
[Fact]
public async Task SetPasswordHash_StoresAHashMadeElsewhere()
{
// An administrator password given in configuration as a hash (htpasswd -B writes $2y$):
// stored as is, and the default hasher accepts it at login.
var login = FreshLogin();
var user = await Redb.UserProvider.CreateUserAsync(
new CreateUserRequest { Login = login, Name = login, Password = "placeholder" });
var hash = BCrypt.Net.BCrypt.HashPassword("from-config", BCrypt.Net.BCrypt.GenerateSalt(4, 'y'));
(await Redb.UserProvider.SetPasswordHashAsync(user, hash)).Should().BeTrue();
(await Redb.Context.ExecuteScalarAsync($"SELECT _password FROM _users WHERE _id = {user.Id}"))
.Should().Be(hash, "the hash is written as given, not hashed again");
(await Redb.UserProvider.ValidateUserAsync(login, "from-config")).Should().NotBeNull();
(await Redb.UserProvider.ValidateUserAsync(login, "placeholder")).Should().BeNull();
await FluentActions.Awaiting(() => Redb.UserProvider.SetPasswordHashAsync(user, " "))
.Should().ThrowAsync();
}
}