Add additional proto validation to descriptor builds.

These would normally be caught by the parser, but would be bypassed by dynamic descriptor builds.

PiperOrigin-RevId: 572010482
pull/14326/head
Mike Kruskal 1 year ago committed by Copybara-Service
parent 55fb27df28
commit 863b9d3190
  1. 13
      src/google/protobuf/descriptor.cc
  2. 45
      src/google/protobuf/descriptor_unittest.cc

@ -7920,6 +7920,19 @@ void DescriptorBuilder::ValidateFieldFeatures(
// Rely on our legacy validation for proto2/proto3 files.
if (IsLegacyFeatureSet(field->features())) return;
// Double check proto descriptors in editions. These would usually be caught
// by the parser, but may not be for dynamically built descriptors.
if (proto.label() == FieldDescriptorProto::LABEL_REQUIRED) {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NAME,
"Required label is not allowed under editions. Use the feature "
"field_presence = LEGACY_REQUIRED to control this behavior.");
}
if (proto.type() == FieldDescriptorProto::TYPE_GROUP) {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NAME,
"Group types are not allowed under editions. Use the feature "
"message_encoding = DELIMITED to control this behavior.");
}
// Validate legacy options that have been migrated to features.
if (field->options().has_packed()) {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NAME,

@ -9983,6 +9983,51 @@ TEST_F(FeaturesTest, InvalidJsonUniquenessCustomError) {
"conflicts with the custom JSON name of field \"bar\".\n");
}
TEST_F(FeaturesTest, InvalidRequiredLabel) {
BuildDescriptorMessagesInTestPool();
BuildFileWithErrors(
R"pb(
name: "foo.proto"
syntax: "editions"
edition: EDITION_2023
message_type {
name: "Foo"
field {
name: "bar"
number: 1
label: LABEL_REQUIRED
type: TYPE_STRING
}
}
)pb",
"foo.proto: Foo.bar: NAME: Required label is not allowed under editions. "
" Use the feature field_presence = LEGACY_REQUIRED to control this "
"behavior.\n");
}
TEST_F(FeaturesTest, InvalidGroupLabel) {
BuildDescriptorMessagesInTestPool();
BuildFileWithErrors(
R"pb(
name: "foo.proto"
syntax: "editions"
edition: EDITION_2023
message_type {
name: "Foo"
field {
name: "bar"
number: 1
type_name: ".Foo"
label: LABEL_OPTIONAL
type: TYPE_GROUP
}
}
)pb",
"foo.proto: Foo.bar: NAME: Group types are not allowed under editions. "
"Use the feature message_encoding = DELIMITED to control this "
"behavior.\n");
}
// Test that the result of FileDescriptor::DebugString() can be used to create
// the original descriptors.
class FeaturesDebugStringTest

Loading…
Cancel
Save