-
Notifications
You must be signed in to change notification settings - Fork 930
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Pieter Marsman <[email protected]>
- Loading branch information
1 parent
16cb34c
commit ff359dc
Showing
36 changed files
with
444 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: CIFuzz | ||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
permissions: {} | ||
jobs: | ||
Fuzzing: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
security-events: write | ||
steps: | ||
- name: Build Fuzzers | ||
id: build | ||
uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master | ||
with: | ||
oss-fuzz-project-name: 'pdfminersix' | ||
language: python | ||
- name: Run Fuzzers | ||
uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master | ||
with: | ||
oss-fuzz-project-name: 'pdfminersix' | ||
language: python | ||
fuzz-seconds: 800 | ||
output-sarif: true | ||
- name: Upload Crash | ||
uses: actions/upload-artifact@v3 | ||
if: failure() && steps.build.outcome == 'success' | ||
with: | ||
name: artifacts | ||
path: ./out/artifacts | ||
- name: Upload Sarif | ||
if: always() && steps.build.outcome == 'success' | ||
uses: github/codeql-action/upload-sarif@v2 | ||
with: | ||
# Path to SARIF file relative to the root of the repository | ||
sarif_file: cifuzz-sarif/results.sarif | ||
checkout_path: cifuzz-sarif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
cd "$SRC"/pdfminer.six | ||
pip3 install .[dev] | ||
|
||
# Build fuzzers in $OUT | ||
for fuzzer in $(find fuzzing -name '*_fuzzer.py');do | ||
compile_python_fuzzer "$fuzzer" --collect-all charset_normalizer --hidden-import=_cffi_backend | ||
base_name=$(basename "$fuzzer") | ||
base_name_no_ext=${base_name%.*} | ||
zip -q $OUT/"$base_name_no_ext".zip $SRC/corpus/* | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sys | ||
|
||
import atheris | ||
|
||
from fuzzing.fuzzed_data_provider import PdfminerFuzzedDataProvider | ||
|
||
with atheris.instrument_imports(): | ||
from fuzzing.utils import ( | ||
prepare_pdfminer_fuzzing, | ||
is_valid_byte_stream, | ||
generate_layout_parameters, | ||
) | ||
from pdfminer.high_level import extract_text | ||
|
||
from pdfminer.psexceptions import PSException | ||
|
||
|
||
def fuzz_one_input(data: bytes) -> None: | ||
if not is_valid_byte_stream(data): | ||
# Not worth continuing with this test case | ||
return | ||
|
||
fdp = PdfminerFuzzedDataProvider(data) | ||
|
||
try: | ||
extract_text( | ||
fdp.ConsumeMemoryFile(), | ||
maxpages=fdp.ConsumeIntInRange(0, 10), | ||
page_numbers=fdp.ConsumeOptionalIntList(10, 0, 10), | ||
laparams=generate_layout_parameters(fdp), | ||
) | ||
except (AssertionError, PSException): | ||
return | ||
|
||
|
||
if __name__ == "__main__": | ||
prepare_pdfminer_fuzzing() | ||
atheris.Setup(sys.argv, fuzz_one_input) | ||
atheris.Fuzz() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import io | ||
import sys | ||
|
||
import atheris | ||
|
||
from fuzzing.fuzzed_data_provider import PdfminerFuzzedDataProvider | ||
|
||
with atheris.instrument_imports(): | ||
from fuzzing.utils import ( | ||
prepare_pdfminer_fuzzing, | ||
is_valid_byte_stream, | ||
generate_layout_parameters, | ||
) | ||
from pdfminer.high_level import extract_text_to_fp | ||
from pdfminer.psexceptions import PSException | ||
|
||
available_output_formats = ["text", "html", "xml", "tag"] | ||
available_layout_modes = ["exact", "normal", "loose"] | ||
|
||
|
||
def fuzz_one_input(data: bytes) -> None: | ||
if not is_valid_byte_stream(data): | ||
# Not worth continuing with this test case | ||
return | ||
|
||
fdp = PdfminerFuzzedDataProvider(data) | ||
|
||
try: | ||
with fdp.ConsumeMemoryFile(all_data=False) as f_in, io.BytesIO() as f_out: | ||
extract_text_to_fp( | ||
f_in, | ||
f_out, | ||
output_type=fdp.PickValueInList(available_output_formats), | ||
laparams=generate_layout_parameters(fdp), | ||
maxpages=fdp.ConsumeIntInRange(0, 10), | ||
page_numbers=fdp.ConsumeOptionalIntList(10, 0, 10), | ||
scale=fdp.ConsumeFloatInRange(0.0, 2.0), | ||
rotation=fdp.ConsumeIntInRange(0, 360), | ||
layoutmode=fdp.PickValueInList(available_layout_modes), | ||
strip_control=fdp.ConsumeBool(), | ||
) | ||
except (AssertionError, PSException): | ||
return | ||
|
||
|
||
if __name__ == "__main__": | ||
prepare_pdfminer_fuzzing() | ||
atheris.Setup(sys.argv, fuzz_one_input) | ||
atheris.Fuzz() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import io | ||
from typing import List, Optional | ||
|
||
from atheris import FuzzedDataProvider | ||
|
||
|
||
class PdfminerFuzzedDataProvider(FuzzedDataProvider): # type: ignore[misc] | ||
def ConsumeRandomBytes(self) -> bytes: | ||
int_range = self.ConsumeIntInRange(0, self.remaining_bytes()) | ||
return bytes(self.ConsumeBytes(int_range)) | ||
|
||
def ConsumeRandomString(self) -> str: | ||
int_range = self.ConsumeIntInRange(0, self.remaining_bytes()) | ||
return str(self.ConsumeUnicodeNoSurrogates(int_range)) | ||
|
||
def ConsumeRemainingString(self) -> str: | ||
return str(self.ConsumeUnicodeNoSurrogates(self.remaining_bytes())) | ||
|
||
def ConsumeRemainingBytes(self) -> bytes: | ||
return bytes(self.ConsumeBytes(self.remaining_bytes())) | ||
|
||
def ConsumeMemoryFile(self, all_data: bool = False) -> io.BytesIO: | ||
if all_data: | ||
return io.BytesIO(self.ConsumeRemainingBytes()) | ||
else: | ||
return io.BytesIO(self.ConsumeRandomBytes()) | ||
|
||
def ConsumeOptionalIntList( | ||
self, max_count: int, min: int, max: int | ||
) -> Optional[List[int]]: | ||
if self.ConsumeBool(): | ||
count = self.ConsumeIntInRange(0, max_count) | ||
return [int(i) for i in self.ConsumeIntListInRange(count, min, max)] | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
import atheris | ||
import sys | ||
|
||
from fuzzing.fuzzed_data_provider import PdfminerFuzzedDataProvider | ||
|
||
with atheris.instrument_imports(): | ||
from fuzzing.utils import ( | ||
prepare_pdfminer_fuzzing, | ||
is_valid_byte_stream, | ||
generate_layout_parameters, | ||
) | ||
from pdfminer.high_level import extract_pages | ||
from pdfminer.psexceptions import PSException | ||
|
||
|
||
def fuzz_one_input(data: bytes) -> None: | ||
if not is_valid_byte_stream(data): | ||
# Not worth continuing with this test case | ||
return | ||
|
||
fdp = PdfminerFuzzedDataProvider(data) | ||
|
||
try: | ||
with fdp.ConsumeMemoryFile() as f: | ||
list( | ||
extract_pages( | ||
f, | ||
maxpages=fdp.ConsumeIntInRange(0, 10), | ||
page_numbers=fdp.ConsumeOptionalIntList(10, 0, 10), | ||
laparams=generate_layout_parameters(fdp), | ||
) | ||
) | ||
except (AssertionError, PSException): | ||
return | ||
|
||
|
||
if __name__ == "__main__": | ||
prepare_pdfminer_fuzzing() | ||
atheris.Setup(sys.argv, fuzz_one_input) | ||
atheris.Fuzz() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Utilities shared across the various PDF fuzzing harnesses | ||
""" | ||
import logging | ||
from typing import Optional | ||
|
||
import atheris | ||
|
||
from pdfminer.layout import LAParams | ||
|
||
PDF_MAGIC_BYTES = b"%PDF-" | ||
|
||
|
||
def prepare_pdfminer_fuzzing() -> None: | ||
""" | ||
Used to disable logging of the pdfminer module | ||
""" | ||
logging.getLogger("pdfminer").setLevel(logging.CRITICAL) | ||
|
||
|
||
@atheris.instrument_func # type: ignore[misc] | ||
def generate_layout_parameters( | ||
fdp: atheris.FuzzedDataProvider, | ||
) -> Optional[LAParams]: | ||
if fdp.ConsumeBool(): | ||
return None | ||
|
||
boxes_flow: Optional[float] = None | ||
if fdp.ConsumeBool(): | ||
boxes_flow = fdp.ConsumeFloatInRange(-1.0, 1.0) | ||
|
||
return LAParams( | ||
line_overlap=fdp.ConsumeFloat(), | ||
char_margin=fdp.ConsumeFloat(), | ||
line_margin=fdp.ConsumeFloat(), | ||
word_margin=fdp.ConsumeFloat(), | ||
boxes_flow=boxes_flow, | ||
detect_vertical=fdp.ConsumeBool(), | ||
all_texts=fdp.ConsumeBool(), | ||
) | ||
|
||
|
||
@atheris.instrument_func # type: ignore[misc] | ||
def is_valid_byte_stream(data: bytes) -> bool: | ||
"""Quick check to see if this is worth of passing to atheris | ||
:return: Whether the byte-stream passes the basic checks | ||
""" | ||
if not data.startswith(PDF_MAGIC_BYTES): | ||
return False | ||
if b"/Root" not in data: | ||
return False | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.