| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -euo pipefail |
| 4 |
|
| 5 |
################# |
| 6 |
# Prerequisites # |
| 7 |
################# |
| 8 |
|
| 9 |
for cmd in python3 git wget rsync; do |
| 10 |
command -v "$cmd" >/dev/null 2>&1 || { |
| 11 |
printf '[%s] Required command %s not found, exiting.\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$cmd" >&2 |
| 12 |
exit 1 |
| 13 |
} |
| 14 |
done |
| 15 |
|
| 16 |
############# |
| 17 |
# Functions # |
| 18 |
############# |
| 19 |
|
| 20 |
download_and_concatenate_common_dictionaries() { |
| 21 |
# Assign the first argument as the target file where all contents will be concatenated |
| 22 |
local target_file="$1" |
| 23 |
|
| 24 |
# Shift the arguments so the first argument (target_file path) is removed |
| 25 |
# and only URLs are left for the loop below. |
| 26 |
shift |
| 27 |
|
| 28 |
for url in "$@"; do |
| 29 |
wget -qO- "$url" >>"$target_file" |
| 30 |
# Ensure there's a newline between each file's content |
| 31 |
echo >>"$target_file" |
| 32 |
done |
| 33 |
} |
| 34 |
|
| 35 |
prepare_dictionaries_for_fuzz_targets() { |
| 36 |
local dictionaries_dir="$1" |
| 37 |
local fuzz_targets_dir="$2" |
| 38 |
local common_base_dictionary_filename="$WORK/__base.dict" |
| 39 |
|
| 40 |
printf '[%s] Copying .dict files from %s to %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$dictionaries_dir" "$SRC/" |
| 41 |
cp -v "$dictionaries_dir"/*.dict "$SRC/" |
| 42 |
|
| 43 |
download_and_concatenate_common_dictionaries "$common_base_dictionary_filename" \ |
| 44 |
"https://raw.githubusercontent.com/google/fuzzing/master/dictionaries/json.dict" \ |
| 45 |
"https://raw.githubusercontent.com/google/fuzzing/master/dictionaries/jsonnet.dict" \ |
| 46 |
"https://raw.githubusercontent.com/google/fuzzing/master/dictionaries/utf8.dict" |
| 47 |
|
| 48 |
find "$fuzz_targets_dir" -name 'fuzz_*.py' -print0 | while IFS= read -r -d '' fuzz_harness; do |
| 49 |
if [[ -r "$common_base_dictionary_filename" ]]; then |
| 50 |
# Strip the `.py` extension from the filename and replace it with `.dict`. |
| 51 |
fuzz_harness_dictionary_filename="$(basename "$fuzz_harness" .py).dict" |
| 52 |
local output_file="$SRC/$fuzz_harness_dictionary_filename" |
| 53 |
|
| 54 |
printf '[%s] Appending %s to %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$common_base_dictionary_filename" "$output_file" |
| 55 |
if [[ -s "$output_file" ]]; then |
| 56 |
# If a dictionary file for this fuzzer already exists and is not empty, |
| 57 |
# we append a new line to the end of it before appending any new entries. |
| 58 |
# |
| 59 |
# LibFuzzer will happily ignore multiple empty lines in a dictionary but fail with an error |
| 60 |
# if any single line has incorrect syntax (e.g., if we accidentally add two entries to the same line.) |
| 61 |
# See docs for valid syntax: https://llvm.org/docs/LibFuzzer.html#id32 |
| 62 |
echo >>"$output_file" |
| 63 |
fi |
| 64 |
cat "$common_base_dictionary_filename" >>"$output_file" |
| 65 |
fi |
| 66 |
done |
| 67 |
} |
| 68 |
|
| 69 |
######################## |
| 70 |
# Main execution logic # |
| 71 |
######################## |
| 72 |
|
| 73 |
prepare_dictionaries_for_fuzz_targets "$SRC/jsonpickle/fuzzing/dictionaries" "$SRC/jsonpickle/fuzzing" |
| 74 |
|
| 75 |
# The OSS-Fuzz base image has outdated dependencies by default so we upgrade them below. |
| 76 |
python3 -m pip install --upgrade pip |
| 77 |
# Upgrade to the latest versions known to work at the time the below changes were introduced: |
| 78 |
python3 -m pip install 'atheris~=2.3.0' 'setuptools~=73.0' 'pyinstaller~=6.0' typing_extensions |