mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-09-06 23:11:58 +00:00
Correct type hints for parse_string(s)_from_args. (#10137)
This commit is contained in:
parent
7dc14730d9
commit
9e4610cc27
1
changelog.d/10137.misc
Normal file
1
changelog.d/10137.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Add `parse_strings_from_args` for parsing an array from query parameters.
|
1
mypy.ini
1
mypy.ini
@ -32,6 +32,7 @@ files =
|
|||||||
synapse/http/federation/matrix_federation_agent.py,
|
synapse/http/federation/matrix_federation_agent.py,
|
||||||
synapse/http/federation/well_known_resolver.py,
|
synapse/http/federation/well_known_resolver.py,
|
||||||
synapse/http/matrixfederationclient.py,
|
synapse/http/matrixfederationclient.py,
|
||||||
|
synapse/http/servlet.py,
|
||||||
synapse/http/server.py,
|
synapse/http/server.py,
|
||||||
synapse/http/site.py,
|
synapse/http/site.py,
|
||||||
synapse/logging,
|
synapse/logging,
|
||||||
|
@ -15,10 +15,12 @@
|
|||||||
""" This module contains base REST classes for constructing REST servlets. """
|
""" This module contains base REST classes for constructing REST servlets. """
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import Iterable, List, Optional, Union, overload
|
from typing import Dict, Iterable, List, Optional, overload
|
||||||
|
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
|
|
||||||
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import Codes, SynapseError
|
from synapse.api.errors import Codes, SynapseError
|
||||||
from synapse.util import json_decoder
|
from synapse.util import json_decoder
|
||||||
|
|
||||||
@ -108,13 +110,66 @@ def parse_boolean_from_args(args, name, default=None, required=False):
|
|||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def parse_bytes_from_args(
|
||||||
|
args: Dict[bytes, List[bytes]],
|
||||||
|
name: str,
|
||||||
|
default: Literal[None] = None,
|
||||||
|
required: Literal[True] = True,
|
||||||
|
) -> bytes:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def parse_bytes_from_args(
|
||||||
|
args: Dict[bytes, List[bytes]],
|
||||||
|
name: str,
|
||||||
|
default: Optional[bytes] = None,
|
||||||
|
required: bool = False,
|
||||||
|
) -> Optional[bytes]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def parse_bytes_from_args(
|
||||||
|
args: Dict[bytes, List[bytes]],
|
||||||
|
name: str,
|
||||||
|
default: Optional[bytes] = None,
|
||||||
|
required: bool = False,
|
||||||
|
) -> Optional[bytes]:
|
||||||
|
"""
|
||||||
|
Parse a string parameter as bytes from the request query string.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
args: A mapping of request args as bytes to a list of bytes (e.g. request.args).
|
||||||
|
name: the name of the query parameter.
|
||||||
|
default: value to use if the parameter is absent,
|
||||||
|
defaults to None. Must be bytes if encoding is None.
|
||||||
|
required: whether to raise a 400 SynapseError if the
|
||||||
|
parameter is absent, defaults to False.
|
||||||
|
Returns:
|
||||||
|
Bytes or the default value.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
SynapseError if the parameter is absent and required.
|
||||||
|
"""
|
||||||
|
name_bytes = name.encode("ascii")
|
||||||
|
|
||||||
|
if name_bytes in args:
|
||||||
|
return args[name_bytes][0]
|
||||||
|
elif required:
|
||||||
|
message = "Missing string query parameter %s" % (name,)
|
||||||
|
raise SynapseError(400, message, errcode=Codes.MISSING_PARAM)
|
||||||
|
|
||||||
|
return default
|
||||||
|
|
||||||
|
|
||||||
def parse_string(
|
def parse_string(
|
||||||
request,
|
request: Request,
|
||||||
name: Union[bytes, str],
|
name: str,
|
||||||
default: Optional[str] = None,
|
default: Optional[str] = None,
|
||||||
required: bool = False,
|
required: bool = False,
|
||||||
allowed_values: Optional[Iterable[str]] = None,
|
allowed_values: Optional[Iterable[str]] = None,
|
||||||
encoding: Optional[str] = "ascii",
|
encoding: str = "ascii",
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Parse a string parameter from the request query string.
|
Parse a string parameter from the request query string.
|
||||||
@ -125,66 +180,65 @@ def parse_string(
|
|||||||
Args:
|
Args:
|
||||||
request: the twisted HTTP request.
|
request: the twisted HTTP request.
|
||||||
name: the name of the query parameter.
|
name: the name of the query parameter.
|
||||||
default: value to use if the parameter is absent,
|
default: value to use if the parameter is absent, defaults to None.
|
||||||
defaults to None. Must be bytes if encoding is None.
|
|
||||||
required: whether to raise a 400 SynapseError if the
|
required: whether to raise a 400 SynapseError if the
|
||||||
parameter is absent, defaults to False.
|
parameter is absent, defaults to False.
|
||||||
allowed_values: List of allowed values for the
|
allowed_values: List of allowed values for the
|
||||||
string, or None if any value is allowed, defaults to None. Must be
|
string, or None if any value is allowed, defaults to None. Must be
|
||||||
the same type as name, if given.
|
the same type as name, if given.
|
||||||
encoding : The encoding to decode the string content with.
|
encoding: The encoding to decode the string content with.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A string value or the default. Unicode if encoding
|
A string value or the default.
|
||||||
was given, bytes otherwise.
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
SynapseError if the parameter is absent and required, or if the
|
SynapseError if the parameter is absent and required, or if the
|
||||||
parameter is present, must be one of a list of allowed values and
|
parameter is present, must be one of a list of allowed values and
|
||||||
is not one of those allowed values.
|
is not one of those allowed values.
|
||||||
"""
|
"""
|
||||||
|
args = request.args # type: Dict[bytes, List[bytes]] # type: ignore
|
||||||
return parse_string_from_args(
|
return parse_string_from_args(
|
||||||
request.args, name, default, required, allowed_values, encoding
|
args, name, default, required, allowed_values, encoding
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _parse_string_value(
|
def _parse_string_value(
|
||||||
value: Union[str, bytes],
|
value: bytes,
|
||||||
allowed_values: Optional[Iterable[str]],
|
allowed_values: Optional[Iterable[str]],
|
||||||
name: str,
|
name: str,
|
||||||
encoding: Optional[str],
|
encoding: str,
|
||||||
) -> Union[str, bytes]:
|
) -> str:
|
||||||
if encoding:
|
|
||||||
try:
|
try:
|
||||||
value = value.decode(encoding)
|
value_str = value.decode(encoding)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise SynapseError(400, "Query parameter %r must be %s" % (name, encoding))
|
raise SynapseError(400, "Query parameter %r must be %s" % (name, encoding))
|
||||||
|
|
||||||
if allowed_values is not None and value not in allowed_values:
|
if allowed_values is not None and value_str not in allowed_values:
|
||||||
message = "Query parameter %r must be one of [%s]" % (
|
message = "Query parameter %r must be one of [%s]" % (
|
||||||
name,
|
name,
|
||||||
", ".join(repr(v) for v in allowed_values),
|
", ".join(repr(v) for v in allowed_values),
|
||||||
)
|
)
|
||||||
raise SynapseError(400, message)
|
raise SynapseError(400, message)
|
||||||
else:
|
else:
|
||||||
return value
|
return value_str
|
||||||
|
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def parse_strings_from_args(
|
def parse_strings_from_args(
|
||||||
args: List[str],
|
args: Dict[bytes, List[bytes]],
|
||||||
name: Union[bytes, str],
|
name: str,
|
||||||
default: Optional[List[str]] = None,
|
default: Optional[List[str]] = None,
|
||||||
required: bool = False,
|
required: Literal[True] = True,
|
||||||
allowed_values: Optional[Iterable[str]] = None,
|
allowed_values: Optional[Iterable[str]] = None,
|
||||||
encoding: Literal[None] = None,
|
encoding: str = "ascii",
|
||||||
) -> Optional[List[bytes]]:
|
) -> List[str]:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def parse_strings_from_args(
|
def parse_strings_from_args(
|
||||||
args: List[str],
|
args: Dict[bytes, List[bytes]],
|
||||||
name: Union[bytes, str],
|
name: str,
|
||||||
default: Optional[List[str]] = None,
|
default: Optional[List[str]] = None,
|
||||||
required: bool = False,
|
required: bool = False,
|
||||||
allowed_values: Optional[Iterable[str]] = None,
|
allowed_values: Optional[Iterable[str]] = None,
|
||||||
@ -194,46 +248,40 @@ def parse_strings_from_args(
|
|||||||
|
|
||||||
|
|
||||||
def parse_strings_from_args(
|
def parse_strings_from_args(
|
||||||
args: List[str],
|
args: Dict[bytes, List[bytes]],
|
||||||
name: Union[bytes, str],
|
name: str,
|
||||||
default: Optional[List[str]] = None,
|
default: Optional[List[str]] = None,
|
||||||
required: bool = False,
|
required: bool = False,
|
||||||
allowed_values: Optional[Iterable[str]] = None,
|
allowed_values: Optional[Iterable[str]] = None,
|
||||||
encoding: Optional[str] = "ascii",
|
encoding: str = "ascii",
|
||||||
) -> Optional[List[Union[bytes, str]]]:
|
) -> Optional[List[str]]:
|
||||||
"""
|
"""
|
||||||
Parse a string parameter from the request query string list.
|
Parse a string parameter from the request query string list.
|
||||||
|
|
||||||
If encoding is not None, the content of the query param will be
|
The content of the query param will be decoded to Unicode using the encoding.
|
||||||
decoded to Unicode using the encoding, otherwise it will be encoded
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
args: the twisted HTTP request.args list.
|
args: A mapping of request args as bytes to a list of bytes (e.g. request.args).
|
||||||
name: the name of the query parameter.
|
name: the name of the query parameter.
|
||||||
default: value to use if the parameter is absent,
|
default: value to use if the parameter is absent, defaults to None.
|
||||||
defaults to None. Must be bytes if encoding is None.
|
required: whether to raise a 400 SynapseError if the
|
||||||
required : whether to raise a 400 SynapseError if the
|
|
||||||
parameter is absent, defaults to False.
|
parameter is absent, defaults to False.
|
||||||
allowed_values (list[bytes|unicode]): List of allowed values for the
|
allowed_values: List of allowed values for the
|
||||||
string, or None if any value is allowed, defaults to None. Must be
|
string, or None if any value is allowed, defaults to None.
|
||||||
the same type as name, if given.
|
|
||||||
encoding: The encoding to decode the string content with.
|
encoding: The encoding to decode the string content with.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A string value or the default. Unicode if encoding
|
A string value or the default.
|
||||||
was given, bytes otherwise.
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
SynapseError if the parameter is absent and required, or if the
|
SynapseError if the parameter is absent and required, or if the
|
||||||
parameter is present, must be one of a list of allowed values and
|
parameter is present, must be one of a list of allowed values and
|
||||||
is not one of those allowed values.
|
is not one of those allowed values.
|
||||||
"""
|
"""
|
||||||
|
name_bytes = name.encode("ascii")
|
||||||
|
|
||||||
if not isinstance(name, bytes):
|
if name_bytes in args:
|
||||||
name = name.encode("ascii")
|
values = args[name_bytes]
|
||||||
|
|
||||||
if name in args:
|
|
||||||
values = args[name]
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
_parse_string_value(value, allowed_values, name=name, encoding=encoding)
|
_parse_string_value(value, allowed_values, name=name, encoding=encoding)
|
||||||
@ -241,36 +289,30 @@ def parse_strings_from_args(
|
|||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
if required:
|
if required:
|
||||||
message = "Missing string query parameter %r" % (name)
|
message = "Missing string query parameter %r" % (name,)
|
||||||
raise SynapseError(400, message, errcode=Codes.MISSING_PARAM)
|
raise SynapseError(400, message, errcode=Codes.MISSING_PARAM)
|
||||||
else:
|
|
||||||
|
|
||||||
if encoding and isinstance(default, bytes):
|
|
||||||
return default.decode(encoding)
|
|
||||||
|
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
def parse_string_from_args(
|
def parse_string_from_args(
|
||||||
args: List[str],
|
args: Dict[bytes, List[bytes]],
|
||||||
name: Union[bytes, str],
|
name: str,
|
||||||
default: Optional[str] = None,
|
default: Optional[str] = None,
|
||||||
required: bool = False,
|
required: bool = False,
|
||||||
allowed_values: Optional[Iterable[str]] = None,
|
allowed_values: Optional[Iterable[str]] = None,
|
||||||
encoding: Optional[str] = "ascii",
|
encoding: str = "ascii",
|
||||||
) -> Optional[Union[bytes, str]]:
|
) -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
Parse the string parameter from the request query string list
|
Parse the string parameter from the request query string list
|
||||||
and return the first result.
|
and return the first result.
|
||||||
|
|
||||||
If encoding is not None, the content of the query param will be
|
The content of the query param will be decoded to Unicode using the encoding.
|
||||||
decoded to Unicode using the encoding, otherwise it will be encoded
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
args: the twisted HTTP request.args list.
|
args: A mapping of request args as bytes to a list of bytes (e.g. request.args).
|
||||||
name: the name of the query parameter.
|
name: the name of the query parameter.
|
||||||
default: value to use if the parameter is absent,
|
default: value to use if the parameter is absent, defaults to None.
|
||||||
defaults to None. Must be bytes if encoding is None.
|
|
||||||
required: whether to raise a 400 SynapseError if the
|
required: whether to raise a 400 SynapseError if the
|
||||||
parameter is absent, defaults to False.
|
parameter is absent, defaults to False.
|
||||||
allowed_values: List of allowed values for the
|
allowed_values: List of allowed values for the
|
||||||
@ -279,8 +321,7 @@ def parse_string_from_args(
|
|||||||
encoding: The encoding to decode the string content with.
|
encoding: The encoding to decode the string content with.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A string value or the default. Unicode if encoding
|
A string value or the default.
|
||||||
was given, bytes otherwise.
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
SynapseError if the parameter is absent and required, or if the
|
SynapseError if the parameter is absent and required, or if the
|
||||||
@ -291,12 +332,15 @@ def parse_string_from_args(
|
|||||||
strings = parse_strings_from_args(
|
strings = parse_strings_from_args(
|
||||||
args,
|
args,
|
||||||
name,
|
name,
|
||||||
default=[default],
|
default=[default] if default is not None else None,
|
||||||
required=required,
|
required=required,
|
||||||
allowed_values=allowed_values,
|
allowed_values=allowed_values,
|
||||||
encoding=encoding,
|
encoding=encoding,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if strings is None:
|
||||||
|
return None
|
||||||
|
|
||||||
return strings[0]
|
return strings[0]
|
||||||
|
|
||||||
|
|
||||||
@ -388,9 +432,8 @@ class RestServlet:
|
|||||||
|
|
||||||
def register(self, http_server):
|
def register(self, http_server):
|
||||||
""" Register this servlet with the given HTTP server. """
|
""" Register this servlet with the given HTTP server. """
|
||||||
if hasattr(self, "PATTERNS"):
|
patterns = getattr(self, "PATTERNS", None)
|
||||||
patterns = self.PATTERNS
|
if patterns:
|
||||||
|
|
||||||
for method in ("GET", "PUT", "POST", "DELETE"):
|
for method in ("GET", "PUT", "POST", "DELETE"):
|
||||||
if hasattr(self, "on_%s" % (method,)):
|
if hasattr(self, "on_%s" % (method,)):
|
||||||
servlet_classname = self.__class__.__name__
|
servlet_classname = self.__class__.__name__
|
||||||
|
@ -649,7 +649,7 @@ 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_str = parse_string(request, b"filter", encoding="utf-8")
|
filter_str = parse_string(request, "filter", encoding="utf-8")
|
||||||
if filter_str:
|
if filter_str:
|
||||||
filter_json = urlparse.unquote(filter_str)
|
filter_json = urlparse.unquote(filter_str)
|
||||||
event_filter = Filter(
|
event_filter = Filter(
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from typing import TYPE_CHECKING, Awaitable, Callable, Dict, Optional
|
from typing import TYPE_CHECKING, Awaitable, Callable, Dict, List, Optional
|
||||||
|
|
||||||
from synapse.api.errors import Codes, LoginError, SynapseError
|
from synapse.api.errors import Codes, LoginError, SynapseError
|
||||||
from synapse.api.ratelimiting import Ratelimiter
|
from synapse.api.ratelimiting import Ratelimiter
|
||||||
@ -25,6 +25,7 @@ from synapse.http import get_request_uri
|
|||||||
from synapse.http.server import HttpServer, finish_request
|
from synapse.http.server import HttpServer, finish_request
|
||||||
from synapse.http.servlet import (
|
from synapse.http.servlet import (
|
||||||
RestServlet,
|
RestServlet,
|
||||||
|
parse_bytes_from_args,
|
||||||
parse_json_object_from_request,
|
parse_json_object_from_request,
|
||||||
parse_string,
|
parse_string,
|
||||||
)
|
)
|
||||||
@ -437,9 +438,8 @@ class SsoRedirectServlet(RestServlet):
|
|||||||
finish_request(request)
|
finish_request(request)
|
||||||
return
|
return
|
||||||
|
|
||||||
client_redirect_url = parse_string(
|
args = request.args # type: Dict[bytes, List[bytes]] # type: ignore
|
||||||
request, "redirectUrl", required=True, encoding=None
|
client_redirect_url = parse_bytes_from_args(args, "redirectUrl", required=True)
|
||||||
)
|
|
||||||
sso_url = await self._sso_handler.handle_redirect_request(
|
sso_url = await self._sso_handler.handle_redirect_request(
|
||||||
request,
|
request,
|
||||||
client_redirect_url,
|
client_redirect_url,
|
||||||
|
@ -537,7 +537,7 @@ class RoomMessageListRestServlet(RestServlet):
|
|||||||
self.store, request, default_limit=10
|
self.store, request, default_limit=10
|
||||||
)
|
)
|
||||||
as_client_event = b"raw" not in request.args
|
as_client_event = b"raw" not in request.args
|
||||||
filter_str = parse_string(request, b"filter", encoding="utf-8")
|
filter_str = parse_string(request, "filter", encoding="utf-8")
|
||||||
if filter_str:
|
if filter_str:
|
||||||
filter_json = urlparse.unquote(filter_str)
|
filter_json = urlparse.unquote(filter_str)
|
||||||
event_filter = Filter(
|
event_filter = Filter(
|
||||||
@ -652,7 +652,7 @@ 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_str = parse_string(request, b"filter", encoding="utf-8")
|
filter_str = parse_string(request, "filter", encoding="utf-8")
|
||||||
if filter_str:
|
if filter_str:
|
||||||
filter_json = urlparse.unquote(filter_str)
|
filter_json = urlparse.unquote(filter_str)
|
||||||
event_filter = Filter(
|
event_filter = Filter(
|
||||||
|
@ -17,6 +17,7 @@ import logging
|
|||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from os import path
|
from os import path
|
||||||
|
from typing import Dict, List
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
from jinja2 import TemplateNotFound
|
from jinja2 import TemplateNotFound
|
||||||
@ -24,7 +25,7 @@ from jinja2 import TemplateNotFound
|
|||||||
from synapse.api.errors import NotFoundError, StoreError, SynapseError
|
from synapse.api.errors import NotFoundError, StoreError, SynapseError
|
||||||
from synapse.config import ConfigError
|
from synapse.config import ConfigError
|
||||||
from synapse.http.server import DirectServeHtmlResource, respond_with_html
|
from synapse.http.server import DirectServeHtmlResource, respond_with_html
|
||||||
from synapse.http.servlet import parse_string
|
from synapse.http.servlet import parse_bytes_from_args, parse_string
|
||||||
from synapse.types import UserID
|
from synapse.types import UserID
|
||||||
|
|
||||||
# language to use for the templates. TODO: figure this out from Accept-Language
|
# language to use for the templates. TODO: figure this out from Accept-Language
|
||||||
@ -116,7 +117,8 @@ class ConsentResource(DirectServeHtmlResource):
|
|||||||
has_consented = False
|
has_consented = False
|
||||||
public_version = username == ""
|
public_version = username == ""
|
||||||
if not public_version:
|
if not public_version:
|
||||||
userhmac_bytes = parse_string(request, "h", required=True, encoding=None)
|
args = request.args # type: Dict[bytes, List[bytes]]
|
||||||
|
userhmac_bytes = parse_bytes_from_args(args, "h", required=True)
|
||||||
|
|
||||||
self._check_hash(username, userhmac_bytes)
|
self._check_hash(username, userhmac_bytes)
|
||||||
|
|
||||||
@ -152,7 +154,8 @@ class ConsentResource(DirectServeHtmlResource):
|
|||||||
"""
|
"""
|
||||||
version = parse_string(request, "v", required=True)
|
version = parse_string(request, "v", required=True)
|
||||||
username = parse_string(request, "u", required=True)
|
username = parse_string(request, "u", required=True)
|
||||||
userhmac = parse_string(request, "h", required=True, encoding=None)
|
args = request.args # type: Dict[bytes, List[bytes]]
|
||||||
|
userhmac = parse_bytes_from_args(args, "h", required=True)
|
||||||
|
|
||||||
self._check_hash(username, userhmac)
|
self._check_hash(username, userhmac)
|
||||||
|
|
||||||
|
@ -14,13 +14,13 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import IO, TYPE_CHECKING
|
from typing import IO, TYPE_CHECKING, Dict, List, Optional
|
||||||
|
|
||||||
from twisted.web.server import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import Codes, SynapseError
|
from synapse.api.errors import Codes, SynapseError
|
||||||
from synapse.http.server import DirectServeJsonResource, respond_with_json
|
from synapse.http.server import DirectServeJsonResource, respond_with_json
|
||||||
from synapse.http.servlet import parse_string
|
from synapse.http.servlet import parse_bytes_from_args
|
||||||
from synapse.http.site import SynapseRequest
|
from synapse.http.site import SynapseRequest
|
||||||
from synapse.rest.media.v1.media_storage import SpamMediaException
|
from synapse.rest.media.v1.media_storage import SpamMediaException
|
||||||
|
|
||||||
@ -61,10 +61,11 @@ class UploadResource(DirectServeJsonResource):
|
|||||||
errcode=Codes.TOO_LARGE,
|
errcode=Codes.TOO_LARGE,
|
||||||
)
|
)
|
||||||
|
|
||||||
upload_name = parse_string(request, b"filename", encoding=None)
|
args = request.args # type: Dict[bytes, List[bytes]] # type: ignore
|
||||||
if upload_name:
|
upload_name_bytes = parse_bytes_from_args(args, "filename")
|
||||||
|
if upload_name_bytes:
|
||||||
try:
|
try:
|
||||||
upload_name = upload_name.decode("utf8")
|
upload_name = upload_name_bytes.decode("utf8") # type: Optional[str]
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
raise SynapseError(
|
raise SynapseError(
|
||||||
msg="Invalid UTF-8 filename parameter: %r" % (upload_name), code=400
|
msg="Invalid UTF-8 filename parameter: %r" % (upload_name), code=400
|
||||||
|
Loading…
Reference in New Issue
Block a user