diff options
author | Patrick Cloke <clokep@users.noreply.github.com> | 2020-08-19 07:26:03 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-19 07:26:03 -0400 |
commit | eebf52be060876ff14bbcbbc86b64ff9965b3622 (patch) | |
tree | a5c2e61430ff05dbd04e954a234b697acd6b9698 /synapse/storage | |
parent | Convert runWithConnection to async. (#8121) (diff) | |
download | synapse-eebf52be060876ff14bbcbbc86b64ff9965b3622.tar.xz |
Be stricter about JSON that is accepted by Synapse (#8106)
Diffstat (limited to 'synapse/storage')
-rw-r--r-- | synapse/storage/_base.py | 7 | ||||
-rw-r--r-- | synapse/storage/databases/main/events_worker.py | 16 |
2 files changed, 17 insertions, 6 deletions
diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py index 6814bf5fcf..ab49d227de 100644 --- a/synapse/storage/_base.py +++ b/synapse/storage/_base.py @@ -19,12 +19,11 @@ import random from abc import ABCMeta from typing import Any, Optional -from canonicaljson import json - from synapse.storage.database import LoggingTransaction # noqa: F401 from synapse.storage.database import make_in_list_sql_clause # noqa: F401 from synapse.storage.database import DatabasePool from synapse.types import Collection, get_domain_from_id +from synapse.util import json_decoder logger = logging.getLogger(__name__) @@ -99,13 +98,13 @@ def db_to_json(db_content): if isinstance(db_content, memoryview): db_content = db_content.tobytes() - # Decode it to a Unicode string before feeding it to json.loads, since + # Decode it to a Unicode string before feeding it to the JSON decoder, since # Python 3.5 does not support deserializing bytes. if isinstance(db_content, (bytes, bytearray)): db_content = db_content.decode("utf8") try: - return json.loads(db_content) + return json_decoder.decode(db_content) except Exception: logging.warning("Tried to decode '%r' as JSON and failed", db_content) raise diff --git a/synapse/storage/databases/main/events_worker.py b/synapse/storage/databases/main/events_worker.py index e3a154a527..4a3333c0db 100644 --- a/synapse/storage/databases/main/events_worker.py +++ b/synapse/storage/databases/main/events_worker.py @@ -596,8 +596,20 @@ class EventsWorkerStore(SQLBaseStore): if not allow_rejected and rejected_reason: continue - d = db_to_json(row["json"]) - internal_metadata = db_to_json(row["internal_metadata"]) + # If the event or metadata cannot be parsed, log the error and act + # as if the event is unknown. + try: + d = db_to_json(row["json"]) + except ValueError: + logger.error("Unable to parse json from event: %s", event_id) + continue + try: + internal_metadata = db_to_json(row["internal_metadata"]) + except ValueError: + logger.error( + "Unable to parse internal_metadata from event: %s", event_id + ) + continue format_version = row["format_version"] if format_version is None: |