Skip to content
Draft
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
216 changes: 216 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[codz]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py.cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
# Pipfile.lock

# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# uv.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
# poetry.lock
# poetry.toml

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
# pdm.lock
# pdm.toml
.pdm-python
.pdm-build/

# pixi
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
# pixi.lock
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
# in the .venv directory. It is recommended not to include this directory in version control.
.pixi

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# Redis
*.rdb
*.aof
*.pid

# RabbitMQ
mnesia/
rabbitmq/
rabbitmq-data/

# ActiveMQ
activemq-data/

# SageMath parsed files
*.sage.py

# Environments
.env
.envrc
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
# .idea/

# Abstra
# Abstra is an AI-powered process automation framework.
# Ignore directories containing user credentials, local state, and settings.
# Learn more at https://abstra.io/docs
.abstra/

# Visual Studio Code
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
# and can be added to the global gitignore or merged into this file. However, if you prefer,
# you could uncomment the following to ignore the entire vscode folder
# .vscode/

# Ruff stuff:
.ruff_cache/

# PyPI configuration file
.pypirc

# Marimo
marimo/_static/
marimo/_lsp/
__marimo__/

# Streamlit
.streamlit/secrets.toml
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all : fuzzer-html fuzzer-email fuzzer-httpclient fuzzer-json fuzzer-difflib fuzzer-csv fuzzer-decode fuzzer-ast fuzzer-tarfile fuzzer-zipfile fuzzer-re fuzzer-configparser fuzzer-tomllib fuzzer-plistlib fuzzer-xml
all : fuzzer-html fuzzer-email fuzzer-httpclient fuzzer-json fuzzer-difflib fuzzer-csv fuzzer-decode fuzzer-ast fuzzer-tarfile fuzzer-tarfile-hypothesis fuzzer-zipfile fuzzer-re fuzzer-configparser fuzzer-tomllib fuzzer-plistlib fuzzer-xml

PYTHON_CONFIG_PATH=$(CPYTHON_INSTALL_PATH)/bin/python3-config
CXXFLAGS += $(shell $(PYTHON_CONFIG_PATH) --cflags)
Expand Down Expand Up @@ -26,6 +26,8 @@ fuzzer-zipfile:
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"zipfile.py\"" -ldl $(LDFLAGS) -o fuzzer-zipfile
fuzzer-tarfile:
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"tarfile.py\"" -ldl $(LDFLAGS) -o fuzzer-tarfile
fuzzer-tarfile-hypothesis:
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"tarfile_hypothesis.py\"" -ldl $(LDFLAGS) -o fuzzer-tarfile-hypothesis
fuzzer-configparser:
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"configparser.py\"" -ldl $(LDFLAGS) -o fuzzer-configparser
fuzzer-tomllib:
Expand Down
Empty file.
71 changes: 71 additions & 0 deletions hypothesis_strategies/fspaths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright 2017 Christoph Reiter
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import os
import sys

from vendored.hypothesis.strategies import (
composite,
sampled_from,
lists,
integers,
binary,
randoms,
)


@composite
def fspaths(draw, max_size: int | None = None) -> str:
"""A hypothesis strategy which gives valid path values.

Valid path values are everything which when passed to open() will not raise
ValueError or TypeError (but might raise OSError due to file system or
operating system restrictions).
"""

if os.name == "nt":
hight_surrogate = integers(min_value=0xD800, max_value=0xDBFF).map(
lambda i: chr(i)
)
low_surrogate = integers(min_value=0xDC00, max_value=0xDFFF).map(
lambda i: chr(i)
)
uni_char = integers(min_value=1, max_value=sys.maxunicode).map(lambda i: chr(i))
any_char = sampled_from(
[draw(uni_char), draw(hight_surrogate), draw(low_surrogate)]
)
any_text = lists(any_char, max_size=max_size).map(lambda l: "".join(l))
path_text = any_text
else:
unix_path_bytes = binary(max_size=max_size).map(lambda b: b.replace(b"\x00", b" "))
path_text = unix_path_bytes.map(
lambda b: b.decode(sys.getfilesystemencoding(), "surrogateescape")
)
r = draw(randoms())

def shuffle_text(t):
l = list(t)
r.shuffle(l)
return "".join(l)

path_text = path_text.map(shuffle_text)

return draw(path_text)
71 changes: 71 additions & 0 deletions hypothesis_strategies/tar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import io
import tarfile

import vendored.hypothesis.strategies as st
from .fspaths import fspaths


def tar_integers(
format: int, digits: int = 1, allow_negative: bool = False
) -> st.SearchStrategy[int]:
"""tar has a unique way of encoding integers that is format-dependent
and based on the number of "digits" allowed for a value.
"""
if digits <= 0:
raise ValueError("Digits must be greater than one.")
if format == tarfile.GNU_FORMAT:
min_value = -(256 ** (digits - 1)) if allow_negative else 0
max_value = (256 ** (digits - 1)) - 1
else:
min_value = 0
max_value = (4**digits) - 1
return st.integers(min_value=min_value, max_value=max_value)


@st.composite
def tar_archives(draw):
buf = io.BytesIO()
format = draw(
st.sampled_from((tarfile.GNU_FORMAT, tarfile.PAX_FORMAT, tarfile.USTAR_FORMAT))
)
tar = tarfile.TarFile(fileobj=buf, format=format, mode="w")
types = list(tarfile.REGULAR_TYPES)

for _ in range(draw(st.integers(min_value=1, max_value=10))):
info = tarfile.TarInfo(
name=draw(fspaths(max_size=tarfile.LENGTH_NAME))
)
fileobj = None

info.type = draw(st.sampled_from(types))
info.mode = draw(tar_integers(format=format, digits=8))
info.uid = draw(tar_integers(format=format, digits=8))
info.gid = draw(tar_integers(format=format, digits=8))
info.mtime = draw(tar_integers(format=format, digits=12))
info.devmajor = draw(tar_integers(format=format, digits=8))
info.devminor = draw(tar_integers(format=format, digits=8))

if draw(st.booleans()):
info.linkname = draw(fspaths(max_size=tarfile.LENGTH_LINK))

def maybe_set_pax_header(obj, name, value):
if draw(st.booleans()):
obj.pax_headers[name] = value

if format == tarfile.PAX_FORMAT:
maybe_set_pax_header(info, "uname", draw(st.text(max_size=32)))
maybe_set_pax_header(info, "gname", draw(st.text(max_size=32)))
maybe_set_pax_header(
info,
"path",
draw(fspaths(max_size=tarfile.LENGTH_NAME)),
)
maybe_set_pax_header(
info,
"linkpath",
draw(fspaths(max_size=tarfile.LENGTH_LINK)),
)

tar.addfile(info, fileobj=fileobj)

return buf.getvalue()
15 changes: 15 additions & 0 deletions tarfile_hypothesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import io
import tarfile

from vendored.hypothesis import given
from hypothesis_strategies import tar



@given(tar.tar_archives())
def tar_archive_fuzz_target(tar_archive: bytes) -> None:
tarfile.TarFile(fileobj=io.BytesIO(tar_archive))


# Exposes the Hypothesis fuzz target for integrating with OSS-Fuzz.
FuzzerRunOne = tar_archive_fuzz_target.hypothesis.fuzz_one_input
Empty file added vendored/__init__.py
Empty file.
Loading