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 3 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
25 changes: 13 additions & 12 deletions internal/transport/http2_client.go
Expand Up @@ -1286,7 +1286,7 @@ func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) {
statusGen *status.Status
httpStatusCode *int
httpStatusErr string
rawStatus string
rawStatusCode *codes.Code
// headerError is set if an error is encountered while parsing the headers
headerError string
)
Expand All @@ -1308,7 +1308,14 @@ func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) {
case "grpc-encoding":
s.recvCompress = hf.Value
case "grpc-status":
rawStatus = hf.Value
code, err := strconv.ParseInt(hf.Value, 10, 32)
if err != nil {
se := status.New(codes.Internal, fmt.Sprintf("transport: malformed grpc-status: %v", err))
t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
return
}
c := codes.Code(uint32(code))
rawStatusCode = &c
case "grpc-message":
grpcMessage = decodeGrpcMessage(hf.Value)
case "grpc-status-details-bin":
Expand Down Expand Up @@ -1428,17 +1435,11 @@ func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) {
}

if statusGen == nil {
rawStatusCode := codes.Unknown
if rawStatus != "" {
code, err := strconv.ParseInt(rawStatus, 10, 32)
if err != nil {
se := status.New(codes.Internal, fmt.Sprintf("transport: malformed grpc-status: %v", err))
t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
return
}
rawStatusCode = codes.Code(uint32(code))
rsc := codes.Unknown
if rawStatusCode != nil {
rsc = *rawStatusCode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make rawStatusCode a non-pointer and initialize it to codes.Unknown instead? Also "rsc" = "Russ Cox" 😆 (github.com/rsc)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've initialized rawStatusCode as codes.Unknown instead. When I wrote that I was definitely thinking about Russ haha

}
statusGen = status.New(rawStatusCode, grpcMessage)
statusGen = status.New(rsc, grpcMessage)
}

// if client received END_STREAM from server while stream was still active, send RST_STREAM
Expand Down
111 changes: 49 additions & 62 deletions internal/transport/transport_test.go
Expand Up @@ -1978,6 +1978,31 @@ func (s) TestClientHandshakeInfo(t *testing.T) {
}

func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
testStream := func() *Stream {
return &Stream{
done: make(chan struct{}),
headerChan: make(chan struct{}),
buf: &recvBuffer{
c: make(chan recvMsg),
mu: sync.Mutex{},
},
}
}

testClient := func(ts *Stream) *http2Client {
return &http2Client{
mu: sync.Mutex{},
activeStreams: map[uint32]*Stream{
0: ts,
},
controlBuf: &controlBuffer{
ch: make(chan struct{}),
done: make(chan struct{}),
list: &itemList{},
},
}
}

for _, test := range []struct {
name string
// input
Expand All @@ -1993,12 +2018,6 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
{Name: "grpc-status", Value: "0"},
{Name: ":status", Value: "200"},
},
HeadersFrame: &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
Flags: http2.FlagHeadersEndStream,
},
},
},
// no error
wantStatus: status.New(codes.OK, ""),
Expand All @@ -2010,12 +2029,6 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
{Name: "grpc-status", Value: "0"},
{Name: ":status", Value: "200"},
},
HeadersFrame: &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
Flags: http2.FlagHeadersEndStream,
},
},
},
wantStatus: status.New(
codes.Unknown,
Expand All @@ -2030,12 +2043,6 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
{Name: "grpc-status", Value: "xxxx"},
{Name: ":status", Value: "200"},
},
HeadersFrame: &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
Flags: http2.FlagHeadersEndStream,
},
},
},
wantStatus: status.New(
codes.Internal,
Expand All @@ -2048,12 +2055,6 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
Fields: []hpack.HeaderField{
{Name: "content-type", Value: "application/json"},
},
HeadersFrame: &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
Flags: http2.FlagHeadersEndStream,
},
},
},
wantStatus: status.New(
codes.Internal,
Expand All @@ -2067,12 +2068,6 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
// No content type provided then fallback into handling http error.
{Name: ":status", Value: "xxxx"},
},
HeadersFrame: &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
Flags: http2.FlagHeadersEndStream,
},
},
},
wantStatus: status.New(
codes.Internal,
Expand All @@ -2084,12 +2079,6 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
metaHeaderFrame: &http2.MetaHeadersFrame{
Fields: nil,
Truncated: true,
HeadersFrame: &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
Flags: http2.FlagHeadersEndStream,
},
},
},
wantStatus: status.New(
codes.Internal,
Expand All @@ -2104,11 +2093,6 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
{Name: "grpc-status", Value: "0"},
{Name: ":status", Value: "504"},
},
HeadersFrame: &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
},
},
},
wantStatus: status.New(
codes.Unavailable,
Expand All @@ -2121,37 +2105,40 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) {
Fields: []hpack.HeaderField{
{Name: "content-type", Value: "application/grpc"},
},
HeadersFrame: &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
Flags: http2.FlagHeadersEndStream,
},
},
},
wantStatus: status.New(
codes.Internal,
"malformed header: missing HTTP status",
),
},
} {

t.Run(test.name, func(t *testing.T) {
ts := &Stream{
done: make(chan struct{}),
headerChan: make(chan struct{}),
buf: &recvBuffer{
c: make(chan recvMsg),
mu: sync.Mutex{},
ts := testStream()
s := testClient(ts)

test.metaHeaderFrame.HeadersFrame = &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
},
}
s := &http2Client{
mu: sync.Mutex{},
activeStreams: map[uint32]*Stream{
0: ts,
},
controlBuf: &controlBuffer{
ch: make(chan struct{}),
done: make(chan struct{}),
list: &itemList{},

s.operateHeaders(test.metaHeaderFrame)

got := ts.status
want := test.wantStatus
if got.Code() != want.Code() || got.Message() != want.Message() {
t.Fatalf("operateHeaders(%v); status = \ngot: %s\nwant: %s", test.metaHeaderFrame, got, want)
}
})
t.Run(fmt.Sprintf("%s-end_stream", test.name), func(t *testing.T) {
ts := testStream()
s := testClient(ts)

test.metaHeaderFrame.HeadersFrame = &http2.HeadersFrame{
FrameHeader: http2.FrameHeader{
StreamID: 0,
Flags: http2.FlagHeadersEndStream,
},
}

Expand Down
2 changes: 1 addition & 1 deletion test/end2end_test.go
Expand Up @@ -7263,7 +7263,7 @@ func (s) TestHTTPHeaderFrameErrorHandlingInitialHeader(t *testing.T) {
"content-type", "application/grpc",
"grpc-status", "abc",
},
errCode: codes.Unavailable,
errCode: codes.Internal,
},
{
// Malformed grpc-tags-bin field.
Expand Down