summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkpavlov00 <[email protected]>2025-07-16 20:33:53 +0300
committerkpavlov00 <[email protected]>2025-07-16 21:03:13 +0300
commit6ff1cedabcf41909b7c953687ca4fcddb9b3ed2b (patch)
tree04f9ead2bd66c15552c12d644f2c704c5ba98d0c
parentedd33f92e99348fe240f3b558d4f24eabf6fc657 (diff)
feat contrib/protobuf: enable debug_redact for message fields
Backport of upstream commits: <https://github.com/protocolbuffers/protobuf/commit/759fd91388235d7f23f2621d7b8e962cded51781> <https://github.com/protocolbuffers/protobuf/commit/c3f61a61281e23d8ad2bd6ca0e55d1c31576f36d> commit_hash:94f61146a91a24c44303b1bad6c98922b1f8c75e
-rw-r--r--contrib/libs/protobuf/patches/enable-debug-redact-for-message-fields-backport-ci-759fd91-c3f61a6.patch72
-rw-r--r--contrib/libs/protobuf/src/google/protobuf/text_format.cc33
-rw-r--r--contrib/libs/protobuf/src/google/protobuf/text_format.h7
3 files changed, 109 insertions, 3 deletions
diff --git a/contrib/libs/protobuf/patches/enable-debug-redact-for-message-fields-backport-ci-759fd91-c3f61a6.patch b/contrib/libs/protobuf/patches/enable-debug-redact-for-message-fields-backport-ci-759fd91-c3f61a6.patch
new file mode 100644
index 00000000000..cb9d245a36e
--- /dev/null
+++ b/contrib/libs/protobuf/patches/enable-debug-redact-for-message-fields-backport-ci-759fd91-c3f61a6.patch
@@ -0,0 +1,72 @@
+--- contrib/libs/protobuf/src/google/protobuf/text_format.cc (7e573068bfd84adfb48ca975dd5bf3837a5bc0d1)
++++ contrib/libs/protobuf/src/google/protobuf/text_format.cc (working tree)
+@@ -2508,6 +2508,10 @@ void TextFormat::Printer::PrintField(const Message& message,
+ PrintFieldName(message, field_index, count, reflection, field, generator);
+
+ if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
++ if (TryRedactFieldValue(message, field, generator,
++ /*insert_value_separator=*/true)) {
++ break;
++ }
+ const FastFieldValuePrinter* printer = GetFieldPrinter(field);
+ const Message& sub_message =
+ field->is_repeated()
+@@ -2582,9 +2586,8 @@ void TextFormat::Printer::PrintFieldValue(const Message& message,
+ << "Index must be -1 for non-repeated fields";
+
+ const FastFieldValuePrinter* printer = GetFieldPrinter(field);
+- if (redact_debug_string_ && field->options().debug_redact()) {
+- // TODO(b/258975650): Create OSS redaction documentation
+- generator->PrintString("[REDACTED]");
++ if (TryRedactFieldValue(message, field, generator,
++ /*insert_value_separator=*/false)) {
+ return;
+ }
+
+@@ -2799,6 +2802,30 @@ void TextFormat::Printer::PrintUnknownFields(
+ }
+ }
+
++bool TextFormat::Printer::TryRedactFieldValue(
++ const Message& message, const FieldDescriptor* field,
++ BaseTextGenerator* generator, bool insert_value_separator) const {
++ auto do_redact = [&](const TProtoStringType& replacement) {
++ if (insert_value_separator) {
++ generator->PrintMaybeWithMarker(MarkerToken(), ": ");
++ }
++ generator->PrintString(replacement);
++ if (insert_value_separator) {
++ if (single_line_mode_) {
++ generator->PrintLiteral(" ");
++ } else {
++ generator->PrintLiteral("\n");
++ }
++ }
++ };
++
++ if (redact_debug_string_ && field->options().debug_redact()) {
++ do_redact("[REDACTED]");
++ return true;
++ }
++ return false;
++}
++
+ } // namespace protobuf
+ } // namespace google
+
+--- contrib/libs/protobuf/src/google/protobuf/text_format.h (7e573068bfd84adfb48ca975dd5bf3837a5bc0d1)
++++ contrib/libs/protobuf/src/google/protobuf/text_format.h (working tree)
+@@ -487,6 +487,13 @@ class PROTOBUF_EXPORT TextFormat {
+
+ bool PrintAny(const Message& message, BaseTextGenerator* generator) const;
+
++ // Try to redact a field value based on the annotations associated with
++ // the field. This function returns true if it redacts the field value.
++ bool TryRedactFieldValue(const Message& message,
++ const FieldDescriptor* field,
++ BaseTextGenerator* generator,
++ bool insert_value_separator) const;
++
+ const FastFieldValuePrinter* GetFieldPrinter(
+ const FieldDescriptor* field) const {
+ auto it = custom_printers_.find(field);
diff --git a/contrib/libs/protobuf/src/google/protobuf/text_format.cc b/contrib/libs/protobuf/src/google/protobuf/text_format.cc
index 5ec37f1e3ee..b98e265ee5a 100644
--- a/contrib/libs/protobuf/src/google/protobuf/text_format.cc
+++ b/contrib/libs/protobuf/src/google/protobuf/text_format.cc
@@ -2508,6 +2508,10 @@ void TextFormat::Printer::PrintField(const Message& message,
PrintFieldName(message, field_index, count, reflection, field, generator);
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
+ if (TryRedactFieldValue(message, field, generator,
+ /*insert_value_separator=*/true)) {
+ break;
+ }
const FastFieldValuePrinter* printer = GetFieldPrinter(field);
const Message& sub_message =
field->is_repeated()
@@ -2582,9 +2586,8 @@ void TextFormat::Printer::PrintFieldValue(const Message& message,
<< "Index must be -1 for non-repeated fields";
const FastFieldValuePrinter* printer = GetFieldPrinter(field);
- if (redact_debug_string_ && field->options().debug_redact()) {
- // TODO(b/258975650): Create OSS redaction documentation
- generator->PrintString("[REDACTED]");
+ if (TryRedactFieldValue(message, field, generator,
+ /*insert_value_separator=*/false)) {
return;
}
@@ -2799,6 +2802,30 @@ void TextFormat::Printer::PrintUnknownFields(
}
}
+bool TextFormat::Printer::TryRedactFieldValue(
+ const Message& message, const FieldDescriptor* field,
+ BaseTextGenerator* generator, bool insert_value_separator) const {
+ auto do_redact = [&](const TProtoStringType& replacement) {
+ if (insert_value_separator) {
+ generator->PrintMaybeWithMarker(MarkerToken(), ": ");
+ }
+ generator->PrintString(replacement);
+ if (insert_value_separator) {
+ if (single_line_mode_) {
+ generator->PrintLiteral(" ");
+ } else {
+ generator->PrintLiteral("\n");
+ }
+ }
+ };
+
+ if (redact_debug_string_ && field->options().debug_redact()) {
+ do_redact("[REDACTED]");
+ return true;
+ }
+ return false;
+}
+
} // namespace protobuf
} // namespace google
diff --git a/contrib/libs/protobuf/src/google/protobuf/text_format.h b/contrib/libs/protobuf/src/google/protobuf/text_format.h
index d79923a5c4b..05ac4b2d330 100644
--- a/contrib/libs/protobuf/src/google/protobuf/text_format.h
+++ b/contrib/libs/protobuf/src/google/protobuf/text_format.h
@@ -487,6 +487,13 @@ class PROTOBUF_EXPORT TextFormat {
bool PrintAny(const Message& message, BaseTextGenerator* generator) const;
+ // Try to redact a field value based on the annotations associated with
+ // the field. This function returns true if it redacts the field value.
+ bool TryRedactFieldValue(const Message& message,
+ const FieldDescriptor* field,
+ BaseTextGenerator* generator,
+ bool insert_value_separator) const;
+
const FastFieldValuePrinter* GetFieldPrinter(
const FieldDescriptor* field) const {
auto it = custom_printers_.find(field);