rpc: always close http bodies (backport #8712) (#8715)

(cherry picked from commit 931c98f7ad)

Co-authored-by: Sam Kleinman <garen@tychoish.com>
Co-authored-by: M. J. Fromberger <fromberger@interchain.io>
This commit is contained in:
mergify[bot]
2022-06-08 11:57:55 -07:00
committed by GitHub
parent 3c29b6996b
commit 46c27b45ab
3 changed files with 10 additions and 4 deletions

View File

@@ -147,9 +147,15 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han
}
}
func ensureBodyClose(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
next(w, r)
}
}
func handleInvalidJSONRPCPaths(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Since the pattern "/" matches all paths not matched by other registered patterns,
// we check whether the path is indeed "/", otherwise return a 404 error
if r.URL.Path != "/" {
http.NotFound(w, r)

View File

@@ -217,7 +217,7 @@ func TestRPCNotificationInBatch(t *testing.T) {
func TestUnknownRPCPath(t *testing.T) {
mux := testMux()
req, _ := http.NewRequest("GET", "http://localhost/unknownrpcpath", nil)
req, _ := http.NewRequest("GET", "http://localhost/unknownrpcpath", strings.NewReader(""))
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()

View File

@@ -15,11 +15,11 @@ import (
func RegisterRPCFuncs(mux *http.ServeMux, funcMap map[string]*RPCFunc, logger log.Logger) {
// HTTP endpoints
for funcName, rpcFunc := range funcMap {
mux.HandleFunc("/"+funcName, makeHTTPHandler(rpcFunc, logger))
mux.HandleFunc("/"+funcName, ensureBodyClose(makeHTTPHandler(rpcFunc, logger)))
}
// JSONRPC endpoints
mux.HandleFunc("/", handleInvalidJSONRPCPaths(makeJSONRPCHandler(funcMap, logger)))
mux.HandleFunc("/", ensureBodyClose(handleInvalidJSONRPCPaths(makeJSONRPCHandler(funcMap, logger))))
}
// Function introspection