Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Ensure that calls to json.dumps are compatible with the standard library json. #7836

Merged
merged 9 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/7836.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Switch from simplejson to the standard library json.
2 changes: 1 addition & 1 deletion synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from typing import List

import jsonschema
from canonicaljson import json
from jsonschema import FormatChecker

from twisted.internet import defer
Expand Down
3 changes: 1 addition & 2 deletions synapse/app/admin_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import json
import logging
import os
import sys
import tempfile

from canonicaljson import json

from twisted.internet import defer, task

import synapse
Expand Down
3 changes: 1 addition & 2 deletions synapse/federation/sender/transaction_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
from typing import TYPE_CHECKING, List

from canonicaljson import json

from synapse.api.errors import HttpResponseException
from synapse.events import EventBase
from synapse.federation.persistence import TransactionActions
Expand Down
4 changes: 1 addition & 3 deletions synapse/handlers/devicemessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import logging
from typing import Any, Dict

from canonicaljson import json

from synapse.api.errors import SynapseError
from synapse.logging.context import run_in_background
from synapse.logging.opentracing import (
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/e2e_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import logging

import attr
from canonicaljson import encode_canonical_json, json
from canonicaljson import encode_canonical_json
from signedjson.key import decode_verify_key_bytes
from signedjson.sign import SignatureVerifyException, verify_signed_json
from unpaddedbase64 import decode_base64
Expand Down
3 changes: 1 addition & 2 deletions synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
# limitations under the License.

"""Utilities for interacting with Identity Servers"""

import json
import logging
import urllib.parse

from canonicaljson import json
from signedjson.key import decode_verify_key_bytes
from signedjson.sign import verify_signed_json
from unpaddedbase64 import decode_base64
Expand Down
3 changes: 2 additions & 1 deletion synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
from typing import TYPE_CHECKING, Optional, Tuple

from canonicaljson import encode_canonical_json, json
from canonicaljson import encode_canonical_json

from twisted.internet import defer
from twisted.internet.defer import succeed
Expand Down
5 changes: 2 additions & 3 deletions synapse/handlers/ui_auth/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging

from canonicaljson import json

from twisted.internet import defer
from twisted.web.client import PartialDownloadError

Expand Down Expand Up @@ -117,7 +116,7 @@ def check_auth(self, authdict, clientip):
except PartialDownloadError as pde:
# Twisted is silly
data = pde.response
resp_body = json.loads(data)
resp_body = json.loads(data.decode("utf-8"))

if "success" in resp_body:
# Note that we do NOT check the hostname here: we explicitly
Expand Down
3 changes: 2 additions & 1 deletion synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import abc
import collections
import html
import json
import logging
import types
import urllib
Expand All @@ -25,7 +26,7 @@
from typing import Any, Callable, Dict, Tuple, Union

import jinja2
from canonicaljson import encode_canonical_json, encode_pretty_printed_json, json
from canonicaljson import encode_canonical_json, encode_pretty_printed_json

from twisted.internet import defer
from twisted.python import failure
Expand Down
2 changes: 1 addition & 1 deletion synapse/logging/opentracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ def set_fates(clotho, lachesis, atropos, father="Zues", mother="Themis"):
"""
import contextlib
import inspect
import json
import logging
import re
from functools import wraps
from typing import TYPE_CHECKING, Dict, Optional, Type

import attr
from canonicaljson import json

from twisted.internet import defer

Expand Down
15 changes: 7 additions & 8 deletions synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
# limitations under the License.

""" This module contains REST servlets to do with rooms: /rooms/<paths> """
import json
import logging
import re
from typing import List, Optional
from urllib import parse as urlparse

from canonicaljson import json

from synapse.api.constants import EventTypes, Membership
from synapse.api.errors import (
AuthError,
Expand Down Expand Up @@ -515,9 +514,9 @@ async def on_GET(self, request, room_id):
requester = await self.auth.get_user_by_req(request, allow_guest=True)
pagination_config = PaginationConfig.from_request(request, default_limit=10)
as_client_event = b"raw" not in request.args
filter_bytes = parse_string(request, b"filter", encoding=None)
if filter_bytes:
filter_json = urlparse.unquote(filter_bytes.decode("UTF-8"))
filter_str = parse_string(request, b"filter", encoding="utf-8")
if filter_str:
filter_json = urlparse.unquote(filter_str)
event_filter = Filter(json.loads(filter_json)) # type: Optional[Filter]
if (
event_filter
Expand Down Expand Up @@ -627,9 +626,9 @@ async def on_GET(self, request, room_id, event_id):
limit = parse_integer(request, "limit", default=10)

# picking the API shape for symmetry with /messages
filter_bytes = parse_string(request, "filter")
if filter_bytes:
filter_json = urlparse.unquote(filter_bytes)
filter_str = parse_string(request, b"filter", encoding="utf-8")
if filter_str:
filter_json = urlparse.unquote(filter_str)
event_filter = Filter(json.loads(filter_json)) # type: Optional[Filter]
else:
event_filter = None
Expand Down
3 changes: 1 addition & 2 deletions synapse/rest/client/v2_alpha/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
# limitations under the License.

import itertools
import json
import logging

from canonicaljson import json

from synapse.api.constants import PresenceState
from synapse.api.errors import Codes, StoreError, SynapseError
from synapse.api.filtering import DEFAULT_FILTER_COLLECTION, FilterCollection
Expand Down
8 changes: 5 additions & 3 deletions synapse/rest/key/v2/remote_key_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import logging
from typing import Dict, Set

from canonicaljson import encode_canonical_json, json
from canonicaljson import encode_canonical_json
from signedjson.sign import sign_json

from synapse.api.errors import Codes, SynapseError
Expand Down Expand Up @@ -202,9 +202,11 @@ async def query_keys(self, request, query, query_remote_on_cache_miss=False):

if miss:
cache_misses.setdefault(server_name, set()).add(key_id)
# Cast to bytes since postgresql returns a memoryview.
json_results.add(bytes(most_recent_result["key_json"]))
else:
for ts_added, result in results:
# Cast to bytes since postgresql returns a memoryview.
json_results.add(bytes(result["key_json"]))

if cache_misses and query_remote_on_cache_miss:
Expand All @@ -213,7 +215,7 @@ async def query_keys(self, request, query, query_remote_on_cache_miss=False):
else:
signed_keys = []
for key_json in json_results:
key_json = json.loads(key_json)
key_json = json.loads(key_json.decode("utf-8"))
Copy link
Member Author

@clokep clokep Jul 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be using db_to_json instead (and the byte() calls above could be removed)? Feels really weird to put that into the REST layer though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, arguably get_server_keys_json should be manipulating the results so as not to return memoryview objects.

I think this is fine though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect it is doing it for efficiency since only some of the results actually end up being deserialized into JSON objects? Not sure though.

for signing_key in self.config.key_server_signing_keys:
key_json = sign_json(key_json, self.config.server_name, signing_key)

Expand Down
3 changes: 1 addition & 2 deletions synapse/rest/media/v1/preview_url_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import errno
import fnmatch
import itertools
import json
import logging
import os
import re
Expand All @@ -26,8 +27,6 @@
from typing import Dict, Optional
from urllib import parse as urlparse

from canonicaljson import json

from twisted.internet import defer
from twisted.internet.error import DNSLookupError

Expand Down
2 changes: 1 addition & 1 deletion synapse/util/frozenutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json

from canonicaljson import json
from frozendict import frozendict


Expand Down
4 changes: 1 addition & 3 deletions tests/storage/test_redaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json

from mock import Mock

from canonicaljson import json

from twisted.internet import defer

from synapse.api.constants import EventTypes, Membership
Expand Down
3 changes: 1 addition & 2 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
import hashlib
import hmac
import inspect
import json
import logging
import time
from typing import Optional, Tuple, Type, TypeVar, Union

from mock import Mock

from canonicaljson import json

from twisted.internet.defer import Deferred, ensureDeferred, succeed
from twisted.python.threadpool import ThreadPool
from twisted.trial import unittest
Expand Down