validating protobuf messages

3,106 views
Skip to first unread message

Jeroen Ooms

unread,
Nov 16, 2012, 7:08:16 PM11/16/12
to prot...@googlegroups.com
I am using the RProtoBuf package, which interfaces to the c++ protobuf library. I was wondering if there is any way of 'validating' a pb message, in the sense that you can check if it actually is a valid message for a given pb description. Currently, RProtoBuf is very permissive and will basically accept anything. E.g the code below will not throw an error, even though the file obviously is not a valid tutorial.Person message:

library(RProtoBuf)
read(tutorial.Person, "/etc/passwd")

In practice, it is quite easy to introduce errors by accidentally reading a message using the wrong description. It would be very helpful if there was a way of preventing this kind of problems by some sort of validation.

Is there support for this in the c++ library, or does protobuf basically assume that the user/application has some other way of validating a message?

BJ Neilsen

unread,
Nov 18, 2012, 12:02:36 AM11/18/12
to prot...@googlegroups.com
Hi Jeroen,

It's been my experience that creating your own generator classes that hook directly into libprotoc has solved this exact issue. I've implemented a ruby generator for the protobuf ruby gem[1]. By hooking in and registering your generator you let protoc do all the work of parsing the .proto files and validating their structure. If everything checks out you're handed nice descriptor objects that you can use to generate your own language-specific definitions. The gem used to have a custom racc parser but I moved away from it because it had inconsistent parsing behavior from that of protoc. Now that we're hooking into libprotoc we get all the parse validation you normally get.

If it's any help you can check out my generator[2][3] class. I've also linked to a sample file that was created by the generator[4]. Let me know if that helps answer your question, or if it raises more. :)

Good luck,
BJ Neilsen

alex

unread,
Nov 19, 2012, 9:51:17 AM11/19/12
to prot...@googlegroups.com
What's the definition of "Validate"? pb support optional fields, extensions , that means you can ignore unknown fields.
If some fields are necessary why not just set to required.  

Annu Bansal

unread,
Dec 10, 2017, 1:35:35 PM12/10/17
to Protocol Buffers
Hey Jeroen!
Dis you get any method to check if a .pb file is generated from givem .proto file? Actually, I'm getting error while deserializing a .pb file, "The protocol message contained invalid tag(zero)." I was wondering if the two files are compatible. 
Thanks in advance. 

Marc Gravell

unread,
Dec 10, 2017, 1:38:55 PM12/10/17
to Annu Bansal, Protocol Buffers
a zero tag is never valid in any protobuf data, although it wouldn't be unheard of for folks to use a zero tag as a sentinel value to demark multiple root messages. Protoc has some facilities to check the insides of a message that might help you figure out how likely it is to be a match, but it won't make a zero tag work.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+unsubscribe@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages