Escape EventPipeProvider argument key as well (#825)
authorSung Yoon Whang <suwhang@microsoft.com>
Wed, 12 Feb 2020 23:10:13 +0000 (15:10 -0800)
committerGitHub <noreply@github.com>
Wed, 12 Feb 2020 23:10:13 +0000 (15:10 -0800)
* Escape EventPipeProvider argument key as well

* put the brace where it was

src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/EventPipeProvider.cs
src/tests/Microsoft.Diagnostics.NETCore.Client/EventPipeProviderTests.cs

index e3d00f4ee614e910302be20e016d2cec8c62bc2e..53f8591245f253f88c7c9b47396e4a4bcb2319c4 100644 (file)
@@ -69,8 +69,12 @@ namespace Microsoft.Diagnostics.NETCore.Client
             {
                 return "";
             }
-            return string.Join(";", Arguments.Select(a => (a.Value.Contains(";") || a.Value.Contains("=")) ? $"{a.Key}=\"{a.Value}\"" : $"{a.Key}={a.Value}"));
+            return string.Join(";", Arguments.Select(a => {
+                var escapedKey = a.Key.Contains(";") || a.Key.Contains("=") ? $"\"{a.Key}\"" : a.Key;
+                var escapedValue = a.Value.Contains(";") || a.Value.Contains("=") ? $"\"{a.Value}\"" : a.Value;
+                return $"{escapedKey}={escapedValue}";
+            }));
         }
 
     }
-}
\ No newline at end of file
+}
index 46d216d3cb7f914b3ec9112ff478e63c7a97a553..31f87b34a50926df601eed24bba61247566f11c7 100644 (file)
@@ -111,7 +111,7 @@ namespace Microsoft.Diagnostics.NETCore.Client
         }
 
         [Fact]
-        public void DiagnosticSourceArgumentStringTest1()
+        public void DiagnosticSourceArgumentStringTestWithEscapedValue1()
         {
             string diagnosticFilterString = "Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-" +
                 "Request.Path" +
@@ -130,7 +130,7 @@ namespace Microsoft.Diagnostics.NETCore.Client
 
 
         [Fact]
-        public void DiagnosticSourceArgumentStringTest2()
+        public void DiagnosticSourceArgumentStringTestWithEscapedValue2()
         {
             string diagnosticFilterString = "Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-" +
                 "Request.Path" +
@@ -147,5 +147,44 @@ namespace Microsoft.Diagnostics.NETCore.Client
             Assert.Equal("DiagnosticSourceProvider:0x00000000DEADBEEF:5:FilterAndPayloadSpecs=\"Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-Request.Path;Request.Method;RequestName=SomeRequest\r\n\"",
                 provider.ToString());
         }
+
+        [Fact]
+        public void DiagnosticSourceArgumentStringTestWithEscapedKey()
+        {
+            string diagnosticFilterString = "Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-" +
+                "Request.Path" +
+                ";Request.Method" +
+                ";RequestName=SomeRequest" +
+                "\r\n";
+
+            var provider = new EventPipeProvider("DiagnosticSourceProvider", EventLevel.Verbose, (long)(0xdeadbeef),
+                new Dictionary<string, string>()
+                {
+                    { "ArgumentKeyWith;Semicolon=Equal", diagnosticFilterString }
+                });
+
+            Assert.Equal("DiagnosticSourceProvider:0x00000000DEADBEEF:5:\"ArgumentKeyWith;Semicolon=Equal\"=\"Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-Request.Path;Request.Method;RequestName=SomeRequest\r\n\"",
+                provider.ToString());
+        }
+
+        [Fact]
+        public void DiagnosticSourceArgumentStringTestWithManyArgs()
+        {
+            string diagnosticFilterString = "Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-" +
+                "Request.Path" +
+                ";Request.Method" +
+                ";RequestName=SomeRequest" +
+                "\r\n";
+
+            var provider = new EventPipeProvider("DiagnosticSourceProvider", EventLevel.Verbose, (long)(0xdeadbeef),
+                new Dictionary<string, string>()
+                {
+                    { "ArgumentKeyWith;Semicolon=Equal", diagnosticFilterString },
+                    { "ArgumentKeyWith;Semicolon=Equal2", diagnosticFilterString }
+                });
+
+            Assert.Equal("DiagnosticSourceProvider:0x00000000DEADBEEF:5:\"ArgumentKeyWith;Semicolon=Equal\"=\"Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-Request.Path;Request.Method;RequestName=SomeRequest\r\n\";\"ArgumentKeyWith;Semicolon=Equal2\"=\"Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-Request.Path;Request.Method;RequestName=SomeRequest\r\n\"",
+                provider.ToString());
+        }
     }
 }