2024 年 12 月 4 日宣布的變更

Protocol Buffers 於 2024 年 12 月 4 日宣布的變更。

我們計劃在 v30 版本中修改 C++ 的 Protobuf debug API (包含 Protobuf AbslStringify、proto2::ShortFormatproto2::Utf8FormatMessage::DebugStringMessage::ShortDebugStringMessage::Utf8DebugString),以編輯標註為 debug_redact 的敏感欄位;這些 API 的輸出將包含每個程序隨機產生的前綴,因此將無法再由 Protobuf TextFormat Parser 解析。

動機

目前 Protobuf debug API 會將 proto 中的每個欄位列印成人類可讀的格式。這可能會導致隱私事件,開發人員不小心記錄了包含敏感欄位的 Protobuf debug 輸出。

如何註解敏感欄位

有兩種方式可以將欄位標記為敏感

  • 直接使用欄位選項 debug_redact = true 標記欄位。

    message Foo {
      optional string secret = 1 [debug_redact = true];
    }
    
  • 如果您已經透過擴充 proto2.FieldOptions 定義了 Enum 類型的欄位註解,並且此註解的某些值用於註解您想要編輯的欄位,則可以使用 debug_redact = true 註解這些值。所有已使用這些值註解的欄位都將被編輯。

    package my.package;
    
    extend proto2.FieldOptions {
      # The existing field annotation
      optional ContentType content_type = 1234567;
    };
    
    enum ContentType {
      PUBLIC = 0;
      SECRET = 1 [debug_redact = true];
    };
    
    message Foo {
      # will not be redacted
      optional string public_info = 1 [
        (my.package.content_type) = PUBLIC
      ];
      # will be redacted
      optional string secret = 1 [
        (my.package.content_type) = SECRET
      ];
    }
    

新的 Debug 格式

與現有的 debug 格式相比,新的 debug 格式有兩個主要差異

  • 標註為 debug_redact 的敏感欄位會在輸出格式中自動被編輯
  • 輸出格式將包含每個程序隨機產生的前綴,這將使其無法再由 TextFormat parser 解析。

請注意,第二個變更無論 proto 是否包含敏感欄位都成立,這確保了無論 proto 內容為何,任何 debug 輸出始終無法被還原序列化。