|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2026 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package test |
| 20 | + |
| 21 | +import ( |
| 22 | + "bytes" |
| 23 | + "context" |
| 24 | + "net" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "golang.org/x/net/http2" |
| 28 | + "golang.org/x/net/http2/hpack" |
| 29 | + "google.golang.org/grpc/internal/envconfig" |
| 30 | + "google.golang.org/grpc/internal/stubserver" |
| 31 | + "google.golang.org/grpc/internal/testutils" |
| 32 | + |
| 33 | + testpb "google.golang.org/grpc/interop/grpc_testing" |
| 34 | +) |
| 35 | + |
| 36 | +// TestMalformedMethodPath tests that the server responds with Unimplemented |
| 37 | +// when the method path is malformed. This verifies that the server does not |
| 38 | +// route requests with a malformed method path to the application handler. |
| 39 | +func (s) TestMalformedMethodPath(t *testing.T) { |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + path string |
| 43 | + envVar bool |
| 44 | + wantStatus string // string representation of codes.Code |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "missing_leading_slash_disableStrictPathChecking_false", |
| 48 | + path: "grpc.testing.TestService/UnaryCall", |
| 49 | + wantStatus: "12", // Unimplemented |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "empty_path_disableStrictPathChecking_false", |
| 53 | + path: "", |
| 54 | + wantStatus: "12", // Unimplemented |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "just_slash_disableStrictPathChecking_false", |
| 58 | + path: "/", |
| 59 | + wantStatus: "12", // Unimplemented |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "missing_leading_slash_disableStrictPathChecking_true", |
| 63 | + path: "grpc.testing.TestService/UnaryCall", |
| 64 | + envVar: true, |
| 65 | + wantStatus: "0", // OK |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "empty_path_disableStrictPathChecking_true", |
| 69 | + path: "", |
| 70 | + envVar: true, |
| 71 | + wantStatus: "12", // Unimplemented |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "just_slash_disableStrictPathChecking_true", |
| 75 | + path: "/", |
| 76 | + envVar: true, |
| 77 | + wantStatus: "12", // Unimplemented |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + for _, tc := range tests { |
| 82 | + t.Run(tc.name, func(t *testing.T) { |
| 83 | + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 84 | + defer cancel() |
| 85 | + |
| 86 | + testutils.SetEnvConfig(t, &envconfig.DisableStrictPathChecking, tc.envVar) |
| 87 | + |
| 88 | + ss := &stubserver.StubServer{ |
| 89 | + UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { |
| 90 | + return &testpb.SimpleResponse{Payload: &testpb.Payload{Body: []byte("pwned")}}, nil |
| 91 | + }, |
| 92 | + } |
| 93 | + if err := ss.Start(nil); err != nil { |
| 94 | + t.Fatalf("Error starting endpoint server: %v", err) |
| 95 | + } |
| 96 | + defer ss.Stop() |
| 97 | + |
| 98 | + // Open a raw TCP connection to the server and speak HTTP/2 directly. |
| 99 | + tcpConn, err := net.Dial("tcp", ss.Address) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("Failed to dial tcp: %v", err) |
| 102 | + } |
| 103 | + defer tcpConn.Close() |
| 104 | + |
| 105 | + // Write the HTTP/2 connection preface and the initial settings frame. |
| 106 | + if _, err := tcpConn.Write([]byte("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")); err != nil { |
| 107 | + t.Fatalf("Failed to write preface: %v", err) |
| 108 | + } |
| 109 | + framer := http2.NewFramer(tcpConn, tcpConn) |
| 110 | + if err := framer.WriteSettings(); err != nil { |
| 111 | + t.Fatalf("Failed to write settings: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + // Encode and write the HEADERS frame. |
| 115 | + var headerBuf bytes.Buffer |
| 116 | + enc := hpack.NewEncoder(&headerBuf) |
| 117 | + writeHeader := func(name, value string) { |
| 118 | + enc.WriteField(hpack.HeaderField{Name: name, Value: value}) |
| 119 | + } |
| 120 | + writeHeader(":method", "POST") |
| 121 | + writeHeader(":scheme", "http") |
| 122 | + writeHeader(":authority", ss.Address) |
| 123 | + writeHeader(":path", tc.path) |
| 124 | + writeHeader("content-type", "application/grpc") |
| 125 | + writeHeader("te", "trailers") |
| 126 | + if err := framer.WriteHeaders(http2.HeadersFrameParam{ |
| 127 | + StreamID: 1, |
| 128 | + BlockFragment: headerBuf.Bytes(), |
| 129 | + EndStream: false, |
| 130 | + EndHeaders: true, |
| 131 | + }); err != nil { |
| 132 | + t.Fatalf("Failed to write headers: %v", err) |
| 133 | + } |
| 134 | + |
| 135 | + // Send a small gRPC-encoded data frame (0 length). |
| 136 | + if err := framer.WriteData(1, true, []byte{0, 0, 0, 0, 0}); err != nil { |
| 137 | + t.Fatalf("Failed to write data: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + // Read responses and look for grpc-status. |
| 141 | + gotStatus := "" |
| 142 | + dec := hpack.NewDecoder(4096, func(f hpack.HeaderField) { |
| 143 | + if f.Name == "grpc-status" { |
| 144 | + gotStatus = f.Value |
| 145 | + } |
| 146 | + }) |
| 147 | + done := make(chan struct{}) |
| 148 | + go func() { |
| 149 | + defer close(done) |
| 150 | + for { |
| 151 | + frame, err := framer.ReadFrame() |
| 152 | + if err != nil { |
| 153 | + return |
| 154 | + } |
| 155 | + if headers, ok := frame.(*http2.HeadersFrame); ok { |
| 156 | + if _, err := dec.Write(headers.HeaderBlockFragment()); err != nil { |
| 157 | + return |
| 158 | + } |
| 159 | + if headers.StreamEnded() { |
| 160 | + return |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + }() |
| 165 | + |
| 166 | + select { |
| 167 | + case <-done: |
| 168 | + case <-ctx.Done(): |
| 169 | + t.Fatalf("Timed out waiting for response") |
| 170 | + } |
| 171 | + |
| 172 | + if gotStatus != tc.wantStatus { |
| 173 | + t.Errorf("Got grpc-status %v, want %v", gotStatus, tc.wantStatus) |
| 174 | + } |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
0 commit comments