Close what the security review found
A session alone could change the password, switch the second factor on or off, send the key of the language model to another address and remove the rules that stop purchases. Each now needs the password and the code, with one limit on wrong attempts that a restart of the services does not clear, and the other sessions end when the password or the factor changes. Requests to the brokers pass a list of allowed calls, never follow a redirect with a key and are signed by one client only. Pages are closed by default, nothing is cached, requests are limited to 1 MB, secrets are masked in logs and notifications, exported tables cannot run as formulas, archives carry no sessions and the container refuses to run as root. refs #68 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4897594b87
commit
4d92a39fef
35 files changed
+907
-118
No files matched your search
@@ -265,15 +265,26 @@ public sealed class AccessTests
|
||||
|
||||
// Second session, then a password change ends every other session.
|
||||
var (_, other) = await access.SignInAsync("initial-pass-1", null, null, null).ConfigureAwait(false);
|
||||
Assert.IsTrue(await access.ChangePasswordAsync(session.User.Id, "initial-pass-1", "una password nuova", session.Id).ConfigureAwait(false));
|
||||
var stolen = token;
|
||||
(var changed, token) = await access.ChangePasswordAsync(session.User.Id, "initial-pass-1", "una password nuova", session.Id).ConfigureAwait(false);
|
||||
Assert.AreEqual(AccessChange.Done, changed);
|
||||
Assert.IsNull(await access.ValidateAsync(stolen).ConfigureAwait(false), "A copy of the cookie stops working too.");
|
||||
Assert.IsNotNull(await access.ValidateAsync(token).ConfigureAwait(false));
|
||||
Assert.IsNull(await access.ValidateAsync(other).ConfigureAwait(false));
|
||||
Assert.IsFalse((await access.ValidateAsync(token).ConfigureAwait(false))!.User.MustChangePassword);
|
||||
|
||||
// Second factor.
|
||||
var secret = Totp.NewSecret();
|
||||
Assert.IsFalse(await access.EnableSecondFactorAsync(session.User.Id, secret, Totp.Compute(secret, clock.UtcNow.AddMinutes(10))).ConfigureAwait(false));
|
||||
Assert.IsTrue(await access.EnableSecondFactorAsync(session.User.Id, secret, Totp.Compute(secret, clock.UtcNow)).ConfigureAwait(false));
|
||||
var (_, elsewhere) = await access.SignInAsync("una password nuova", null, null, null).ConfigureAwait(false);
|
||||
Task<AccessChange> EnableAsync(byte[] with, string password, string number) => access.EnableSecondFactorAsync(session.User.Id, with, password, number, session.Id);
|
||||
Assert.AreEqual(AccessChange.Wrong, await EnableAsync(secret, "una password nuova", Totp.Compute(secret, clock.UtcNow.AddMinutes(10))).ConfigureAwait(false));
|
||||
Assert.AreEqual(AccessChange.Wrong, await EnableAsync(secret, "not the password", Totp.Compute(secret, clock.UtcNow)).ConfigureAwait(false), "A session alone is not enough.");
|
||||
Assert.IsNotNull(await access.ValidateAsync(elsewhere).ConfigureAwait(false));
|
||||
Assert.AreEqual(AccessChange.Done, await EnableAsync(secret, "una password nuova", Totp.Compute(secret, clock.UtcNow)).ConfigureAwait(false));
|
||||
Assert.IsNull(await access.ValidateAsync(elsewhere).ConfigureAwait(false), "The other sessions signed in without the factor: they end.");
|
||||
Assert.IsNotNull(await access.ValidateAsync(token).ConfigureAwait(false));
|
||||
var draft = Totp.NewSecret();
|
||||
Assert.AreEqual(AccessChange.AlreadyOn, await EnableAsync(draft, "una password nuova", Totp.Compute(draft, clock.UtcNow)).ConfigureAwait(false), "Never over an active one.");
|
||||
Assert.AreEqual(LoginResult.SecondFactorNeeded, (await access.SignInAsync("una password nuova", null, null, null).ConfigureAwait(false)).Result);
|
||||
var badCode = Totp.Compute(secret, clock.UtcNow.AddMinutes(10));
|
||||
Assert.AreEqual(LoginResult.WrongSecondFactor, (await access.SignInAsync("una password nuova", badCode, null, null).ConfigureAwait(false)).Result);
|
||||
@@ -287,8 +298,8 @@ public sealed class AccessTests
|
||||
|
||||
// Switching it off asks for the password and a fresh code.
|
||||
clock.Advance(TimeSpan.FromSeconds(30));
|
||||
Assert.IsFalse(await access.DisableSecondFactorAsync(session.User.Id, "una password nuova", null).ConfigureAwait(false));
|
||||
Assert.IsTrue(await access.DisableSecondFactorAsync(session.User.Id, "una password nuova", Totp.Compute(secret, clock.UtcNow)).ConfigureAwait(false));
|
||||
Assert.AreEqual(AccessChange.Wrong, await access.DisableSecondFactorAsync(session.User.Id, "una password nuova", null, session.Id).ConfigureAwait(false));
|
||||
Assert.AreEqual(AccessChange.Done, await access.DisableSecondFactorAsync(session.User.Id, "una password nuova", Totp.Compute(secret, clock.UtcNow), session.Id).ConfigureAwait(false));
|
||||
|
||||
// Idle and absolute timeouts.
|
||||
clock.Advance(AccessStore.IdleTimeout);
|
||||
@@ -318,4 +329,73 @@ public sealed class AccessTests
|
||||
clock.Advance(LoginLimiter.Window);
|
||||
Assert.IsTrue(await access.ReauthenticateAsync("password-giusta", null).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Integration)]
|
||||
public async Task EveryCheckOfThePasswordSharesOneLimitThatTheServicesCannotClear()
|
||||
{
|
||||
var clock = new ManualClock(new DateTimeOffset(2026, 11, 30, 9, 0, 0, TimeSpan.Zero));
|
||||
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
|
||||
var guards = new AccessGuards(clock);
|
||||
AccessStore Started() => new(test.Db, clock, new SecretStore(test.Db, clock, new byte[32]), iterations: 1000, guards);
|
||||
var access = Started();
|
||||
Assert.IsTrue(await access.EnsureUserAsync("password-giusta", mustChange: false).ConfigureAwait(false));
|
||||
var user = (await access.GetUserAsync().ConfigureAwait(false))!.Id;
|
||||
var secret = Totp.NewSecret();
|
||||
|
||||
Assert.AreEqual(AccessChange.Wrong, (await access.ChangePasswordAsync(user, "sbagliata-1", "una password nuova", Guid.Empty).ConfigureAwait(false)).Outcome);
|
||||
Assert.AreEqual(AccessChange.Wrong, await access.DisableSecondFactorAsync(user, "sbagliata-2", null, Guid.Empty).ConfigureAwait(false));
|
||||
Assert.AreEqual(AccessChange.Wrong, await access.EnableSecondFactorAsync(user, secret, "sbagliata-3", Totp.Compute(secret, clock.UtcNow), Guid.Empty).ConfigureAwait(false));
|
||||
Assert.IsFalse(await Started().ReauthenticateAsync("sbagliata-4", null).ConfigureAwait(false), "The services start again, the count goes on.");
|
||||
Assert.IsFalse(await Started().ReauthenticateAsync("sbagliata-5", null).ConfigureAwait(false));
|
||||
|
||||
access = Started();
|
||||
Assert.AreEqual(AccessChange.Locked, (await access.ChangePasswordAsync(user, "password-giusta", "una password nuova", Guid.Empty).ConfigureAwait(false)).Outcome);
|
||||
Assert.AreEqual(AccessChange.Locked, await access.DisableSecondFactorAsync(user, "password-giusta", null, Guid.Empty).ConfigureAwait(false));
|
||||
Assert.AreEqual(AccessChange.Locked, await access.EnableSecondFactorAsync(user, secret, "password-giusta", Totp.Compute(secret, clock.UtcNow), Guid.Empty).ConfigureAwait(false));
|
||||
clock.Advance(LoginLimiter.Window);
|
||||
var code = Totp.Compute(secret, clock.UtcNow);
|
||||
Assert.AreEqual(AccessChange.Done, await access.EnableSecondFactorAsync(user, secret, "password-giusta", code, Guid.Empty).ConfigureAwait(false));
|
||||
Assert.IsFalse(await Started().ReauthenticateAsync("password-giusta", code).ConfigureAwait(false), "A code used once stays used after the services start again.");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Integration)]
|
||||
public async Task EndedSessionsAreRemovedAtTheNextSignIn()
|
||||
{
|
||||
var clock = new ManualClock(new DateTimeOffset(2026, 11, 30, 9, 0, 0, TimeSpan.Zero));
|
||||
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
|
||||
var access = new AccessStore(test.Db, clock, new SecretStore(test.Db, clock, new byte[32]), iterations: 1000);
|
||||
Assert.IsTrue(await access.EnsureUserAsync("password-giusta", mustChange: false).ConfigureAwait(false));
|
||||
var (_, listening) = await access.SignInAsync("password-giusta", null, "browser", "10.0.0.1").ConfigureAwait(false);
|
||||
for (var i = 0; i < 12; i++)
|
||||
{
|
||||
Assert.IsNotNull(await access.ValidateAsync(listening, refresh: false).ConfigureAwait(false));
|
||||
clock.Advance(TimeSpan.FromHours(1));
|
||||
}
|
||||
|
||||
Assert.IsNull(await access.ValidateAsync(listening).ConfigureAwait(false), "Listening does not keep a session alive.");
|
||||
await access.SignInAsync("password-giusta", null, null, null).ConfigureAwait(false);
|
||||
Assert.AreEqual(1L, await test.Db.ReadAsync(s => s.CountAsync("SELECT COUNT(*) FROM sessions")).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void SuccessfulSignInsDoNotFillTheOverallCount()
|
||||
{
|
||||
var clock = new ManualClock(new DateTimeOffset(2026, 11, 30, 9, 0, 0, TimeSpan.Zero));
|
||||
var limiter = new LoginLimiter(clock);
|
||||
for (var i = 0; i < 3 * LoginLimiter.Overall; i++)
|
||||
{
|
||||
Assert.AreEqual(TimeSpan.Zero, limiter.Begin("10.0.0.1"));
|
||||
limiter.Succeeded("10.0.0.1");
|
||||
}
|
||||
|
||||
for (var i = 0; i < LoginLimiter.Overall; i++)
|
||||
{
|
||||
Assert.AreEqual(TimeSpan.Zero, limiter.Begin("10.0.1." + i));
|
||||
}
|
||||
|
||||
Assert.IsGreaterThan(TimeSpan.Zero, limiter.Begin("10.0.0.9"), "Twenty failures, from anywhere, still stop everyone.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user