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: tempfile cleanup for python-requirements parser #3966

Merged
merged 8 commits into from
Apr 3, 2024
37 changes: 27 additions & 10 deletions fuzz/fuzz_python_requirement_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# SPDX-License-Identifier: GPL-3.0-or-later

"""
This module contains fuzz testing for the PythonRequirementsParser's handling of requirements.txt files.
This module contains fuzz testing for the PythonRequirementsParser handling of requirements.txt files.
"""

import os
import shutil
import sys
import tempfile
from pathlib import Path

import atheris
import atheris_libprotobuf_mutator
Expand All @@ -24,15 +25,20 @@
logger = LOGGER.getChild("Fuzz")


def TestParseData(data):
def TestParseData(data, cve_db, logger, tmpdir):
"""
Fuzz test the PythonRequirementsParser's handling of requirements.txt files.
Fuzz test the PythonRequirementsParser handling of requirements.txt files.
Args:
data (protobuf message): The protobuf message to convert to a requirements.txt file.
cve_db: Object for the Database of CVE-BIN-TOOL.
logger: Logger object.
tmpdir: The temporary directory object.
"""
try:
json_data = MessageToDict(
data, preserving_proto_field_name=True, including_default_value_fields=True
)

file_path = os.path.join(tmpdir, "requirements.txt")
with open(file_path, "w") as f:
for dict in json_data.get("packages", []):
extras = ""
Expand All @@ -54,9 +60,20 @@ def TestParseData(data):
return


file_path = str(Path(tempfile.mkdtemp(prefix="cve-bin-tool-")) / "requirements.txt")
def main():
"""Main Function to Run Fuzzing and Facilitate Tempfile cleanup."""
tmpdir = tempfile.mkdtemp(prefix="cve-bin-tool-fuzz-python-requirements")
try:
atheris_libprotobuf_mutator.Setup(
sys.argv,
lambda data: TestParseData(data, cve_db, logger, tmpdir),
proto=python_requirements_pb2.PackageList,
)
atheris.Fuzz()
finally:
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)


atheris_libprotobuf_mutator.Setup(
sys.argv, TestParseData, proto=python_requirements_pb2.PackageList
)
atheris.Fuzz()
if __name__ == "__main__":
main()
Loading