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

metadata: fix validation issues #6001

Merged
merged 8 commits into from Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
9 changes: 4 additions & 5 deletions internal/metadata/metadata.go
Expand Up @@ -82,14 +82,13 @@ func Set(addr resolver.Address, md metadata.MD) resolver.Address {
// - header names must contain one or more characters from this set [0-9 a-z _ - .].
// - if the header-name ends with a "-bin" suffix, no validation of the header value is performed.
// - otherwise, the header value must contain one or more characters from the set [%x20-%x7E].
func Validate(md metadata.MD) (err error) {
func Validate(md metadata.MD) error {
for k, vals := range md {
err = ValidatePair(k, vals...)
if err != nil {
break
if err := ValidatePair(k, vals...); err != nil {
return err
}
}
return
return nil
}

// hasNotPrintable return true if msg contains any characters which are not in %x20-%x7E
Expand Down
2 changes: 1 addition & 1 deletion internal/metadata/metadata_test.go
Expand Up @@ -101,7 +101,7 @@ func TestValidate(t *testing.T) {
want: errors.New("header key \"test\" contains value with non-printable ASCII characters"),
},
{
md: map[string][]string{"": {string(rune(0x19))}},
md: map[string][]string{"": {"valid"}},
want: errors.New("there is an empty key in the header"),
},
{
Expand Down
6 changes: 2 additions & 4 deletions stream.go
Expand Up @@ -170,15 +170,13 @@ func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (_ ClientStream, err error) {
if md, added, ok := metadata.FromOutgoingContextRaw(ctx); ok {
// validate md
err := imetadata.Validate(md)
if err != nil {
if err := imetadata.Validate(md); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
// validate added
for _, kvs := range added {
for i := 0; i < len(kvs); i += 2 {
err = imetadata.ValidatePair(kvs[i], kvs[i+1])
if err != nil {
if err = imetadata.ValidatePair(kvs[i], kvs[i+1]); err != nil {
ktalg marked this conversation as resolved.
Show resolved Hide resolved
return nil, status.Error(codes.Internal, err.Error())
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/metadata_test.go
Expand Up @@ -103,8 +103,8 @@ func (s) TestInvalidMetadata(t *testing.T) {
}
test := tests[testNum]
testNum++
// merge original md and added md
md := test.md.Copy()
// merge original md and added md.
md := metadata.Join(test.md, metadata.Pairs(test.appendMD...))
for i := 0; i < len(test.appendMD); i += 2 {
md.Append(test.appendMD[i], test.appendMD[i+1])
}
ktalg marked this conversation as resolved.
Show resolved Hide resolved
Expand Down