Skip to content

Commit 4fe30bf

Browse files
bas-vkfjl
authored andcommitted
rpc: check content-type for HTTP requests (#15220)
1 parent 4732ee8 commit 4fe30bf

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

‎rpc/http.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"io"
2525
"io/ioutil"
26+
"mime"
2627
"net"
2728
"net/http"
2829
"sync"
@@ -151,6 +152,16 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
151152
http.StatusRequestEntityTooLarge)
152153
return
153154
}
155+
156+
ct := r.Header.Get("content-type")
157+
mt, _, err := mime.ParseMediaType(ct)
158+
if err != nil || mt != "application/json" {
159+
http.Error(w,
160+
"invalid content type, only application/json is supported",
161+
http.StatusUnsupportedMediaType)
162+
return
163+
}
164+
154165
w.Header().Set("content-type", "application/json")
155166

156167
// create a codec that reads direct from the request body until

0 commit comments

Comments
 (0)