protocolbuffers / protocolbuffers/protobuf

Enable debug_redact for Java Logging

Open
#13,576 26 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement java keep open
Dominant language
C++
Stars
72k
Forks
16.3k
Avg merge
1d 17h
Merged PRs (30d)
140

Description

What language does this apply to?

Java and Proto version -> libprotoc 23.4

Describe the problem you are trying to solve.

There are many ways that protocol buffers might be stringified into logs and then printed into stack traces or service/application logs, etc. The built-in behavior stringifies the entire protocol buffer (protobuf) recursively, including all field data.

So for example, assuming that Person is a protobuf generated class (so it extends com.google.protobuf.GeneratedMessageV3) and there is an instance of it "person" then System.out.print(person); will print all the fields and values.

I can see a two conversations going on around redacting sensitive information from protocol buffers when they are stringified. Those discussions can be found at:

It seems like debug_redact has been added into descriptor.proto and generate the code, I can see that the generated Java files can have access to it. However, when I try to stringfy the object, the values for debug_redactenabled fields are still being printed into the logs. I believe that happens because there is no check in toString() method for this field in Java.

Describe the solution you'd like

It seems like currently toString() method uses private printFieldValue() method while stringifyinh values - and it does not check whether the descriptor is being used or not - or whether debug_redact set to true or not. Maybe we can add a check at the beginning of that method, so that if the debug_redact is set to true then the value will not be printed. The following might be added before line 546 (in this method), where the switch statement is:

......
    private void printFieldValue(
        final FieldDescriptor field, final Object value, final TextGenerator generator)
        throws IOException {
      if (field.getOptions().hasDebugRedact() && field.getOptions().getDebugRedact()) {
        break;  // do not print anything
     }
      switch (field.getType()) {
        case INT32:
        case SINT32:
        case SFIXED32:
......
}

There are other ways to achieve this goal as well but this is a straigtforward solution I believe.

Dear @acozzette, I kindly wanted to ping you as well since you have some background information and idea about this issues. And it seems like you also recently made some changes into TextFormat.java class.

Additional context

https://github.com/protocolbuffers/protobuf/blob/71a9ae22326d4a9b9fc6c4c87265c2967d4497ab/java/core/src/main/java/com/google/protobuf/TextFormat.java#L543-L607

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with java/core/src/main/java/com/google/protobuf/TextFormat.java, especially printFieldValue and the toString path, then inspect descriptor.proto for the debug_redact option. Confirm how Java generated descriptors expose the option and define completion as preventing enabled fields from appearing in stringified output without changing other field formatting.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
security
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.