Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
dddab15
Document managed rotation invariants and neutralize repository examples
magiccodingman Nov 26, 2026
13686ee
Authenticate record generation selection and prepare key conversion
magiccodingman Nov 26, 2026
6748a3b
Authenticate managed rotation progress with both generations
magiccodingman Nov 26, 2026
88bb07f
Compare stored rows and authenticated progress in one rotation transa…
magiccodingman Nov 26, 2026
7c3fd92
Advance bounded conversion and target verification from durable check…
magiccodingman Nov 26, 2026
02eca07
Bind rotation admission and verification to authenticated write fences
magiccodingman Nov 26, 2026
2787c84
Admit managed rotations and install a separate verification barrier
magiccodingman Nov 26, 2026
a46a11c
Fence protected operations by rotation mode and finalize authenticate…
magiccodingman Nov 26, 2026
ee65a02
Expose managed rotation lifecycle through browser and Blazor APIs
magiccodingman Nov 26, 2026
f440fcf
Revoke in-flight protected write transactions on lock
magiccodingman Nov 26, 2026
d954be2
Inspect key routing without closing the managed connection
magiccodingman Nov 26, 2026
54490ac
Fence final key handoff and exercise abrupt tab loss and record prote…
magiccodingman Nov 26, 2026
a0cee6c
Verify rotation preserves compound keys and unrelated stores
magiccodingman Nov 26, 2026
4d8e173
Validate rotation after real package upgrades and reject incomparable…
magiccodingman Nov 26, 2026
0bfac63
Keep the connection open during the verified key handoff
magiccodingman Nov 26, 2026
3acfb4f
Measure readiness fences separately from benchmark latency
magiccodingman Nov 26, 2026
ffe48e2
Protect retained recovery evidence and fence rotation authorization a…
magiccodingman Nov 27, 2026
8702708
Authenticate retained audit evidence before admitting rotation
magiccodingman Nov 27, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# These are supported funding model platforms

github: [magiccodingman]
custom: ['https://sayou.biz/support', 'https://paypal.me/lancewr']
custom: ['https://sayou.io/support', 'https://paypal.me/lancewr']
2 changes: 1 addition & 1 deletion .github/workflows/validate-migrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
node-version: 24

- name: Validate canonical JSON and portable document safety
run: node --test tests/migrations/protocol.test.mjs tests/migrations/protected-protocol.test.mjs tests/migrations/protected-adapter.test.mjs tests/migrations/registration.test.mjs
run: node --test tests/migrations/protocol.test.mjs tests/migrations/protected-protocol.test.mjs tests/migrations/protected-adapter.test.mjs tests/migrations/registration.test.mjs tests/migrations/rotation-record.test.mjs tests/migrations/rotation-journal.test.mjs

packaged-cli:
name: Packaged CLI contract
Expand Down
6 changes: 5 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ You can contribute by:

Contributions of all sizes are appreciated.

## Domain neutrality

Keep implementation, APIs, tests, fixtures, benchmarks, and developer documentation domain-neutral. Use fictional data in examples and avoid application-specific workflows, private infrastructure, and internal endpoints. Preserve authentic compatibility fixtures, applicable notices, and intentional project licensing, funding, and attribution references.

## Development philosophy

Magic IndexedDB aims to provide a reliable, intuitive, strongly typed, and production-oriented experience for developers working with browser-based databases.
Expand Down Expand Up @@ -70,7 +74,7 @@ If you discover a potential security vulnerability, please use the project's pri

Avoid publicly disclosing sensitive vulnerability details before the maintainers have had an opportunity to investigate and respond.

Do not include confidential information, production credentials, patient records, or other sensitive data in public issues or pull requests.
Do not include confidential information, production credentials, or other sensitive data in public issues or pull requests.

## Community conduct

Expand Down
74 changes: 68 additions & 6 deletions Magic.IndexedDb.UnitTests/ProtectedMigrationFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task AuthorizationPrecedesManagedInitialization()
public async Task LockCanRevokeAuthorizationWhileMigrationIsWaiting()
{
var browser = new Browser { HoldMigration = true };
await using var factory = Create(browser, new());
await using var factory = Create(browser, new Provider());
var unlocking = factory.UnlockDetailedAsync(Repository.Database);
await browser.MigrationStarted.Task.WaitAsync(TimeSpan.FromSeconds(5));

Expand All @@ -41,11 +41,50 @@ public async Task LockCanRevokeAuthorizationWhileMigrationIsWaiting()
Assert.IsTrue(browser.Calls.Contains("lockProtectedDatabase"));
}

[TestMethod]
public async Task LockReachesBrowserBeforePendingRotationAuthorizationCompletes()
{
var browser = new Browser { HoldRotationAuthorization = true };
await using var factory = Create(browser, new RotationProvider());
var unlocking = factory.UnlockDetailedAsync(Repository.Database);
await browser.RotationStarted.Task.WaitAsync(TimeSpan.FromSeconds(5));

try
{
await factory.LockAsync(Repository.Database).WaitAsync(TimeSpan.FromSeconds(5));
Assert.IsTrue(browser.Calls.Contains("lockProtectedDatabase"));
Assert.IsFalse(browser.RotationFinished.Task.IsCompleted);
}
finally
{
browser.RotationFinished.TrySetResult();
}

Assert.AreEqual(MagicUnlockResult.Cancelled, await unlocking.WaitAsync(TimeSpan.FromSeconds(5)));
Assert.IsFalse(browser.Calls.Contains("ensureProtectedMigration"));
Assert.IsFalse(browser.Calls.Contains("scheduleManagedKeyRotationResume"));
}

[TestMethod]
public async Task ReadyRotationStatusReportsOrdinaryReadAndWriteAvailability()
{
var browser = new Browser();
await using var factory = Create(browser, new Provider());
Assert.IsTrue(await factory.EnsureUnlockedAsync(Repository.Database));

var status = await factory.GetKeyRotationStatusAsync(Repository.Database);

Assert.AreEqual(MagicKeyRotationPhase.NotStarted, status.Phase);
Assert.IsTrue(status.ReadsAllowed);
Assert.IsTrue(status.WritesAllowed);
Assert.IsFalse(status.RetirementReady);
}

[TestMethod]
public async Task ExplicitBrowserCancellationReportsCancelled()
{
var browser = new Browser { MigrationDiagnostic = "MAGDB_CANCELLED: Migration was cancelled." };
await using var factory = Create(browser, new());
await using var factory = Create(browser, new Provider());

Assert.AreEqual(MagicUnlockResult.Cancelled, await factory.UnlockDetailedAsync(Repository.Database));
}
Expand All @@ -72,7 +111,7 @@ public async Task RecoveryAuthorizesWithoutRetryingTheFailedOriginalMigration()
public async Task UnavailableAuthorizationDoesNotStartRecovery()
{
var browser = new Browser();
await using var factory = Create(browser, new() { Available = false });
await using var factory = Create(browser, new Provider { Available = false });
var database = await factory.Database(Repository.Database);

var error = await Assert.ThrowsExactlyAsync<InvalidOperationException>(() =>
Expand All @@ -87,7 +126,7 @@ public async Task UnavailableAuthorizationDoesNotStartRecovery()
public async Task ExplicitPhysicalBindingIsRetainedAndDestroyCarriesItsRegisteredRoute()
{
var browser = new Browser();
await using var factory = Create(browser, new(), "existing-browser-records");
await using var factory = Create(browser, new Provider(), "existing-browser-records");
Assert.IsTrue(await factory.EnsureUnlockedAsync(Repository.Database));
var registered = browser.Registration!.Value;
Assert.AreEqual("existing-browser-records", registered.GetProperty("encryption").GetProperty("physicalName").GetString());
Expand All @@ -100,12 +139,12 @@ public async Task ExplicitPhysicalBindingIsRetainedAndDestroyCarriesItsRegistere
Assert.AreEqual(registered.GetProperty("name").GetString(), browser.DestroyedRoute);
}

private static MagicDbFactory Create(Browser browser, Provider provider, string? physicalName = null) => new(browser, 1024, false,
private static MagicDbFactory Create(Browser browser, IMagicEncryptionKeyProvider provider, string? physicalName = null) => new(browser, 1024, false,
tableTypes: [typeof(Row)], repositoryTypes: [typeof(Repository)],
encryption: new Dictionary<string, EncryptionConfiguration>
{
[Repository.Database.DatabaseName] = new(new MagicEncryptionOptions(Repository.Database.DatabaseName, "account", "key")
{ PhysicalDatabaseName = physicalName })
{ PhysicalDatabaseName = physicalName, ResumeKeyRotationOnUnlock = true })
}, keyProvider: provider);

private sealed class Provider : IMagicEncryptionKeyProvider
Expand All @@ -119,6 +158,15 @@ private sealed class Provider : IMagicEncryptionKeyProvider
}
}

