Bytes after: codec DecodeDocument tail pinned; provider command document refuses a tail; dead fallback binding deleted

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-12 03:15:34 +00:00
commit eb90126002
4 changed files with 43 additions and 9 deletions

View file

@ -284,7 +284,10 @@ namespace GameCult.Mesh
throw new ArgumentException($"Expected provider command schema '{CultMeshProviderSessionWireContract.CommandSchema}'.", nameof(document));
if (!string.Equals(document.PayloadEncoding, "messagepack", StringComparison.Ordinal))
throw new ArgumentException("Provider command document must use MessagePack.", nameof(document));
var command = MessagePackSerializer.Deserialize<CultMeshProviderCommandWire>(document.Payload, Options);
// A raw record's payload is one MessagePack value (ruling trailing-bytes-refuse): a tail is refused by count, never walked.
var command = MessagePackSerializer.Deserialize<CultMeshProviderCommandWire>(new ReadOnlyMemory<byte>(document.Payload), Options, out var read);
if (read < document.Payload.Length)
throw new MessagePackSerializationException($"Provider command document: {document.Payload.Length - read} bytes after its first MessagePack value (refused on read).");
if (command == null) throw new MessagePackSerializationException("Provider command decoded to null.");
Validate(command);
return command;

View file

@ -641,23 +641,19 @@ namespace GameCult.Networking
string storedAt,
CultNetDocumentMessageOptions? options = null)
{
var binding = GetByDocumentType(descriptor.DocumentType) ??
new CultNetDocumentBinding(
descriptor.DocumentType,
descriptor.SchemaId,
value => CultDocumentMessagePackSerialization.SerializeUntyped(value, value.GetType(), _documents),
payload => CultDocumentMessagePackSerialization.DeserializeUntyped(descriptor.DocumentType, payload, _documents));
var binding = GetByDocumentType(descriptor.DocumentType);
return new CultNetRawDocumentRecord
{
SchemaId = binding.SchemaId,
SchemaId = binding?.SchemaId ?? descriptor.SchemaId,
SchemaName = descriptor.SchemaName,
SchemaVersion = descriptor.SchemaVersion,
SchemaContentHash = descriptor.ContentHash,
RecordKey = key.Value,
StoredAt = storedAt,
PayloadEncoding = "messagepack",
Payload = binding.PayloadSerializer(document),
Payload = binding?.PayloadSerializer(document) ??
CultDocumentMessagePackSerialization.SerializeUntyped(document, document.GetType(), _documents),
SourceRuntimeId = options?.SourceRuntimeId,
SourceAgentId = options?.SourceAgentId,
SourceRole = options?.SourceRole,

View file

@ -99,6 +99,17 @@ namespace GameCult.Caching.Tests
}
}
// The codec the store and the inspector read through is a path of its own: DecodeDocument<T> deserializes by the codec's
// delegate, so a tail it did not refuse would be read as a document.
[Test]
public void CodecDecodeDocumentRefusesATail()
{
var codec = CultCacheMessagePack.CreateCodec(Registry);
Assert.That(codec.DecodeDocument<UnionSurface>(new CultRecordKey("surface"), Base), Is.Not.Null);
var message = Message(() => codec.DecodeDocument<UnionSurface>(new CultRecordKey("surface"), Base.Concat(new byte[] { 0xc0 }).ToArray()));
Assert.That(message, Does.Contain(typeof(UnionSurface).FullName).And.Contain($"1 {Clause}").And.Contain("refused on read"));
}
[Test]
public void StoreLoadRefuses()
{

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GameCult.Networking;
using MessagePack;
using NUnit.Framework;
@ -168,6 +169,29 @@ public sealed class CultMeshProviderSessionWireTests
});
}
[Test]
public void DecodeCommandDocumentRefusesBytesAfterItsFirstValue()
{
var command = new CultMeshProviderCommandWire
{
CommandId = "cmd-7", CommandKind = "pilot.thrust", ProviderId = "aetheria", ServiceInstanceId = "aetheria-42",
Payload = new Dictionary<string, object> { ["thrust"] = 1 }
};
var payload = Convert.FromBase64String(CultMeshProviderSessionWire.EncodePayload(command));
CultNetRawDocumentRecord Record(byte[] bytes) => new()
{
SchemaId = CultMeshProviderSessionWireContract.CommandSchema,
RecordKey = "provider-command:aetheria:aetheria-42:cmd-7",
PayloadEncoding = "messagepack",
Payload = bytes
};
Assert.That(CultMeshProviderSessionWire.DecodeCommandDocument(Record(payload)).CommandId, Is.EqualTo("cmd-7"));
var thrown = Assert.Throws<MessagePackSerializationException>(() =>
CultMeshProviderSessionWire.DecodeCommandDocument(Record(payload.Concat(new byte[] { 0xc0, 0xc0 }).ToArray())))!;
Assert.That(thrown.Message, Does.Contain("2 bytes after its first MessagePack value"));
}
[Test]
public void LeaseResponse_RoundTrips_WithApplicationAcceptance()
{