2024 年 12 月 4 日宣布的变更

于 2024 年 12 月 4 日宣布的 Protocol Buffers 相关变更。

我们计划在 v30 版本中修改 C++ 的 Protobuf 调试 API(包括 Protobuf AbslStringify、proto2::ShortFormatproto2::Utf8FormatMessage::DebugStringMessage::ShortDebugStringMessage::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 内容如何。