Public Access
Completes the client half of SSH keys: they sync alongside hosts, appear in their own list, and can be selected to authenticate a connection instead of typing a password. The reconciler and the repository were Host-typed throughout, so the choice was to generalise them or to keep a second copy per item type. Generalised, because ItemReconciler's whole premise is that the pull and the push paths must answer the same collision the same way — two copies would drift the first time one of them was fixed. What is genuinely per-type now arrives through IItemKind<TSecret>: the cipher, the merge, the plaintext columns, and the noun to use when telling a person what happened to their item. Generic where the server's IItemKind is not, and for the reason that reverses there — the client needs the concrete type, because it merges field by field. The pull filter is derived from the same registry that builds the reconcilers. That is the specific failure being designed out: an item type that encrypts, merges and lists perfectly and is never once requested from the server, so it works on the machine that made it and exists nowhere else. No client cache migration. The item table's primary key and the outbox's unique index already carry the entity type, and AadResourceTypes already mapped SshKey — so a host and a key may share an id and never see each other's rows, which SshKeySyncTests now arranges deliberately. A key hands the server nothing in plaintext. There is a public_key_fingerprint column and it would be accepted; leaving it null is deliberate. A fingerprint is not secret but it is a stable identifier for a key pair, so filling it would let an operator tell which of their users hold the same key and correlate one across vaults, for a column nothing reads. The design allows itself one plaintext concession — the relay address, which the relay cannot work without — and this is not that. A key is chosen per connection rather than bound to a host, which works the way ssh -i does. Binding one needs a field on HostSecret and therefore a payload schema bump, which makes every host written afterwards read-only on an older build; worth doing deliberately rather than as a side effect of adding keys. Three things this found, all of them by being falsified rather than by review: - Making the reconciler generic silently turned a record comparison into reference equality, because == on a type parameter is not value equality. The effect would have been a conflict recorded on every pass for an unacknowledged create that had in fact landed. Sabotaging the fix left all 73 tests passing — nothing covered that branch — so ConflictMatrixTests now has AnUnacknowledgedCreateThatDidLand_IsDroppedQuietly, which fails without it. - A test asserting that a blank passphrase reaches SSH.NET as null was vacuous: it exercised the editor, not the credential path, and passed with the guard deleted. Resolved by making SshKeySecret.Passphrase normalise an empty string to null, so there is one spelling of one state — which also keeps two clients from producing different payload bytes for an identical key. That exposed a wider gap: SshKeySecret, its codec and its merge had no direct unit tests at all. They have 25 now. - The reason first given for that normalisation was false. It claimed SSH.NET rejects a passphrase supplied for an unprotected key; measured against a real sshd it ignores it and authenticates anyway. Corrected everywhere it was stated and recorded in docs/platform-flags.md. The same test file also closes a real hole: SshPrivateKeyCredential had never been exercised against a server, because the existing key test builds SSH.NET's auth method directly and bypasses the path a vault-held key actually takes. Only one editor may be open at a time. Both sit in the same 340-pixel column as Auto rows and their heights together exceed it at the window's minimum size, so two open editors put the lower one's Save and Cancel past the bottom edge — the same failure this window already shipped once with the setup screens. Expressed as a state rule because that is the only form of it this repository can check: nothing here loads a .axaml. The refusal keeps what was typed, since in the key editor that is a pasted private key the user may have nowhere else. The end-to-end slice now carries a key as well as a host, so both item types go through the real API, the real PostgreSQL and the real crypto in one pass — the three hand-kept mappings between enums that do not line up are the reason that is worth doing rather than trusting the unit suites. 735 tests green, including the container-backed SSH and end-to-end suites. Zero warnings, dotnet format clean.
312 lines
12 KiB
C#
312 lines
12 KiB
C#
using DodoSSH.Client.Domain;
|
|
using DodoSSH.Client.Storage;
|
|
using DodoSSH.Crypto;
|
|
|
|
namespace DodoSSH.Client.Session.Tests;
|
|
|
|
/// <summary>
|
|
/// Enrolling once, then unlocking with nothing but a passphrase and a file.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The headline property here is that the second half needs no server at all. That is asserted directly:
|
|
/// every unlock in this suite runs against a <see cref="SessionOpener"/> that has never been given a
|
|
/// transport and could not reach one if it wanted to.
|
|
/// </remarks>
|
|
public sealed class SessionLifecycleTests : IAsyncLifetime
|
|
{
|
|
private const string Passphrase = "correct horse battery staple";
|
|
private const string ServerUrl = "https://dodossh.example";
|
|
|
|
/// <remarks>
|
|
/// Far below the shipped 256 MiB profile. The stretching is what makes a stolen wrap expensive to
|
|
/// attack and none of these tests attack one; paying a third of a second per derivation — and there
|
|
/// are three per enroll-and-unlock cycle — would only encourage sharing state between tests.
|
|
/// </remarks>
|
|
private static readonly Argon2Profile CheapProfile =
|
|
Argon2Profile.FromStoredParameters(memoryKibibytes: 8 * 1024, passes: 1, parallelism: 1);
|
|
|
|
private readonly FakeAccountServer server = new();
|
|
private readonly StubKeyBinding keyBinding = new();
|
|
|
|
private ClientCacheFactory caches = null!;
|
|
|
|
/// <inheritdoc />
|
|
public async ValueTask InitializeAsync()
|
|
{
|
|
caches = ClientCacheFactory.ForMemory($"session-{Guid.CreateVersion7():N}");
|
|
await caches.MigrateAsync(TestContext.Current.CancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
caches.Dispose();
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AFreshMachine_HasNothingToUnlock()
|
|
{
|
|
(await Opener().ReadProfileAsync(Token)).ShouldBeNull();
|
|
|
|
var outcome = await Opener().UnlockAsync(Passphrase, Token);
|
|
|
|
outcome.Status.ShouldBe(UnlockStatus.NotEnrolled);
|
|
outcome.Session.ShouldBeNull();
|
|
outcome.Message.ShouldContain("not enrolled");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task EnrollingLeavesEverythingAnOfflineUnlockNeeds()
|
|
{
|
|
// The property the whole storage layer exists for. After this point the passphrase alone opens
|
|
// the vault: no salt is fetched, no grant is fetched, nothing is asked of a server.
|
|
var provision = await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
provision.Status.ShouldBe(ProvisionStatus.Ready);
|
|
provision.RecoveryCode.ShouldNotBeNullOrWhiteSpace();
|
|
|
|
await using var session = await UnlockAsync();
|
|
|
|
session.Profile.UserId.ShouldBe(server.UserId);
|
|
session.Profile.ServerUrl.ShouldBe(ServerUrl);
|
|
session.Profile.Issuer.ShouldBe(FakeAccountServer.Issuer);
|
|
session.Vaults.ShouldHaveSingleItem().Name.ShouldBe("Personal");
|
|
session.UnreadableVaults.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheProfileCanBeReadWithoutThePassphrase()
|
|
{
|
|
// So the unlock screen can say who it is asking, rather than showing an unexplained password box.
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
var profile = await Opener().ReadProfileAsync(Token);
|
|
|
|
profile.ShouldNotBeNull();
|
|
profile.Email.ShouldBe("alice@example.com");
|
|
profile.DisplayName.ShouldBe("Alice");
|
|
profile.ServerUrl.ShouldBe(ServerUrl);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheWrongPassphrase_IsAnAnswerRatherThanAnException()
|
|
{
|
|
// The overwhelmingly common failure. It is also indistinguishable from a tampered wrap, which is
|
|
// correct: the AEAD tag is the only evidence either way and no verifier is stored anywhere.
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
var outcome = await Opener().UnlockAsync("not the passphrase", Token);
|
|
|
|
outcome.Status.ShouldBe(UnlockStatus.WrongPassphrase);
|
|
outcome.Session.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task NoDeviceKeyIsRegistered()
|
|
{
|
|
// A device wrap whose private half has nowhere to live is a row nobody can ever open, and it would
|
|
// make the account's device list claim this machine can unlock without a passphrase. Until the OS
|
|
// keystore is wired, not offering it is the honest answer.
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
var request = server.LastEnrollment.ShouldNotBeNull();
|
|
request.DevicePublicKey.ShouldBeNull();
|
|
request.DeviceWrappedPrivateKey.ShouldBeNull();
|
|
|
|
// The recovery wrap is still registered: it is the only route back if the passphrase is lost.
|
|
request.RecoveryWrappedPrivateKey.ShouldNotBeNull();
|
|
request.RecoveryKdfParameters.ShouldNotBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnAlreadyEnrolledAccount_IsNotEnrolledAgain()
|
|
{
|
|
// Re-enrolling would replace an identity key that other members may already have wrapped vault
|
|
// keys to, which is a far worse outcome than asking for the existing passphrase.
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
var second = await Provisioner().EnrollAsync(ServerUrl, "a different one", "desktop", "Personal", Token);
|
|
|
|
second.Status.ShouldBe(ProvisionStatus.Ready);
|
|
second.RecoveryCode.ShouldBeNull();
|
|
server.EnrollmentCount.ShouldBe(1);
|
|
|
|
// And the original passphrase still works, because nothing was replaced.
|
|
await using var session = await UnlockAsync();
|
|
session.Vaults.ShouldHaveSingleItem();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SigningInToAnUnenrolledAccount_AsksForEnrollmentRatherThanFailing()
|
|
{
|
|
var outcome = await Provisioner().RefreshAsync(ServerUrl, Token);
|
|
|
|
outcome.Status.ShouldBe(ProvisionStatus.EnrollmentRequired);
|
|
outcome.Me.EnrollmentRequired.ShouldBeTrue();
|
|
|
|
// Nothing was cached, so an unlock still reports honestly.
|
|
(await Opener().ReadProfileAsync(Token)).ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefreshingAnEnrolledAccount_RepairsACacheThatLostItsVaults()
|
|
{
|
|
// What signing in on a machine whose cache was cleared looks like. The material comes back from
|
|
// the server, and the passphrase — which the server never had — opens it again.
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
using var replacement = ClientCacheFactory.ForMemory($"session-{Guid.CreateVersion7():N}");
|
|
await replacement.MigrateAsync(Token);
|
|
|
|
var outcome = await new AccountProvisioner(
|
|
server, keyBinding, replacement, TimeProvider.System, CheapProfile)
|
|
.RefreshAsync(ServerUrl, Token);
|
|
|
|
outcome.Status.ShouldBe(ProvisionStatus.Ready);
|
|
|
|
var unlocked = await new SessionOpener(replacement, TimeProvider.System)
|
|
.UnlockAsync(Passphrase, Token);
|
|
|
|
unlocked.IsUnlocked.ShouldBeTrue(unlocked.Message);
|
|
await unlocked.Session!.DisposeAsync();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AVaultWhoseGrantWasRevoked_SaysSoRatherThanLookingEmpty()
|
|
{
|
|
// A rekey this client has not been re-issued for. Reporting a wrong passphrase here would send the
|
|
// user to retype something that was never the problem.
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
server.RevokeVaultGrant();
|
|
await Provisioner().RefreshAsync(ServerUrl, Token);
|
|
|
|
var outcome = await Opener().UnlockAsync(Passphrase, Token);
|
|
|
|
outcome.Status.ShouldBe(UnlockStatus.NoReadableVault);
|
|
outcome.Message.ShouldContain("rotated");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnUnsupportedKdf_IsNamedRatherThanThrowingFromLibsodium()
|
|
{
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
var profile = (await Opener().ReadProfileAsync(Token)).ShouldNotBeNull();
|
|
|
|
await new UnlockStore(caches, TimeProvider.System).SaveAsync(
|
|
profile with
|
|
{
|
|
KdfParameters = profile.KdfParameters with { Algorithm = "argon2-from-the-future" },
|
|
},
|
|
Token);
|
|
|
|
var outcome = await Opener().UnlockAsync(Passphrase, Token);
|
|
|
|
outcome.Status.ShouldBe(UnlockStatus.UnsupportedKdf);
|
|
outcome.Message.ShouldContain("argon2-from-the-future");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnUnlockedSession_ReadsAndWritesHostsWithNoServer()
|
|
{
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
await using var session = await UnlockAsync();
|
|
|
|
var entityId = await session.Hosts.CreateAsync(
|
|
session.ActiveVaultId, Host("prod-db"), Token);
|
|
|
|
var listing = await session.Hosts.ListAsync(session.ActiveVaultId, Token);
|
|
|
|
var host = listing.Items.ShouldHaveSingleItem();
|
|
host.EntityId.ShouldBe(entityId);
|
|
host.Secret.Label.ShouldBe("prod-db");
|
|
host.HasUnsyncedChanges.ShouldBeTrue();
|
|
|
|
(await session.PendingChangeCountAsync(Token)).ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ASessionSyncsThroughWhicheverTransportItIsHanded()
|
|
{
|
|
// The session deliberately holds no transport: losing the network invalidates the connection, not
|
|
// the vault.
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
await using var session = await UnlockAsync();
|
|
await session.Hosts.CreateAsync(session.ActiveVaultId, Host("prod-db"), Token);
|
|
|
|
var transport = new EmptySyncApi();
|
|
var report = await session.SyncAsync(transport, Token);
|
|
|
|
report.Pushed.ShouldBe(1);
|
|
transport.PushCount.ShouldBe(1);
|
|
(await session.PendingChangeCountAsync(Token)).ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ADisposedSession_RefusesToBeUsed()
|
|
{
|
|
// Locking is disposing, so this is what "locked" has to mean in practice.
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
var session = await UnlockAsync();
|
|
var vaultId = session.ActiveVaultId;
|
|
|
|
await session.DisposeAsync();
|
|
|
|
await Should.ThrowAsync<ObjectDisposedException>(
|
|
async () => await session.ReadConflictsAsync(Token));
|
|
|
|
await Should.ThrowAsync<ObjectDisposedException>(
|
|
async () => await session.Hosts.ListAsync(vaultId, Token));
|
|
|
|
// Idempotent, because shutdown paths call it more than once.
|
|
await session.DisposeAsync();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ASecondUnlock_ProducesAnIndependentSession()
|
|
{
|
|
// Two windows, or a lock followed by an unlock. Disposing one must not take the other's keys with
|
|
// it, which it would if anything here were shared statically.
|
|
await Provisioner().EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
var first = await UnlockAsync();
|
|
await using var second = await UnlockAsync();
|
|
|
|
await first.DisposeAsync();
|
|
|
|
var listing = await second.Hosts.ListAsync(second.ActiveVaultId, Token);
|
|
listing.Unreadable.ShouldBe(0);
|
|
}
|
|
|
|
// ---- Helpers ----
|
|
|
|
private static CancellationToken Token => TestContext.Current.CancellationToken;
|
|
|
|
private SessionOpener Opener() => new(caches, TimeProvider.System);
|
|
|
|
private AccountProvisioner Provisioner() =>
|
|
new(server, keyBinding, caches, TimeProvider.System, CheapProfile);
|
|
|
|
private async Task<VaultSession> UnlockAsync()
|
|
{
|
|
var outcome = await Opener().UnlockAsync(Passphrase, Token);
|
|
|
|
outcome.IsUnlocked.ShouldBeTrue(outcome.Message);
|
|
return outcome.Session!;
|
|
}
|
|
|
|
private static HostSecret Host(string label) =>
|
|
new()
|
|
{
|
|
Label = label,
|
|
Hostname = "db.internal",
|
|
Port = 22,
|
|
Username = "deploy",
|
|
};
|
|
}
|