Debian Codegraph @

tests/fuzz_idna_codec.py

raw
1 #!/usr/bin/env python3
2 """OSS-Fuzz target for the ``idna2008`` codec.
3
4 Exercises the one-shot codec and, more importantly, the incremental
5 encoder/decoder: the input is fed in fuzzer-chosen chunk sizes and the
6 concatenated result must equal the one-shot result (or both must raise
7 :class:`idna.IDNAError`). The buffering logic in
8 :mod:`idna.codec` is stateful across calls, which is exactly the kind of
9 code a fuzzer is good at breaking.
10
11 OSS-Fuzz builds every ``fuzz_*.py`` it finds in the checkout, so this file
12 needs no registration there. To run it locally::
13
14 pip install atheris .
15 python tests/fuzz_idna_codec.py -max_total_time=60
16
17 Any libFuzzer flag is accepted; a crash writes a ``crash-*`` file which can be
18 passed back as an argument to reproduce. ``tests/test_idna_fuzz_targets.py``
19 smoke-tests this harness in the ordinary test suite without atheris.
20 """
21
22 import codecs
23 import sys
24
25 import atheris # ty: ignore[unresolved-import]
26
27 with atheris.instrument_imports():
28 import idna
29 import idna.codec # registers the "idna2008" codec
30
31 MAX_INPUT = 1100 # just past idna's 1024-character input cap
32 MAX_CHUNKS = 8
33
34
35 def _outcome(fn, *args):
36 try:
37 return fn(*args), None
38 except idna.IDNAError as err:
39 return None, err
40
41
42 def _chunks(fdp, data):
43 cuts = sorted(fdp.ConsumeIntInRange(0, len(data)) for _ in range(fdp.ConsumeIntInRange(0, MAX_CHUNKS)))
44 points = [0, *cuts, len(data)]
45 return [data[i:j] for i, j in zip(points, points[1:])]
46
47
48 def fuzz_encoder(fdp):
49 s = fdp.ConsumeUnicode(MAX_INPUT)
50 if not s:
51 return # the codec maps "" to b"" by design; core raises "Empty domain"
52 one_shot = _outcome(idna.encode, s)
53 assert _outcome(s.encode, "idna2008") == one_shot or one_shot[1] is not None
54
55 chunks = _chunks(fdp, s)
56 encoder = codecs.getincrementalencoder("idna2008")()
57
58 def incremental():
59 out = b"".join(encoder.encode(chunk) for chunk in chunks)
60 return out + encoder.encode("", final=True)
61
62 result = _outcome(incremental)
63 assert (result[1] is None) == (one_shot[1] is None), (s, chunks, one_shot, result)
64 assert result[0] == one_shot[0], (s, chunks, one_shot, result)
65
66
67 def fuzz_decoder(fdp):
68 b = fdp.ConsumeBytes(MAX_INPUT)
69 if not b:
70 return
71 one_shot = _outcome(idna.decode, b)
72 assert _outcome(b.decode, "idna2008") == one_shot or one_shot[1] is not None
73
74 chunks = _chunks(fdp, b)
75 decoder = codecs.getincrementaldecoder("idna2008")()
76
77 def incremental():
78 out = "".join(decoder.decode(chunk) for chunk in chunks)
79 return out + decoder.decode(b"", final=True)
80
81 result = _outcome(incremental)
82 assert (result[1] is None) == (one_shot[1] is None), (b, chunks, one_shot, result)
83 assert result[0] == one_shot[0], (b, chunks, one_shot, result)
84
85
86 def fuzz_stream(fdp):
87 # StreamWriter/StreamReader wrap the one-shot codec; make sure they only
88 # ever raise IDNAError.
89 import io
90
91 if fdp.ConsumeBool():
92 writer = codecs.getwriter("idna2008")(io.BytesIO())
93 _outcome(writer.write, fdp.ConsumeUnicode(MAX_INPUT))
94 else:
95 reader = codecs.getreader("idna2008")(io.BytesIO(fdp.ConsumeBytes(MAX_INPUT)))
96 _outcome(reader.read)
97
98
99 OPERATIONS = (fuzz_encoder, fuzz_decoder, fuzz_stream)
100
101
102 def TestOneInput(data):
103 fdp = atheris.FuzzedDataProvider(data)
104 OPERATIONS[fdp.ConsumeIntInRange(0, len(OPERATIONS) - 1)](fdp)
105
106
107 def main():
108 atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True)
109 atheris.Fuzz()
110
111
112 if __name__ == "__main__":
113 main()