| 1 |
import sys |
| 2 |
from json import JSONDecodeError |
| 3 |
|
| 4 |
import atheris |
| 5 |
from utils import is_expected_error |
| 6 |
|
| 7 |
with atheris.instrument_imports(): |
| 8 |
import jsonpickle |
| 9 |
|
| 10 |
# Keys are filenames and values are lists of tuples. |
| 11 |
# Each tuple contains a substring of an expected exception message and a line number |
| 12 |
# (or -1 to match any line number). |
| 13 |
expected_errors = { |
| 14 |
"jsonpickle/unpickler.py": [ |
| 15 |
("object has no attribute 'split'", 283), |
| 16 |
("not enough values to unpack", 283), |
| 17 |
("object has no attribute 'split'", 197), |
| 18 |
("object has no attribute 'split'", 307), |
| 19 |
("object has no attribute 'encode'", 404), |
| 20 |
("object has no attribute 'encode'", 407), |
| 21 |
("object has no attribute 'encode'", 431), |
| 22 |
("object has no attribute 'encode'", 434), |
| 23 |
("object is not iterable", 448), |
| 24 |
("list indices must be integers or slices", 539), |
| 25 |
("object is not iterable", 448), |
| 26 |
("object is not iterable", 475), |
| 27 |
("object is not iterable", 481), |
| 28 |
("object is not iterable", 508), |
| 29 |
("object is not iterable", 841), |
| 30 |
("object is not iterable", 808), |
| 31 |
("object is not iterable", 859), |
| 32 |
("object is not iterable", 881), |
| 33 |
("object is not iterable", 892), |
| 34 |
("unhashable type", 407), |
| 35 |
("unhashable type", 841), |
| 36 |
("object argument after * must be an iterable", 493), |
| 37 |
("object argument after * must be an iterable", 520), |
| 38 |
("list index out of range", 160), |
| 39 |
("'utf-8' codec can't encode character", 407), |
| 40 |
("too many values to unpack", 283), |
| 41 |
("too many values to unpack", 511), |
| 42 |
("object is not callable", 493), |
| 43 |
("object is not callable", 520), |
| 44 |
], |
| 45 |
"base64.py": [ |
| 46 |
("bad base85 character at", -1), |
| 47 |
("base85 overflow in hunk", -1), |
| 48 |
("Invalid base64-encoded string", -1), |
| 49 |
("Incorrect padding", -1), |
| 50 |
], |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
def TestOneInput(data): |
| 55 |
fdp = atheris.FuzzedDataProvider(data) |
| 56 |
fuzz_string = fdp.ConsumeUnicodeNoSurrogates( |
| 57 |
fdp.ConsumeIntInRange(0, fdp.remaining_bytes()) |
| 58 |
) |
| 59 |
|
| 60 |
try: |
| 61 |
jsonpickle.unpickler.decode(fuzz_string) |
| 62 |
except (RecursionError, JSONDecodeError): |
| 63 |
return -1 |
| 64 |
except Exception as e: |
| 65 |
if is_expected_error(e, expected_errors): |
| 66 |
return 0 |
| 67 |
raise e |
| 68 |
|
| 69 |
|
| 70 |
def main(): |
| 71 |
atheris.Setup(sys.argv, TestOneInput) |
| 72 |
atheris.Fuzz() |
| 73 |
|
| 74 |
|
| 75 |
if __name__ == "__main__": |
| 76 |
main() |