From b478a29bf0945d5b141992885ac9cfde45c66697 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 1 May 2023 08:57:11 -0700 Subject: [PATCH] Forbid embedded nulls in `json_name`. While embedded nulls are technically allowed in JSON strings (and thus as object keys), they put undesirable constraints on implementations. Embedded nulls require that string length is stored explicitly, which wastes memory to accommodate a case that virtually nobody wants or needs. PiperOrigin-RevId: 528484333 --- src/google/protobuf/descriptor.cc | 6 ++++++ src/google/protobuf/descriptor_unittest.cc | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index e93650e2ff..9a0e9f6cbd 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -7336,6 +7336,12 @@ void DescriptorBuilder::ValidateFieldOptions( "option json_name is not allowed on extension fields."); } + if (absl::StrContains(field->json_name(), '\0')) { + AddError(field->full_name(), proto, + DescriptorPool::ErrorCollector::OPTION_NAME, + "json_name cannot have embedded null characters."); + } + } void DescriptorBuilder::ValidateEnumOptions(const EnumDescriptor* enm, diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc index 39a8f1aa15..eb445af8b7 100644 --- a/src/google/protobuf/descriptor_unittest.cc +++ b/src/google/protobuf/descriptor_unittest.cc @@ -5892,6 +5892,24 @@ TEST_F(ValidationErrorTest, JsonNameOptionOnExtensions) { "extension fields.\n"); } +TEST_F(ValidationErrorTest, JsonNameEmbeddedNull) { + BuildFileWithErrors( + "name: \"foo.proto\" " + "package: \"foo\" " + "message_type {" + " name: \"Foo\"" + " field {" + " name: \"value\"" + " number: 10" + " label: LABEL_OPTIONAL" + " type: TYPE_INT32" + " json_name: \"embedded\\000null\"" + " }" + "}", + "foo.proto: foo.Foo.value: OPTION_NAME: json_name cannot have embedded " + "null characters.\n"); +} + TEST_F(ValidationErrorTest, DuplicateExtensionFieldNumber) { BuildDescriptorMessagesInTestPool();