fix(secrets-cli): bundle import validates every entry before writing (scadaproj#45)
Secrets-018: BundleService.ImportAsync now rehydrates every entry and re-wraps every foreign-KEK
row before the first write, so a malformed or tampered entry anywhere fails the import with the
store untouched ("Nothing was imported"). A store fault during the apply phase (no multi-row
transaction) now reports the row it stopped at and every row already written, which the
interactive flow prints, instead of a bare "Import failed".
Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -374,4 +374,93 @@ public sealed class BundleServiceTests : IDisposable
|
||||
StoredSecret? dst = await target.Store.GetAsync(new SecretName("svc/cross"), CancellationToken.None);
|
||||
Assert.Null(dst);
|
||||
}
|
||||
|
||||
// ---------- Secrets-018: a bad entry anywhere fails the import before anything is written ----------
|
||||
|
||||
[Fact]
|
||||
public async Task A_tampered_foreign_kek_entry_late_in_the_bundle_leaves_the_store_untouched()
|
||||
{
|
||||
LiteralMasterKeyProvider kekA = NewKek();
|
||||
LiteralMasterKeyProvider kekB = NewKek();
|
||||
SecretsSession source = BuildSession(Db("a.db"), kekA);
|
||||
await SealAsync(source, "svc/first", "v1");
|
||||
await SealAsync(source, "svc/second", "v2");
|
||||
StoredSecret first = (await source.Store.GetAsync(new SecretName("svc/first"), CancellationToken.None))!;
|
||||
StoredSecret second = (await source.Store.GetAsync(new SecretName("svc/second"), CancellationToken.None))!;
|
||||
byte[] tamperedTag = (byte[])second.WrapTag.Clone();
|
||||
tamperedTag[0] ^= 0xFF;
|
||||
WriteBundle(kekA.KekId, first, second with { WrapTag = tamperedTag });
|
||||
|
||||
SecretsSession target = BuildSession(Db("b.db"), kekB);
|
||||
InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => new BundleService().ImportAsync(target, BundlePath, sourceKek: kekA, conflictOverride: null, CancellationToken.None));
|
||||
|
||||
Assert.Contains("svc/second", ex.Message, StringComparison.Ordinal);
|
||||
Assert.Contains("Nothing was imported", ex.Message, StringComparison.Ordinal);
|
||||
Assert.Null(await target.Store.GetAsync(new SecretName("svc/first"), CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_malformed_entry_after_good_ones_leaves_the_store_untouched()
|
||||
{
|
||||
LiteralMasterKeyProvider kek = NewKek();
|
||||
SecretsSession source = BuildSession(Db("a.db"), kek);
|
||||
await SealAsync(source, "svc/good", "v");
|
||||
StoredSecret good = (await source.Store.GetAsync(new SecretName("svc/good"), CancellationToken.None))!;
|
||||
WriteBundle(kek.KekId, good, good with { Name = new SecretName("svc/bad") });
|
||||
|
||||
// Corrupt the second entry's ciphertext base64 in the file.
|
||||
string json = await File.ReadAllTextAsync(BundlePath);
|
||||
int at = json.LastIndexOf("\"Ciphertext\"", StringComparison.Ordinal);
|
||||
int open = json.IndexOf('"', json.IndexOf(':', at)) + 1;
|
||||
json = json[..open] + "@@not-base64@@" + json[json.IndexOf('"', open)..];
|
||||
await File.WriteAllTextAsync(BundlePath, json);
|
||||
|
||||
SecretsSession target = BuildSession(Db("b.db"), kek);
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => new BundleService().ImportAsync(target, BundlePath, sourceKek: null, conflictOverride: null, CancellationToken.None));
|
||||
|
||||
Assert.Null(await target.Store.GetAsync(new SecretName("svc/good"), CancellationToken.None));
|
||||
}
|
||||
|
||||
private sealed class FailOnNameStore(ISecretStore inner, string failName) : ISecretStore
|
||||
{
|
||||
public Task<StoredSecret?> GetAsync(SecretName name, CancellationToken ct) => inner.GetAsync(name, ct);
|
||||
public Task UpsertAsync(StoredSecret row, CancellationToken ct) => inner.UpsertAsync(row, ct);
|
||||
public Task<bool> DeleteAsync(SecretName name, string? actor, CancellationToken ct) => inner.DeleteAsync(name, actor, ct);
|
||||
public Task<IReadOnlyList<SecretMetadata>> ListAsync(bool includeDeleted, CancellationToken ct) => inner.ListAsync(includeDeleted, ct);
|
||||
public Task<IReadOnlyList<SecretManifestEntry>> GetManifestAsync(CancellationToken ct) => inner.GetManifestAsync(ct);
|
||||
public Task ApplyReplicatedAsync(StoredSecret row, CancellationToken ct) =>
|
||||
row.Name.Value == failName ? throw new IOException("disk full") : inner.ApplyReplicatedAsync(row, ct);
|
||||
public Task<bool> ApplyRewrapAsync(StoredSecret rewrappedRow, byte[] expectedCurrentWrappedDek, CancellationToken ct) =>
|
||||
inner.ApplyRewrapAsync(rewrappedRow, expectedCurrentWrappedDek, ct);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_store_fault_mid_import_reports_which_rows_were_already_written()
|
||||
{
|
||||
LiteralMasterKeyProvider kek = NewKek();
|
||||
SecretsSession source = BuildSession(Db("a.db"), kek);
|
||||
await SealAsync(source, "svc/a", "1");
|
||||
await SealAsync(source, "svc/b", "2");
|
||||
await SealAsync(source, "svc/c", "3");
|
||||
await new BundleService().ExportAsync(source, BundlePath, includeDeleted: false, CancellationToken.None);
|
||||
|
||||
SecretsSession built = BuildSession(Db("b.db"), kek);
|
||||
var target = new SecretsSession
|
||||
{
|
||||
Target = built.Target,
|
||||
Store = new FailOnNameStore(built.Store, "svc/b"),
|
||||
Migrator = built.Migrator,
|
||||
MasterKey = built.MasterKey,
|
||||
Cipher = built.Cipher,
|
||||
StoreKind = built.StoreKind,
|
||||
};
|
||||
|
||||
InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => new BundleService().ImportAsync(target, BundlePath, sourceKek: null, conflictOverride: null, CancellationToken.None));
|
||||
|
||||
Assert.Contains("stopped at 'svc/b'", ex.Message, StringComparison.Ordinal);
|
||||
Assert.Contains("ALREADY written: svc/a", ex.Message, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user