Skip to content

Commit

Permalink
Disallow unknown fields when unmarshaling requests in the handler
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSLevy committed Oct 19, 2018
1 parent 59d3d2d commit 9e4c091
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package jsonrpc2

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -65,9 +66,9 @@ func HTTPRequestHandlerFunc(w http.ResponseWriter, req *http.Request) {
Request
Params json.RawMessage `json:"params,omitempty"`
}{}

var res *Response
if err = json.Unmarshal(rawReq, &req); err != nil || !req.IsValid() {

if err = unmarshalStrict(rawReq, &req); err != nil || !req.IsValid() {
res = newErrorResponse(nil, InvalidRequest)
} else {
if req.IsValid() {
Expand Down Expand Up @@ -102,6 +103,13 @@ func HTTPRequestHandlerFunc(w http.ResponseWriter, req *http.Request) {
}
}

func unmarshalStrict(data []byte, v interface{}) error {
b := bytes.NewBuffer(data)
d := json.NewDecoder(b)
d.DisallowUnknownFields()
return d.Decode(v)
}

func respondError(w http.ResponseWriter, e Error) {
res := newErrorResponse(nil, e)
respond(w, res)
Expand Down

0 comments on commit 9e4c091

Please sign in to comment.