Skip to content

Commit 5d1bad7

Browse files
committed
Add fuzz target for WebSocket parser.
1 parent a26cec2 commit 5d1bad7

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

fuzzing/fuzz_websocket_parser.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
3+
import atheris
4+
5+
6+
with atheris.instrument_imports():
7+
from websockets.exceptions import PayloadTooBig, ProtocolError
8+
from websockets.frames import Frame
9+
from websockets.streams import StreamReader
10+
11+
12+
def test_one_input(data):
13+
fdp = atheris.FuzzedDataProvider(data)
14+
mask = fdp.ConsumeBool()
15+
max_size_enabled = fdp.ConsumeBool()
16+
max_size = fdp.ConsumeInt(4)
17+
payload = fdp.ConsumeBytes(atheris.ALL_REMAINING)
18+
19+
reader = StreamReader()
20+
reader.feed_data(payload)
21+
reader.feed_eof()
22+
23+
parser = Frame.parse(
24+
reader.read_exact,
25+
mask=mask,
26+
max_size=max_size if max_size_enabled else None,
27+
)
28+
29+
try:
30+
next(parser)
31+
except StopIteration:
32+
pass # response is available in exc.value
33+
except (
34+
PayloadTooBig, # frame's payload size exceeds ``max_size``
35+
ProtocolError, # frame contains incorrect values
36+
):
37+
pass
38+
39+
40+
def main():
41+
atheris.Setup(sys.argv, test_one_input)
42+
atheris.Fuzz()
43+
44+
45+
if __name__ == "__main__":
46+
main()

0 commit comments

Comments
 (0)