diff options
author | Michael Lumish <mlumish@google.com> | 2015-08-28 09:47:53 -0700 |
---|---|---|
committer | Michael Lumish <mlumish@google.com> | 2015-08-28 09:47:53 -0700 |
commit | 9b1f91e7eac88ba477871a60912cc3b6ed3380ed (patch) | |
tree | 6a94767e0acae5e95f688c198d7784a46b80bfc0 | |
parent | a620e99a323d333991e091bf5bf33b8e96b9d761 (diff) | |
parent | 727956741da9e79b1f8c725ee61bc184e72e701e (diff) |
Merge pull request #3125 from jtattermusch/metadata_key_check
Check for allowed characters in metadata key.
-rw-r--r-- | src/csharp/Grpc.Core.Tests/MetadataTest.cs | 11 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Metadata.cs | 7 |
2 files changed, 17 insertions, 1 deletions
diff --git a/src/csharp/Grpc.Core.Tests/MetadataTest.cs b/src/csharp/Grpc.Core.Tests/MetadataTest.cs index c00f945d6a..ddeb7d0926 100644 --- a/src/csharp/Grpc.Core.Tests/MetadataTest.cs +++ b/src/csharp/Grpc.Core.Tests/MetadataTest.cs @@ -75,6 +75,17 @@ namespace Grpc.Core.Tests } [Test] + public void AsciiEntry_KeyValidity() + { + new Metadata.Entry("ABC", "XYZ"); + new Metadata.Entry("0123456789abc", "XYZ"); + new Metadata.Entry("-abc", "XYZ"); + new Metadata.Entry("a_bc_", "XYZ"); + Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("abc[", "xyz")); + Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("abc/", "xyz")); + } + + [Test] public void Entry_ConstructionPreconditions() { Assert.Throws(typeof(ArgumentNullException), () => new Metadata.Entry(null, "xyz")); diff --git a/src/csharp/Grpc.Core/Metadata.cs b/src/csharp/Grpc.Core/Metadata.cs index 2b08e0de51..21bdf4f114 100644 --- a/src/csharp/Grpc.Core/Metadata.cs +++ b/src/csharp/Grpc.Core/Metadata.cs @@ -36,6 +36,7 @@ using System.Collections.Specialized; using System.Globalization; using System.Runtime.InteropServices; using System.Text; +using System.Text.RegularExpressions; using Grpc.Core.Utils; @@ -189,6 +190,7 @@ namespace Grpc.Core public struct Entry { private static readonly Encoding Encoding = Encoding.ASCII; + private static readonly Regex ValidKeyRegex = new Regex("^[a-z0-9_-]+$"); readonly string key; readonly string value; @@ -321,7 +323,10 @@ namespace Grpc.Core private static string NormalizeKey(string key) { - return Preconditions.CheckNotNull(key, "key").ToLower(CultureInfo.InvariantCulture); + var normalized = Preconditions.CheckNotNull(key, "key").ToLower(CultureInfo.InvariantCulture); + Preconditions.CheckArgument(ValidKeyRegex.IsMatch(normalized), + "Metadata entry key not valid. Keys can only contain lowercase alphanumeric characters, underscores and hyphens."); + return normalized; } } } |