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

feat: fuzz testing for GoModParser #3434

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions fuzz/fuzz_go.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: GPL-3.0-or-later

import sys
import tempfile
from pathlib import Path

import atheris
import atheris_libprotobuf_mutator
from google.protobuf.json_format import MessageToDict

import fuzz.generated.go_mod_pb2 as go_mod_pb2
from cve_bin_tool.cvedb import CVEDB
from cve_bin_tool.log import LOGGER

with atheris.instrument_imports():
from cve_bin_tool.parsers.go import GoParser

cve_db = CVEDB()
logger = LOGGER.getChild("Fuzz")


def GoModBuilder(data):
json_data = MessageToDict(
data, preserving_proto_field_name=True, including_default_value_fields=True
)

with open(file_path, "w") as f:
module_name = json_data.get("module_name", "")
go_version = json_data.get("go_version", "")

f.write(f"module {module_name}\n")
f.write(f"go {go_version}\n")

f.write("require (\n")
for dependency in json_data.get("require", []):
module_name = dependency.get("module_name", "")
version = dependency.get("version", "")
f.write(f"{module_name} {version}\n")
f.write(")\n")

f.write("replace (\n")
for replacement in json_data.get("replace", []):
old_module = replacement.get("old_module", "")
old_version = replacement.get("old_version", "")
new_module = replacement.get("new_module", "")
new_version = replacement.get("new_version", "")
f.write(f"{old_module} {old_version} => {new_module} {new_version}\n")
f.write(")\n")

f.write("exclude (\n")
for exclusion in json_data.get("exclude", []):
module_name = exclusion.get("module_name", "")
version = exclusion.get("version", "")
f.write(f"{module_name} {version}\n")
f.write(")\n")


def TestParseData(data):
try:
GoModBuilder(data)

go_parser = GoParser(cve_db, logger)
go_parser.run_checker(file_path)

except SystemExit:
return


file_path = str(Path(tempfile.mkdtemp(prefix="cve-bin-tool-")) / "go.mod")

atheris_libprotobuf_mutator.Setup(sys.argv, TestParseData, proto=go_mod_pb2.GoModFile)
atheris.Fuzz()
32 changes: 32 additions & 0 deletions fuzz/generated/go_mod_pb2.py

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

27 changes: 27 additions & 0 deletions fuzz/proto_files/go_mod.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: GPL-3.0-or-later

syntax = "proto3";

message GoModFile {
message ModuleDependency {
string module_name = 1;
float version = 2;
}
message ModuleReplacement {
string old_module = 1;
float old_version = 2;
string new_module = 3;
float new_version = 4;
}
message ModuleExclude {
string module_name = 1;
float version = 2;
}
string module_name = 1;
float go_version = 2;
repeated ModuleDependency require = 3;
repeated ModuleExclude exclude = 4;
repeated ModuleReplacement replace = 5;

}