Skip to content

Commit bd6df6d

Browse files
committed
tests: add search_chunks fuzzing with atheris
oss-fuzz recommends project maintainers to place their fuzzing harness into their project repository. This is an optimized fuzzing harness to test search_chunks by creating an mmap'ed File from the bytes received by the fuzzer and submitting it to search_chunks. Other fuzzing harness can be added to the fuzzing directory later on and they will be executed by oss-fuzz.
1 parent f3be256 commit bd6df6d

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

fuzzing/search_chunks_fuzzer.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python3
2+
import logging
3+
import sys
4+
from pathlib import Path
5+
6+
import atheris
7+
import structlog
8+
9+
10+
def set_unblob_log_level(level=logging.CRITICAL):
11+
logger = logging.getLogger("unblob")
12+
13+
def logger_factory():
14+
return logger
15+
16+
structlog.configure(logger_factory=logger_factory)
17+
logger.setLevel(level)
18+
19+
20+
def extract(inpath: Path, outpath: Path): # noqa: ARG001
21+
return
22+
23+
24+
with atheris.instrument_imports(include=["unblob"], exclude=["unblob_native"]):
25+
from unblob.extractors.command import Command
26+
from unblob.file_utils import File
27+
from unblob.finder import search_chunks
28+
from unblob.models import Task, TaskResult
29+
from unblob.processing import ExtractionConfig
30+
31+
# NOTE: monkey patch Command extractor so we don't loose time executing subprocesses
32+
Command.extract = classmethod(extract)
33+
34+
35+
@atheris.instrument_func
36+
def test_search_chunks(data):
37+
config = ExtractionConfig(
38+
extract_root=Path("/dev/shm"), # noqa: S108
39+
force_extract=True,
40+
entropy_depth=0,
41+
entropy_plot=False,
42+
skip_magic=[],
43+
skip_extension=[],
44+
skip_extraction=False,
45+
process_num=1,
46+
keep_extracted_chunks=True,
47+
verbose=0,
48+
)
49+
50+
if not len(data):
51+
return
52+
53+
with File.from_bytes(data) as file:
54+
task = Task(
55+
path=Path("/dev/shm/nonexistent"), depth=0, blob_id="" # noqa: S108
56+
)
57+
result = TaskResult(task)
58+
search_chunks(file, len(data), config.handlers, result)
59+
60+
61+
set_unblob_log_level()
62+
atheris.Setup(sys.argv, test_search_chunks)
63+
atheris.Fuzz()

poetry.lock

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pre-commit = "^3.5.0"
4141
pytest-cov = "^3.0.0"
4242
ruff = "^0.1.13"
4343
pyyaml = "^6.0.1"
44+
atheris = { version = "^2.3.0", python = "<3.12" }
4445

4546
[tool.poetry.group.docs]
4647
optional = true

0 commit comments

Comments
 (0)