2024 年 12 月 4 日宣布的变更

2024 年 12 月 4 日为 Protocol Buffers 宣布的变更。

我们计划在 v30 中修改 C++ 的 Protobuf 调试 API(包括 Protobuf AbslStringify、`proto2::ShortFormat`、`proto2::Utf8Format`、`Message::DebugString`、`Message::ShortDebugString`、`Message::Utf8DebugString`),以编辑由 `debug_redact` 注释的敏感字段;这些 API 的输出将包含每个进程的随机前缀,因此将不再能被 Protobuf TextFormat 解析器解析。

动机

目前,Protobuf 调试 API 将 proto 中的每个字段打印为人类可读的格式。这可能会导致隐私事件,即开发人员意外地记录包含敏感字段的 Protobuf 调试输出。

如何注释敏感字段

有两种方法可以将字段标记为敏感

  • 直接使用字段选项 `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_redact` 注释的敏感字段在输出格式中自动被编辑
  • 输出格式将包含每个进程的随机前缀,这将使其不再能被 TextFormat 解析器解析。

请注意,第二个更改是正确的,无论 proto 是否包含敏感字段,这确保了任何调试输出始终无法反序列化,而与 proto 内容无关。