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 <noreply@anthropic.com>
This commit is contained in:
parent
d6bc6484dd
commit
195aa736c3
2 changed files with 22 additions and 3 deletions
|
|
@ -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';
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue