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

pull/7868/head
Patrick Cloke 2020-07-15 13:40:54 -04:00 committed by GitHub
parent a57df9b827
commit 35450519de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 22 additions and 13 deletions

1
changelog.d/7836.misc Normal file
View File

@ -0,0 +1 @@
Ensure that calls to `json.dumps` are compatible with the standard library json.

View File

@ -15,12 +15,14 @@
# limitations under the License. # limitations under the License.
"""Contains exceptions and error codes.""" """Contains exceptions and error codes."""
import json
import logging import logging
import typing import typing
from http import HTTPStatus from http import HTTPStatus
from typing import Dict, List, Optional, Union from typing import Dict, List, Optional, Union
from canonicaljson import json
from twisted.web import http from twisted.web import http
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:

View File

@ -14,10 +14,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
import logging import logging
from typing import Any, Callable, Dict, List, Match, Optional, Tuple, Union from typing import Any, Callable, Dict, List, Match, Optional, Tuple, Union
from canonicaljson import json
from prometheus_client import Counter, Histogram from prometheus_client import Counter, Histogram
from twisted.internet import defer from twisted.internet import defer

View File

@ -12,6 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging import logging
from canonicaljson import json from canonicaljson import json
@ -117,7 +118,7 @@ class RecaptchaAuthChecker(UserInteractiveAuthChecker):
except PartialDownloadError as pde: except PartialDownloadError as pde:
# Twisted is silly # Twisted is silly
data = pde.response data = pde.response
resp_body = json.loads(data) resp_body = json.loads(data.decode("utf-8"))
if "success" in resp_body: if "success" in resp_body:
# Note that we do NOT check the hostname here: we explicitly # Note that we do NOT check the hostname here: we explicitly

View File

@ -13,13 +13,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
import logging import logging
import urllib import urllib
from io import BytesIO from io import BytesIO
import treq import treq
from canonicaljson import encode_canonical_json from canonicaljson import encode_canonical_json, json
from netaddr import IPAddress from netaddr import IPAddress
from prometheus_client import Counter from prometheus_client import Counter
from zope.interface import implementer, provider from zope.interface import implementer, provider

View File

@ -14,9 +14,11 @@
# limitations under the License. # limitations under the License.
""" This module contains base REST classes for constructing REST servlets. """ """ This module contains base REST classes for constructing REST servlets. """
import json
import logging import logging
from canonicaljson import json
from synapse.api.errors import Codes, SynapseError from synapse.api.errors import Codes, SynapseError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -15,6 +15,7 @@
# limitations under the License. # limitations under the License.
""" This module contains REST servlets to do with rooms: /rooms/<paths> """ """ This module contains REST servlets to do with rooms: /rooms/<paths> """
import logging import logging
import re import re
from typing import List, Optional from typing import List, Optional
@ -515,9 +516,9 @@ class RoomMessageListRestServlet(RestServlet):
requester = await self.auth.get_user_by_req(request, allow_guest=True) requester = await self.auth.get_user_by_req(request, allow_guest=True)
pagination_config = PaginationConfig.from_request(request, default_limit=10) pagination_config = PaginationConfig.from_request(request, default_limit=10)
as_client_event = b"raw" not in request.args as_client_event = b"raw" not in request.args
filter_bytes = parse_string(request, b"filter", encoding=None) filter_str = parse_string(request, b"filter", encoding="utf-8")
if filter_bytes: if filter_str:
filter_json = urlparse.unquote(filter_bytes.decode("UTF-8")) filter_json = urlparse.unquote(filter_str)
event_filter = Filter(json.loads(filter_json)) # type: Optional[Filter] event_filter = Filter(json.loads(filter_json)) # type: Optional[Filter]
if ( if (
event_filter event_filter
@ -627,9 +628,9 @@ class RoomEventContextServlet(RestServlet):
limit = parse_integer(request, "limit", default=10) limit = parse_integer(request, "limit", default=10)
# picking the API shape for symmetry with /messages # picking the API shape for symmetry with /messages
filter_bytes = parse_string(request, "filter") filter_str = parse_string(request, b"filter", encoding="utf-8")
if filter_bytes: if filter_str:
filter_json = urlparse.unquote(filter_bytes) filter_json = urlparse.unquote(filter_str)
event_filter = Filter(json.loads(filter_json)) # type: Optional[Filter] event_filter = Filter(json.loads(filter_json)) # type: Optional[Filter]
else: else:
event_filter = None event_filter = None

View File

@ -202,9 +202,11 @@ class RemoteKey(DirectServeJsonResource):
if miss: if miss:
cache_misses.setdefault(server_name, set()).add(key_id) 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"])) json_results.add(bytes(most_recent_result["key_json"]))
else: else:
for ts_added, result in results: for ts_added, result in results:
# Cast to bytes since postgresql returns a memoryview.
json_results.add(bytes(result["key_json"])) json_results.add(bytes(result["key_json"]))
if cache_misses and query_remote_on_cache_miss: if cache_misses and query_remote_on_cache_miss:
@ -213,7 +215,7 @@ class RemoteKey(DirectServeJsonResource):
else: else:
signed_keys = [] signed_keys = []
for key_json in json_results: 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: for signing_key in self.config.key_server_signing_keys:
key_json = sign_json(key_json, self.config.server_name, signing_key) key_json = sign_json(key_json, self.config.server_name, signing_key)