-
Notifications
You must be signed in to change notification settings - Fork 4.6k
client: ignore http status header for gRPC streams #8548
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
Changes from 4 commits
50b5337
b1a7265
b4cce55
7087664
aceb8be
734cd4d
bebc8d3
09cf708
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1465,17 +1465,13 @@ func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) { | |
contentTypeErr = "malformed header: missing HTTP content-type" | ||
grpcMessage string | ||
recvCompress string | ||
httpStatusCode *int | ||
httpStatusErr string | ||
rawStatusCode = codes.Unknown | ||
rawStatusCode = codes.Internal | ||
// headerError is set if an error is encountered while parsing the headers | ||
headerError string | ||
headerError string | ||
receivedHTTPStatus string | ||
|
||
) | ||
|
||
if initialHeader { | ||
httpStatusErr = "malformed header: missing HTTP status" | ||
} | ||
|
||
for _, hf := range frame.Fields { | ||
switch hf.Name { | ||
case "content-type": | ||
|
@@ -1499,9 +1495,39 @@ func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) { | |
case "grpc-message": | ||
grpcMessage = decodeGrpcMessage(hf.Value) | ||
case ":status": | ||
c, err := strconv.ParseInt(hf.Value, 10, 32) | ||
if !isGRPC { | ||
dfawley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
receivedHTTPStatus = hf.Value | ||
} | ||
default: | ||
if isReservedHeader(hf.Name) && !isWhitelistedHeader(hf.Name) { | ||
break | ||
} | ||
v, err := decodeMetadataHeader(hf.Name, hf.Value) | ||
if err != nil { | ||
headerError = fmt.Sprintf("transport: malformed %s: %v", hf.Name, err) | ||
logger.Warningf("Failed to decode metadata header (%q, %q): %v", hf.Name, hf.Value, err) | ||
break | ||
} | ||
mdata[hf.Name] = append(mdata[hf.Name], v) | ||
} | ||
} | ||
|
||
// If a non gRPC response is received, then evaluate entire http status and process close stream / response. | ||
// In case http status doesn't provide any error information (status : 200), evalute response code to be Unknown. | ||
dfawley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if !isGRPC { | ||
var grpcErrorCode = codes.Internal // when header does not include HTTP status, return INTERNAL | ||
var errs []string | ||
dfawley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
switch receivedHTTPStatus { | ||
case "": | ||
httpStatusErr = "malformed header: missing HTTP status" | ||
case "200": | ||
grpcErrorCode = codes.Unknown | ||
|
||
default: | ||
// Any other status code (e.g., "404", "503"). We must parse it. | ||
dfawley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
c, err := strconv.ParseInt(receivedHTTPStatus, 10, 32) | ||
if err != nil { | ||
se := status.New(codes.Internal, fmt.Sprintf("transport: malformed http-status: %v", err)) | ||
se := status.New(grpcErrorCode, fmt.Sprintf("transport: malformed http-status: %v", err)) | ||
t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream) | ||
return | ||
} | ||
dfawley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -1512,52 +1538,31 @@ func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) { | |
"protocol error: informational header with status code %d must not have END_STREAM set", statusCode)) | ||
t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream) | ||
} | ||
//In case of informational headers return | ||
dfawley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return | ||
} | ||
httpStatusCode = &statusCode | ||
if statusCode == 200 { | ||
httpStatusErr = "" | ||
break | ||
} | ||
|
||
httpStatusErr = fmt.Sprintf( | ||
"unexpected HTTP status code received from server: %d (%s)", | ||
statusCode, | ||
http.StatusText(statusCode), | ||
) | ||
default: | ||
if isReservedHeader(hf.Name) && !isWhitelistedHeader(hf.Name) { | ||
break | ||
} | ||
v, err := decodeMetadataHeader(hf.Name, hf.Value) | ||
if err != nil { | ||
headerError = fmt.Sprintf("transport: malformed %s: %v", hf.Name, err) | ||
logger.Warningf("Failed to decode metadata header (%q, %q): %v", hf.Name, hf.Value, err) | ||
break | ||
} | ||
mdata[hf.Name] = append(mdata[hf.Name], v) | ||
} | ||
} | ||
|
||
if !isGRPC || httpStatusErr != "" { | ||
var code = codes.Internal // when header does not include HTTP status, return INTERNAL | ||
|
||
if httpStatusCode != nil { | ||
var ok bool | ||
code, ok = HTTPStatusConvTab[*httpStatusCode] | ||
grpcErrorCode, ok = HTTPStatusConvTab[statusCode] | ||
if !ok { | ||
code = codes.Unknown | ||
grpcErrorCode = codes.Unknown | ||
} | ||
|
||
} | ||
var errs []string | ||
|
||
if httpStatusErr != "" { | ||
errs = append(errs, httpStatusErr) | ||
} | ||
|
||
if contentTypeErr != "" { | ||
errs = append(errs, contentTypeErr) | ||
} | ||
// Verify the HTTP response is a 200. | ||
se := status.New(code, strings.Join(errs, "; ")) | ||
|
||
se := status.New(grpcErrorCode, strings.Join(errs, "; ")) | ||
t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream) | ||
return | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,10 +103,9 @@ func (s) TestHTTPHeaderFrameErrorHandlingInitialHeader(t *testing.T) { | |
errCode codes.Code | ||
}{ | ||
{ | ||
name: "missing gRPC status", | ||
name: "missing gRPC content type", | ||
arjan-bal marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like you have changed this test instead of adding a new one? Shouldn't we still have a test that sets these exact header fields? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing grpc status is no longer relevant in the the initial headers - it is tested in the trailers test where grpc status is expected/mandatory. |
||
header: []string{ | ||
":status", "403", | ||
"content-type", "application/grpc", | ||
}, | ||
errCode: codes.PermissionDenied, | ||
}, | ||
|
@@ -120,23 +119,23 @@ func (s) TestHTTPHeaderFrameErrorHandlingInitialHeader(t *testing.T) { | |
errCode: codes.Internal, | ||
}, | ||
{ | ||
name: "Malformed grpc-tags-bin field", | ||
name: "Malformed grpc-tags-bin field ignores http status", | ||
header: []string{ | ||
":status", "502", | ||
"content-type", "application/grpc", | ||
"grpc-status", "0", | ||
"grpc-tags-bin", "???", | ||
}, | ||
errCode: codes.Unavailable, | ||
errCode: codes.Internal, | ||
}, | ||
{ | ||
name: "gRPC status error", | ||
name: "gRPC status error ignoring http status", | ||
header: []string{ | ||
":status", "502", | ||
"content-type", "application/grpc", | ||
"grpc-status", "3", | ||
}, | ||
errCode: codes.Unavailable, | ||
errCode: codes.InvalidArgument, | ||
}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
|
@@ -161,7 +160,7 @@ func (s) TestHTTPHeaderFrameErrorHandlingNormalTrailer(t *testing.T) { | |
errCode codes.Code | ||
}{ | ||
{ | ||
name: "trailer missing grpc-status", | ||
name: "trailer missing grpc-status to ignore http status", | ||
responseHeader: []string{ | ||
":status", "200", | ||
"content-type", "application/grpc", | ||
|
@@ -170,10 +169,10 @@ func (s) TestHTTPHeaderFrameErrorHandlingNormalTrailer(t *testing.T) { | |
// trailer missing grpc-status | ||
":status", "502", | ||
}, | ||
errCode: codes.Unavailable, | ||
errCode: codes.Internal, | ||
}, | ||
{ | ||
name: "malformed grpc-status-details-bin field with status 404", | ||
name: "malformed grpc-status-details-bin field with status 404 to be ignored due to content type", | ||
responseHeader: []string{ | ||
":status", "404", | ||
"content-type", "application/grpc", | ||
|
@@ -183,20 +182,19 @@ func (s) TestHTTPHeaderFrameErrorHandlingNormalTrailer(t *testing.T) { | |
"grpc-status", "0", | ||
"grpc-status-details-bin", "????", | ||
}, | ||
errCode: codes.Unimplemented, | ||
errCode: codes.Internal, | ||
}, | ||
{ | ||
name: "malformed grpc-status-details-bin field with status 200", | ||
name: "malformed grpc-status-details-bin field with status 404 and no content type", | ||
arjan-bal marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar - don't we still want to have a test with an illegal grpc-status-details-bin and status 200? We can also have this new test case, but it seems like having more test cases is always better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the content type is application/grpc (as was in previous version of the test) the http status is immaterial (200/404 etc won't make a difference) and it is tested in the test case above this with 404. This was more important variation of data where we check without correct content type. |
||
responseHeader: []string{ | ||
":status", "200", | ||
"content-type", "application/grpc", | ||
":status", "404", | ||
}, | ||
trailer: []string{ | ||
// malformed grpc-status-details-bin field | ||
"grpc-status", "0", | ||
"grpc-status-details-bin", "????", | ||
}, | ||
errCode: codes.Internal, | ||
errCode: codes.Unimplemented, | ||
}, | ||
} | ||
for _, test := range tests { | ||
|
Uh oh!
There was an error while loading. Please reload this page.