Heimdall sign-in: attempt cookies live in a ring of two fixed slots, so the Cookie header is bounded by construction; pin the completion form's id

Start takes a free slot or overwrites the one expiring first; parallel starts carry the same request
cookies, choose the same slot and overwrite each other. Complete finds its attempt by scanning the
slots for the posted nonce; an overwritten attempt finds none and is refused like any other. The
per-request counting sweep, its byte budget and the nonce-named cookie are deleted.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-11 22:25:24 +02:00
commit 15f96c408c
4 changed files with 122 additions and 105 deletions

View file

@ -378,13 +378,9 @@ app.MapGet("/auth/heimdall/{provider}", async (
hostOptions.Value.PublicBaseUrl,
returnPath,
requireMemberAccess,
httpContext.Request.Cookies,
cancellationToken);
foreach (var superseded in heimdallAuthService.SupersededAttemptCookies(httpContext.Request.Cookies, start))
{
httpContext.Response.Cookies.Delete(superseded, HeimdallAttemptCookie.DeleteOptions());
}
httpContext.Response.Cookies.Append(start.CookieName, start.CookieValue, HeimdallAttemptCookie.Options());
return Results.Redirect(start.AuthorizationUrl.ToString());
}
@ -533,25 +529,25 @@ app.MapPost("/auth/heimdall/complete", async (
{
var form = await ReadFormOrNullAsync(httpContext.Request, cancellationToken);
var attempt = form?["attempt"].ToString();
var cookieName = HeimdallAttemptCookie.NameFor(attempt);
var held = heimdallAuthService.FindAttemptCookie(httpContext.Request.Cookies, attempt);
try
{
var signIn = await heimdallAuthService.CompleteAsync(
hostOptions.Value.PublicBaseUrl,
attempt,
cookieName is null ? null : httpContext.Request.Cookies[cookieName],
held?.Value,
form?["code"].ToString(),
cancellationToken);
await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, signIn.Principal);
httpContext.Response.Cookies.Delete(cookieName!, HeimdallAttemptCookie.DeleteOptions());
httpContext.Response.Cookies.Delete(held!.Value.Name, HeimdallAttemptCookie.DeleteOptions());
return Results.LocalRedirect(signIn.ReturnPath.Value);
}
catch (HeimdallAuthException error)
{
logger.LogWarning("Heimdall completion refused: {Reason}", error.Message);
if (cookieName is not null)
if (held is not null)
{
httpContext.Response.Cookies.Delete(cookieName, HeimdallAttemptCookie.DeleteOptions());
httpContext.Response.Cookies.Delete(held.Value.Name, HeimdallAttemptCookie.DeleteOptions());
}
return Results.Redirect(HeimdallAuthService.ErrorRedirect);