private sealed class RotationProvider : IMagicEncryptionRotationKeyProvider
{
public ValueTask<MagicEncryptionKey?> GetKeyAsync(MagicEncryptionKeyRequest request, CancellationToken cancellationToken) =>
ValueTask.FromResult<MagicEncryptionKey?>(MagicEncryptionKey.FromBytes(new byte[32]));

public ValueTask<bool> CanReacquireKeysAsync(MagicEncryptionKeyRequest source, MagicEncryptionKeyRequest target,
CancellationToken cancellationToken) => ValueTask.FromResult(true);
}

private sealed class Browser : IJSRuntime, IJSObjectReference
{
public List<string> Calls { get; } = [];
Expand All @@ -128,6 +176,9 @@ private sealed class Browser : IJSRuntime, IJSObjectReference
public string? DestroyedPhysicalName { get; private set; }
public string? DestroyedRoute { get; private set; }
public bool HoldMigration { get; init; }
public bool HoldRotationAuthorization { get; init; }
public TaskCompletionSource RotationStarted { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
public TaskCompletionSource RotationFinished { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
public string? MigrationDiagnostic { get; init; }
public TaskCompletionSource MigrationStarted { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
private TaskCompletionSource<bool> MigrationFinished { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
Expand All @@ -151,6 +202,16 @@ public ValueTask<T> InvokeAsync<T>(string identifier, CancellationToken cancella
DestroyedPhysicalName = (string)parameters[0]!;
DestroyedRoute = (string?)parameters[1];
}
if (method == "getManagedKeyRotationStatus")
return ValueTask.FromResult((T)(object)JsonSerializer.SerializeToElement(new
{ phase = "not-started", readsAllowed = true, writesAllowed = true, retirementReady = false }));
if (method == "getManagedEncryptionKeyHints")
return ValueTask.FromResult((T)(object)JsonSerializer.SerializeToElement(new { keyId = "key", targetKeyId = "next" }));
if (method == "authorizeManagedKeyRotation" && HoldRotationAuthorization)
{
RotationStarted.TrySetResult();
return WaitForRotation<T>();
}
return ValueTask.FromResult((T)(object)(method != "isProtectedDatabaseUnlocked"));
}
Calls.Add(identifier);
Expand All @@ -170,6 +231,7 @@ public ValueTask<T> InvokeAsync<T>(string identifier, CancellationToken cancella
}

private async ValueTask<T> WaitForMigration<T>() { await MigrationFinished.Task; return default!; }
private async ValueTask<T> WaitForRotation<T>() { await RotationFinished.Task; return default!; }
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}

Expand Down
53 changes: 53 additions & 0 deletions Magic.IndexedDb.UnitTests/PublicApiBaseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ interface Magic.IndexedDb.IMagicEncryptionContexts
method System.Threading.Tasks.Task<Magic.IndexedDb.MagicEncryptionContext> ActivateAsync(System.Collections.Generic.IEnumerable<Magic.IndexedDb.MagicEncryptionOptions> databases)
interface Magic.IndexedDb.IMagicEncryptionKeyProvider
method System.Threading.Tasks.ValueTask<Magic.IndexedDb.MagicEncryptionKey> GetKeyAsync(Magic.IndexedDb.MagicEncryptionKeyRequest request, System.Threading.CancellationToken cancellationToken)
interface Magic.IndexedDb.IMagicEncryptionRotationKeyProvider : Magic.IndexedDb.IMagicEncryptionKeyProvider
method System.Threading.Tasks.ValueTask<System.Boolean> CanReacquireKeysAsync(Magic.IndexedDb.MagicEncryptionKeyRequest source, Magic.IndexedDb.MagicEncryptionKeyRequest target, System.Threading.CancellationToken cancellationToken)
interface Magic.IndexedDb.IMagicExecute<T>
method System.Collections.Generic.IAsyncEnumerable<T> AsAsyncEnumerable(System.Threading.CancellationToken cancellationToken = null)
method System.Threading.Tasks.Task<System.Collections.Generic.List<T>> ToListAsync()
Expand All @@ -138,6 +140,12 @@ interface Magic.IndexedDb.IMagicIndexedDbEncryption
interface Magic.IndexedDb.IMagicIndexedDbEncryptionLifecycle : Magic.IndexedDb.IMagicIndexedDbEncryption
method System.Threading.Tasks.Task<Magic.IndexedDb.MagicEncryptionState> GetStateAsync(Magic.IndexedDb.IndexedDbSet database, System.Threading.CancellationToken cancellationToken = null)
method System.Threading.Tasks.Task<Magic.IndexedDb.MagicUnlockResult> UnlockDetailedAsync(Magic.IndexedDb.IndexedDbSet database, System.Boolean initialize = false, System.Threading.CancellationToken cancellationToken = null)
interface Magic.IndexedDb.IMagicIndexedDbKeyRotation
method System.Threading.Tasks.Task CancelKeyRotationAsync(Magic.IndexedDb.IndexedDbSet database)
method System.Threading.Tasks.Task<Magic.IndexedDb.MagicKeyRotationStatus> GetKeyRotationStatusAsync(Magic.IndexedDb.IndexedDbSet database, System.Threading.CancellationToken cancellationToken = null)
method System.Threading.Tasks.Task<Magic.IndexedDb.MagicKeyRotationStatus> PauseKeyRotationAsync(Magic.IndexedDb.IndexedDbSet database, System.Threading.CancellationToken cancellationToken = null)
method System.Threading.Tasks.Task<Magic.IndexedDb.MagicKeyRotationStatus> ResumeKeyRotationAsync(Magic.IndexedDb.IndexedDbSet database, System.Int32 batchSize = 128, System.Threading.CancellationToken cancellationToken = null)
method System.Threading.Tasks.Task<Magic.IndexedDb.MagicKeyRotationStatus> RotateEncryptionKeyAsync(Magic.IndexedDb.IndexedDbSet database, Magic.IndexedDb.MagicKeyRotationOptions options, System.Threading.CancellationToken cancellationToken = null)
interface Magic.IndexedDb.IMagicQuery<T> : Magic.IndexedDb.IMagicExecute<T>
method Magic.IndexedDb.IMagicCursor<T> Cursor(System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> predicate)
method Magic.IndexedDb.IMagicQueryFinal<T> Skip(System.Int32 amount)
Expand Down Expand Up @@ -252,6 +260,7 @@ class Magic.IndexedDb.MagicEncryptionContext : System.IEquatable<Magic.IndexedDb
method System.Void Deconstruct(out Magic.IndexedDb.IMagicIndexedDb Database, out Magic.IndexedDb.IMagicIndexedDbEncryptionLifecycle Encryption)
property Magic.IndexedDb.IMagicIndexedDb Database { get; set; }
property Magic.IndexedDb.IMagicIndexedDbEncryptionLifecycle Encryption { get; set; }
property Magic.IndexedDb.IMagicIndexedDbKeyRotation KeyRotation { get; }
class Magic.IndexedDb.MagicEncryptionInteractionRequiredException : System.InvalidOperationException, System.Runtime.Serialization.ISerializable
ctor Magic.IndexedDb.MagicEncryptionInteractionRequiredException()
class Magic.IndexedDb.MagicEncryptionKey
Expand All @@ -277,6 +286,7 @@ class Magic.IndexedDb.MagicEncryptionOptions : System.IEquatable<Magic.IndexedDb
method System.Int32 GetHashCode()
method System.String ToString()
method System.Void Deconstruct(out System.String DatabaseName, out System.String AccountScope, out System.String KeyId, out System.Collections.Generic.IReadOnlyDictionary<System.String, System.Collections.Generic.IReadOnlyCollection<System.String>> RecordPolicies)
property System.Boolean ResumeKeyRotationOnUnlock { get; set; }
property System.Collections.Generic.IReadOnlyDictionary<System.String, System.Collections.Generic.IReadOnlyCollection<System.String>> RecordPolicies { get; set; }
property System.String AccountScope { get; set; }
property System.String DatabaseName { get; set; }
Expand All @@ -289,6 +299,49 @@ enum Magic.IndexedDb.MagicEncryptionState : System.IComparable, System.IConverti
field static Magic.IndexedDb.MagicEncryptionState PolicyMismatch = 3
field static Magic.IndexedDb.MagicEncryptionState Uninitialized = 0
field static Magic.IndexedDb.MagicEncryptionState Unlocked = 2
enum Magic.IndexedDb.MagicKeyRotationMode : System.IComparable, System.IConvertible, System.IFormattable, System.ISpanFormattable
field System.Int32 value__
field static Magic.IndexedDb.MagicKeyRotationMode Exclusive = 0
field static Magic.IndexedDb.MagicKeyRotationMode Online = 2
field static Magic.IndexedDb.MagicKeyRotationMode ReadOnly = 1
class Magic.IndexedDb.MagicKeyRotationOptions : System.IEquatable<Magic.IndexedDb.MagicKeyRotationOptions>
ctor Magic.IndexedDb.MagicKeyRotationOptions(System.String TargetKeyId, Magic.IndexedDb.MagicKeyRotationMode Mode = Exclusive, System.Int32 BatchSize = 128, System.Boolean YieldBetweenBatches = false)
method Magic.IndexedDb.MagicKeyRotationOptions <Clone>$()
method System.Boolean Equals(Magic.IndexedDb.MagicKeyRotationOptions other)
method System.Boolean Equals(System.Object obj)
method System.Int32 GetHashCode()
method System.String ToString()
method System.Void Deconstruct(out System.String TargetKeyId, out Magic.IndexedDb.MagicKeyRotationMode Mode, out System.Int32 BatchSize, out System.Boolean YieldBetweenBatches)
property Magic.IndexedDb.MagicKeyRotationMode Mode { get; set; }
property System.Boolean YieldBetweenBatches { get; set; }
property System.Int32 BatchSize { get; set; }
property System.String TargetKeyId { get; set; }
enum Magic.IndexedDb.MagicKeyRotationPhase : System.IComparable, System.IConvertible, System.IFormattable, System.ISpanFormattable
field System.Int32 value__
field static Magic.IndexedDb.MagicKeyRotationPhase AuthorizationRequired = 1
field static Magic.IndexedDb.MagicKeyRotationPhase Completed = 4
field static Magic.IndexedDb.MagicKeyRotationPhase Converting = 2
field static Magic.IndexedDb.MagicKeyRotationPhase NotStarted = 0
field static Magic.IndexedDb.MagicKeyRotationPhase Verifying = 3
class Magic.IndexedDb.MagicKeyRotationStatus : System.IEquatable<Magic.IndexedDb.MagicKeyRotationStatus>
ctor Magic.IndexedDb.MagicKeyRotationStatus(Magic.IndexedDb.MagicKeyRotationPhase Phase, System.String RotationId, System.String SourceKeyId, System.String TargetKeyId, System.Nullable<Magic.IndexedDb.MagicKeyRotationMode> Mode, System.Boolean Paused, System.Int64 Converted, System.Int64 Verified, System.Boolean ReadsAllowed, System.Boolean WritesAllowed, System.Boolean RetirementReady)
method Magic.IndexedDb.MagicKeyRotationStatus <Clone>$()
method System.Boolean Equals(Magic.IndexedDb.MagicKeyRotationStatus other)
method System.Boolean Equals(System.Object obj)
method System.Int32 GetHashCode()
method System.String ToString()
method System.Void Deconstruct(out Magic.IndexedDb.MagicKeyRotationPhase Phase, out System.String RotationId, out System.String SourceKeyId, out System.String TargetKeyId, out System.Nullable<Magic.IndexedDb.MagicKeyRotationMode> Mode, out System.Boolean Paused, out System.Int64 Converted, out System.Int64 Verified, out System.Boolean ReadsAllowed, out System.Boolean WritesAllowed, out System.Boolean RetirementReady)
property Magic.IndexedDb.MagicKeyRotationPhase Phase { get; set; }
property System.Boolean Paused { get; set; }
property System.Boolean ReadsAllowed { get; set; }
property System.Boolean RetirementReady { get; set; }
property System.Boolean WritesAllowed { get; set; }
property System.Int64 Converted { get; set; }
property System.Int64 Verified { get; set; }
property System.Nullable<Magic.IndexedDb.MagicKeyRotationMode> Mode { get; set; }
property System.String RotationId { get; set; }
property System.String SourceKeyId { get; set; }
property System.String TargetKeyId { get; set; }
enum Magic.IndexedDb.MagicNumericStorageMode : System.IComparable, System.IConvertible, System.IFormattable, System.ISpanFormattable
field System.Int32 value__
field static Magic.IndexedDb.MagicNumericStorageMode Legacy = 0
Expand Down
1 change: 1 addition & 0 deletions Magic.IndexedDb/Encryption/EncryptionConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal sealed class EncryptionConfiguration
public MagicEncryptionKeyRequest Request { get; }
public string PhysicalName { get; }
public string Route { get; }
internal bool ResumeKeyRotationOnUnlock => _options.ResumeKeyRotationOnUnlock;
internal bool HasExplicitPhysicalBinding => _options.PhysicalDatabaseName is not null;
internal IReadOnlyDictionary<string, IReadOnlyCollection<string>> RecordPolicies =>
_recordPolicies.ToDictionary(pair => pair.Key, pair => (IReadOnlyCollection<string>)pair.Value, StringComparer.Ordinal);
Expand Down
Loading
Loading