diff --git a/lang/csharp/src/apache/main/Protocol/Protocol.cs b/lang/csharp/src/apache/main/Protocol/Protocol.cs index 94ae1268a0d..241fe13bb41 100644 --- a/lang/csharp/src/apache/main/Protocol/Protocol.cs +++ b/lang/csharp/src/apache/main/Protocol/Protocol.cs @@ -126,6 +126,14 @@ public static Protocol Parse(string jstring) private static Protocol Parse(JToken jtok) { string name = JsonHelper.GetRequiredString(jtok, "protocol"); + try + { + SchemaName.ValidateName(name, "protocol"); + } + catch (SchemaParseException e) + { + throw new ProtocolParseException($"Invalid protocol name: {name}", e); + } string space = JsonHelper.GetOptionalString(jtok, "namespace"); string doc = JsonHelper.GetOptionalString(jtok, "doc"); diff --git a/lang/csharp/src/apache/main/Schema/EnumSchema.cs b/lang/csharp/src/apache/main/Schema/EnumSchema.cs index 225780310a6..9bec3cca353 100644 --- a/lang/csharp/src/apache/main/Schema/EnumSchema.cs +++ b/lang/csharp/src/apache/main/Schema/EnumSchema.cs @@ -102,6 +102,15 @@ internal static EnumSchema NewInstance(JToken jtok, PropertyMap props, SchemaNam if (symbolMap.ContainsKey(s)) throw new SchemaParseException($"Duplicate symbol: {s} at '{jtok.Path}'"); + try + { + ValidateSymbolName(s); + } + catch (AvroException e) + { + throw new SchemaParseException($"{e.Message} at '{jtok.Path}'", e); + } + symbolMap[s] = i++; symbols.Add(s); } diff --git a/lang/csharp/src/apache/test/Protocol/ProtocolTest.cs b/lang/csharp/src/apache/test/Protocol/ProtocolTest.cs index 47f8d98ef00..1e1501a2f69 100644 --- a/lang/csharp/src/apache/test/Protocol/ProtocolTest.cs +++ b/lang/csharp/src/apache/test/Protocol/ProtocolTest.cs @@ -198,6 +198,17 @@ public static void TestInvalidMessageName(string str) Assert.Throws(() => Protocol.Parse(str)); } + [TestCase(@"{ ""protocol"": ""9bad"", ""types"": [], ""messages"": {} }", + TestName = "ProtocolNameLeadingDigit")] + [TestCase(@"{ ""protocol"": ""bad name"", ""types"": [], ""messages"": {} }", + TestName = "ProtocolNameWithSpace")] + [TestCase(@"{ ""protocol"": ""x; class Evil"", ""types"": [], ""messages"": {} }", + TestName = "ProtocolNameInjection")] + public static void TestInvalidProtocolName(string str) + { + Assert.Throws(() => Protocol.Parse(str)); + } + // Protocols match [TestCase( @"{ diff --git a/lang/csharp/src/apache/test/Schema/SchemaTests.cs b/lang/csharp/src/apache/test/Schema/SchemaTests.cs index d2958a82042..e20c2231811 100644 --- a/lang/csharp/src/apache/test/Schema/SchemaTests.cs +++ b/lang/csharp/src/apache/test/Schema/SchemaTests.cs @@ -85,6 +85,12 @@ public class SchemaTests typeof(SchemaParseException), Description = "No name")] [TestCase("{\"type\": \"enum\", \"name\": \"Test\", \"symbols\" : [\"AA\", \"AA\"]}", typeof(SchemaParseException), Description = "Duplicate symbol")] + [TestCase("{\"type\": \"enum\", \"name\": \"Test\", \"symbols\" : [\"1A\"]}", + typeof(SchemaParseException), Description = "Symbol with leading digit")] + [TestCase("{\"type\": \"enum\", \"name\": \"Test\", \"symbols\" : [\"A B\"]}", + typeof(SchemaParseException), Description = "Symbol with space")] + [TestCase("{\"type\": \"enum\", \"name\": \"Test\", \"symbols\" : [\"A-B\"]}", + typeof(SchemaParseException), Description = "Symbol with hyphen")] // Array [TestCase("{\"type\": \"array\", \"items\": \"long\"}")]