Skip to content

Commit c5b8569

Browse files
Armani Ferrantefjl
authored andcommitted
rpc: disallow PUT and DELETE on HTTP (#15501)
Fixes #15493
1 parent b019018 commit c5b8569

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

rpc/http.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
)
3434

3535
const (
36+
contentType = "application/json"
3637
maxHTTPRequestContentLength = 1024 * 128
3738
)
3839

@@ -69,8 +70,8 @@ func DialHTTP(endpoint string) (*Client, error) {
6970
if err != nil {
7071
return nil, err
7172
}
72-
req.Header.Set("Content-Type", "application/json")
73-
req.Header.Set("Accept", "application/json")
73+
req.Header.Set("Content-Type", contentType)
74+
req.Header.Set("Accept", contentType)
7475

7576
initctx := context.Background()
7677
return newClient(initctx, func(context.Context) (net.Conn, error) {
@@ -150,21 +151,11 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
150151
if r.Method == "GET" && r.ContentLength == 0 && r.URL.RawQuery == "" {
151152
return
152153
}
153-
// For meaningful requests, validate it's size and content type
154-
if r.ContentLength > maxHTTPRequestContentLength {
155-
http.Error(w,
156-
fmt.Sprintf("content length too large (%d>%d)", r.ContentLength, maxHTTPRequestContentLength),
157-
http.StatusRequestEntityTooLarge)
158-
return
159-
}
160-
ct := r.Header.Get("content-type")
161-
mt, _, err := mime.ParseMediaType(ct)
162-
if err != nil || mt != "application/json" {
163-
http.Error(w,
164-
"invalid content type, only application/json is supported",
165-
http.StatusUnsupportedMediaType)
154+
if responseCode, errorMessage := httpErrorResponse(r); responseCode != 0 {
155+
http.Error(w, errorMessage, responseCode)
166156
return
167157
}
158+
168159
// All checks passed, create a codec that reads direct from the request body
169160
// untilEOF and writes the response to w and order the server to process a
170161
// single request.
@@ -175,6 +166,28 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
175166
srv.ServeSingleRequest(codec, OptionMethodInvocation)
176167
}
177168

169+
// Returns a non-zero response code and error message if the request is invalid.
170+
func httpErrorResponse(r *http.Request) (int, string) {
171+
if r.Method == "PUT" || r.Method == "DELETE" {
172+
errorMessage := "method not allowed"
173+
return http.StatusMethodNotAllowed, errorMessage
174+
}
175+
176+
if r.ContentLength > maxHTTPRequestContentLength {
177+
errorMessage := fmt.Sprintf("content length too large (%d>%d)", r.ContentLength, maxHTTPRequestContentLength)
178+
return http.StatusRequestEntityTooLarge, errorMessage
179+
}
180+
181+
ct := r.Header.Get("content-type")
182+
mt, _, err := mime.ParseMediaType(ct)
183+
if err != nil || mt != contentType {
184+
errorMessage := fmt.Sprintf("invalid content type, only %s is supported", contentType)
185+
return http.StatusUnsupportedMediaType, errorMessage
186+
}
187+
188+
return 0, ""
189+
}
190+
178191
func newCorsHandler(srv *Server, allowedOrigins []string) http.Handler {
179192
// disable CORS support if user has not specified a custom CORS configuration
180193
if len(allowedOrigins) == 0 {

rpc/http_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package rpc
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestHTTPErrorResponseWithDelete(t *testing.T) {
11+
httpErrorResponseTest(t, "DELETE", contentType, "", http.StatusMethodNotAllowed)
12+
}
13+
14+
func TestHTTPErrorResponseWithPut(t *testing.T) {
15+
httpErrorResponseTest(t, "PUT", contentType, "", http.StatusMethodNotAllowed)
16+
}
17+
18+
func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
19+
body := make([]rune, maxHTTPRequestContentLength+1, maxHTTPRequestContentLength+1)
20+
httpErrorResponseTest(t,
21+
"POST", contentType, string(body), http.StatusRequestEntityTooLarge)
22+
}
23+
24+
func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
25+
httpErrorResponseTest(t, "POST", "", "", http.StatusUnsupportedMediaType)
26+
}
27+
28+
func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
29+
httpErrorResponseTest(t, "POST", contentType, "", 0)
30+
}
31+
32+
func httpErrorResponseTest(t *testing.T,
33+
method, contentType, body string, expectedResponse int) {
34+
35+
request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body))
36+
request.Header.Set("content-type", contentType)
37+
if response, _ := httpErrorResponse(request); response != expectedResponse {
38+
t.Fatalf("response code should be %d not %d", expectedResponse, response)
39+
}
40+
}

0 commit comments

Comments
 (0)