Signed Mind intake: verify before revealing state, dummy verify, issuedAt window and nonce pruning, per-source rate limit and body cap, one account per persona, DbUpdateException is replay only for the nonce
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8e6afc43ed
commit
1c9cb9937e
10 changed files with 346 additions and 37 deletions
|
|
@ -47,6 +47,8 @@ public sealed class BifrostDbContext(DbContextOptions<BifrostDbContext> options)
|
|||
|
||||
public DbSet<MindNonce> MindNonces => Set<MindNonce>();
|
||||
|
||||
public DbSet<MindPersona> MindPersonas => Set<MindPersona>();
|
||||
|
||||
public DbSet<LedgerEntry> LedgerEntries => Set<LedgerEntry>();
|
||||
|
||||
public DbSet<PayoutProposalBatch> PayoutProposalBatches => Set<PayoutProposalBatch>();
|
||||
|
|
@ -325,6 +327,12 @@ public sealed class BifrostDbContext(DbContextOptions<BifrostDbContext> options)
|
|||
entity.HasIndex(x => new { x.PersonaId, x.PublicKey }).IsUnique();
|
||||
});
|
||||
|
||||
modelBuilder.Entity<MindPersona>(entity =>
|
||||
{
|
||||
entity.HasIndex(x => x.PersonaId).IsUnique();
|
||||
entity.HasIndex(x => x.UserAccountId).IsUnique();
|
||||
});
|
||||
|
||||
modelBuilder.Entity<MindNonce>(entity =>
|
||||
{
|
||||
entity.HasIndex(x => new { x.PersonaId, x.Nonce }).IsUnique();
|
||||
|
|
|
|||
|
|
@ -30,6 +30,18 @@ namespace Bifrost.Web.Data.Migrations
|
|||
constraints: table => table.PrimaryKey("PK_MindKeys", x => x.Id));
|
||||
migrationBuilder.CreateIndex(name: "IX_MindKeys_PersonaId_PublicKey", table: "MindKeys", columns: new[] { "PersonaId", "PublicKey" }, unique: true);
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "MindPersonas",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
PersonaId = table.Column<string>(type: "text", nullable: false),
|
||||
UserAccountId = table.Column<Guid>(type: "uuid", nullable: false)
|
||||
},
|
||||
constraints: table => table.PrimaryKey("PK_MindPersonas", x => x.Id));
|
||||
migrationBuilder.CreateIndex(name: "IX_MindPersonas_PersonaId", table: "MindPersonas", column: "PersonaId", unique: true);
|
||||
migrationBuilder.CreateIndex(name: "IX_MindPersonas_UserAccountId", table: "MindPersonas", column: "UserAccountId", unique: true);
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "MindNonces",
|
||||
columns: table => new
|
||||
|
|
@ -46,6 +58,7 @@ namespace Bifrost.Web.Data.Migrations
|
|||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(name: "MindNonces");
|
||||
migrationBuilder.DropTable(name: "MindPersonas");
|
||||
migrationBuilder.DropTable(name: "MindKeys");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1477,6 +1477,30 @@ namespace Bifrost.Web.Data.Migrations
|
|||
b.ToTable("MindKeys");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Bifrost.Web.Domain.MindPersona", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("PersonaId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<Guid>("UserAccountId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PersonaId")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UserAccountId")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("MindPersonas");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Bifrost.Web.Domain.MindNonce", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
|
|
|
|||
|
|
@ -1187,3 +1187,13 @@ public sealed class MindNonce
|
|||
|
||||
public DateTimeOffset SeenAtUtc { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>The one member account a persona votes as. Unique on both columns: a persona has one account and an account one persona.</summary>
|
||||
public sealed class MindPersona
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
public string PersonaId { get; set; } = string.Empty;
|
||||
|
||||
public Guid UserAccountId { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,8 +20,33 @@ public sealed class MindIntakeService(
|
|||
TimeProvider timeProvider)
|
||||
{
|
||||
public const string Domain = "gamecult.sealed_intake.v0";
|
||||
private static readonly Regex PersonaPattern = new("^[a-z0-9][a-z0-9-]{0,31}$", RegexOptions.Compiled);
|
||||
private static readonly Regex NoncePattern = new("^[A-Za-z0-9_-]{16,64}$", RegexOptions.Compiled);
|
||||
public static readonly TimeSpan Skew = TimeSpan.FromMinutes(10);
|
||||
|
||||
// Diagnostics: every Ed25519 verification the intake performs, real or dummy. A refusal that skips the verify is a timing tell.
|
||||
public static long SignatureChecks;
|
||||
|
||||
// A valid signature by a throwaway key over a different message: verifying it does the full work and fails at the end.
|
||||
private static readonly (byte[] Public, byte[] Signature, byte[] Message) Dummy = MakeDummy();
|
||||
|
||||
private static (byte[], byte[], byte[]) MakeDummy()
|
||||
{
|
||||
var secret = new byte[32];
|
||||
System.Security.Cryptography.RandomNumberGenerator.Fill(secret);
|
||||
var pub = new byte[32];
|
||||
Ed25519.GeneratePublicKey(secret, 0, pub, 0);
|
||||
var message = Encoding.UTF8.GetBytes("dummy");
|
||||
var sig = new byte[64];
|
||||
Ed25519.Sign(secret, 0, pub, 0, null, message, 0, message.Length, sig, 0);
|
||||
return (pub, sig, Encoding.UTF8.GetBytes("not the dummy message"));
|
||||
}
|
||||
|
||||
private static bool Verify(byte[] signature, byte[] publicKey, byte[] message)
|
||||
{
|
||||
Interlocked.Increment(ref SignatureChecks);
|
||||
return Ed25519.Verify(signature, 0, publicKey, 0, message, 0, message.Length);
|
||||
}
|
||||
private static readonly Regex PersonaPattern = new(@"^[a-z0-9][a-z0-9-]{0,31}\z", RegexOptions.Compiled);
|
||||
private static readonly Regex NoncePattern = new(@"^[A-Za-z0-9_-]{16,64}\z", RegexOptions.Compiled);
|
||||
|
||||
public async Task<MindKeyView> RegisterKeyAsync(
|
||||
CurrentBifrostActor actor, string persona, Guid userAccountId, string publicKey, CancellationToken cancellationToken)
|
||||
|
|
@ -44,10 +69,12 @@ public sealed class MindIntakeService(
|
|||
throw new SealedBallotException(SealedBallotFailure.Invalid, "The persona's member account must exist and be Active.");
|
||||
}
|
||||
|
||||
var bound = await dbContext.MindKeys.Where(x => x.PersonaId == persona).Select(x => (Guid?)x.UserAccountId).FirstOrDefaultAsync(cancellationToken);
|
||||
if (bound is { } other && other != userAccountId)
|
||||
// One persona <-> one account, both ways (a shared account would let one persona overwrite the other's commit).
|
||||
// The unique indexes on MindPersonas are the arbiter; these reads only give a plain error before them.
|
||||
var binding = await dbContext.MindPersonas.SingleOrDefaultAsync(x => x.PersonaId == persona || x.UserAccountId == userAccountId, cancellationToken);
|
||||
if (binding is not null && (binding.PersonaId != persona || binding.UserAccountId != userAccountId))
|
||||
{
|
||||
throw new SealedBallotException(SealedBallotFailure.Invalid, "That persona is already bound to a different member account.");
|
||||
throw new SealedBallotException(SealedBallotFailure.Invalid, "That persona or member account is already bound to a different account or persona.");
|
||||
}
|
||||
|
||||
if (await dbContext.MindKeys.AnyAsync(x => x.PersonaId == persona && x.PublicKey == publicKey, cancellationToken))
|
||||
|
|
@ -61,7 +88,21 @@ public sealed class MindIntakeService(
|
|||
RegisteredByUserAccountId = moderator.Id, RegisteredAtUtc = timeProvider.GetUtcNow()
|
||||
};
|
||||
dbContext.MindKeys.Add(key);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
if (binding is null)
|
||||
{
|
||||
dbContext.MindPersonas.Add(new MindPersona { PersonaId = persona, UserAccountId = userAccountId });
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
catch (DbUpdateException) when (binding is null)
|
||||
{
|
||||
// Lost a registration race on the unique persona/account indexes.
|
||||
throw new SealedBallotException(SealedBallotFailure.Invalid, "That persona or member account is already bound to a different account or persona.");
|
||||
}
|
||||
|
||||
await auditTrailService.RecordAsync(moderator.Id, nameof(MindKey), key.Id, "mindkey.registered",
|
||||
$"Registered a key for persona {persona} (member account {userAccountId}).", cancellationToken);
|
||||
return View(key);
|
||||
|
|
@ -101,37 +142,63 @@ public sealed class MindIntakeService(
|
|||
throw new SealedBallotException(SealedBallotFailure.Invalid, "The envelope is malformed.");
|
||||
}
|
||||
|
||||
if (envelope.MotionId != routeMotionId)
|
||||
{
|
||||
throw new SealedBallotException(SealedBallotFailure.Invalid, "The envelope was signed for a different motion.");
|
||||
}
|
||||
|
||||
// Signature first. Every path below that can tell a caller anything about state comes after a verified signature, and
|
||||
// every refusal before it costs one Ed25519 verification (a dummy one when there is no key or no usable signature).
|
||||
var keys = await dbContext.MindKeys.AsNoTracking().Where(x => x.PersonaId == persona && x.RevokedAtUtc == null).ToListAsync(cancellationToken);
|
||||
var message = Encoding.UTF8.GetBytes(SigningText(envelope));
|
||||
var signature = Convert.TryFromBase64String(envelope.Signature ?? string.Empty, new byte[64], out var written) && written == 64
|
||||
? Convert.FromBase64String(envelope.Signature!) : null;
|
||||
var signer = signature is null ? null : keys.FirstOrDefault(k =>
|
||||
TryKey(k.PublicKey, out var pub) && Ed25519.Verify(signature, 0, pub, 0, message, 0, message.Length));
|
||||
var raw = new byte[64];
|
||||
var wellFormed = Convert.TryFromBase64String(envelope.Signature ?? string.Empty, raw, out var written) && written == 64;
|
||||
MindKey? signer = null;
|
||||
if (!wellFormed || keys.Count == 0)
|
||||
{
|
||||
_ = Verify(Dummy.Signature, Dummy.Public, Dummy.Message);
|
||||
}
|
||||
else
|
||||
{
|
||||
signer = keys.FirstOrDefault(k => TryKey(k.PublicKey, out var pub) && Verify(raw, pub, message));
|
||||
}
|
||||
|
||||
if (signer is null)
|
||||
{
|
||||
// One answer for unknown persona, revoked key, wrong key and bad signature: nothing to probe.
|
||||
throw new SealedBallotException(SealedBallotFailure.Unauthorized, "The envelope is not signed by a registered key for that persona.");
|
||||
}
|
||||
|
||||
// Spend the nonce only after the signature verifies, so an unsigned caller cannot burn a persona's nonces.
|
||||
if (envelope.MotionId != routeMotionId)
|
||||
{
|
||||
throw new SealedBallotException(SealedBallotFailure.Invalid, "The envelope was signed for a different motion.");
|
||||
}
|
||||
|
||||
var now = timeProvider.GetUtcNow();
|
||||
if (Math.Abs((now - DateTimeOffset.FromUnixTimeSeconds(Math.Clamp(envelope.IssuedAt, 0, 253402300799))).TotalSeconds) > Skew.TotalSeconds)
|
||||
{
|
||||
throw new SealedBallotException(SealedBallotFailure.Invalid, "The envelope is outside the accepted clock window.");
|
||||
}
|
||||
|
||||
// Spend the nonce only after the signature verifies, so an unsigned caller cannot burn a persona's nonces. Nonces older
|
||||
// than twice the skew can no longer pass the window above, so they are pruned here.
|
||||
if (await dbContext.MindNonces.AnyAsync(x => x.PersonaId == persona && x.Nonce == envelope.Nonce, cancellationToken))
|
||||
{
|
||||
throw new SealedBallotException(SealedBallotFailure.WrongPhase, "That nonce was already used.");
|
||||
}
|
||||
|
||||
dbContext.MindNonces.Add(new MindNonce { PersonaId = persona, Nonce = envelope.Nonce!, SeenAtUtc = timeProvider.GetUtcNow() });
|
||||
var cutoff = now - 2 * Skew;
|
||||
dbContext.MindNonces.RemoveRange(await dbContext.MindNonces.Where(x => x.SeenAtUtc < cutoff).ToListAsync(cancellationToken));
|
||||
dbContext.MindNonces.Add(new MindNonce { PersonaId = persona, Nonce = envelope.Nonce!, SeenAtUtc = now });
|
||||
try
|
||||
{
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
throw new SealedBallotException(SealedBallotFailure.WrongPhase, "That nonce was already used.");
|
||||
// A replay only if the nonce is now there (the unique index lost a race); any other write failure is a fault, not a replay.
|
||||
dbContext.ChangeTracker.Clear();
|
||||
if (await dbContext.MindNonces.AnyAsync(x => x.PersonaId == persona && x.Nonce == envelope.Nonce, cancellationToken))
|
||||
{
|
||||
throw new SealedBallotException(SealedBallotFailure.WrongPhase, "That nonce was already used.");
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
var account = await dbContext.UserAccounts.AsNoTracking()
|
||||
|
|
@ -151,7 +218,7 @@ public sealed class MindIntakeService(
|
|||
|
||||
/// <summary>The exact text a Mind signs: every field, fixed order, newline separated, unused fields empty.</summary>
|
||||
public static string SigningText(MindEnvelope e) =>
|
||||
string.Join('\n', Domain, e.Persona, e.Verb, e.MotionId.ToString("D"), e.Nonce, e.Commitment ?? "", e.Round, e.Candidate ?? "", e.Reason ?? "", e.Salt ?? "");
|
||||
string.Join('\n', Domain, e.Persona, e.Verb, e.MotionId.ToString("D"), e.Nonce, e.IssuedAt, e.Commitment ?? "", e.Round, e.Candidate ?? "", e.Reason ?? "", e.Salt ?? "");
|
||||
|
||||
private static bool TryKey(string base64, out byte[] key)
|
||||
{
|
||||
|
|
@ -169,9 +236,31 @@ public sealed class MindIntakeService(
|
|||
|
||||
/// <summary>A signed Mind verb. <c>Signature</c> is base64 Ed25519 over <see cref="MindIntakeService.SigningText"/>.</summary>
|
||||
public sealed record MindEnvelope(
|
||||
string Persona, string Verb, Guid MotionId, string Nonce,
|
||||
string Persona, string Verb, Guid MotionId, string Nonce, long IssuedAt,
|
||||
string? Commitment, int Round, string? Candidate, string? Reason, string? Salt, string Signature);
|
||||
|
||||
public sealed record MindKeyView(Guid Id, string Persona, Guid UserAccountId, string PublicKey, DateTimeOffset RegisteredAtUtc, DateTimeOffset? RevokedAtUtc);
|
||||
|
||||
public sealed record RegisterMindKeyRequest(string Persona, Guid UserAccountId, string PublicKey);
|
||||
|
||||
/// <summary>Limits on the anonymous signed-intake endpoint (section "MindIntake").</summary>
|
||||
public sealed class MindIntakeOptions
|
||||
{
|
||||
public const string SectionName = "MindIntake";
|
||||
|
||||
public int Permits { get; init; } = 30;
|
||||
|
||||
public TimeSpan Window { get; init; } = TimeSpan.FromMinutes(1);
|
||||
|
||||
public int MaxTrackedKeys { get; init; } = 10_000;
|
||||
|
||||
public int MaxBodyBytes { get; init; } = 4096;
|
||||
}
|
||||
|
||||
/// <summary>Per-source permit budget for the intake, on the shared <see cref="FixedWindowBudget"/>. Spent before the body is read.</summary>
|
||||
public sealed class MindIntakeRateLimit(Microsoft.Extensions.Options.IOptions<MindIntakeOptions> options, TimeProvider clock)
|
||||
{
|
||||
private readonly FixedWindowBudget budget = new(options.Value.Permits, options.Value.Window, options.Value.MaxTrackedKeys, clock);
|
||||
|
||||
public BudgetDenial? Spend(string source) => budget.Spend(source);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,6 +123,8 @@ builder.Services.AddScoped<GitHubWebhookService>();
|
|||
builder.Services.AddScoped<MotionGovernanceService>();
|
||||
builder.Services.AddScoped<SealedBallotService>();
|
||||
builder.Services.AddScoped<MindIntakeService>();
|
||||
builder.Services.AddOptions<MindIntakeOptions>().BindConfiguration(MindIntakeOptions.SectionName);
|
||||
builder.Services.AddSingleton<MindIntakeRateLimit>(); // TimeProvider and options resolve from the container
|
||||
builder.Services.AddHostedService<SealedBallotCloser>();
|
||||
builder.Services.AddScoped<MotionEveSurfaceService>();
|
||||
builder.Services.AddScoped<PatronageService>();
|
||||
|
|
@ -595,8 +597,49 @@ sealedMotions.MapDelete("/mind-keys/{keyId:guid}", (ICurrentBifrostActorAccessor
|
|||
|
||||
// Signed Mind intake: the Ed25519 signature is the credential. No session, no cookie; the verified envelope runs the same
|
||||
// SealedBallotService verbs as a member cast.
|
||||
app.MapPost("/api/sealed-motions/{id:guid}/mind", (MindIntakeService service, Guid id, MindEnvelope envelope, CancellationToken ct) =>
|
||||
SealedCall(async () => await service.HandleAsync(id, envelope, ct))).AllowAnonymous();
|
||||
app.MapPost("/api/sealed-motions/{id:guid}/mind", async (
|
||||
HttpContext http, MindIntakeService service, MindIntakeRateLimit limiter, IOptions<MindIntakeOptions> limits, Guid id, CancellationToken ct) =>
|
||||
{
|
||||
// Per-source permit and a hard body cap, both before any parsing or database work.
|
||||
if (limiter.Spend(http.Connection.RemoteIpAddress?.ToString() ?? "unknown") is { } denied)
|
||||
{
|
||||
http.Response.Headers.RetryAfter = Math.Ceiling(denied.RetryAfter.TotalSeconds).ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
return Results.Text("{\"error\":\"Too many requests.\"}", "application/json", statusCode: StatusCodes.Status429TooManyRequests);
|
||||
}
|
||||
|
||||
var max = limits.Value.MaxBodyBytes;
|
||||
if (http.Request.ContentLength > max)
|
||||
{
|
||||
return Results.Text("{\"error\":\"Envelope too large.\"}", "application/json", statusCode: StatusCodes.Status413PayloadTooLarge);
|
||||
}
|
||||
|
||||
var buffer = new byte[max + 1];
|
||||
var read = 0;
|
||||
int n;
|
||||
while (read < buffer.Length && (n = await http.Request.Body.ReadAsync(buffer.AsMemory(read), ct)) > 0)
|
||||
{
|
||||
read += n;
|
||||
}
|
||||
|
||||
if (read > max)
|
||||
{
|
||||
return Results.Text("{\"error\":\"Envelope too large.\"}", "application/json", statusCode: StatusCodes.Status413PayloadTooLarge);
|
||||
}
|
||||
|
||||
MindEnvelope? envelope;
|
||||
try
|
||||
{
|
||||
envelope = JsonSerializer.Deserialize<MindEnvelope>(buffer.AsSpan(0, read), new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
envelope = null;
|
||||
}
|
||||
|
||||
return envelope is null
|
||||
? Results.Text("{\"error\":\"The envelope is malformed.\"}", "application/json", statusCode: StatusCodes.Status400BadRequest)
|
||||
: await SealedCall(async () => await service.HandleAsync(id, envelope, ct));
|
||||
}).AllowAnonymous();
|
||||
|
||||
app.MapGet("/auth/heimdall/complete", (HttpContext httpContext) =>
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue