diff --git a/src/GameCult.Mesh/CultMeshProviderSessionWire.cs b/src/GameCult.Mesh/CultMeshProviderSessionWire.cs index 03f3a70c..ad9ae66e 100644 --- a/src/GameCult.Mesh/CultMeshProviderSessionWire.cs +++ b/src/GameCult.Mesh/CultMeshProviderSessionWire.cs @@ -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(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(new ReadOnlyMemory(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; diff --git a/src/GameCult.Networking/CultNetDocumentRegistry.cs b/src/GameCult.Networking/CultNetDocumentRegistry.cs index 19f17c8c..bf17a91f 100644 --- a/src/GameCult.Networking/CultNetDocumentRegistry.cs +++ b/src/GameCult.Networking/CultNetDocumentRegistry.cs @@ -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, diff --git a/tests/GameCult.Caching.Tests/BytesAfterVectorTests.cs b/tests/GameCult.Caching.Tests/BytesAfterVectorTests.cs index 00e54b7a..86d677e6 100644 --- a/tests/GameCult.Caching.Tests/BytesAfterVectorTests.cs +++ b/tests/GameCult.Caching.Tests/BytesAfterVectorTests.cs @@ -99,6 +99,17 @@ namespace GameCult.Caching.Tests } } + // The codec the store and the inspector read through is a path of its own: DecodeDocument 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(new CultRecordKey("surface"), Base), Is.Not.Null); + var message = Message(() => codec.DecodeDocument(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() { diff --git a/tests/GameCult.Mesh.Tests/CultMeshProviderSessionWireTests.cs b/tests/GameCult.Mesh.Tests/CultMeshProviderSessionWireTests.cs index 75125148..41339a85 100644 --- a/tests/GameCult.Mesh.Tests/CultMeshProviderSessionWireTests.cs +++ b/tests/GameCult.Mesh.Tests/CultMeshProviderSessionWireTests.cs @@ -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 { ["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(() => + 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() {