Files
DodoSSH/src/DodoSSH.Client.Domain/SpelunkKeySecretCodec.cs
T
jaap-jan b08f2c58e3
ci / build and test (pull_request) Failing after 3m42s
ci / desktop nightly (pull_request) Skipped
ci / api image (pull_request) Skipped
ci / android head (pull_request) Successful in 5m24s
Fill in the Spelunk API key on the Security page, as a synced Spelunk key that goes only to its own address
Settings → Security gains a SPELUNK section: an API address and a key, saved as a new keychain item, a Spelunk key (SyncEntityType 14, AadResourceType 17, server table spelunk_key via the AddSpelunkKeyItem migration). It is ciphertext only on the server, lives in the personal vault, syncs like every secret, and holds the key together with the one address it may be sent to. The key box empties once saved; the list shows name, address and how many hosts use each key, never the key. Saving a key for an address that already has one replaces it, and deleting goes through the shared confirmation card with the hosts that would stop connecting.

A host's route names a saved key where it named a credential, in the same apiKeyCredentialId field, so no host schema changes and an older build reads such a host as one whose key credential has gone. At connect a saved key is sent only to its own address; a host naming it with another Spelunk address is refused in words. Keys kept as a credential's password still work. The editor offers saved keys first and picks the one saved for an address as it is typed.

A link from a Spelunk with a saved key now connects straight away, including the first one: saving the key is the user's say-so that the first-link editor used to ask for.

The address and key merge as one pair, so two concurrent edits can never pair one device's key with another's address, and the key never reaches the conflict log.
2026-09-29 10:34:38 +02:00

113 lines
3.8 KiB
C#

using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace DodoSSH.Client.Domain;
/// <summary>A decoded Spelunk key payload, together with the schema version it was written at.</summary>
/// <param name="Key">The key.</param>
/// <param name="SchemaVersion">The version the writing client used.</param>
public sealed record SpelunkKeySecretDocument(SpelunkKeySecret Key, int SchemaVersion)
{
/// <inheritdoc cref="HostSecretDocument.IsReadOnly" />
public bool IsReadOnly => SchemaVersion > SpelunkKeySecretCodec.CurrentSchemaVersion;
}
/// <summary>
/// Encodes and decodes the plaintext inside a Spelunk key item's encrypted payload.
/// </summary>
/// <remarks>
/// Mirrors <see cref="ObjectStoreSecretCodec"/>, for the same reasons and with the same guarantees.
/// </remarks>
public static class SpelunkKeySecretCodec
{
/// <summary>The schema version this build writes.</summary>
public const int CurrentSchemaVersion = 1;
/// <summary>Serialises a key to the bytes that get sealed.</summary>
/// <exception cref="ArgumentException">The key is not valid for storage.</exception>
public static byte[] Encode(SpelunkKeySecret key)
{
ArgumentNullException.ThrowIfNull(key);
if (!key.TryValidate(out var reason))
{
throw new ArgumentException(reason, nameof(key));
}
var document = new SpelunkKeyPayloadDocument
{
SchemaVersion = CurrentSchemaVersion,
Label = key.Label,
ApiBase = key.ApiBase,
ApiKey = key.ApiKey,
};
return JsonSerializer.SerializeToUtf8Bytes(
document, SpelunkKeyPayloadJsonContext.Default.SpelunkKeyPayloadDocument);
}
/// <summary>Parses a decrypted payload.</summary>
/// <inheritdoc cref="HostSecretCodec.TryDecode" path="/remarks" />
public static bool TryDecode(
ReadOnlySpan<byte> payload,
[NotNullWhen(true)] out SpelunkKeySecretDocument? document)
{
document = null;
SpelunkKeyPayloadDocument? parsed;
try
{
parsed = JsonSerializer.Deserialize(
payload, SpelunkKeyPayloadJsonContext.Default.SpelunkKeyPayloadDocument);
}
catch (JsonException)
{
return false;
}
if (parsed is null || parsed.SchemaVersion < 1)
{
return false;
}
var candidate = new SpelunkKeySecret
{
Label = parsed.Label ?? string.Empty,
ApiBase = parsed.ApiBase ?? string.Empty,
ApiKey = parsed.ApiKey ?? string.Empty,
};
// A key whose address is not in its normal form is refused rather than normalised here: the address
// is what decides where the key may go, and a payload that disagrees with how every client writes it
// is not one to guess about.
if (!candidate.TryValidate(out _))
{
return false;
}
document = new SpelunkKeySecretDocument(candidate, parsed.SchemaVersion);
return true;
}
}
/// <summary>The serialised shape. Mutable and nullable because it models untrusted input.</summary>
/// <inheritdoc cref="HostPayloadDocument" path="/remarks" />
internal sealed class SpelunkKeyPayloadDocument
{
public int SchemaVersion { get; set; }
public string? Label { get; set; }
public string? ApiBase { get; set; }
public string? ApiKey { get; set; }
}
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip)]
[JsonSerializable(typeof(SpelunkKeyPayloadDocument))]
internal sealed partial class SpelunkKeyPayloadJsonContext : JsonSerializerContext;