diff options
| author | Nicolas Paul <n@nc0.fr> | 2023-04-07 01:18:07 +0200 | 
|---|---|---|
| committer | Nicolas Paul <nicolaspaul45400@gmail.com> | 2023-04-07 01:18:36 +0200 | 
| commit | 96c174f226767294186467715a1931cce850678d (patch) | |
| tree | 553b3a2457387667743ecc7df61b83abd7d3d46e /vendor/google.golang.org/grpc/internal/metadata | |
| parent | 2e4db08bc6360a13e167ad9eca7f21dc8a2f89a9 (diff) | |
update deps
Diffstat (limited to 'vendor/google.golang.org/grpc/internal/metadata')
| -rw-r--r-- | vendor/google.golang.org/grpc/internal/metadata/metadata.go | 62 | 
1 files changed, 37 insertions, 25 deletions
diff --git a/vendor/google.golang.org/grpc/internal/metadata/metadata.go b/vendor/google.golang.org/grpc/internal/metadata/metadata.go index b2980f8..c82e608 100644 --- a/vendor/google.golang.org/grpc/internal/metadata/metadata.go +++ b/vendor/google.golang.org/grpc/internal/metadata/metadata.go @@ -76,33 +76,11 @@ func Set(addr resolver.Address, md metadata.MD) resolver.Address {  	return addr  } -// Validate returns an error if the input md contains invalid keys or values. -// -// If the header is not a pseudo-header, the following items are checked: -// - 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]. +// Validate validates every pair in md with ValidatePair.  func Validate(md metadata.MD) error {  	for k, vals := range md { -		// pseudo-header will be ignored -		if k[0] == ':' { -			continue -		} -		// check key, for i that saving a conversion if not using for range -		for i := 0; i < len(k); i++ { -			r := k[i] -			if !(r >= 'a' && r <= 'z') && !(r >= '0' && r <= '9') && r != '.' && r != '-' && r != '_' { -				return fmt.Errorf("header key %q contains illegal characters not in [0-9a-z-_.]", k) -			} -		} -		if strings.HasSuffix(k, "-bin") { -			continue -		} -		// check value -		for _, val := range vals { -			if hasNotPrintable(val) { -				return fmt.Errorf("header key %q contains value with non-printable ASCII characters", k) -			} +		if err := ValidatePair(k, vals...); err != nil { +			return err  		}  	}  	return nil @@ -118,3 +96,37 @@ func hasNotPrintable(msg string) bool {  	}  	return false  } + +// ValidatePair validate a key-value pair with the following rules (the pseudo-header will be skipped) : +// +// - key must contain one or more characters. +// - the characters in the key must be contained in [0-9 a-z _ - .]. +// - if the key ends with a "-bin" suffix, no validation of the corresponding value is performed. +// - the characters in the every value must be printable (in [%x20-%x7E]). +func ValidatePair(key string, vals ...string) error { +	// key should not be empty +	if key == "" { +		return fmt.Errorf("there is an empty key in the header") +	} +	// pseudo-header will be ignored +	if key[0] == ':' { +		return nil +	} +	// check key, for i that saving a conversion if not using for range +	for i := 0; i < len(key); i++ { +		r := key[i] +		if !(r >= 'a' && r <= 'z') && !(r >= '0' && r <= '9') && r != '.' && r != '-' && r != '_' { +			return fmt.Errorf("header key %q contains illegal characters not in [0-9a-z-_.]", key) +		} +	} +	if strings.HasSuffix(key, "-bin") { +		return nil +	} +	// check value +	for _, val := range vals { +		if hasNotPrintable(val) { +			return fmt.Errorf("header key %q contains value with non-printable ASCII characters", key) +		} +	} +	return nil +}  | 
