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

Commit

Permalink
Ensure that calls to json.dumps are compatible with the standard li…
Browse files Browse the repository at this point in the history
…brary json. (#7836)
  • Loading branch information
clokep authored Jul 15, 2020
1 parent a57df9b commit 3545051
Showing 8 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog.d/7836.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure that calls to `json.dumps` are compatible with the standard library json.
4 changes: 3 additions & 1 deletion synapse/api/errors.py
Original file line number Diff line number Diff line change
@@ -15,12 +15,14 @@
# limitations under the License.

"""Contains exceptions and error codes."""
import json

import logging
import typing
from http import HTTPStatus
from typing import Dict, List, Optional, Union

from canonicaljson import json

from twisted.web import http

if typing.TYPE_CHECKING:
2 changes: 1 addition & 1 deletion synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
@@ -14,10 +14,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, Callable, Dict, List, Match, Optional, Tuple, Union

from canonicaljson import json
from prometheus_client import Counter, Histogram

from twisted.internet import defer
3 changes: 2 additions & 1 deletion synapse/handlers/ui_auth/checkers.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
# 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 logging

from canonicaljson import json
@@ -117,7 +118,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
4 changes: 2 additions & 2 deletions synapse/http/client.py
Original file line number Diff line number Diff line change
@@ -13,13 +13,13 @@
# 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 urllib
from io import BytesIO

import treq
from canonicaljson import encode_canonical_json
from canonicaljson import encode_canonical_json, json
from netaddr import IPAddress
from prometheus_client import Counter
from zope.interface import implementer, provider
4 changes: 3 additions & 1 deletion synapse/http/servlet.py
Original file line number Diff line number Diff line change
@@ -14,9 +14,11 @@
# limitations under the License.

""" This module contains base REST classes for constructing REST servlets. """
import json

import logging

from canonicaljson import json

from synapse.api.errors import Codes, SynapseError

logger = logging.getLogger(__name__)
13 changes: 7 additions & 6 deletions synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
# limitations under the License.

""" This module contains REST servlets to do with rooms: /rooms/<paths> """

import logging
import re
from typing import List, Optional
@@ -515,9 +516,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
@@ -627,9 +628,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
4 changes: 3 additions & 1 deletion synapse/rest/key/v2/remote_key_resource.py
Original file line number Diff line number Diff line change
@@ -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:
@@ -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"))
for signing_key in self.config.key_server_signing_keys:
key_json = sign_json(key_json, self.config.server_name, signing_key)

0 comments on commit 3545051

Please sign in to comment.