Skip to content

Commit

Permalink
Configure the JSON decoder for safer parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Apr 13, 2021
1 parent 845fc62 commit dd1db2e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion engineio/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from base64 import b64encode
from json import JSONDecodeError
from engineio.json import JSONDecodeError
import logging
try:
import queue
Expand Down
16 changes: 16 additions & 0 deletions engineio/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""JSON-compatible module with sane defaults."""

from json import * # noqa: F401, F403
from json import loads as original_loads


def _safe_int(s):
if len(s) > 100:
raise ValueError('Integer is too large')
return int(s)


def loads(*args, **kwargs):
if 'parse_int' not in kwargs:
kwargs['parse_int'] = _safe_int
return original_loads(*args, **kwargs)
2 changes: 1 addition & 1 deletion engineio/packet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import base64
import json as _json
from engineio import json as _json

(OPEN, CLOSE, PING, PONG, MESSAGE, UPGRADE, NOOP) = (0, 1, 2, 3, 4, 5, 6)
packet_names = ['OPEN', 'CLOSE', 'PING', 'PONG', 'MESSAGE', 'UPGRADE', 'NOOP']
Expand Down

0 comments on commit dd1db2e

Please sign in to comment.