From ec2f812b599ab6feb88b3e4dd9d38ff97804a615 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 11 Oct 2026 21:26:31 +0200 Subject: [PATCH] Heimdall attempt cookie bound: pin the exact byte boundary and that unrelated cookies are never swept Co-Authored-By: Claude Sonnet 5.5 --- .../HeimdallCompletionTests.cs | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/Bifrost.Web.Tests/HeimdallCompletionTests.cs b/tests/Bifrost.Web.Tests/HeimdallCompletionTests.cs index 647852b..ea2467a 100644 --- a/tests/Bifrost.Web.Tests/HeimdallCompletionTests.cs +++ b/tests/Bifrost.Web.Tests/HeimdallCompletionTests.cs @@ -1316,7 +1316,10 @@ public sealed class HeimdallCompletionTests for (var i = 0; i < 5; i++) { host.Clock.Advance(TimeSpan.FromSeconds(1)); - var attempt = await host.StartAsync(returnUrl: "/App", cookieHeader: jar.Count == 0 ? null : CookieHeader(jar)); + var unrelated = ".AspNetCore.Cookies=session; x=1"; + var attempt = await host.StartAsync( + returnUrl: "/App", cookieHeader: jar.Count == 0 ? unrelated : CookieHeader(jar) + "; " + unrelated); + Assert.NotNull(attempt.Cookie); Assert.Equal(0, Deletions(attempt.Response)); ApplySetCookies(jar, attempt.Response); started.Add(attempt); @@ -1333,6 +1336,42 @@ public sealed class HeimdallCompletionTests Assert.Contains("1970", HeimdallHost.SetCookieHeader(next.Response, deadName)); } + [Fact] + public async Task An_attempt_that_exactly_fills_the_cookie_bound_is_kept_and_one_byte_over_gives_up_the_oldest() + { + using var host = new HeimdallHost(); + // Each cookie's bytes in a Cookie header, written out here and not taken from the service. + static int Bytes(string name, string value) => name.Length + 1 + value.Length + 2; + var byLength = new Dictionary(); + for (var length = 1; length <= LocalReturnPath.MaxLength - 1; length++) + { + var attempt = await host.StartAsync(returnUrl: "/" + new string('a', length)); + byLength[length] = (attempt, Bytes(HeimdallAttemptCookie.Prefix + attempt.Nonce, attempt.Cookie!)); + } + + (int Old, int New)? Pair(int total) => + (from older in byLength from newer in byLength where older.Value.Bytes + newer.Value.Bytes == total + select ((int Old, int New)?)(older.Key, newer.Key)).FirstOrDefault(); + + var fits = Pair(HeimdallAttemptCookie.MaxAggregateBytes); + var overflows = Pair(HeimdallAttemptCookie.MaxAggregateBytes + 1); + Assert.NotNull(fits); + Assert.NotNull(overflows); + + foreach (var (pair, overflow) in new[] { (fits.Value, false), (overflows.Value, true) }) + { + host.Clock.Advance(TimeSpan.FromSeconds(1)); + var older = await host.StartAsync(returnUrl: "/" + new string('a', pair.Old)); + host.Clock.Advance(TimeSpan.FromSeconds(1)); + var newer = await host.StartAsync( + returnUrl: "/" + new string('a', pair.New), + cookieHeader: $"{HeimdallAttemptCookie.Prefix + older.Nonce}={older.Cookie}"); + + Assert.Equal(overflow ? 1 : 0, Deletions(newer.Response)); + Assert.NotNull(newer.Cookie); + } + } + [Fact] public async Task The_complete_page_form_has_the_fields_and_action_the_server_reads() {