Skip to content

Commit

Permalink
Downgrade type of string extension fields to bytes if UTF8 valida…
Browse files Browse the repository at this point in the history
…tion is not validated.

PiperOrigin-RevId: 712107026
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 4, 2025
1 parent 6c3ff5a commit ec58a34
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/google/protobuf/compiler/cpp/extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor,
variables_["constant_name"] = FieldConstantName(descriptor_);
variables_["field_type"] =
absl::StrCat(static_cast<int>(descriptor_->type()));
// Downgrade string to bytes if it is not UTF8 validated.
if (descriptor_->type() == FieldDescriptor::TYPE_STRING &&
!descriptor_->requires_utf8_validation()) {
variables_["field_type"] =
absl::StrCat(static_cast<int>(FieldDescriptor::TYPE_BYTES));
}
variables_["repeated"] = descriptor_->is_repeated() ? "true" : "false";
variables_["packed"] = descriptor_->is_packed() ? "true" : "false";
variables_["dllexport_decl"] = options.dllexport_decl;
Expand Down
5 changes: 4 additions & 1 deletion src/google/protobuf/generated_message_reflection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,10 @@ void Reflection::AddString(Message* message, const FieldDescriptor* field,
std::string value) const {
USAGE_MUTABLE_CHECK_ALL(AddString, REPEATED, STRING);
if (field->is_extension()) {
MutableExtensionSet(message)->AddString(field->number(), field->type(),
MutableExtensionSet(message)->AddString(field->number(),
field->requires_utf8_validation()
? FieldDescriptor::TYPE_STRING
: FieldDescriptor::TYPE_BYTES,
std::move(value), field);
} else {
switch (field->cpp_string_type()) {
Expand Down
17 changes: 17 additions & 0 deletions src/google/protobuf/generated_message_reflection_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "google/protobuf/message.h"
#include "google/protobuf/port.h"
#include "google/protobuf/test_util.h"
#include "google/protobuf/text_format.h"
#include "google/protobuf/unittest.pb.h"
#include "google/protobuf/unittest_mset.pb.h"
#include "google/protobuf/unittest_mset_wire_format.pb.h"
Expand Down Expand Up @@ -1807,6 +1808,22 @@ TEST(GeneratedMessageReflection, SwapImplicitPresenceShouldWork) {
EXPECT_EQ(lhs.child().optional_int32(), -1);
}

TEST(GeneratedMessageReflection, UnvalidatedStringsAreDowngradedToBytes) {
protobuf_unittest::TestChildExtension parsed_msg;
google::protobuf::TextFormat::ParseFromString(
R"pb(
optional_extension <
[protobuf_unittest.repeated_string_extension]: "foo"
>
)pb",
&parsed_msg);
protobuf_unittest::TestChildExtension msg;
msg.mutable_optional_extension()->AddExtension(
protobuf_unittest::repeated_string_extension, "bar");
parsed_msg.mutable_optional_extension()->Swap(
msg.mutable_optional_extension());
}

} // namespace
} // namespace protobuf
} // namespace google
Expand Down
7 changes: 7 additions & 0 deletions src/google/protobuf/unittest.proto
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,13 @@ extend TestAllExtensions {
TestAllTypes.NestedMessage oneof_nested_message_extension = 112;
string oneof_string_extension = 113;
bytes oneof_bytes_extension = 114;

string optional_utf8_string_extension = 115 [
features.utf8_validation = VERIFY
];
repeated string repeated_utf8_string_extension = 116 [
features.utf8_validation = VERIFY
];
}

message OptionalGroup_extension {
Expand Down

0 comments on commit ec58a34

Please sign in to comment.