Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzz: json_fuzz_test for RapidJSON and Protobuf parsing. #8658

Merged
merged 5 commits into from Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/common/json/BUILD
Expand Up @@ -2,6 +2,7 @@ licenses(["notice"]) # Apache 2

load(
"//bazel:envoy_build_system.bzl",
"envoy_cc_fuzz_test",
"envoy_cc_test",
"envoy_package",
)
Expand All @@ -23,6 +24,18 @@ envoy_cc_test(
],
)

envoy_cc_fuzz_test(
name = "json_fuzz_test",
srcs = ["json_fuzz_test.cc"],
corpus = "json_corpus",
deps = [
"//source/common/common:assert_lib",
"//source/common/json:json_loader_lib",
"//source/common/protobuf",
"//source/common/protobuf:utility_lib",
],
)

envoy_cc_test(
name = "json_loader_test",
srcs = ["json_loader_test.cc"],
Expand Down
6 changes: 6 additions & 0 deletions test/common/json/json_corpus/basic_descriptors.json

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

2 changes: 2 additions & 0 deletions test/common/json/json_corpus/basic_double.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/basic_double_null.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/basic_empty.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/basic_empty_braces.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/basic_empty_inner.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/basic_failure.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/basic_hello_bool.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/basic_hello_int.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/basic_nested_int_list.json

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

3 changes: 3 additions & 0 deletions test/common/json/json_corpus/basic_unterminated.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/double_some_values.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/int_max_min.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/int_too_high.json

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

1 change: 1 addition & 0 deletions test/common/json/json_corpus/int_too_low.json

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

6 changes: 6 additions & 0 deletions test/common/json/json_corpus/missing_enclosing_document.json

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

189 changes: 189 additions & 0 deletions test/common/json/json_corpus/some_complex_example.json

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

67 changes: 67 additions & 0 deletions test/common/json/json_fuzz_test.cc
@@ -0,0 +1,67 @@
#include "common/common/assert.h"
#include "common/json/json_loader.h"
#include "common/protobuf/protobuf.h"
#include "common/protobuf/utility.h"

#include "test/fuzz/fuzz_runner.h"

// Strong assertion that applies across all compilation modes and doesn't rely
// on gtest, which only provides soft fails that don't trip oss-fuzz failures.
#define FUZZ_ASSERT(x) RELEASE_ASSERT(x, "")
htuch marked this conversation as resolved.
Show resolved Hide resolved

namespace Envoy {
namespace Fuzz {

// We have multiple third party JSON parsers in Envoy, both RapidJSON and Protobuf.
// Fuzz both RapidJSON and Protobuf JSON loading from a test corpus derived from
// json_loader_test.cc. Ideally we would be doing differential fuzzing and be
// able to compare outputs, e.g. success/failure, recursive traversal of the
// structured objects for equivalence checking. However, even on basic files
// with non-printable ASCII, there are some difference, so we'll need to think a
// bit more about the modulo operator we want to use. For now, at least we get
// crash fuzzing.
// TODO(htuch): maybe only compare structurally on success (but might need to deal
// with RapidJSON integer regression in JsonLoaderTest.Integer?). Maybe also
// do some character set scrubbing before differential comparison.
DEFINE_FUZZER(const uint8_t* buf, size_t len) {
std::string json_string{reinterpret_cast<const char*>(buf), len};

// Load via RapidJSON if we can.
Json::ObjectSharedPtr object;
bool rapid_json_ok = false;
try {
object = Json::Factory::loadFromString(json_string);
rapid_json_ok = true;
} catch (const Envoy::Json::Exception&) {
}
// Validate we can re-serialize outside of the exception block, as this should
// never fail.
if (rapid_json_ok) {
object->asJsonString();
}

// Load via Protobuf JSON parsing if we can
ProtobufWkt::Struct message;
bool protobuf_ok = false;
try {
MessageUtil::loadFromJson(json_string, message);
protobuf_ok = true;
} catch (const Envoy::EnvoyException&) {
}
// Validate we can re-serialize outside of the exception block, as this should
// never fail.
if (protobuf_ok) {
MessageUtil::getJsonStringFromMessage(message);
}
if (rapid_json_ok != protobuf_ok) {
ENVOY_LOG_MISC(debug, "Inconsistent success for JSON parsers: RapidJSON {}, Protobuf {}",
rapid_json_ok, protobuf_ok);
}
// It would be nice to compare the output, but the two parsers have different
// behaviors around edge cases, e.g. weird non-printable ASCII sequences. As a
// result, we leave to future work a true differential fuzzer.
// FUZZ_ASSERT(rapid_json_ok == protobuf_ok);
}

} // namespace Fuzz
} // namespace Envoy