summary refs log tree commit diff
path: root/synapse/util
diff options
context:
space:
mode:
authorPatrick Cloke <clokep@users.noreply.github.com>2020-08-19 07:26:03 -0400
committerGitHub <noreply@github.com>2020-08-19 07:26:03 -0400
commiteebf52be060876ff14bbcbbc86b64ff9965b3622 (patch)
treea5c2e61430ff05dbd04e954a234b697acd6b9698 /synapse/util
parentConvert runWithConnection to async. (#8121) (diff)
downloadsynapse-eebf52be060876ff14bbcbbc86b64ff9965b3622.tar.xz
Be stricter about JSON that is accepted by Synapse (#8106)
Diffstat (limited to 'synapse/util')
-rw-r--r--synapse/util/__init__.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py
index b3f76428b6..b2a22dbd5c 100644
--- a/synapse/util/__init__.py
+++ b/synapse/util/__init__.py
@@ -25,8 +25,18 @@ from synapse.logging import context
 
 logger = logging.getLogger(__name__)
 
-# Create a custom encoder to reduce the whitespace produced by JSON encoding.
-json_encoder = json.JSONEncoder(separators=(",", ":"))
+
+def _reject_invalid_json(val):
+    """Do not allow Infinity, -Infinity, or NaN values in JSON."""
+    raise json.JSONDecodeError("Invalid JSON value: '%s'" % val)
+
+
+# Create a custom encoder to reduce the whitespace produced by JSON encoding and
+# ensure that valid JSON is produced.
+json_encoder = json.JSONEncoder(allow_nan=False, separators=(",", ":"))
+
+# Create a custom decoder to reject Python extensions to JSON.
+json_decoder = json.JSONDecoder(parse_constant=_reject_invalid_json)
 
 
 def unwrapFirstError(failure):