aboutsummaryrefslogtreecommitdiff
path: root/clientapi/httputil
diff options
context:
space:
mode:
authorPestdoktor <jonas@fentker.eu>2020-10-09 10:15:51 +0200
committerGitHub <noreply@github.com>2020-10-09 09:15:51 +0100
commitc4c8bfd0270f3d7009f0fb7c953a26e2cb65442d (patch)
tree133be1e4a7a76648c3a00bc9334de0ee23ec1b54 /clientapi/httputil
parentf3e8ae01efb0abd0904509ddaa2ae85017ca4aa5 (diff)
reject invalid UTF-8 (#1472)
* reject invalid UTF-8 Signed-off-by: Jonas Fentker <jonas@fentker.eu> * update sytest-whitelist Signed-off-by: Jonas Fentker <jonas@fentker.eu> Co-authored-by: Kegsay <kegan@matrix.org>
Diffstat (limited to 'clientapi/httputil')
-rw-r--r--clientapi/httputil/httputil.go20
1 files changed, 19 insertions, 1 deletions
diff --git a/clientapi/httputil/httputil.go b/clientapi/httputil/httputil.go
index b0fe6a6c..29d7b0b3 100644
--- a/clientapi/httputil/httputil.go
+++ b/clientapi/httputil/httputil.go
@@ -16,7 +16,9 @@ package httputil
import (
"encoding/json"
+ "io/ioutil"
"net/http"
+ "unicode/utf8"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/util"
@@ -25,7 +27,23 @@ import (
// UnmarshalJSONRequest into the given interface pointer. Returns an error JSON response if
// there was a problem unmarshalling. Calling this function consumes the request body.
func UnmarshalJSONRequest(req *http.Request, iface interface{}) *util.JSONResponse {
- if err := json.NewDecoder(req.Body).Decode(iface); err != nil {
+ // encoding/json allows invalid utf-8, matrix does not
+ // https://matrix.org/docs/spec/client_server/r0.6.1#api-standards
+ body, err := ioutil.ReadAll(req.Body)
+ if err != nil {
+ util.GetLogger(req.Context()).WithError(err).Error("ioutil.ReadAll failed")
+ resp := jsonerror.InternalServerError()
+ return &resp
+ }
+
+ if !utf8.Valid(body) {
+ return &util.JSONResponse{
+ Code: http.StatusBadRequest,
+ JSON: jsonerror.NotJSON("Body contains invalid UTF-8"),
+ }
+ }
+
+ if err := json.Unmarshal(body, iface); err != nil {
// TODO: We may want to suppress the Error() return in production? It's useful when
// debugging because an error will be produced for both invalid/malformed JSON AND
// valid JSON with incorrect types for values.