diff --git a/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs b/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs deleted file mode 100644 index 208ce1fcb6..0000000000 --- a/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs +++ /dev/null @@ -1,17 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file or at -// https://developers.google.com/open-source/licenses/bsd -#endregion - -namespace Google.Protobuf.Reflection; - -internal sealed partial class FeatureSetDescriptor -{ - // Canonical serialized form of the edition defaults, generated by embed_edition_defaults. - private const string DefaultsBase64 = - "ChMYhAciACoMCAEQAhgCIAMoATACChMY5wciACoMCAIQARgBIAIoATABChMY6AciDAgBEAEYASACKAEwASoAIOYHKOgH"; -} diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 4458d14e02..edf1f90f9f 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -1253,12 +1253,13 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { return EXIT_FAILURE; } - // Enforce extension declarations only when compiling. We want to skip - // this enforcement when protoc is just being invoked to encode or decode - // protos. - if (mode_ == MODE_COMPILE - ) { - descriptor_pool->EnforceExtensionDeclarations(true); + // Enforce extension declarations only when compiling. We want to skip this + // enforcement when protoc is just being invoked to encode or decode + // protos. If allowlist is disabled, we will not check for descriptor + // extensions declarations, either. + if (mode_ == MODE_COMPILE) { + descriptor_pool->EnforceExtensionDeclarations( + ExtDeclEnforcementLevel::kCustomExtensions); } if (!ParseInputFiles(descriptor_pool.get(), disk_source_tree.get(), &parsed_files)) { diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index c9f4dc765c..96130d19bc 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -2104,7 +2104,7 @@ DescriptorPool::DescriptorPool() lazily_build_dependencies_(false), allow_unknown_(false), enforce_weak_(false), - enforce_extension_declarations_(false), + enforce_extension_declarations_(ExtDeclEnforcementLevel::kNoEnforcement), disallow_enforce_utf8_(false), deprecated_legacy_json_field_conflicts_(false) {} @@ -2119,7 +2119,7 @@ DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database, lazily_build_dependencies_(false), allow_unknown_(false), enforce_weak_(false), - enforce_extension_declarations_(false), + enforce_extension_declarations_(ExtDeclEnforcementLevel::kNoEnforcement), disallow_enforce_utf8_(false), deprecated_legacy_json_field_conflicts_(false) {} @@ -2133,7 +2133,7 @@ DescriptorPool::DescriptorPool(const DescriptorPool* underlay) lazily_build_dependencies_(false), allow_unknown_(false), enforce_weak_(false), - enforce_extension_declarations_(false), + enforce_extension_declarations_(ExtDeclEnforcementLevel::kNoEnforcement), disallow_enforce_utf8_(false), deprecated_legacy_json_field_conflicts_(false) {} @@ -8045,7 +8045,7 @@ void DescriptorBuilder::ValidateOptions(const FieldDescriptor* field, return; } - if (pool_->enforce_extension_declarations_) { + if (pool_->EnforceCustomExtensionDeclarations()) { for (const auto& declaration : extension_range->options_->declaration()) { if (declaration.number() != field->number()) continue; if (declaration.reserved()) { diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h index 1222bac872..d30a6ffa4d 100644 --- a/src/google/protobuf/descriptor.h +++ b/src/google/protobuf/descriptor.h @@ -2041,6 +2041,18 @@ class PROTOBUF_EXPORT FileDescriptor : private internal::SymbolBase { PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(FileDescriptor, 168); +#ifndef SWIG +enum class ExtDeclEnforcementLevel : uint8_t { + // No enforcement. + kNoEnforcement = 0, + // All extensions excluding descriptor.proto extensions + // (go/extension-declarations#descriptor-proto) + kCustomExtensions = 1, + // All extensions including descriptor.proto extensions. + kAllExtensions = 2, +}; +#endif // !SWIG + // =================================================================== // Used to construct descriptors. @@ -2266,10 +2278,22 @@ class PROTOBUF_EXPORT DescriptorPool { // This enforcement is disabled by default because it requires full // descriptors with source-retention options, which are generally not // available at runtime. - void EnforceExtensionDeclarations(bool enforce) { + void EnforceExtensionDeclarations(google::protobuf::ExtDeclEnforcementLevel enforce) { enforce_extension_declarations_ = enforce; } + bool EnforceDescriptorExtensionDeclarations() const { + return enforce_extension_declarations_ == + ExtDeclEnforcementLevel::kAllExtensions; + } + + bool EnforceCustomExtensionDeclarations() const { + return enforce_extension_declarations_ == + ExtDeclEnforcementLevel::kAllExtensions || + enforce_extension_declarations_ == + ExtDeclEnforcementLevel::kCustomExtensions; + } + #ifndef SWIG // Dispatch recursive builds to a callback that may stick them onto a separate // thread. This is primarily to avoid stack overflows on untrusted inputs. @@ -2484,7 +2508,7 @@ class PROTOBUF_EXPORT DescriptorPool { bool lazily_build_dependencies_; bool allow_unknown_; bool enforce_weak_; - bool enforce_extension_declarations_; + ExtDeclEnforcementLevel enforce_extension_declarations_; bool disallow_enforce_utf8_; bool deprecated_legacy_json_field_conflicts_; mutable bool build_started_ = false; diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc index 587852416c..17eddae43f 100644 --- a/src/google/protobuf/descriptor_unittest.cc +++ b/src/google/protobuf/descriptor_unittest.cc @@ -4300,7 +4300,7 @@ class ValidationErrorTest : public testing::Test { void SetUp() override { // Enable extension declaration enforcement since most test cases want to // exercise the full validation. - pool_.EnforceExtensionDeclarations(true); + pool_.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); } // Parse file_text as a FileDescriptorProto in text format and add it // to the DescriptorPool. Expect no errors. @@ -11735,7 +11735,7 @@ TEST_F(ValidationErrorTest, ExtensionDeclarationsMismatchFullNameAllowed) { // Make sure that extension declaration names and types are not validated // outside of protoc. This is important for allowing extensions to be renamed // safely. - pool_.EnforceExtensionDeclarations(false); + pool_.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kNoEnforcement); BuildFile( R"pb( name: "foo.proto" @@ -11921,7 +11921,7 @@ TEST_P(ExtensionDeclarationsTest, DotPrefixTypeCompile) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); EXPECT_NE(pool.BuildFile(*file_proto), nullptr); } @@ -11954,7 +11954,7 @@ TEST_P(ExtensionDeclarationsTest, EnumTypeCompile) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); EXPECT_NE(pool.BuildFile(*file_proto), nullptr); } @@ -11991,7 +11991,7 @@ TEST_P(ExtensionDeclarationsTest, MismatchEnumType) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); MockErrorCollector error_collector; EXPECT_EQ(pool.BuildFileCollectingErrors(*file_proto, &error_collector), nullptr); @@ -12027,7 +12027,7 @@ TEST_P(ExtensionDeclarationsTest, DotPrefixFullNameCompile) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); EXPECT_NE(pool.BuildFile(*file_proto), nullptr); } @@ -12056,7 +12056,7 @@ TEST_P(ExtensionDeclarationsTest, MismatchDotPrefixTypeExpectingMessage) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); MockErrorCollector error_collector; EXPECT_EQ(pool.BuildFileCollectingErrors(*file_proto, &error_collector), nullptr); @@ -12086,7 +12086,7 @@ TEST_P(ExtensionDeclarationsTest, MismatchDotPrefixTypeExpectingNonMessage) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); MockErrorCollector error_collector; EXPECT_EQ(pool.BuildFileCollectingErrors(*file_proto, &error_collector), nullptr); @@ -12121,7 +12121,7 @@ TEST_P(ExtensionDeclarationsTest, MismatchMessageType) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); MockErrorCollector error_collector; EXPECT_EQ(pool.BuildFileCollectingErrors(*file_proto, &error_collector), nullptr); @@ -12151,7 +12151,7 @@ TEST_P(ExtensionDeclarationsTest, NonMessageTypeCompile) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); EXPECT_NE(pool.BuildFile(*file_proto), nullptr); } @@ -12180,7 +12180,7 @@ TEST_P(ExtensionDeclarationsTest, MismatchNonMessageType) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); MockErrorCollector error_collector; EXPECT_EQ(pool.BuildFileCollectingErrors(*file_proto, &error_collector), nullptr); @@ -12215,7 +12215,7 @@ TEST_P(ExtensionDeclarationsTest, MismatchCardinalityExpectingRepeated) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); MockErrorCollector error_collector; EXPECT_EQ(pool.BuildFileCollectingErrors(*file_proto, &error_collector), nullptr); @@ -12255,7 +12255,7 @@ TEST_P(ExtensionDeclarationsTest, MismatchCardinalityExpectingOptional) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); MockErrorCollector error_collector; EXPECT_EQ(pool.BuildFileCollectingErrors(*file_proto, &error_collector), nullptr); @@ -12287,7 +12287,7 @@ TEST_P(ExtensionDeclarationsTest, TypeDoesNotLookLikeIdentifier) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); MockErrorCollector error_collector; EXPECT_EQ(pool.BuildFileCollectingErrors(*file_proto, &error_collector), nullptr); @@ -12333,7 +12333,7 @@ TEST_P(ExtensionDeclarationsTest, MultipleDeclarationsInARangeCompile) { ASSERT_OK(file_proto); DescriptorPool pool; - pool.EnforceExtensionDeclarations(true); + pool.EnforceExtensionDeclarations(ExtDeclEnforcementLevel::kAllExtensions); EXPECT_NE(pool.BuildFile(*file_proto), nullptr); }