Skip to content

Commit

Permalink
Internal change.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 627582199
  • Loading branch information
protobuf-github-bot authored and Copybara-Service committed Apr 24, 2024
1 parent 322d4d4 commit 2c16dec
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 48 deletions.
109 changes: 65 additions & 44 deletions src/google/protobuf/text_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,61 @@ const char kDebugStringSilentMarkerForDetection[] = "\t ";
// Controls insertion of kDebugStringSilentMarker into DebugString() output.
PROTOBUF_EXPORT std::atomic<bool> enable_debug_text_format_marker;

// Controls insertion of a marker making debug strings non-parseable, and
// redacting annotated fields in Protobuf's DebugString APIs.
PROTOBUF_EXPORT std::atomic<bool> enable_debug_string_safe_format{false};

int64_t GetRedactedFieldCount() {
return num_redacted_field.load(std::memory_order_relaxed);
}

enum class Option { kNone, kShort, kUTF8 };

std::string StringifyMessage(const Message& message, Option option,
FieldReporterLevel reporter_level,
bool enable_safe_format) {
// Indicate all scoped reflection calls are from DebugString function.
ScopedReflectionMode scope(ReflectionMode::kDebugString);

TextFormat::Printer printer;
internal::FieldReporterLevel reporter = reporter_level;
switch (option) {
case Option::kShort:
printer.SetSingleLineMode(true);
break;
case Option::kUTF8:
printer.SetUseUtf8StringEscaping(true);
break;
case Option::kNone:
break;
}
printer.SetExpandAny(true);
printer.SetRedactDebugString(enable_safe_format);
printer.SetRandomizeDebugString(enable_safe_format);
printer.SetReportSensitiveFields(reporter);
std::string result;
printer.PrintToString(message, &result);

if (option == Option::kShort) {
TrimTrailingSpace(result);
}

return result;
}

PROTOBUF_EXPORT std::string StringifyMessage(const Message& message) {
return StringifyMessage(message, Option::kNone,
FieldReporterLevel::kAbslStringify, true);
}
} // namespace internal

std::string Message::DebugString() const {
bool enable_safe_format =
internal::enable_debug_string_safe_format.load(std::memory_order_relaxed);
if (enable_safe_format) {
return StringifyMessage(*this, internal::Option::kNone,
FieldReporterLevel::kDebugString, true);
}
// Indicate all scoped reflection calls are from DebugString function.
ScopedReflectionMode scope(ReflectionMode::kDebugString);
std::string debug_string;
Expand All @@ -119,6 +168,12 @@ std::string Message::DebugString() const {
}

std::string Message::ShortDebugString() const {
bool enable_safe_format =
internal::enable_debug_string_safe_format.load(std::memory_order_relaxed);
if (enable_safe_format) {
return StringifyMessage(*this, internal::Option::kShort,
FieldReporterLevel::kShortDebugString, true);
}
// Indicate all scoped reflection calls are from DebugString function.
ScopedReflectionMode scope(ReflectionMode::kDebugString);
std::string debug_string;
Expand All @@ -137,6 +192,12 @@ std::string Message::ShortDebugString() const {
}

std::string Message::Utf8DebugString() const {
bool enable_safe_format =
internal::enable_debug_string_safe_format.load(std::memory_order_relaxed);
if (enable_safe_format) {
return StringifyMessage(*this, internal::Option::kUTF8,
FieldReporterLevel::kUtf8DebugString, true);
}
// Indicate all scoped reflection calls are from DebugString function.
ScopedReflectionMode scope(ReflectionMode::kDebugString);
std::string debug_string;
Expand All @@ -155,54 +216,14 @@ std::string Message::Utf8DebugString() const {

void Message::PrintDebugString() const { printf("%s", DebugString().c_str()); }

namespace internal {

enum class Option { kNone, kShort, kUTF8 };

std::string StringifyMessage(const Message& message, Option option) {
// Indicate all scoped reflection calls are from DebugString function.
ScopedReflectionMode scope(ReflectionMode::kDebugString);

TextFormat::Printer printer;
internal::FieldReporterLevel reporter = FieldReporterLevel::kAbslStringify;
switch (option) {
case Option::kShort:
printer.SetSingleLineMode(true);
reporter = FieldReporterLevel::kShortFormat;
break;
case Option::kUTF8:
printer.SetUseUtf8StringEscaping(true);
reporter = FieldReporterLevel::kUtf8Format;
break;
case Option::kNone:
break;
}
printer.SetExpandAny(true);
printer.SetRedactDebugString(true);
printer.SetRandomizeDebugString(true);
printer.SetReportSensitiveFields(reporter);
std::string result;
printer.PrintToString(message, &result);

if (option == Option::kShort) {
TrimTrailingSpace(result);
}

return result;
}

PROTOBUF_EXPORT std::string StringifyMessage(const Message& message) {
return StringifyMessage(message, Option::kNone);
}

} // namespace internal

PROTOBUF_EXPORT std::string ShortFormat(const Message& message) {
return internal::StringifyMessage(message, internal::Option::kShort);
return internal::StringifyMessage(message, internal::Option::kShort,
FieldReporterLevel::kShortFormat, true);
}

PROTOBUF_EXPORT std::string Utf8Format(const Message& message) {
return internal::StringifyMessage(message, internal::Option::kUTF8);
return internal::StringifyMessage(message, internal::Option::kUTF8,
FieldReporterLevel::kUtf8Format, true);
}


Expand Down
14 changes: 10 additions & 4 deletions src/google/protobuf/text_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ PROTOBUF_EXPORT extern const char kDebugStringSilentMarker[1];
PROTOBUF_EXPORT extern const char kDebugStringSilentMarkerForDetection[3];

PROTOBUF_EXPORT extern std::atomic<bool> enable_debug_text_format_marker;
PROTOBUF_EXPORT extern std::atomic<bool> enable_debug_string_safe_format;
PROTOBUF_EXPORT int64_t GetRedactedFieldCount();
PROTOBUF_EXPORT bool ShouldRedactField(const FieldDescriptor* field);

Expand Down Expand Up @@ -86,9 +87,13 @@ namespace internal {
// Enum used to set printing options for StringifyMessage.
PROTOBUF_EXPORT enum class Option;

// Converts a protobuf message to a string with redaction enabled.
// Converts a protobuf message to a string. If enable_safe_format is true,
// sensitive fields are redacted, and a per-process randomized prefix is
// inserted.
PROTOBUF_EXPORT std::string StringifyMessage(const Message& message,
Option option);
Option option,
FieldReporterLevel reporter_level,
bool enable_safe_format);

class UnsetFieldsMetadataTextFormatTestUtil;
class UnsetFieldsMetadataMessageDifferencerTestUtil;
Expand Down Expand Up @@ -451,8 +456,9 @@ class PROTOBUF_EXPORT TextFormat {
friend std::string Message::DebugString() const;
friend std::string Message::ShortDebugString() const;
friend std::string Message::Utf8DebugString() const;
friend std::string internal::StringifyMessage(const Message& message,
internal::Option option);
friend std::string internal::StringifyMessage(
const Message& message, internal::Option option,
internal::FieldReporterLevel reporter_level, bool enable_safe_format);

// Sets whether silent markers will be inserted.
void SetInsertSilentMarker(bool v) { insert_silent_marker_ = v; }
Expand Down

0 comments on commit 2c16dec

Please sign in to comment.