A Protobuf Descriptor Is a Message Too (and the Tool Now Tells When Your Bytes Are a Schema)

There is a class of payload that is its own schema. A protoc --descriptor_set_out dump is one. The answer from a gRPC server reflection service is another. A lone FileDescriptorProto, one file pulled out of a dump, is the third. None of them are the message bytes you were expecting to decode, and the reason is that a schema is not a special kind of file. It is a protobuf message. FileDescriptorSet is a message with one field, field 1, repeated FileDescriptorProto, so a dump of any .proto starts with tag 0x0a and carries its own file the way a box carries its own label. I ran a two field User message through protoc 25.3 with --descriptor_set_out and read the result back: one entry, name u.proto, package demo, one message, and the set is the message. When those bytes used to land in the decode tab of the tool, they came out as an opaque base64 blob. Now the tab checks first whether the bytes are a schema, and if so it shows the reconstructed .proto instead of the blob.

The check is three decodes in a fixed order, and the order exists because the shapes are not nested. A lone file is not a one entry set, even though it looks like one. I took a single file out of a real dump, re encoded it, and fed it to the FileDescriptorSet decoder. It threw, invalid wire type 7 at offset 7, because the file name string is not a tag and the decoder reads it as one. So the set probe has to fail first, the way it is built to fail. A reflection answer is a different message again, a ServerReflectionResponse, and its fields are emitted in id order, so a valid_host on field 1 leads the wire while the actual descriptor sits in field 5. The file_list_response field is a trap. It carries file names as plain strings with no descriptor, and it must not trigger, so the detector only accepts the response when field 5 holds real file entries. The order is set, then reflection, then file. Each probe decodes, and then asks the result the same shape question.

The shape question is where it gets honest. It asks whether the decoded thing is a file descriptor, and a file descriptor is a message with a string name in field 1. Now take any ordinary message with a string in field 1. It has the same tag. The bytes of a one field message holding the string hello are 0a05 followed by hello, and when the set decoder reads them, hello is not a tag either, and it throws invalid wire type 4 at offset 5. The detector catches the throw and moves on. Most ordinary data never fires, and it does not fire by luck, it fires by arithmetic. The data has to survive being re read as a descriptor, and most of it cannot. But some can, and the cost is measurable. A one field string message holding the value x.proto is byte identical to a FileDescriptorProto with only the name set. Same tag, same payload, one byte for one. The detector accepts it, because it cannot do better. If the string bytes decode as a name and a package, it fires with nothing in the bytes that says proto. And the everyday shape is the worst of all. A repeated message field whose values are file names decodes as a real multi file set, two entries, a.proto and b.proto, and the detector has to say yes, because the bytes really are a set. Just not the one you meant.

So the detector is a guess with a known error class, and the guess is: the bytes decode as file descriptors, and every entry has content, or at least a name that looks like a file. It fires on real dumps, real reflection answers, and real single files, and it fires on a string in field 1 that looks like a file name. When the rendered .proto looks wrong for your payload, the answer is in the raw decode tab, which reads the same bytes with no guess and shows you the tags as they are. The note under the output names which kind matched, the protoc descriptor set, the reflection answer, or the single file, and the .proto is copyable and downloadable as a plain text file. For the bytes that are genuinely a schema, that is the whole fix. You stop staring at base64 and start reading your own API.

What the reconstruction keeps and what it does not is worth knowing, because the descriptor is the boundary, the same boundary the rest of this series is about. It keeps types and numbers and packages and enums and oneofs and services, and it keeps the map marker, the repeated entry type flagged map_entry that the renderer turns back into a map line. It keeps proto2, with one wrinkle. A proto2 file has no syntax field in its descriptor, because proto2 is enum value zero, the default, and defaults are not written. So the renderer has to fall back and print the syntax line and the rule words itself. What it does not keep are comments, and it does not keep the well known type imports in full. Run protoc with --include_imports and the set lists google/protobuf/timestamp.proto as a first class entry, ordered before your own file, and the renderer omits those as a one line comment instead of re rendering the standard library.

That last point is the theme of the whole series in one line. Everything in this format is relative to a schema, and the schema is itself encoded with the same format, with itself. The descriptor is a message, the dump is a message about messages, and the reflection service is a message answering a message. The tool's job is to know which side of the door your bytes are on, to say the schema when they are the schema, and to hand you back a .proto you can paste into the converter and keep working with. If your payload ever came out of a dump, it was already a message. You had just not read it that way. See also the schemaless raw read post, which is what the raw decode tab is built from.

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