Skip to content

Commit

Permalink
[fuzz] fix json fuzzer (protobuf parsing error) (#15459)
Browse files Browse the repository at this point in the history
Signed-off-by: Asra Ali <[email protected]>
  • Loading branch information
asraa authored Mar 19, 2021
1 parent 9a89e31 commit 2b8a744
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions test/common/json/json_corpus/protobuf_invalid_recursion

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions test/common/json/json_fuzz_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ DEFINE_FUZZER(const uint8_t* buf, size_t len) {
MessageUtil::loadFromJson(json_string, message);
// We should be able to serialize, parse again and get the same result.
ProtobufWkt::Struct message2;
MessageUtil::loadFromJson(MessageUtil::getJsonStringFromMessageOrDie(message), message2);
FUZZ_ASSERT(TestUtility::protoEqual(message, message2));
// This can sometimes fail on too deep recursion in case protobuf parsing is configured to have
// less recursion depth than json parsing in the proto library.
// This is the only version of MessageUtil::getJsonStringFromMessage function safe to use on
// untrusted inputs.
std::string deserialized = MessageUtil::getJsonStringFromMessageOrError(message);
if (!absl::StartsWith(deserialized, "Failed to convert")) {
MessageUtil::loadFromJson(deserialized, message2);
FUZZ_ASSERT(TestUtility::protoEqual(message, message2));
}

// MessageUtil::getYamlStringFromMessage automatically convert types, so we have to do another
// round-trip.
Expand Down

0 comments on commit 2b8a744

Please sign in to comment.