Skip to content

Commit 143712e

Browse files
joydeep049terriko
andauthored
feat: fuzz testing for JavaParser (#3514)
Signed-off-by: Joydeep <[email protected]> Co-authored-by: Terri Oda <[email protected]>
1 parent f41fc46 commit 143712e

File tree

3 files changed

+406
-0
lines changed

3 files changed

+406
-0
lines changed

fuzz/fuzz_pom_xml.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Copyright (C) 2023 Intel Corporation
2+
# SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
import sys
5+
import tempfile
6+
from pathlib import Path
7+
8+
import atheris
9+
import atheris_libprotobuf_mutator
10+
import generated.pom_xml_pb2 as pom_xml_pb2
11+
from google.protobuf.json_format import MessageToDict
12+
13+
from cve_bin_tool.cvedb import CVEDB
14+
from cve_bin_tool.log import LOGGER
15+
16+
parent_dir = str(Path(__file__).parent.parent)
17+
if parent_dir not in sys.path:
18+
sys.path.append(parent_dir)
19+
20+
21+
with atheris.instrument_imports():
22+
from cve_bin_tool.parsers.java import JavaParser
23+
24+
cve_db = CVEDB()
25+
logger = LOGGER.getChild("Fuzz")
26+
27+
28+
def PomXmlBuilder(data):
29+
json_data = MessageToDict(
30+
data, preserving_proto_field_name=True, including_default_value_fields=True
31+
)
32+
33+
with open(file_path, "w") as f:
34+
xml_namespace = json_data.get("xml_namespace", "")
35+
xml_schema_instance = json_data.get("xml_schema_instance", "")
36+
xml_namespace_uri1 = json_data.get("xml_namespace_uri1", "")
37+
xml_namespace_uri2 = json_data.get("xml_namespace_uri2", "")
38+
model_version = json_data.get("model_version", "")
39+
packaging = json_data.get("packaging", "")
40+
group_id = json_data.get("group_Id", "")
41+
artifactid = json_data.get("artifactId", "")
42+
name = json_data.get("name", "")
43+
url = json_data.get("url", "")
44+
version = json_data.get("version", "")
45+
46+
f.write(f'<project xmlns="{xml_namespace}"\n')
47+
f.write(f'xmlns:xsi="{xml_schema_instance}"\n')
48+
f.write(f'xsi:schemaLocation="{xml_namespace_uri1} {xml_namespace_uri2}">\n')
49+
f.write(f"<modelVersion>{model_version}</modelVersion>\n")
50+
f.write(f"<groupId>{group_id}</groupId>\n")
51+
f.write(f"<artifactId>{artifactid}</artifactId>\n")
52+
f.write(f"<version>{version}</version>\n")
53+
f.write(f"<packaging>{packaging}</packaging>\n")
54+
f.write(f"<name>{name}</name>\n")
55+
f.write(f"<url>{url}</url>\n")
56+
57+
f.write("<dependencies>\n")
58+
dependencies = json_data.get("dependencies", [])
59+
for dependency in dependencies:
60+
f.write("<dependency>\n")
61+
group_id = dependency.get("group_Id", "")
62+
artifactid = dependency.get("artifactId", "")
63+
version = dependency.get("version", "")
64+
scope = dependency.get("scope", "")
65+
f.write(f"<groupId>{group_id}</groupId>\n")
66+
f.write(f"<artifactId>{artifactid}</artifactId>\n")
67+
f.write(f"<version>{version}</version>\n")
68+
f.write(f"<scope>{scope}</scope>\n")
69+
f.write("</dependency>\n")
70+
f.write("</dependencies>\n")
71+
72+
f.write("<build>\n")
73+
f.write("<plugins>\n")
74+
plugins = json_data.get("plugins", [])
75+
for plugin in plugins:
76+
f.write("<plugin>\n")
77+
group_id = plugin.get("group_Id", "")
78+
artifactid = plugin.get("artifactId", "")
79+
version = plugin.get("version", "")
80+
f.write(f"<groupId>{group_id}</groupId>\n")
81+
f.write(f"<artifactId>{artifactid}</artifactId>\n")
82+
f.write(f"<version>{version}</version>\n")
83+
f.write("</plugin>\n")
84+
f.write("</build>\n")
85+
f.write("</plugins>\n")
86+
87+
f.write("</project>\n")
88+
89+
90+
def TestParseData(data):
91+
try:
92+
PomXmlBuilder(data)
93+
94+
java_parser = JavaParser(cve_db, logger)
95+
java_parser.run_checker(file_path)
96+
97+
except SystemExit:
98+
return
99+
100+
101+
file_path = str(Path(tempfile.mkdtemp(prefix="cve-bin-tool-")) / "pom.xml")
102+
103+
atheris_libprotobuf_mutator.Setup(
104+
sys.argv, TestParseData, proto=pom_xml_pb2.PomXmlProject
105+
)
106+
atheris.Fuzz()

fuzz/generated/pom_xml_pb2.py

Lines changed: 267 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)