From b6a69ee78c1d233c8cc3a11d9e3a1d4c06faf7bc Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 13 Nov 2026 19:41:10 -0700 Subject: [PATCH] Test escaping and untrimmed passthrough in every JSON string field Quote was only exercised on tag values and bare control characters. Pin that the application, name and tag keys are escaped the same way, that DEL and U+2028/U+2029 go out unescaped (valid JSON), and that Report sends an event's name, tag keys and tag values untrimmed, unlike the application, version and key the constructor trims. Co-Authored-By: Claude Opus 5.5 --- tests/TraceClient.Tests/TraceClientTest.cs | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/TraceClient.Tests/TraceClientTest.cs b/tests/TraceClient.Tests/TraceClientTest.cs index 377cf06..c1dd52c 100644 --- a/tests/TraceClient.Tests/TraceClientTest.cs +++ b/tests/TraceClient.Tests/TraceClientTest.cs @@ -929,6 +929,45 @@ public void Quote_EscapesCarriageReturnAndEveryOtherControlCharacter() Assert.Equal("\" \u00e9/\u00fc\"", TraceClient.Quote(" \u00e9/\u00fc")); } + [Fact] + public void Quote_WritesDeleteAndTheUnicodeLineSeparatorsAsIs() + { + // JSON only requires U+0000-U+001F to be escaped, so DEL and the + // line/paragraph separators are valid as they are and go out unescaped. + Assert.Equal("\"\u007f\u2028\u2029\"", TraceClient.Quote("\u007f\u2028\u2029")); + } + + [Fact] + public void Json_EscapesTheApplicationNameAndTagKeysLikeTagValues() + { + // Every string in the body goes through Quote, not only tag values. + var tags = new List> + { + new KeyValuePair("k\"1", "v"), + new KeyValuePair("line\nkey", "v"), + }; + + Assert.Null(TraceClient.Json("My \"Game\"", "a\\b", null, tags, out string json)); + + Assert.Equal("{\"application\":\"My \\\"Game\\\"\",\"name\":\"a\\\\b\"," + + "\"tags\":{\"k\\\"1\":\"v\",\"line\\nkey\":\"v\"}}", json); + } + + [Fact] + public void Report_SendsTheNameAndTagsUntrimmed() + { + // The constructor trims the application, version and key; Report + // sends an event's name, tag keys and tag values exactly as given. + var client = new TraceClient(_server.BaseUrl, "MyGame", "1.2.3", key: "k"); + + client.Report(" startup\t", tags: new Dictionary { { " k ", " v " } }); + client.Close(); + + Assert.Equal("{\"application\":\"MyGame\",\"name\":\" startup\\t\"," + + "\"tags\":{\" k \":\" v \",\"version\":\"1.2.3\"}}", + _server.Received.Single().Body); + } + [Fact] public void EnvironmentDisables_IsFalseWhenTheEnvironmentCannotBeRead() {