package runtime import ( "context" "encoding/base64" "fmt" "math" "net" "net/http" "net/textproto" "strconv" "strings" "sync" "time" "google.golang.org/grpc/codes" "google.golang.org/grpc/grpclog" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" ) // MetadataHeaderPrefix is the http prefix that represents custom metadata // parameters to or from a gRPC call. const MetadataHeaderPrefix = "Grpc-Metadata-" // MetadataPrefix is prepended to permanent HTTP header keys (as specified // by the IANA) when added to the gRPC context. const MetadataPrefix = "grpcgateway-" // MetadataTrailerPrefix is prepended to gRPC metadata as it is converted to // HTTP headers in a response handled by grpc-gateway const MetadataTrailerPrefix = "Grpc-Trailer-" const metadataGrpcTimeout = "Grpc-Timeout" const metadataHeaderBinarySuffix = "-Bin" const xForwardedFor = "X-Forwarded-For" const xForwardedHost = "X-Forwarded-Host" // gRPC metadata keys are lower-case, so these two are appended to the pairs under their // lower-case spelling. Writing it out as a constant keeps strings.ToLower off the per-request // path, where it allocated a new string for a compile-time constant on every call, and lets // metadata.Pairs skip its own lower-casing for these keys as well. const xForwardedForKey = "x-forwarded-for" const xForwardedHostKey = "x-forwarded-host" // DefaultContextTimeout is used for gRPC call context.WithTimeout whenever a Grpc-Timeout inbound // header isn't present. If the value is 0 the sent `context` will not have a timeout. var DefaultContextTimeout = 0 * time.Second // malformedHTTPHeaders lists the headers that the gRPC server may reject outright as malformed. // See https://github.com/grpc/grpc-go/pull/4803#issuecomment-986093310 for more context. var malformedHTTPHeaders = map[string]struct{}{ "connection": {}, } type ( rpcMethodKey struct{} httpPathPatternKey struct{} httpPatternKey struct{} AnnotateContextOption func(ctx context.Context) context.Context ) func WithHTTPPathPattern(pattern string) AnnotateContextOption { return func(ctx context.Context) context.Context { return withHTTPPathPattern(ctx, pattern) } } func decodeBinHeader(v string) ([]byte, error) { if len(v)%4 == 0 { // Input was padded, or padding was not necessary. return base64.StdEncoding.DecodeString(v) } return base64.RawStdEncoding.DecodeString(v) } /* AnnotateContext adds context information such as metadata from the request. At a minimum, the RemoteAddr is included in the fashion of "X-Forwarded-For", except that the forwarded destination is not another HTTP service but rather a gRPC service. */ func AnnotateContext(ctx context.Context, mux *ServeMux, req *http.Request, rpcMethodName string, options ...AnnotateContextOption) (context.Context, error) { ctx, md, err := annotateContext(ctx, mux, req, rpcMethodName, options...) if err != nil { return nil, err } if md == nil { return ctx, nil } return metadata.NewOutgoingContext(ctx, md), nil } // AnnotateIncomingContext adds context information such as metadata from the request. // Attach metadata as incoming context. func AnnotateIncomingContext(ctx context.Context, mux *ServeMux, req *http.Request, rpcMethodName string, options ...AnnotateContextOption) (context.Context, error) { ctx, md, err := annotateContext(ctx, mux, req, rpcMethodName, options...) if err != nil { return nil, err } if md == nil { return ctx, nil } return metadata.NewIncomingContext(ctx, md), nil } func isValidGRPCMetadataKey(key string) bool { // Must be a valid gRPC "Header-Name" as defined here: // https://github.com/grpc/grpc/blob/4b05dc88b724214d0c725c8e7442cbc7a61b1374/doc/PROTOCOL-HTTP2.md // This means 0-9 a-z _ - . // Only lowercase letters are valid in the wire protocol, but the client library will normalize // uppercase ASCII to lowercase, so uppercase ASCII is also acceptable. // An empty key is not a valid header name, and gRPC fails the call with one. if key == "" { return false } bytes := []byte(key) // gRPC validates strings on the byte level, not Unicode. for _, ch := range bytes { validLowercaseLetter := ch >= 'a' && ch <= 'z' validUppercaseLetter := ch >= 'A' && ch <= 'Z' validDigit := ch >= '0' && ch <= '9' validOther := ch == '.' || ch == '-' || ch == '_' if !validLowercaseLetter && !validUppercaseLetter && !validDigit && !validOther { return false } } return true } func isValidGRPCMetadataTextValue(textValue string) bool { // Must be a valid gRPC "ASCII-Value" as defined here: // https://github.com/grpc/grpc/blob/4b05dc88b724214d0c725c8e7442cbc7a61b1374/doc/PROTOCOL-HTTP2.md // This means printable ASCII (including/plus spaces); 0x20 to 0x7E inclusive. bytes := []byte(textValue) // gRPC validates strings on the byte level, not Unicode. for _, ch := range bytes { if ch < 0x20 || ch > 0x7E { return false } } return true } func annotateContext(ctx context.Context, mux *ServeMux, req *http.Request, rpcMethodName string, options ...AnnotateContextOption) (context.Context, metadata.MD, error) { ctx = withRPCMethod(ctx, rpcMethodName) for _, o := range options { ctx = o(ctx) } timeout := DefaultContextTimeout if tm := req.Header.Get(metadataGrpcTimeout); tm != "" { var err error timeout, err = timeoutDecode(tm) if err != nil { return nil, nil, status.Errorf(codes.InvalidArgument, "invalid grpc-timeout: %s", tm) } } // Each forwarded header contributes a key and a value, and the two X-Forwarded-* pairs the // gateway derives below add two more. Sizing for that up front replaces the handful of // reallocations an empty slice goes through while it doubles. pairs := make([]string, 0, 2*(len(req.Header)+2)) // For backwards-compatibility, pass through 'authorization' header with no prefix. // It is added before the header loop so that it is always the first // "authorization" value: the loop iterates a map, so a header the matcher // forwards under the same key (Grpc-Metadata-Authorization with the default // matcher) would otherwise land ahead of it in random order, and a backend // reading the first value would get the client-forwarded one. for _, val := range req.Header.Values("Authorization") { if isValidGRPCMetadataTextValue(val) { pairs = append(pairs, "authorization", val) } } for key, vals := range req.Header { key = textproto.CanonicalMIMEHeaderKey(key) switch key { case xForwardedFor, xForwardedHost: // Handled separately below continue } for _, val := range vals { if h, ok := mux.incomingHeaderMatcher(key); ok { // The gateway derives x-forwarded-host and x-forwarded-for below from // the trusted request (Host / X-Forwarded-* / RemoteAddr), and backends // rely on those metadata keys for client host/IP decisions. Refuse a // forwarded header that the matcher strips into one of them (e.g. // Grpc-Metadata-X-Forwarded-For) so a client cannot slip a spoofed value // in ahead of the gateway-set one. if strings.EqualFold(h, xForwardedFor) || strings.EqualFold(h, xForwardedHost) { grpclog.Errorf("HTTP header %q maps to gateway-reserved metadata key %q; skipping", key, h) continue } if !isValidGRPCMetadataKey(h) { grpclog.Errorf("HTTP header name %q is not valid as gRPC metadata key; skipping", h) continue } // Handles "-bin" metadata in grpc, since grpc will do another base64 // encode before sending to server, we need to decode it first. if strings.HasSuffix(key, metadataHeaderBinarySuffix) { b, err := decodeBinHeader(val) if err != nil { return nil, nil, status.Errorf(codes.InvalidArgument, "invalid binary header %s: %s", key, err) } val = string(b) } else if !isValidGRPCMetadataTextValue(val) { grpclog.Errorf("Value of HTTP header %q contains non-ASCII value (not valid as gRPC metadata): skipping", h) continue } pairs = append(pairs, h, val) } } } if host := req.Header.Get(xForwardedHost); host != "" && isValidGRPCMetadataTextValue(host) { pairs = append(pairs, xForwardedHostKey, host) } else if req.Host != "" && isValidGRPCMetadataTextValue(req.Host) { pairs = append(pairs, xForwardedHostKey, req.Host) } // Validate each X-Forwarded-For value on its own, so that an invalid // client-supplied value is dropped without also dropping the remote // address the gateway appends. var xff []string for _, value := range req.Header.Values(xForwardedFor) { if isValidGRPCMetadataTextValue(value) { xff = append(xff, value) } } if addr := req.RemoteAddr; addr != "" { if remoteIP, _, err := net.SplitHostPort(addr); err == nil && isValidGRPCMetadataTextValue(remoteIP) { xff = append(xff, remoteIP) } } if len(xff) > 0 { pairs = append(pairs, xForwardedForKey, strings.Join(xff, ", ")) } if timeout != 0 { ctx, _ = context.WithTimeout(ctx, timeout) } md := metadata.Pairs(pairs...) var annotated metadata.MD for _, mda := range mux.metadataAnnotators { annotated = metadata.Join(annotated, mda(ctx, req)) } // Annotators are the gateway's own code, so their values replace whatever // the request headers put under the same key. Joining them after the // header values would let a client send Grpc-Metadata- and have its // value read first by a backend that trusts the annotator. // // With no annotators registered -- the default -- there is nothing to merge, and // metadata.Join would rebuild the whole map for nothing: it starts from an empty MD and // appends every key's values into a fresh slice, so the result is an identical copy of md // bought with one allocation per key. if len(annotated) > 0 { for k := range annotated { delete(md, strings.ToLower(k)) } md = metadata.Join(md, annotated) } if len(md) == 0 { return ctx, nil, nil } return ctx, md, nil } // ServerMetadata consists of metadata sent from gRPC server. type ServerMetadata struct { HeaderMD metadata.MD TrailerMD metadata.MD } type serverMetadataKey struct{} // NewServerMetadataContext creates a new context with ServerMetadata func NewServerMetadataContext(ctx context.Context, md ServerMetadata) context.Context { if ctx == nil { ctx = context.Background() } return context.WithValue(ctx, serverMetadataKey{}, md) } // ServerMetadataFromContext returns the ServerMetadata in ctx func ServerMetadataFromContext(ctx context.Context) (md ServerMetadata, ok bool) { if ctx == nil { return md, false } md, ok = ctx.Value(serverMetadataKey{}).(ServerMetadata) return } // ServerTransportStream implements grpc.ServerTransportStream. // It should only be used by the generated files to support grpc.SendHeader // outside of gRPC server use. type ServerTransportStream struct { mu sync.Mutex header metadata.MD trailer metadata.MD } // Method returns the method for the stream. func (s *ServerTransportStream) Method() string { return "" } // Header returns the header metadata of the stream. func (s *ServerTransportStream) Header() metadata.MD { s.mu.Lock() defer s.mu.Unlock() return s.header.Copy() } // SetHeader sets the header metadata. func (s *ServerTransportStream) SetHeader(md metadata.MD) error { if md.Len() == 0 { return nil } s.mu.Lock() s.header = metadata.Join(s.header, md) s.mu.Unlock() return nil } // SendHeader sets the header metadata. func (s *ServerTransportStream) SendHeader(md metadata.MD) error { return s.SetHeader(md) } // Trailer returns the cached trailer metadata. func (s *ServerTransportStream) Trailer() metadata.MD { s.mu.Lock() defer s.mu.Unlock() return s.trailer.Copy() } // SetTrailer sets the trailer metadata. func (s *ServerTransportStream) SetTrailer(md metadata.MD) error { if md.Len() == 0 { return nil } s.mu.Lock() s.trailer = metadata.Join(s.trailer, md) s.mu.Unlock() return nil } func timeoutDecode(s string) (time.Duration, error) { size := len(s) if size < 2 { return 0, fmt.Errorf("timeout string is too short: %q", s) } d, ok := timeoutUnitToDuration(s[size-1]) if !ok { return 0, fmt.Errorf("timeout unit is not recognized: %q", s) } t, err := strconv.ParseUint(s[:size-1], 10, 64) if err != nil { return 0, err } // Guard against overflowing time.Duration, which is an int64 nanosecond // count: without this a large value wraps to a bogus (often negative) // deadline instead of the long timeout the client asked for. if t > uint64(math.MaxInt64)/uint64(d) { return time.Duration(math.MaxInt64), nil } return d * time.Duration(t), nil } func timeoutUnitToDuration(u uint8) (d time.Duration, ok bool) { switch u { case 'H': return time.Hour, true case 'M': return time.Minute, true case 'S': return time.Second, true case 'm': return time.Millisecond, true case 'u': return time.Microsecond, true case 'n': return time.Nanosecond, true default: return } } // isPermanentHTTPHeader checks whether hdr belongs to the list of // permanent request headers maintained by IANA. // http://www.iana.org/assignments/message-headers/message-headers.xml func isPermanentHTTPHeader(hdr string) bool { switch hdr { case "Accept", "Accept-Charset", "Accept-Language", "Accept-Ranges", "Authorization", "Cache-Control", "Content-Type", "Cookie", "Date", "Expect", "From", "Host", "If-Match", "If-Modified-Since", "If-None-Match", "If-Schedule-Tag-Match", "If-Unmodified-Since", "Max-Forwards", "Origin", "Pragma", "Referer", "User-Agent", "Via", "Warning": return true } return false } // isMalformedHTTPHeader checks whether header belongs to the list of // "malformed headers" and would be rejected by the gRPC server. func isMalformedHTTPHeader(header string) bool { _, isMalformed := malformedHTTPHeaders[strings.ToLower(header)] return isMalformed } // RPCMethod returns the method string for the server context. The returned // string is in the format of "/package.service/method". func RPCMethod(ctx context.Context) (string, bool) { m := ctx.Value(rpcMethodKey{}) if m == nil { return "", false } ms, ok := m.(string) if !ok { return "", false } return ms, true } func withRPCMethod(ctx context.Context, rpcMethodName string) context.Context { return context.WithValue(ctx, rpcMethodKey{}, rpcMethodName) } // HTTPPathPattern returns the HTTP path pattern string relating to the HTTP handler, if one exists. // The format of the returned string is defined by the google.api.http path template type. func HTTPPathPattern(ctx context.Context) (string, bool) { m := ctx.Value(httpPathPatternKey{}) if m == nil { return "", false } ms, ok := m.(string) if !ok { return "", false } return ms, true } func withHTTPPathPattern(ctx context.Context, httpPathPattern string) context.Context { return context.WithValue(ctx, httpPathPatternKey{}, httpPathPattern) } // HTTPPattern returns the HTTP path pattern struct relating to the HTTP handler, if one exists. func HTTPPattern(ctx context.Context) (Pattern, bool) { v, ok := ctx.Value(httpPatternKey{}).(Pattern) return v, ok } func withHTTPPattern(ctx context.Context, httpPattern Pattern) context.Context { return context.WithValue(ctx, httpPatternKey{}, httpPattern) }