diff --git a/src/GameCult.Caching.MessagePack/CultMathExprResolver.cs b/src/GameCult.Caching.MessagePack/CultMathExprResolver.cs index 4c70b2a8..6b327e09 100644 --- a/src/GameCult.Caching.MessagePack/CultMathExprResolver.cs +++ b/src/GameCult.Caching.MessagePack/CultMathExprResolver.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using CultMath.Expr; using MessagePack; @@ -13,7 +14,8 @@ namespace GameCult.Caching.MessagePack; /// (the payload is the port index of an input or param, the float bits of a constant, the result index of a pick, /// zero for a call). Decoding runs , the one validator, so a graph that breaks a rule /// (unknown op, newer version, forward operand) is refused whole with an and never -/// partly loaded. A nil graph is refused both ways. +/// partly loaded. Decoding also refuses any encoding other than the one Serialize writes (a wider integer, +/// a longer length prefix), so a graph has one byte string. A nil graph is nil, as in every reference slot. /// public sealed class CultMathExprResolver : IFormatterResolver { @@ -28,11 +30,21 @@ public sealed class CultMathExprResolver : IFormatterResolver typeof(T) == typeof(ExprGraph) ? (IMessagePackFormatter)(object)new GraphFormatter() : null; } - private sealed class GraphFormatter : IMessagePackFormatter + private sealed class GraphFormatter : IMessagePackFormatter { - public void Serialize(ref MessagePackWriter writer, ExprGraph value, MessagePackSerializerOptions options) + public void Serialize(ref MessagePackWriter writer, ExprGraph? value, MessagePackSerializerOptions options) + { + if (value is null) + { + writer.WriteNil(); + return; + } + + Write(ref writer, value); + } + + private static void Write(ref MessagePackWriter writer, ExprGraph value) { - if (value is null) throw new ExprGraphException("shape"); writer.WriteArrayHeader(5); writer.Write(ExprGraph.FormatVersion); @@ -66,12 +78,13 @@ public sealed class CultMathExprResolver : IFormatterResolver } } - public ExprGraph Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) + public ExprGraph? Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { - if (reader.TryReadNil()) throw new ExprGraphException("shape"); + if (reader.TryReadNil()) return null; options.Security.DepthStep(ref reader); try { + var start = reader.Position; Expect(ref reader, 5); var version = reader.ReadInt32(); @@ -106,7 +119,9 @@ public sealed class CultMathExprResolver : IFormatterResolver nodes.Add(ExprNode.FromWire(wireName, operands, reader.ReadInt32())); } - return ExprGraph.Create(version, inputs, paramNames, paramBits, outputNames, outputNodes, nodes); + var graph = ExprGraph.Create(version, inputs, paramNames, paramBits, outputNames, outputNodes, nodes); + RequireCanonical(ref reader, start, graph); + return graph; } finally { @@ -114,6 +129,18 @@ public sealed class CultMathExprResolver : IFormatterResolver } } + /// The bytes just read must be the bytes produces for the graph they decoded to. + private static void RequireCanonical(ref MessagePackReader reader, SequencePosition start, ExprGraph graph) + { + var canonical = new ArrayBufferWriter(); + var writer = new MessagePackWriter(canonical); + Write(ref writer, graph); + writer.Flush(); + var read = reader.Sequence.Slice(start, reader.Position); + if (read.Length != canonical.WrittenCount || !read.ToArray().AsSpan().SequenceEqual(canonical.WrittenSpan)) + throw new ExprGraphException("non-canonical"); + } + private static void Expect(ref MessagePackReader reader, int count) { if (reader.ReadArrayHeader() != count) throw new ExprGraphException("shape"); diff --git a/src/GameCult.Caching.MessagePack/README.md b/src/GameCult.Caching.MessagePack/README.md index cfe332bf..acb1142f 100644 --- a/src/GameCult.Caching.MessagePack/README.md +++ b/src/GameCult.Caching.MessagePack/README.md @@ -89,7 +89,7 @@ registered ahead of these wins. with a param `[name, default float bits]`, an output `[name, node]` and a node `[op name, operands, payload]`. Decoding runs the graph's one validator and refuses a broken graph whole (an `ExprGraphException` naming the rule), so a consumer -`[CultDocument]` record with an `ExprGraph` field needs no registration. A nil graph is refused. +`[CultDocument]` record with an `ExprGraph` field needs no registration. A nil graph is nil, as in every reference slot; decoding refuses any encoding but the canonical one. | Type | MessagePack | JSON | | --- | --- | --- | diff --git a/tests/GameCult.Caching.Tests/CultMathExprSerializationTests.cs b/tests/GameCult.Caching.Tests/CultMathExprSerializationTests.cs index 1ffc5abf..503fa14f 100644 --- a/tests/GameCult.Caching.Tests/CultMathExprSerializationTests.cs +++ b/tests/GameCult.Caching.Tests/CultMathExprSerializationTests.cs @@ -177,10 +177,90 @@ namespace GameCult.Caching.Tests [Test] public void DecodeRefusesNilAndWrongShapes() { - Refused(Bytes(null), "shape"); Refused(Bytes(new object[] { 1, new object[0] }), "shape"); - var act = () => MessagePackSerializer.Serialize(null!, Options); - act.Should().Throw().Which.InnerException.Should().BeOfType(); + } + + [Test] + public void ANilGraphIsNilLikeEveryOtherReferenceSlot() + { + MessagePackSerializer.Serialize(null, Options).Should().Equal(new byte[] { 0xc0 }); + MessagePackSerializer.Deserialize(new byte[] { 0xc0 }, Options).Should().BeNull(); + + var bytes = MessagePackSerializer.Serialize(new ExprHolder { Graph = null!, Scale = 3f }, Options); + var decoded = MessagePackSerializer.Deserialize(bytes, Options); + + decoded.Graph.Should().BeNull(); + decoded.Scale.Should().Be(3f); + } + + // [1, ["x"], [], [["o", 0]], [["input", [], 0]]] + private static readonly byte[] Minimal = + { + 0x95, 0x01, 0x91, 0xa1, 0x78, 0x90, 0x91, 0x92, 0xa1, 0x6f, 0x00, 0x91, 0x93, 0xa5, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x90, 0x00, + }; + + private static byte[] Replace(byte[] bytes, int at, int length, params byte[] with) => + bytes.Take(at).Concat(with).Concat(bytes.Skip(at + length)).ToArray(); + + [Test] + public void DecodeRefusesEveryEncodingButTheOneEncodeWrites() + { + Encode(Decode(Minimal)).Should().Equal(Minimal); + + Refused(Replace(Minimal, 1, 1, 0xd2, 0, 0, 0, 1), "non-canonical"); // version as int32 + Refused(Replace(Minimal, 1, 1, 0xcc, 1), "non-canonical"); // version as uint8 + Refused(Replace(Minimal, 3, 2, 0xd9, 1, 0x78), "non-canonical"); // "x" as str8 + Refused(Replace(Minimal, 2, 1, 0xdc, 0, 1), "non-canonical"); // input list header as array16 + Refused(Replace(Minimal, 5, 1, 0xdd, 0, 0, 0, 0), "non-canonical"); // empty param list as array32 + Refused(Replace(Minimal, Minimal.Length - 1, 1, 0xcc, 0), "non-canonical"); // payload as uint8 + Refused(Replace(Minimal, 10, 1, 0xd0, 0), "non-canonical"); // output node as int8 + } + + [Test] + public void DecodeRefusesNestedShapesAndNilNames() + { + var node = new object[] { "input", new object[0], 0 }; + object[] Graph(object[]? param = null, object[]? output = null, object? node0 = null) => + new object[] + { + 1, + new object[] { "x" }, + param is null ? new object[0] : new object[] { param }, + new object[] { output ?? new object[] { "o", 0 } }, + new object[] { node0 ?? node }, + }; + + Decode(Bytes(Graph(param: new object[] { "k", 0 }, node0: new object[] { "param", new object[0], 0 }))) + .ParamNames.Should().Equal("k"); + + Refused(Bytes(Graph(param: new object[] { "k", 0, 0 })), "shape"); + Refused(Bytes(Graph(param: new object[] { "k" })), "shape"); + Refused(Bytes(Graph(output: new object[] { "o", 0, 0 })), "shape"); + Refused(Bytes(Graph(output: new object[] { "o" })), "shape"); + Refused(Bytes(Graph(node0: new object[] { "input", new object[0], 0, 0 })), "shape"); + Refused(Bytes(Graph(node0: new object[] { "input", new object[0] })), "shape"); + Refused(Bytes(new object[] { 1, new object?[] { null }, new object[0], new object[0], new object[0] }), "shape"); + Refused(Bytes(Graph(param: new object?[] { null, 0 })), "shape"); + Refused(Bytes(Graph(output: new object?[] { null, 0 })), "shape"); + Refused(Bytes(Graph(node0: new object?[] { null, new object[0], 0 })), "shape"); + Refused(Bytes(new object[] { 1, new object[0], new object[0], new object[0], new object[0], 0 }), "shape"); + } + + [Test] + public void DepthIsReleasedAfterEachGraph() + { + var shallow = Options.WithSecurity(MessagePackSecurity.UntrustedData.WithMaximumObjectGraphDepth(4)); + var graphs = Enumerable.Range(0, 40).Select(_ => Sample()).ToArray(); + + var bytes = MessagePackSerializer.Serialize(graphs, shallow); + var decoded = MessagePackSerializer.Deserialize(bytes, shallow); + + decoded.Should().HaveCount(40); + Encode(decoded[39]).Should().Equal(Encode(graphs[0])); + + var tooDeep = Options.WithSecurity(MessagePackSecurity.UntrustedData.WithMaximumObjectGraphDepth(1)); + var act = () => MessagePackSerializer.Deserialize(bytes, tooDeep); + act.Should().Throw(); } } }