From 7d3e9c77b3205c369b0d354bb0808ea8abc9b05c Mon Sep 17 00:00:00 2001 From: Libby Date: Thu, 12 Nov 2026 01:18:59 +0000 Subject: [PATCH] Refuse a document payload with bytes after its first MessagePack value; bytes-after vectors, contract, changelog Co-Authored-By: Claude Sonnet 5.5 --- .../CultPayloadDepth.cs | 13 ++++++++--- .../Contracts/cultcache-persistence-format.md | 2 ++ .../BytesAfterVectorTests.cs | 21 ++++++++---------- tests/vectors/union-parity/README.md | 17 ++++++++++++++ .../bytes-after-deep-tail.msgpack | Bin 0 -> 601 bytes .../bytes-after-never-used.msgpack | Bin 0 -> 279 bytes .../union-parity/bytes-after-nil.msgpack | Bin 0 -> 278 bytes .../union-parity/bytes-after-outcomes.msgpack | 1 + .../bytes-after-second-document.msgpack | Bin 0 -> 554 bytes unity/org.gamecult.cultlib/CHANGELOG.md | 1 + 10 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 tests/vectors/union-parity/bytes-after-deep-tail.msgpack create mode 100644 tests/vectors/union-parity/bytes-after-never-used.msgpack create mode 100644 tests/vectors/union-parity/bytes-after-nil.msgpack create mode 100644 tests/vectors/union-parity/bytes-after-outcomes.msgpack create mode 100644 tests/vectors/union-parity/bytes-after-second-document.msgpack diff --git a/src/GameCult.Caching.MessagePack/CultPayloadDepth.cs b/src/GameCult.Caching.MessagePack/CultPayloadDepth.cs index 258663e8..6179f8be 100644 --- a/src/GameCult.Caching.MessagePack/CultPayloadDepth.cs +++ b/src/GameCult.Caching.MessagePack/CultPayloadDepth.cs @@ -7,11 +7,12 @@ namespace GameCult.Caching.MessagePack; internal enum PayloadDirection { Read, Write } /// -/// The one payload nesting count. Walks the MessagePack headers of a document payload iteratively, counting every array and +/// The one walk of a payload's shape: its depth and its one value. Walks the MessagePack headers of a document payload iteratively, counting every array and /// map (read by a formatter or skipped past it; the outermost array is depth 1) and refusing the first container past /// . Scalars are skipped by length, nothing is allocated per /// element, and the count stack is bounded by the limit. A refusal names the type, the depth reached and the limit, -/// never payload bytes. A payload that ends early is the deserializer's to refuse. +/// never payload bytes. A payload that ends early is the deserializer's to refuse; bytes after the first value are refused +/// with their count and never walked. /// internal static class CultPayloadDepth { @@ -73,7 +74,13 @@ internal static class CultPayloadDepth // One element is complete; close every container it was the last element of. while (depth > 0 && --remaining[depth - 1] == 0) depth--; - if (depth == 0) return; + if (depth == 0) + { + if (position < bytes.Length) + throw new MessagePackSerializationException( + $"{type.FullName} payload has {bytes.Length - position} bytes after its first MessagePack value (refused on {(direction == PayloadDirection.Read ? "read" : "write")})."); + return; + } } } diff --git a/src/GameCult.Caching/Contracts/cultcache-persistence-format.md b/src/GameCult.Caching/Contracts/cultcache-persistence-format.md index 5ac6c3b5..0f3c6009 100644 --- a/src/GameCult.Caching/Contracts/cultcache-persistence-format.md +++ b/src/GameCult.Caching/Contracts/cultcache-persistence-format.md @@ -403,6 +403,8 @@ runtime enforces the same bound on read and on write, and the refusal names the the depth and the limit, never payload bytes. The vectors are in `tests/vectors/union-parity` (`bounds-outcomes.msgpack`). +A record's payload is exactly one MessagePack value: every runtime refuses a payload with bytes after its first value wherever it reads a payload under the nesting bound, and on write where a write takes bytes the library did not encode itself (a C# type's own formatter, a Rust raw envelope), without walking those bytes, so a tail past the limit is refused as bytes after the value and not as depth; the refusal names the type and the count of bytes after the value, never the bytes (vectors: `tests/vectors/union-parity/bytes-after-outcomes.msgpack`). + ## Variant Records A variant record is a record whose document is a base record's document with some diff --git a/tests/GameCult.Caching.Tests/BytesAfterVectorTests.cs b/tests/GameCult.Caching.Tests/BytesAfterVectorTests.cs index 9eea0651..b61487d5 100644 --- a/tests/GameCult.Caching.Tests/BytesAfterVectorTests.cs +++ b/tests/GameCult.Caching.Tests/BytesAfterVectorTests.cs @@ -37,18 +37,11 @@ namespace GameCult.Caching.Tests ("bytes-after-second-document.msgpack", Base), }; - private static byte[] Raw(Action write) + // The bounds row shape: [file, outcome, union, key|nil, detail]. + private static byte[] OutcomesFile() { var buffer = new System.Buffers.ArrayBufferWriter(); - var writer = new MessagePackWriter(buffer); - write(writer); - writer.Flush(); - return buffer.WrittenSpan.ToArray(); - } - - // The bounds row shape: [file, outcome, union, key|nil, detail]. - private static byte[] OutcomesFile() => Raw(w => - { + var w = new MessagePackWriter(buffer); var cases = Cases(); w.WriteArrayHeader(cases.Length); foreach (var (file, _) in cases) @@ -60,7 +53,10 @@ namespace GameCult.Caching.Tests w.WriteNil(); w.Write("bytes-after"); } - }); + + w.Flush(); + return buffer.WrittenSpan.ToArray(); + } private static string Message(Action action) { @@ -135,10 +131,11 @@ namespace GameCult.Caching.Tests } [CultDocument("tests.two_values", "tests.two_values.v1")] + [MessagePackObject] [MessagePackFormatter(typeof(TwoValueFormatter))] public sealed class TwoValues { - public string Id = string.Empty; + [Key(0)] [CultName] public string Id = string.Empty; } [Test] diff --git a/tests/vectors/union-parity/README.md b/tests/vectors/union-parity/README.md index fbf9c6c1..b2fe1df0 100644 --- a/tests/vectors/union-parity/README.md +++ b/tests/vectors/union-parity/README.md @@ -170,3 +170,20 @@ arrays around a canary string, which no formatter reads. | `depth-skipped-past-limit.msgpack` | 257 | refused, depth | refuse on read though the typed depth is under 10 | A walk of the payload must not recurse: 100000 nested arrays in an extra slot are refused on a 256 KiB stack. + +## Bytes after the value (bytes-after-outcomes.msgpack) + +A record's payload is exactly one MessagePack value. Each file is `union-surface.msgpack` followed by a tail the first value does not own; the C# reference writes them and every other runtime is held to the same refusal. + +| File | Tail after the value | outcome, detail | +|---|---|---| +| `bytes-after-nil.msgpack` | `c0` (1 byte) | refused, bytes-after | +| `bytes-after-never-used.msgpack` | `c1 c1` (2 bytes, the never-used code) | refused, bytes-after | +| `bytes-after-deep-tail.msgpack` | 300 nested fixarray(1) around a canary string (324 bytes), nested past the limit | refused, bytes-after | +| `bytes-after-second-document.msgpack` | a second copy of `union-surface.msgpack` | refused, bytes-after | + +`bytes-after-outcomes.msgpack` is an array of `[file, "refused", "UnionSurface", nil, "bytes-after"]` rows, the row shape of `bounds-outcomes.msgpack` but in its own file, so the bounds walkers (which assert 12 rows) are unchanged. + +Must: refuse on read, naming the type and the count of bytes after the first value (`1`, `2`, the tail length) in a clause that says "bytes after its first MessagePack value"; never echo the canary; never report the depth refusal ("the limit is") for any of them, because the tail is not walked; and a single-file store holding `bytes-after-nil.msgpack` as a record payload refuses on open. + +What a runtime walks: only the first value of the payload, to find where it ends (a leaf counts as complete at its own length, a container when its last child completes). It compares that end with the payload length; a payload that ends early is not bytes after the value and stays the decoder's to refuse. It never walks the tail, so the tail's own shape (a stray `c1`, 300 levels) is never judged. diff --git a/tests/vectors/union-parity/bytes-after-deep-tail.msgpack b/tests/vectors/union-parity/bytes-after-deep-tail.msgpack new file mode 100644 index 0000000000000000000000000000000000000000..2dcc82f9cf5235956c37ed6c9e429bfca556b629 GIT binary patch literal 601 zcmbQfytuR|EipNjc@o2%6!aU5${fLW866AE~O}j2j2oI3a+MgjvPa?*y267{?Fp%UW~* literal 0 HcmV?d00001 diff --git a/tests/vectors/union-parity/bytes-after-nil.msgpack b/tests/vectors/union-parity/bytes-after-nil.msgpack new file mode 100644 index 0000000000000000000000000000000000000000..3bbdaa3321d1d4def22b1d20c9bb3a044b211d1c GIT binary patch literal 278 zcmZ9GK@P$o5Jk0W+<2D)-Fcf4&^Cc!D2*ON2NSm%(QZ5hN0Wsy;H+kT{(pb2V?Wp} za5=*XzR7Pm@i;96`fR0A1}mT27zS?iz6=Y3WmwSr5f_&%DA+jIZe7<9AE4Y{nWUjt w#>EmV*{Ou^<_p=xn@`?wfX-zlK+_w`Twqw8^SeogR|{H8yGm==NwD)6Ka^HkvH$=8 literal 0 HcmV?d00001 diff --git a/tests/vectors/union-parity/bytes-after-outcomes.msgpack b/tests/vectors/union-parity/bytes-after-outcomes.msgpack new file mode 100644 index 00000000..8237924e --- /dev/null +++ b/tests/vectors/union-parity/bytes-after-outcomes.msgpack @@ -0,0 +1 @@ +”•·bytes-after-nil.msgpack§refused¬UnionSurfaceÀ«bytes-after•¾bytes-after-never-used.msgpack§refused¬UnionSurfaceÀ«bytes-after•½bytes-after-deep-tail.msgpack§refused¬UnionSurfaceÀ«bytes-after•Ù#bytes-after-second-document.msgpack§refused¬UnionSurfaceÀ«bytes-after \ No newline at end of file diff --git a/tests/vectors/union-parity/bytes-after-second-document.msgpack b/tests/vectors/union-parity/bytes-after-second-document.msgpack new file mode 100644 index 0000000000000000000000000000000000000000..80d136fa80a0680a2d93d8a5b7a42a3adbc660e1 GIT binary patch literal 554 zcmbQfytuR|EipNjc@o2GE Dx2>_` literal 0 HcmV?d00001 diff --git a/unity/org.gamecult.cultlib/CHANGELOG.md b/unity/org.gamecult.cultlib/CHANGELOG.md index a2a1efae..4ecb63d0 100644 --- a/unity/org.gamecult.cultlib/CHANGELOG.md +++ b/unity/org.gamecult.cultlib/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this package are documented in this file. ### Breaking +- A document payload with bytes after its first MessagePack value is refused on read and on write (`MessagePackSerializationException` naming the type and the count of bytes after the value, never the bytes); the tail is not walked, so a tail nested past the limit is refused as bytes after the value, not as depth. Before, the tail was ignored on read and dropped on the next write. A payload that ends early is still the deserializer's to refuse. - A single-file store holding two records under one key is unreadable: the reader refuses it with `CultStoreUnreadableException` naming the key (keys compare by ordinal code units, no case folding or normalisation), so a cache, `ReadStoreSnapshot` and a flush refuse the file and leave it as it is. Before, the later record silently replaced the earlier. A directory store reads its manifest through the same reader and refuses a duplicate key the same way. Repair the duplicate with the runtime that wrote it before reading the store here. - A record whose payload carries a slot past the slots its reading type declares, at any depth (the record, a nested object, a list element, a union arm or a unit arm), is loaded read-only: it is served, and every staged write or removal of its key (`Upsert`, `UpsertVariant`, `Remove`, a batch, and `Flatten` of a variant) is refused with `CultSchemaConflictException` before anything is staged, and the store keeps the record byte for byte. Before, the extra slots were skipped on read and dropped on the next write. Declaring an id compatible consents only to depth-0 slots the schema comparison reports ignored under that id; any other extra slot keeps the record read-only whatever is declared, including under the reader's own id. A variant overriding a slot its type does not have is read-only too (it used to be shed on flush). Write such a record from the build that declares those slots. Missing trailing slots still default and stay writable. - `Flatten` (and `FlattenAsync`) of a variant whose base chain holds a record its store does not own (a record held read-only for extra slots, or undeclared drift that drops slots) is refused with `CultSchemaConflictException` naming that base record, and the store is left as it was. Before, the flatten wrote the base's decoded view into a plain record and the base's unread data was lost for that variant. Writing and removing the variant itself are still accepted: they persist only its delta.