xref: /aosp_15_r20/external/grpc-grpc/test/core/bad_client/tests/headers.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 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 #include <grpc/grpc.h>
20 #include <grpc/support/log.h>
21 
22 #include "src/core/lib/surface/server.h"
23 #include "test/core/bad_client/bad_client.h"
24 #include "test/core/util/test_config.h"
25 
26 #define PFX_STR                      \
27   "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
28   "\x00\x00\x00\x04\x00\x00\x00\x00\x00"
29 
verifier(grpc_server * server,grpc_completion_queue * cq,void *)30 static void verifier(grpc_server* server, grpc_completion_queue* cq,
31                      void* /*registered_method*/) {
32   while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
33     GPR_ASSERT(grpc_completion_queue_next(
34                    cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
35                    .type == GRPC_QUEUE_TIMEOUT);
36   }
37 }
38 
main(int argc,char ** argv)39 int main(int argc, char** argv) {
40   grpc::testing::TestEnvironment env(&argc, argv);
41   grpc_init();
42 
43   // partial http2 header prefixes
44   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00",
45                            GRPC_BAD_CLIENT_DISCONNECT);
46   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00",
47                            GRPC_BAD_CLIENT_DISCONNECT);
48   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00",
49                            GRPC_BAD_CLIENT_DISCONNECT);
50   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01",
51                            GRPC_BAD_CLIENT_DISCONNECT);
52   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01\x00",
53                            GRPC_BAD_CLIENT_DISCONNECT);
54   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01\x04",
55                            GRPC_BAD_CLIENT_DISCONNECT);
56   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01\x05",
57                            GRPC_BAD_CLIENT_DISCONNECT);
58   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
59                            PFX_STR "\x00\x00\x00\x01\x04\x00",
60                            GRPC_BAD_CLIENT_DISCONNECT);
61   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
62                            PFX_STR "\x00\x00\x00\x01\x04\x00\x00",
63                            GRPC_BAD_CLIENT_DISCONNECT);
64   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
65                            PFX_STR "\x00\x00\x00\x01\x04\x00\x00\x00",
66                            GRPC_BAD_CLIENT_DISCONNECT);
67   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
68                            PFX_STR "\x00\x00\x00\x01\x04\x00\x00\x00\x00",
69                            GRPC_BAD_CLIENT_DISCONNECT);
70   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
71                            PFX_STR "\x00\x00\x00\x01\x04\x00\x00\x00\x01",
72                            GRPC_BAD_CLIENT_DISCONNECT);
73 
74   // test adding prioritization data
75   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
76                            PFX_STR
77                            "\x00\x00\x01\x01\x24\x00\x00\x00\x01"
78                            "\x00",
79                            0);
80   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
81                            PFX_STR
82                            "\x00\x00\x02\x01\x24\x00\x00\x00\x01"
83                            "\x00\x00",
84                            0);
85   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
86                            PFX_STR
87                            "\x00\x00\x03\x01\x24\x00\x00\x00\x01"
88                            "\x00\x00\x00",
89                            0);
90   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
91                            PFX_STR
92                            "\x00\x00\x04\x01\x24\x00\x00\x00\x01"
93                            "\x00\x00\x00\x00",
94                            0);
95   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
96                            PFX_STR
97                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
98                            "",
99                            GRPC_BAD_CLIENT_DISCONNECT);
100   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
101                            PFX_STR
102                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
103                            "\x00",
104                            GRPC_BAD_CLIENT_DISCONNECT);
105   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
106                            PFX_STR
107                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
108                            "\x00\x00",
109                            GRPC_BAD_CLIENT_DISCONNECT);
110   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
111                            PFX_STR
112                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
113                            "\x00\x00\x00",
114                            GRPC_BAD_CLIENT_DISCONNECT);
115   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
116                            PFX_STR
117                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
118                            "\x00\x00\x00\x00",
119                            GRPC_BAD_CLIENT_DISCONNECT);
120   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
121                            PFX_STR
122                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
123                            "\x00\x00\x00\x00\x00",
124                            GRPC_BAD_CLIENT_DISCONNECT);
125 
126   // test looking up an invalid index
127   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
128                            PFX_STR
129                            "\x00\x00\x01\x01\x04\x00\x00\x00\x01"
130                            "\xfe",
131                            0);
132   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
133                            PFX_STR
134                            "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
135                            "\x7f\x7f\x01"
136                            "a",
137                            0);
138   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
139                            PFX_STR
140                            "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
141                            "\x0f\x7f\x01"
142                            "a",
143                            0);
144   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
145                            PFX_STR
146                            "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
147                            "\x1f\x7f\x01"
148                            "a",
149                            0);
150   // test nvr, not indexed in static table
151   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
152                            PFX_STR
153                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
154                            "\x01\x01"
155                            "a",
156                            GRPC_BAD_CLIENT_DISCONNECT);
157   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
158                            PFX_STR
159                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
160                            "\x11\x01"
161                            "a",
162                            GRPC_BAD_CLIENT_DISCONNECT);
163   // illegal op code
164   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
165                            PFX_STR
166                            "\x00\x00\x01\x01\x04\x00\x00\x00\x01"
167                            "\x80",
168                            0);
169   // parse some long indices
170   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
171                            PFX_STR
172                            "\x00\x00\x02\x01\x04\x00\x00\x00\x01"
173                            "\xff\x00",
174                            0);
175   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
176                            PFX_STR
177                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
178                            "\xff\x80\x00",
179                            0);
180   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
181                            PFX_STR
182                            "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
183                            "\xff\x80\x80\x00",
184                            0);
185   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
186                            PFX_STR
187                            "\x00\x00\x05\x01\x04\x00\x00\x00\x01"
188                            "\xff\x80\x80\x80\x00",
189                            0);
190   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
191                            PFX_STR
192                            "\x00\x00\x06\x01\x04\x00\x00\x00\x01"
193                            "\xff\x80\x80\x80\x80\x00",
194                            0);
195   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
196                            PFX_STR
197                            "\x00\x00\x07\x01\x04\x00\x00\x00\x01"
198                            "\xff\x80\x80\x80\x80\x80\x00",
199                            0);
200   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
201                            PFX_STR
202                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
203                            "\xff",
204                            GRPC_BAD_CLIENT_DISCONNECT);
205   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
206                            PFX_STR
207                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
208                            "\xff\x80",
209                            GRPC_BAD_CLIENT_DISCONNECT);
210   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
211                            PFX_STR
212                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
213                            "\xff\x80\x80",
214                            GRPC_BAD_CLIENT_DISCONNECT);
215   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
216                            PFX_STR
217                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
218                            "\xff\x80\x80\x80",
219                            GRPC_BAD_CLIENT_DISCONNECT);
220   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
221                            PFX_STR
222                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
223                            "\xff\x80\x80\x80\x80",
224                            GRPC_BAD_CLIENT_DISCONNECT);
225   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
226                            PFX_STR
227                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
228                            "\xff\x80\x80\x80\x80\x80",
229                            GRPC_BAD_CLIENT_DISCONNECT);
230   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
231                            PFX_STR
232                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
233                            "\xff\x80\x80\x80\x80\x80\x80",
234                            GRPC_BAD_CLIENT_DISCONNECT);
235   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
236                            PFX_STR
237                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
238                            "\xff\x80\x80\x80\x80\x80\x80\x00",
239                            0);
240   // overflow on byte 4
241   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
242                            PFX_STR
243                            "\x00\x00\x06\x01\x04\x00\x00\x00\x01"
244                            "\xff\x80\x80\x80\x80\x7f",
245                            GRPC_BAD_CLIENT_DISCONNECT);
246   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
247                            PFX_STR
248                            "\x00\x00\x06\x01\x04\x00\x00\x00\x01"
249                            "\xff\xff\xff\xff\xff\x0f",
250                            GRPC_BAD_CLIENT_DISCONNECT);
251   // overflow after byte 4
252   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
253                            PFX_STR
254                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
255                            "\xff\x80\x80\x80\x80\x80\x80\x02",
256                            0);
257   // end of headers mid-opcode
258   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
259                            PFX_STR
260                            "\x00\x00\x01\x01\x04\x00\x00\x00\x01"
261                            "\x01",
262                            GRPC_BAD_CLIENT_DISCONNECT);
263 
264   // dynamic table size update: set to default
265   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
266                            PFX_STR
267                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
268                            "\x3f\xe1\x1f",
269                            GRPC_BAD_CLIENT_DISCONNECT);
270   // dynamic table size update: set too large
271   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
272                            PFX_STR
273                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
274                            "\x3f\xf1\x1f",
275                            0);
276   // dynamic table size update: set twice
277   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
278                            PFX_STR
279                            "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
280                            "\x20\x3f\xe1\x1f",
281                            GRPC_BAD_CLIENT_DISCONNECT);
282   // dynamic table size update: set thrice
283   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
284                            PFX_STR
285                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
286                            "\x20\x20\x20",
287                            0);
288 
289   // non-ending header followed by continuation frame
290   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
291                            PFX_STR
292                            "\x00\x00\x00\x01\x00\x00\x00\x00\x01"
293                            "\x00\x00\x00\x09\x04\x00\x00\x00\x01",
294                            GRPC_BAD_CLIENT_DISCONNECT);
295   // non-ending header followed by non-continuation frame
296   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
297                            PFX_STR
298                            "\x00\x00\x00\x01\x00\x00\x00\x00\x01"
299                            "\x00\x00\x00\x00\x04\x00\x00\x00\x01",
300                            0);
301   // non-ending header followed by a continuation frame for a different stream
302   //
303   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
304                            PFX_STR
305                            "\x00\x00\x00\x01\x04\x00\x00\x00\x01"
306                            "\x00\x00\x00\x01\x00\x00\x00\x00\x03"
307                            "\x00\x00\x00\x09\x04\x00\x00\x00\x01",
308                            0);
309   // opening with a continuation frame
310   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
311                            PFX_STR "\x00\x00\x00\x09\x04\x00\x00\x00\x01", 0);
312   // three header frames
313   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
314                            PFX_STR
315                            "\x00\x00\x00\x01\x04\x00\x00\x00\x01"
316                            "\x00\x00\x00\x01\x04\x00\x00\x00\x01"
317                            "\x00\x00\x00\x01\x04\x00\x00\x00\x01",
318                            GRPC_BAD_CLIENT_DISCONNECT);
319 
320   // an invalid header found with fuzzing
321   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
322                            PFX_STR "\x00\x00\x00\x01\x39\x67\xed\x1d\x64",
323                            GRPC_BAD_CLIENT_DISCONNECT);
324 
325   // a badly encoded timeout value
326   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
327                            PFX_STR
328                            "\x00\x00\x19\x01\x04\x00\x00\x00\x01"
329                            "\x10\x0cgrpc-timeout\x0a"
330                            "15 seconds",
331                            GRPC_BAD_CLIENT_DISCONNECT);
332   // a badly encoded timeout value: twice (catches caching)
333   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
334                            PFX_STR
335                            "\x00\x00\x19\x01\x04\x00\x00\x00\x01"
336                            "\x10\x0cgrpc-timeout\x0a"
337                            "15 seconds"
338                            "\x00\x00\x19\x01\x04\x00\x00\x00\x03"
339                            "\x10\x0cgrpc-timeout\x0a"
340                            "15 seconds",
341                            GRPC_BAD_CLIENT_DISCONNECT);
342 
343   grpc_shutdown();
344   return 0;
345 }
346