From 195aa736c3ffbd83aeac5fd745127eec3505c158 Mon Sep 17 00:00:00 2001 From: Libby Date: Sat, 14 Nov 2026 05:12:30 +0000 Subject: [PATCH] cultnet-cs: a stamp's UTC instant must lie in years 0001-9999; pin the host door exemption Co-Authored-By: Claude Sonnet 5.5 --- src/GameCult.Networking/CultNetStoredAtStamp.cs | 11 ++++++++--- .../RemoteWriteAuthorityTests.cs | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/GameCult.Networking/CultNetStoredAtStamp.cs b/src/GameCult.Networking/CultNetStoredAtStamp.cs index 8540f49d..a4ead645 100644 --- a/src/GameCult.Networking/CultNetStoredAtStamp.cs +++ b/src/GameCult.Networking/CultNetStoredAtStamp.cs @@ -5,7 +5,7 @@ namespace GameCult.Networking { // The one shape a peer's storedAt must have: RFC 3339 date-time, YYYY-MM-DDThh:mm:ss, an optional fraction of 1 to 9 digits, // then Z or +hh:mm / -hh:mm. Uppercase T and Z, ASCII digits, no whitespace, a real calendar date, seconds 00-59, years - // 0001-9999, offset hours 00-23 and minutes 00-59. Checked by shape and range, never by DateTimeOffset.TryParse, which is + // 0001-9999 for both the local date and the UTC instant, offset hours 00-23 and minutes 00-59. Checked by shape and range, never by DateTimeOffset.TryParse, which is // culture-aware and takes far more than this (tests/vectors/stored-at-stamp, variants:ruling:raw-put-without-stamp-refused). internal static class CultNetStoredAtStamp { @@ -30,9 +30,14 @@ namespace GameCult.Networking if (i >= text.Length) return false; if (text[i] == 'Z') return i == text.Length - 1; - return text.Length - i == 6 && (text[i] == '+' || text[i] == '-') + if (!(text.Length - i == 6 && (text[i] == '+' || text[i] == '-') && Digits(text, i + 1, 2, out var offsetHour) && text[i + 3] == ':' && Digits(text, i + 4, 2, out var offsetMinute) - && offsetHour <= 23 && offsetMinute <= 59; + && offsetHour <= 23 && offsetMinute <= 59)) + return false; + // The UTC instant must lie in years 0001-9999 too: local minus offset may not leave DateTime's range. + var offsetTicks = (offsetHour * 60L + offsetMinute) * TimeSpan.TicksPerMinute * (text[i] == '-' ? -1 : 1); + var utcTicks = new DateTime(year, month, day, hour, minute, second).Ticks - offsetTicks; + return utcTicks >= DateTime.MinValue.Ticks && utcTicks <= DateTime.MaxValue.Ticks; } private static bool IsDigit(char c) => c is >= '0' and <= '9'; diff --git a/tests/GameCult.Networking.Tests/RemoteWriteAuthorityTests.cs b/tests/GameCult.Networking.Tests/RemoteWriteAuthorityTests.cs index f178483e..7cdaecbc 100644 --- a/tests/GameCult.Networking.Tests/RemoteWriteAuthorityTests.cs +++ b/tests/GameCult.Networking.Tests/RemoteWriteAuthorityTests.cs @@ -461,6 +461,20 @@ namespace GameCult.Networking.Tests Assert.That(wire.Errors, Is.Empty); } + // The host's own door (the public ApplyPutAsync overload, no authorize) is exempt from the stamp judgment: the cache mints + // a stamp for it. Applying the check to every call would refuse this put. + [Test] + public async Task TheHostsOwnDoorIsExemptFromTheStampJudgment() + { + var rig = Build(new RecordingAuthority(_ => CultNetRemoteWriteDecision.Refuse("no"))); + var put = rig.PutOf(KeyA, "host"); + put.Document.StoredAt = "yesterday"; + + await rig.Database.ApplyPutAsync(put); + + Assert.That(rig.TextOf(KeyA), Is.EqualTo("host"), "the host's put with no stamp is applied, not refused"); + } + [Test] public async Task AnUngrantedPeerIsToldItIsNotAuthorizedBeforeItsStampIsJudgedAndAnAliasAsksTheCanonicalId() {