A Protobuf Any Is a URL and a Bag of Bytes (and Nobody Checks Either)

I wrapped a message in an Any this week and read the bytes. The well-known type has exactly two fields: a string type_url as field one and a bytes blob as field two, and the string is readable. type.googleapis.com/demo.User sits there in plain text, twenty-nine characters, no encoding, no lookup needed. Most of the format does not let you do that, a field number points at a file you may not have and the name never travels. Any puts its one answer in the clear. I encoded an event carrying a wrapped User with a name and an age, fourty-four bytes on the wire, and the payload itself was nine bytes of ordinary protobuf, tag, length, the name, the age varint, riding inside the bytes field. A stream in a stream, the way nested messages always are, except the outer bag tells you what the inner one is supposed to be.

The format does not check the pairing. There is no registry, no checksum, no signature tying the URL to the blob. I set the type_url to totally/made.up/Foo and the whole thing encoded and decoded without complaint, and a bytes field that the URL promises to be one message can hold anything. The URL tells you what the bytes mean. Nothing enforces it. The raw decode view is what a no-schema reader gets: a string field with a URL you can read off the page, and a bytes field that will decode cleanly if the URL is honest and decode to junk or nothing if it is not. When those disagree, that is a protocol bug in the service that packed the Any, and the bytes show it to you directly.

The JSON side is the one-way door from the Timestamp and Duration posts, and it is worse than theirs. The official mapping says an Any serializes as an object with an @type key set to the URL and the payload's fields inlined right next to it, the shape a compliant library emits and the shape everyone expects in JSON logs. The converter's library, protobufjs, does not speak that shape on output. It gives you the raw message: type_url as a string, value as a base64 blob, twelve characters of base64 standing in for the nine payload bytes. And it accepts that raw shape on the way in, fine. The door only one way would be tolerable. What I measured is that the official shape on input is not rejected. It is dropped, key by key, silently.

Hand the encoder an object with @type, name and age, the exact form a spec-compliant library wrote, and every key it does not recognize vanishes with no error. What encodes is an empty Any, and on the wire that is two bytes: the field tag and a zero length, 0a00. The field is present, the content is gone, and the stream is still perfectly valid. Your payload did not fail to serialize. It left, and nothing told you. That is a different failure mode from the other two well-known types, where the mismatched form hits an object expected error and at least the round trip stops. Here the round trip completes. It just returns less than you gave it.

This is where it hurts in practice, because Any is the shape of a lot of real traffic: gRPC APIs that wrap heterogeneous payloads, event buses, audit logs, anything that stuffs a typed message into one field so the outer schema can stay stable. The incident pattern is a service serializing events to JSON with the official mapping, a downstream consumer decoding them with a different library, and the consumer's Any coming back empty. The log line shows a well-formed message with no payload, and the debugging starts with your data. The check that ends it: decode the bytes with no schema, read the URL off as text, decode the value blob with the schema the URL names, and compare what you got with what you sent. That loop is the only one the format gives you, because the format gives you no other.

All of it runs in the browser with no protoc. Paste a .proto that imports the any file, encode an event with a wrapped message, and watch the URL come out as plain text with the payload riding in the bytes field as a base64 blob in JSON. Decode a stream that carries an Any and see the object form, not the @type form, and remember which direction each library speaks. The raw decode tab at proto.datajutsu.com reads the type name off the wire before the schema gets a vote, which is the fastest way to see whether a packed Any is carrying what it claims. The sister posts on the other two well-known time types are the Timestamp post and the Duration post, and the official JSON form all three are judged against is in the JSON mapping post.

Subscribe to Data Jutsu

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe