Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 698068189
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 19, 2024
1 parent ec5acb6 commit 13157d4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
17 changes: 0 additions & 17 deletions csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs

This file was deleted.

13 changes: 7 additions & 6 deletions src/google/protobuf/compiler/command_line_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 4 additions & 4 deletions src/google/protobuf/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand All @@ -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) {}

Expand All @@ -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) {}

Expand Down Expand Up @@ -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()) {
Expand Down
28 changes: 26 additions & 2 deletions src/google/protobuf/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 15 additions & 15 deletions src/google/protobuf/descriptor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 13157d4

Please sign in to comment.