Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transport: validate http 200 status for responses #4474

Merged
merged 23 commits into from Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fe87cc5
Validate http 200 for all non-end-of-stream messages
JNProtzman May 21, 2021
154e687
Add additional test
JNProtzman May 21, 2021
e437a72
Merge branch 'master' of https://github.com/grpc/grpc-go into http_200
JNProtzman May 24, 2021
9cb2546
Add test for bad http status code in gRPC mode
JNProtzman May 25, 2021
17d7508
Merge branch 'master' of https://github.com/grpc/grpc-go into http_200
JNProtzman May 25, 2021
71ad202
fix error messages
JNProtzman May 27, 2021
29c60a1
Merge branch 'master' of https://github.com/grpc/grpc-go into http_200
JNProtzman May 27, 2021
0e104a3
new test case, fix error messaging
JNProtzman May 27, 2021
17758ef
address pr comments
JNProtzman May 28, 2021
6de802f
Merge branch 'master' of https://github.com/grpc/grpc-go into http_200
JNProtzman Jun 4, 2021
399b76e
Fix test log, pr comments
JNProtzman Jun 4, 2021
253fd40
Merge branch 'master' of https://github.com/grpc/grpc-go into http_200
JNProtzman Jun 5, 2021
1446920
Use status in error instead of proto, add String method to Status
JNProtzman Jun 7, 2021
fa0bff4
Merge branch 'master' of https://github.com/grpc/grpc-go into http_200
JNProtzman Jun 9, 2021
efaf7ab
return status directly
JNProtzman Jun 9, 2021
c0cda22
Merge branch 'master' of https://github.com/grpc/grpc-go into http_200
JNProtzman Jun 17, 2021
6230a62
pr comment
JNProtzman Jun 17, 2021
43990b6
test updates
JNProtzman Jun 20, 2021
8b0e761
fix go vet issue
JNProtzman Jun 25, 2021
472d502
test endstream and not endstream
JNProtzman Jul 2, 2021
6642a86
Merge branch 'master' of https://github.com/grpc/grpc-go into http_200
JNProtzman Jul 2, 2021
adfc275
test grpc-status, not http status
JNProtzman Jul 2, 2021
9207e93
Minor suggestions
JNProtzman Jul 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions internal/transport/http2_client.go
Expand Up @@ -1266,7 +1266,7 @@ func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) {
// that the peer is speaking gRPC and we are in gRPC mode.
isGRPC = !initialHeader
mdata = make(map[string][]string)
contentTypeErr string
contentTypeErr = "missing content-type"
dfawley marked this conversation as resolved.
Show resolved Hide resolved
JNProtzman marked this conversation as resolved.
Show resolved Hide resolved
grpcMessage string
statusGen *status.Status

Expand All @@ -1283,6 +1283,7 @@ func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) {
contentTypeErr = fmt.Sprintf("transport: received the unexpected content-type %q", hf.Value)
JNProtzman marked this conversation as resolved.
Show resolved Hide resolved
break
}
contentTypeErr = ""
mdata[hf.Name] = append(mdata[hf.Name], hf.Value)
isGRPC = true
case "grpc-encoding":
Expand Down Expand Up @@ -1313,10 +1314,10 @@ func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) {
}
}

if !isGRPC {
if !isGRPC || (httpStatus != "200" && !endStream) {
var (
code = codes.Internal // when header does not include HTTP status, return INTERNAL
httpStatusCode int
httpStatusCode *int
)

if httpStatus != "" {
Expand All @@ -1326,17 +1327,18 @@ func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) {
t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
return
}
httpStatusCode = int(c)
statusCode := int(c)
httpStatusCode = &statusCode

var ok bool
code, ok = HTTPStatusConvTab[httpStatusCode]
code, ok = HTTPStatusConvTab[statusCode]
if !ok {
code = codes.Unknown
}
}

// Verify the HTTP response is a 200.
se := status.New(code, constructHTTPErrMsg(&httpStatusCode, contentTypeErr))
se := status.New(code, constructHTTPErrMsg(httpStatusCode, contentTypeErr))
t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
return
}
Expand Down
11 changes: 6 additions & 5 deletions internal/transport/http_util.go
Expand Up @@ -177,16 +177,17 @@ func decodeGRPCStatusDetails(rawDetails string) (*status.Status, error) {
// Format: HTTP status code and its corresponding message + content-type error message.
func constructHTTPErrMsg(httpStatus *int, contentTypeErr string) string {
var errMsgs []string

if httpStatus == nil {
errMsgs = append(errMsgs, "malformed header: missing HTTP status")
} else {
errMsgs = append(errMsgs, fmt.Sprintf("%s: HTTP status code %d", http.StatusText(*(httpStatus)), *httpStatus))
errMsgs = append(errMsgs, fmt.Sprintf(
"unexpected HTTP status code received from server: %d (%s)",
*httpStatus,
http.StatusText(*(httpStatus)),
))
}

if contentTypeErr == "" {
errMsgs = append(errMsgs, "transport: missing content-type field")
} else {
if contentTypeErr != "" {
JNProtzman marked this conversation as resolved.
Show resolved Hide resolved
errMsgs = append(errMsgs, contentTypeErr)
}

Expand Down
49 changes: 43 additions & 6 deletions internal/transport/transport_test.go
Expand Up @@ -1991,17 +1991,33 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
Fields: []hpack.HeaderField{
{Name: "content-type", Value: "application/grpc"},
dfawley marked this conversation as resolved.
Show resolved Hide resolved
{Name: "grpc-status", Value: "0"},
{Name: ":status", Value: "200"},
},
},
// no error
wantStatus: status.New(codes.OK, ""),
},
{
name: "missing content-type header",
metaHeaderFrame: &http2.MetaHeadersFrame{
Fields: []hpack.HeaderField{
{Name: "grpc-status", Value: "0"},
{Name: ":status", Value: "200"},
},
},
// no error
JNProtzman marked this conversation as resolved.
Show resolved Hide resolved
wantStatus: status.New(
codes.Unknown,
"unexpected HTTP status code received from server: 200 (OK); missing content-type",
JNProtzman marked this conversation as resolved.
Show resolved Hide resolved
),
},
{
name: "invalid grpc status header field",
metaHeaderFrame: &http2.MetaHeadersFrame{
Fields: []hpack.HeaderField{
{Name: "content-type", Value: "application/grpc"},
{Name: "grpc-status", Value: "xxxx"},
{Name: ":status", Value: "200"},
},
},
wantStatus: status.New(
Expand All @@ -2018,7 +2034,7 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
},
wantStatus: status.New(
codes.Internal,
": HTTP status code 0; transport: received the unexpected content-type \"application/json\"",
"malformed header: missing HTTP status; transport: received the unexpected content-type \"application/json\"",
),
},
{
Expand All @@ -2045,6 +2061,25 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
"peer header list size exceeded limit",
),
},
{
name: "bad status in grpc mode",
metaHeaderFrame: &http2.MetaHeadersFrame{
Fields: []hpack.HeaderField{
{Name: "content-type", Value: "application/grpc"},
{Name: "grpc-status", Value: "0"},
{Name: ":status", Value: "504"},
},
HeadersFrame: &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
},
},
},
wantStatus: status.New(
codes.Unavailable,
"unexpected HTTP status code received from server: 504 (Gateway Timeout)",
),
},
} {
t.Run(test.name, func(t *testing.T) {
ts := &Stream{
Expand All @@ -2066,11 +2101,13 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
list: &itemList{},
},
}
test.metaHeaderFrame.HeadersFrame = &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
Flags: http2.FlagHeadersEndStream,
},
if test.metaHeaderFrame.HeadersFrame == nil {
test.metaHeaderFrame.HeadersFrame = &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
Flags: http2.FlagHeadersEndStream,
},
}
}

s.operateHeaders(test.metaHeaderFrame)
Expand Down
14 changes: 13 additions & 1 deletion test/end2end_test.go
Expand Up @@ -7289,7 +7289,7 @@ func (s) TestHTTPHeaderFrameErrorHandlingInitialHeader(t *testing.T) {
}
}

// Testing non-Trailers-only Trailers (delievered in second HEADERS frame)
// Testing non-Trailers-only Trailers (delivered in second HEADERS frame)
func (s) TestHTTPHeaderFrameErrorHandlingNormalTrailer(t *testing.T) {
for _, test := range []struct {
responseHeader []string
Expand Down Expand Up @@ -7317,6 +7317,18 @@ func (s) TestHTTPHeaderFrameErrorHandlingNormalTrailer(t *testing.T) {
"grpc-status", "0",
"grpc-status-details-bin", "????",
},
errCode: codes.Unimplemented,
},
{
responseHeader: []string{
":status", "200",
"content-type", "application/grpc",
},
trailer: []string{
// malformed grpc-status-details-bin field
"grpc-status", "0",
"grpc-status-details-bin", "????",
},
dfawley marked this conversation as resolved.
Show resolved Hide resolved
errCode: codes.Internal,
},
} {
Expand Down