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 "src/core/ext/transport/chttp2/transport/hpack_parser.h"
20
21 #include <grpc/event_engine/memory_allocator.h>
22 #include <grpc/grpc.h>
23 #include <grpc/slice.h>
24 #include <grpc/status.h>
25 #include <grpc/support/alloc.h>
26
27 #include <memory>
28 #include <string>
29
30 #include "absl/cleanup/cleanup.h"
31 #include "absl/random/random.h"
32 #include "absl/status/status.h"
33 #include "absl/status/statusor.h"
34 #include "absl/strings/str_cat.h"
35 #include "absl/types/optional.h"
36 #include "gmock/gmock.h"
37 #include "gtest/gtest.h"
38 #include "src/core/lib/iomgr/exec_ctx.h"
39 #include "src/core/lib/resource_quota/arena.h"
40 #include "src/core/lib/resource_quota/memory_quota.h"
41 #include "src/core/lib/resource_quota/resource_quota.h"
42 #include "src/core/lib/slice/slice.h"
43 #include "src/core/lib/transport/error_utils.h"
44 #include "src/core/util/ref_counted_ptr.h"
45 #include "src/core/util/status_helper.h"
46 #include "src/core/util/time.h"
47 #include "test/core/test_util/parse_hexstring.h"
48 #include "test/core/test_util/slice_splitter.h"
49 #include "test/core/test_util/test_config.h"
50
51 namespace grpc_core {
52 namespace {
53
54 const uint32_t kFailureIsConnectionError = 1;
55 const uint32_t kWithPriority = 2;
56 const uint32_t kEndOfStream = 4;
57 const uint32_t kEndOfHeaders = 8;
58
59 struct TestInput {
60 std::string input;
61 absl::StatusOr<std::string> expected_parse;
62 uint32_t flags;
63 };
64
65 struct Test {
66 std::string name;
67 absl::optional<size_t> table_size;
68 absl::optional<size_t> max_metadata_size;
69 std::vector<TestInput> inputs;
70 };
71
72 // Produce a nice name to print next to each test case for gtest.
NameFromConfig(const::testing::TestParamInfo<Test> & config)73 inline const char* NameFromConfig(
74 const ::testing::TestParamInfo<Test>& config) {
75 return config.param.name.c_str();
76 }
77
78 class ParseTest : public ::testing::TestWithParam<Test> {
79 public:
ParseTest()80 ParseTest() { grpc_init(); }
81
~ParseTest()82 ~ParseTest() override {
83 {
84 ExecCtx exec_ctx;
85 parser_.reset();
86 }
87
88 grpc_shutdown();
89 }
90
SetUp()91 void SetUp() override {
92 parser_ = std::make_unique<HPackParser>();
93 if (GetParam().table_size.has_value()) {
94 parser_->hpack_table()->SetMaxBytes(GetParam().table_size.value());
95 EXPECT_TRUE(parser_->hpack_table()->SetCurrentTableSize(
96 GetParam().table_size.value()));
97 }
98 }
99
IsStreamError(const absl::Status & status)100 static bool IsStreamError(const absl::Status& status) {
101 intptr_t stream_id;
102 return grpc_error_get_int(status, StatusIntProperty::kStreamId, &stream_id);
103 }
104
TestVector(grpc_slice_split_mode mode,absl::optional<size_t> max_metadata_size,std::string hexstring,absl::StatusOr<std::string> expect,uint32_t flags)105 void TestVector(grpc_slice_split_mode mode,
106 absl::optional<size_t> max_metadata_size,
107 std::string hexstring, absl::StatusOr<std::string> expect,
108 uint32_t flags) {
109 ExecCtx exec_ctx;
110 auto input = ParseHexstring(hexstring);
111 grpc_slice* slices;
112 size_t nslices;
113 size_t i;
114 absl::BitGen bitgen;
115
116 grpc_metadata_batch b;
117
118 parser_->BeginFrame(
119 &b, max_metadata_size.value_or(4096), max_metadata_size.value_or(4096),
120 (flags & kEndOfStream)
121 ? HPackParser::Boundary::EndOfStream
122 : ((flags & kEndOfHeaders) ? HPackParser::Boundary::EndOfHeaders
123 : HPackParser::Boundary::None),
124 flags & kWithPriority ? HPackParser::Priority::Included
125 : HPackParser::Priority::None,
126 HPackParser::LogInfo{1, HPackParser::LogInfo::kHeaders, false});
127
128 grpc_split_slices(mode, const_cast<grpc_slice*>(&input.c_slice()), 1,
129 &slices, &nslices);
130 auto cleanup_slices = absl::MakeCleanup([slices, nslices] {
131 for (size_t i = 0; i < nslices; i++) {
132 grpc_slice_unref(slices[i]);
133 }
134 gpr_free(slices);
135 });
136
137 bool saw_error = false;
138 for (i = 0; i < nslices; i++) {
139 ExecCtx exec_ctx;
140 auto err =
141 parser_->Parse(slices[i], i == nslices - 1, absl::BitGenRef(bitgen),
142 /*call_tracer=*/nullptr);
143 if (!err.ok() && (flags & kFailureIsConnectionError) == 0) {
144 EXPECT_TRUE(IsStreamError(err)) << err;
145 }
146 if (!saw_error && !err.ok()) {
147 // one byte at a time mode might fail with a stream error early
148 if (mode == GRPC_SLICE_SPLIT_ONE_BYTE &&
149 (flags & kFailureIsConnectionError) && IsStreamError(err)) {
150 continue;
151 }
152 grpc_status_code code;
153 std::string message;
154 grpc_error_get_status(err, Timestamp::InfFuture(), &code, &message,
155 nullptr, nullptr);
156 EXPECT_EQ(code, static_cast<grpc_status_code>(expect.status().code()))
157 << err << " slice[" << i << "]; input: " << hexstring
158 << "\nTABLE:\n"
159 << parser_->hpack_table()->TestOnlyDynamicTableAsString();
160 EXPECT_THAT(message, ::testing::HasSubstr(expect.status().message()))
161 << err << " slice[" << i << "]; input: " << hexstring
162 << "\nTABLE:\n"
163 << parser_->hpack_table()->TestOnlyDynamicTableAsString();
164 saw_error = true;
165 if (flags & kFailureIsConnectionError) return;
166 }
167 }
168
169 if (!saw_error) {
170 EXPECT_TRUE(expect.ok()) << expect.status();
171 }
172
173 if (expect.ok()) {
174 TestEncoder encoder;
175 b.Encode(&encoder);
176 EXPECT_EQ(encoder.result(), *expect) << "Input: " << hexstring;
177 }
178 }
179
180 private:
181 class TestEncoder {
182 public:
result()183 std::string result() { return out_; }
184
Encode(const Slice & key,const Slice & value)185 void Encode(const Slice& key, const Slice& value) {
186 out_.append(absl::StrCat(key.as_string_view(), ": ",
187 value.as_string_view(), "\n"));
188 }
189
190 template <typename T, typename V>
Encode(T,const V & v)191 void Encode(T, const V& v) {
192 out_.append(
193 absl::StrCat(T::key(), ": ", T::Encode(v).as_string_view(), "\n"));
194 }
195
196 private:
197 std::string out_;
198 };
199
200 std::unique_ptr<HPackParser> parser_;
201 };
202
TEST_P(ParseTest,WholeSlices)203 TEST_P(ParseTest, WholeSlices) {
204 for (const auto& input : GetParam().inputs) {
205 TestVector(GRPC_SLICE_SPLIT_MERGE_ALL, GetParam().max_metadata_size,
206 input.input, input.expected_parse, input.flags);
207 }
208 }
209
TEST_P(ParseTest,OneByteAtATime)210 TEST_P(ParseTest, OneByteAtATime) {
211 for (const auto& input : GetParam().inputs) {
212 TestVector(GRPC_SLICE_SPLIT_ONE_BYTE, GetParam().max_metadata_size,
213 input.input, input.expected_parse, input.flags);
214 }
215 }
216
217 INSTANTIATE_TEST_SUITE_P(
218 ParseTest, ParseTest,
219 ::testing::Values(
220 Test{"RfcTestD2",
221 {},
222 {},
223 {
224 // D.2.1
225 {"400a 6375 7374 6f6d 2d6b 6579 0d63 7573"
226 "746f 6d2d 6865 6164 6572",
227 "custom-key: custom-header\n", 0},
228 // D.2.2
229 {"040c 2f73 616d 706c 652f 7061 7468", ":path: /sample/path\n",
230 0},
231 // D.2.3
232 {"1008 7061 7373 776f 7264 0673 6563 7265"
233 "74",
234 "password: secret\n", 0},
235 // D.2.4
236 {"82", ":method: GET\n", 0},
237 }},
238 Test{"RfcTestD3",
239 {},
240 {},
241 {
242 // D.3.1
243 {"8286 8441 0f77 7777 2e65 7861 6d70 6c65"
244 "2e63 6f6d",
245 ":path: /\n"
246 ":authority: www.example.com\n"
247 ":method: GET\n"
248 ":scheme: http\n",
249 0},
250 // D.3.2
251 {"8286 84be 5808 6e6f 2d63 6163 6865",
252 ":path: /\n"
253 ":authority: www.example.com\n"
254 ":method: GET\n"
255 ":scheme: http\n"
256 "cache-control: no-cache\n",
257 0},
258 // D.3.3
259 {"8287 85bf 400a 6375 7374 6f6d 2d6b 6579"
260 "0c63 7573 746f 6d2d 7661 6c75 65",
261 ":path: /index.html\n"
262 ":authority: www.example.com\n"
263 ":method: GET\n"
264 ":scheme: https\n"
265 "custom-key: custom-value\n",
266 0},
267 }},
268 Test{"RfcTestD4",
269 {},
270 {},
271 {
272 // D.4.1
273 {"8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4"
274 "ff",
275 ":path: /\n"
276 ":authority: www.example.com\n"
277 ":method: GET\n"
278 ":scheme: http\n",
279 0},
280 // D.4.2
281 {"8286 84be 5886 a8eb 1064 9cbf",
282 ":path: /\n"
283 ":authority: www.example.com\n"
284 ":method: GET\n"
285 ":scheme: http\n"
286 "cache-control: no-cache\n",
287 0},
288 // D.4.3
289 {"8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925"
290 "a849 e95b b8e8 b4bf",
291 ":path: /index.html\n"
292 ":authority: www.example.com\n"
293 ":method: GET\n"
294 ":scheme: https\n"
295 "custom-key: custom-value\n",
296 0},
297 }},
298 Test{"RfcTestD5",
299 {256},
300 {},
301 {
302 // D.5.1
303 {"4803 3330 3258 0770 7269 7661 7465 611d"
304 "4d6f 6e2c 2032 3120 4f63 7420 3230 3133"
305 "2032 303a 3133 3a32 3120 474d 546e 1768"
306 "7474 7073 3a2f 2f77 7777 2e65 7861 6d70"
307 "6c65 2e63 6f6d",
308 ":status: 302\n"
309 "cache-control: private\n"
310 "date: Mon, 21 Oct 2013 20:13:21 GMT\n"
311 "location: https://www.example.com\n",
312 0},
313 // D.5.2
314 {"4803 3330 37c1 c0bf",
315 ":status: 307\n"
316 "cache-control: private\n"
317 "date: Mon, 21 Oct 2013 20:13:21 GMT\n"
318 "location: https://www.example.com\n",
319 0},
320 // D.5.3
321 {"88c1 611d 4d6f 6e2c 2032 3120 4f63 7420"
322 "3230 3133 2032 303a 3133 3a32 3220 474d"
323 "54c0 5a04 677a 6970 7738 666f 6f3d 4153"
324 "444a 4b48 514b 425a 584f 5157 454f 5049"
325 "5541 5851 5745 4f49 553b 206d 6178 2d61"
326 "6765 3d33 3630 303b 2076 6572 7369 6f6e"
327 "3d31",
328 ":status: 200\n"
329 "cache-control: private\n"
330 "date: Mon, 21 Oct 2013 20:13:22 GMT\n"
331 "location: https://www.example.com\n"
332 "content-encoding: gzip\n"
333 "set-cookie: foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; "
334 "version=1\n",
335 0},
336 }},
337 Test{"RfcTestD6",
338 {256},
339 {},
340 {
341 // D.6.1
342 {"4882 6402 5885 aec3 771a 4b61 96d0 7abe"
343 "9410 54d4 44a8 2005 9504 0b81 66e0 82a6"
344 "2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8"
345 "e9ae 82ae 43d3",
346 ":status: 302\n"
347 "cache-control: private\n"
348 "date: Mon, 21 Oct 2013 20:13:21 GMT\n"
349 "location: https://www.example.com\n",
350 0},
351 // D.6.2
352 {"4883 640e ffc1 c0bf",
353 ":status: 307\n"
354 "cache-control: private\n"
355 "date: Mon, 21 Oct 2013 20:13:21 GMT\n"
356 "location: https://www.example.com\n",
357 0},
358 // D.6.3
359 {"88c1 6196 d07a be94 1054 d444 a820 0595"
360 "040b 8166 e084 a62d 1bff c05a 839b d9ab"
361 "77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b"
362 "3960 d5af 2708 7f36 72c1 ab27 0fb5 291f"
363 "9587 3160 65c0 03ed 4ee5 b106 3d50 07",
364 ":status: 200\n"
365 "cache-control: private\n"
366 "date: Mon, 21 Oct 2013 20:13:22 GMT\n"
367 "location: https://www.example.com\n"
368 "content-encoding: gzip\n"
369 "set-cookie: foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; "
370 "version=1\n",
371 0},
372 }},
373 Test{"IllegalHpackTableGrowth",
374 {},
375 {1024},
376 {{"3fc43fc4", absl::InternalError("Attempt to make hpack table"),
377 kFailureIsConnectionError}}},
378 Test{"FuzzerFoundInvalidHpackIndexFuzzerFound1",
379 {},
380 {},
381 {{"3ba4a41007f0a40f2d62696e8b632a5b29a40fa4a4281007f0",
382 absl::InternalError("Invalid HPACK index received"),
383 kFailureIsConnectionError}}},
384 Test{"FuzzerFoundMultipleTableSizeChanges1",
385 {},
386 {},
387 {{"2aa41007f0a40f2d62696e8163a41f1f00275bf0692862a4dbf0f00963",
388 absl::InternalError(
389 "More than two max table size changes in a single frame"),
390 kFailureIsConnectionError}}},
391 Test{"FuzzerFoundIllegalHeaderKey1",
392 {},
393 {},
394 {{"2aa41007f0a40f2d62696e8363271f00275bf06928626e2d213fa40fdbf0212"
395 "8215cf00963",
396 absl::InternalError("Illegal header key"), 0}}},
397 Test{"FuzzerFoundMultipleTableSizeChanges2",
398 {},
399 {},
400 {{"a4a41007f0a40f2d62696e8b635b29282d2762696e3b0921213fa41fdbf0211"
401 "007f07b282d62696ef009215c0921e51fe91b3b3f47ed5b282821215cf0",
402 absl::InternalError(
403 "More than two max table size changes in a single frame"),
404 kFailureIsConnectionError}}},
405 Test{
406 "FuzzerFoundInterOverflow1",
407 {},
408 {},
409 {{"696969696969696969696969696969696969696969696969696969696969696"
410 "969696969696969696969696969696969696969696969696969696969696969"
411 "6969696969696969696969696969bababababababababababababababababab"
412 "abababababababababababababababababababababababababababababababa"
413 "bababababababababababababababababababababababababababababababab"
414 "abababababaa4a41007f0a40f2d62696e8bffffffffffffffffffffffffffff"
415 "ffffffffffff632a5b29a428a42d0fdbf027f0628363696e092121",
416 absl::InternalError("integer overflow in hpack integer decoding"),
417 kEndOfHeaders | kFailureIsConnectionError}}},
418 Test{"StatusIsAnInteger",
419 {},
420 {},
421 {{"0e 00 00 df",
422 absl::InternalError("Error parsing ':status' metadata"), 0}}},
423 Test{"BinaryMetadataFromBase64",
424 {},
425 {},
426 {
427 // Binary metadata: created using:
428 // tools/codegen/core/gen_header_frame.py
429 // --compression inc --no_framing --output hexstr
430 // < test/core/transport/chttp2/binary-metadata.headers
431 {"40 09 61 2e 62 2e 63 2d 62 69 6e 0c 62 32 31 6e 4d 6a 41 79 "
432 "4d 51 3d 3d",
433 "a.b.c-bin: omg2021\n", 0},
434 }},
435 Test{"Base64LegalEncoding",
436 {},
437 {},
438 {
439 // Binary metadata: created using:
440 // tools/codegen/core/gen_header_frame.py
441 // --compression inc --no_framing --output hexstr
442 // < test/core/transport/chttp2/bad-base64.headers
443 {"4009612e622e632d62696e1c6c75636b696c7920666f722075732c206974"
444 "27732074756573646179",
445 absl::InternalError("Error parsing 'a.b.c-bin' metadata: "
446 "illegal base64 encoding"),
447 0},
448 {"be",
449 absl::InternalError("Error parsing 'a.b.c-bin' metadata: "
450 "illegal base64 encoding"),
451 kEndOfHeaders},
452 {"82", ":method: GET\n", 0},
453 }},
454 Test{"Base64LegalEncodingWorksAfterFailure",
455 {},
456 {},
457 {
458 // Binary metadata: created using:
459 // tools/codegen/core/gen_header_frame.py
460 // --compression inc --no_framing --output hexstr
461 // < test/core/transport/chttp2/bad-base64.headers
462 {"4009612e622e632d62696e1c6c75636b696c7920666f722075732c206974"
463 "27732074756573646179",
464 absl::InternalError("Error parsing 'a.b.c-bin' metadata: "
465 "illegal base64 encoding"),
466 0},
467 {"be",
468 absl::InternalError("Error parsing 'a.b.c-bin' metadata: "
469 "illegal base64 encoding"),
470 0},
471 {"400e636f6e74656e742d6c656e6774680135",
472 absl::InternalError("Error parsing 'a.b.c-bin' metadata: "
473 "illegal base64 encoding"),
474 kEndOfHeaders},
475 {"be", "content-length: 5\n", 0},
476 }},
477 Test{"Base64LegalEncodingWorksAfterFailure2",
478 {},
479 {},
480 {
481 {// Generated with: tools/codegen/core/gen_header_frame.py
482 // --compression inc --output hexstr --no_framing <
483 // test/core/transport/chttp2/MiXeD-CaSe.headers
484 "400a4d695865442d436153651073686f756c64206e6f74207061727365",
485 absl::InternalError("Illegal header key: MiXeD-CaSe"), 0},
486 // Binary metadata: created using:
487 // tools/codegen/core/gen_header_frame.py
488 // --compression inc --no_framing --output hexstr
489 // < test/core/transport/chttp2/bad-base64.headers
490 {"4009612e622e632d62696e1c6c75636b696c7920666f722075732c206974"
491 "27732074756573646179",
492 absl::InternalError("Illegal header key: MiXeD-CaSe"), 0},
493 {"be", absl::InternalError("Illegal header key: MiXeD-CaSe"),
494 0},
495 {"400e636f6e74656e742d6c656e6774680135",
496 absl::InternalError("Illegal header key: MiXeD-CaSe"),
497 kEndOfHeaders},
498 {"be", "content-length: 5\n", 0},
499 {"bf",
500 absl::InternalError("Error parsing 'a.b.c-bin' metadata: "
501 "illegal base64 encoding"),
502 0},
503 // Only the first error in each frame is reported, so we should
504 // still see the same error here...
505 {"c0",
506 absl::InternalError("Error parsing 'a.b.c-bin' metadata: "
507 "illegal base64 encoding"),
508 kEndOfHeaders},
509 // ... but if we look at the next frame we should see the
510 // stored error
511 {"c0", absl::InternalError("Illegal header key: MiXeD-CaSe"),
512 kEndOfHeaders},
513 }},
514 Test{"TeIsTrailers",
515 {},
516 {},
517 {// created using:
518 // tools/codegen/core/gen_header_frame.py
519 // --compression inc --no_framing --output hexstr
520 // < test/core/transport/chttp2/bad-te.headers
521 {"400274650767617262616765",
522 absl::InternalError("Error parsing 'te' metadata"), 0},
523 {"be", absl::InternalError("Error parsing 'te' metadata"), 0}}},
524 Test{"MetadataSizeLimitCheck",
525 {},
526 128,
527 {
528 {// Generated with: tools/codegen/core/gen_header_frame.py
529 // --compression inc --output hexstr --no_framing <
530 // test/core/transport/chttp2/large-metadata.headers
531 "40096164616c64726964610a6272616e64796275636b40086164616c6772"
532 "696d04746f6f6b4008616d6172616e74680a6272616e64796275636b4008"
533 "616e67656c6963610762616767696e73",
534 absl::ResourceExhaustedError(
535 "received metadata size exceeds hard limit"),
536 kEndOfHeaders},
537 // Should be able to look up the added elements individually
538 // (do not corrupt the hpack table test!)
539 {"be", "angelica: baggins\n", kEndOfHeaders},
540 {"bf", "amaranth: brandybuck\n", kEndOfHeaders},
541 {"c0", "adalgrim: took\n", kEndOfHeaders},
542 {"c1", "adaldrida: brandybuck\n", kEndOfHeaders},
543 // But not as a whole - that exceeds metadata limits for one
544 // request again
545 {"bebfc0c1",
546 absl::ResourceExhaustedError(
547 "received metadata size exceeds hard limit"),
548 0},
549 }},
550 Test{
551 "SingleByteBE",
552 {},
553 {},
554 {{"be", absl::InternalError("Invalid HPACK index received"),
555 kFailureIsConnectionError}},
556 },
557 Test{
558 "SingleByte80",
559 {},
560 {},
561 {{"80", absl::InternalError("Illegal hpack op code"),
562 kFailureIsConnectionError}},
563 },
564 Test{
565 "SingleByte29",
566 {},
567 {},
568 {{"29", "", kFailureIsConnectionError}},
569 },
570 Test{
571 "EmptyWithPriority",
572 {},
573 {},
574 {{"", "", kWithPriority}},
575 },
576 Test{
577 "SingleByteF5",
578 {},
579 {},
580 {{"f5", absl::InternalError("Invalid HPACK index received"),
581 kFailureIsConnectionError}},
582 },
583 Test{
584 "SingleByte0f",
585 {},
586 {},
587 {{"0f", "", 0}},
588 },
589 Test{
590 "SingleByte7f",
591 {},
592 {},
593 {{"7f", "", 0}},
594 },
595 Test{
596 "FuzzerCoverage1bffffff7c1b",
597 {},
598 {},
599 {{"1bffffff7c1b",
600 absl::ResourceExhaustedError(
601 "received metadata size exceeds hard limit"),
602 0}},
603 },
604 Test{
605 "FuzzerCoverageffffffffff00ff",
606 {},
607 {},
608 {{"ffffffffff00ff",
609 absl::InternalError("Invalid HPACK index received"),
610 kFailureIsConnectionError}},
611 },
612 Test{
613 "FuzzerCoverageIntegerOverflow2",
614 {},
615 {},
616 {{"ff8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8"
617 "d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d"
618 "8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8"
619 "d8d8d8d8d8d8d8d",
620 absl::InternalError("integer overflow in hpack integer decoding"),
621 kFailureIsConnectionError}}},
622 Test{
623 "FuzzerCoverageMetadataLimits",
624 {},
625 {9},
626 {{"3f6672616d6573207ba2020656e645f6f665f686561646572733a2074727565a"
627 "2020656e645f6f665f73747265616d3a2074727565a202073746f705f6275666"
628 "66572696e675f61667465725f7365676d656e74733a2039a202070617273653a"
629 "20225c3030305c3030305c3030305c3030305c3030305c3030305c3030305c30"
630 "30305c3030305c3030305c3030305c3030305c3030305c3030305c3030305c30"
631 "30305c3030305c3030305c3030305c3030305c3030305c3030305c3030305c",
632 absl::ResourceExhaustedError(
633 "received metadata size exceeds hard limit"),
634 kWithPriority}}},
635 Test{"FuzzerCoverage52046772706300073a737461747573033230300e7f",
636 {},
637 {},
638 {{"52046772706300073a737461747573033230300e7f",
639 ":status: 200\naccept-ranges: grpc\n", 0}}},
640 Test{"FuzzerCoveragea4a41007f0a40f2d62696e8beda42d5b63272129a410626907",
641 {},
642 {},
643 {{"a4a41007f0a40f2d62696e8beda42d5b63272129a410626907",
644 absl::InternalError("Illegal header key"), 0}}},
645 Test{
646 "HpackTableSizeWithBase64",
647 // haiku segment: 149bytes*2, a:a segment: 34 bytes
648 // So we arrange for one less than the total so we force a hpack
649 // table overflow
650 {149 * 2 + 34 - 1},
651 {},
652 {
653 {// Generated with: tools/codegen/core/gen_header_frame.py
654 // --compression inc --output hexstr --no_framing <
655 // test/core/transport/chttp2/long-base64.headers
656 "4005782d62696e70516d467a5a545930494756755932396b6157356e4f67"
657 "704a644342305957746c6379426961573568636e6b675a47463059534268"
658 "626d5167625746725a584d6761585167644756346443344b56584e6c5a6e5"
659 "67349475a766369427a644739796157356e49475a706247567a4c673d3d",
660 // Haiku by Bard.
661 "x-bin: Base64 encoding:\nIt takes binary data and makes it "
662 "text.\nUseful for storing files.\n",
663 0},
664 // Should go into the hpack table (x-bin: ... is 149 bytes long
665 // by hpack rules)
666 {"be",
667 "x-bin: Base64 encoding:\nIt takes binary data and "
668 "makes it text.\nUseful for storing files.\n",
669 0},
670 // Add another copy
671 {"4005782d62696e70516d467a5a545930494756755932396b6157356e4f67"
672 "704a644342305957746c6379426961573568636e6b675a47463059534268"
673 "626d5167625746725a584d6761585167644756346443344b56584e6c5a6e5"
674 "67349475a766369427a644739796157356e49475a706247567a4c673d3d",
675 "x-bin: Base64 encoding:\nIt takes binary data and makes it "
676 "text.\nUseful for storing files.\n",
677 0},
678 // 149*2 == 298, so we should have two copies in the hpack table
679 {"bebf",
680 "x-bin: Base64 encoding:\nIt takes binary data and makes it "
681 "text.\nUseful for storing files.\n"
682 "x-bin: Base64 encoding:\nIt takes binary data and makes it "
683 "text.\nUseful for storing files.\n",
684 0},
685 // Add some very short headers (should push the first long thing
686 // out)
687 // Generated with: tools/codegen/core/gen_header_frame.py
688 // --compression inc --output hexstr --no_framing <
689 // test/core/transport/chttp2/short.headers
690 {"4001610161", "a: a\n", 0},
691 // First two entries should be what was just pushed and then one
692 // long entry
693 {"bebf",
694 "a: a\nx-bin: Base64 encoding:\nIt takes binary data and "
695 "makes "
696 "it text.\nUseful for storing files.\n",
697 0},
698 // Third entry should be improbable (it's no longer in the
699 // table!)
700 {"c0", absl::InternalError("Invalid HPACK index received"),
701 kFailureIsConnectionError},
702 }},
703 Test{
704 "HpackTableSizeWithBase64AndHuffman",
705 // haiku segment: 149bytes*2, a:a segment: 34 bytes
706 // So we arrange for one less than the total so we force a hpack
707 // table overflow
708 {149 * 2 + 34 - 1},
709 {},
710 {
711 {// Generated with: tools/codegen/core/gen_header_frame.py
712 // --compression inc --output hexstr --no_framing --huff <
713 // test/core/transport/chttp2/long-base64.headers
714 "4005782d62696edbd94e1f7fbbf983262e36f313fd47c9bab54d5e592f5d0"
715 "73e49a09eae987c9b9c95759bf7161073dd7678e9d9347cb0d9fbf9a261fe"
716 "6c9a4c5c5a92f359b8fe69a3f6ae28c98bf7b90d77dc989ff43e4dd59317e"
717 "d71e2e3ef3cd041",
718 // Haiku by Bard.
719 "x-bin: Base64 encoding:\nIt takes binary data and makes it "
720 "text.\nUseful for storing files.\n",
721 0},
722 // Should go into the hpack table (x-bin: ... is 149 bytes long
723 // by hpack rules)
724 {"be",
725 "x-bin: Base64 encoding:\nIt takes binary data and "
726 "makes it text.\nUseful for storing files.\n",
727 0},
728 // Add another copy
729 {"4005782d62696edbd94e1f7fbbf983262e36f313fd47c9bab54d5e592f5d0"
730 "73e49a09eae987c9b9c95759bf7161073dd7678e9d9347cb0d9fbf9a261fe"
731 "6c9a4c5c5a92f359b8fe69a3f6ae28c98bf7b90d77dc989ff43e4dd59317e"
732 "d71e2e3ef3cd041",
733 "x-bin: Base64 encoding:\nIt takes binary data and makes it "
734 "text.\nUseful for storing files.\n",
735 0},
736 // 149*2 == 298, so we should have two copies in the hpack table
737 {"bebf",
738 "x-bin: Base64 encoding:\nIt takes binary data and makes it "
739 "text.\nUseful for storing files.\n"
740 "x-bin: Base64 encoding:\nIt takes binary data and makes it "
741 "text.\nUseful for storing files.\n",
742 0},
743 // Add some very short headers (should push the first long thing
744 // out)
745 // Generated with: tools/codegen/core/gen_header_frame.py
746 // --compression inc --output hexstr --no_framing <
747 // test/core/transport/chttp2/short.headers
748 {"4001610161", "a: a\n", 0},
749 // First two entries should be what was just pushed and then one
750 // long entry
751 {"bebf",
752 "a: a\nx-bin: Base64 encoding:\nIt takes binary data and "
753 "makes "
754 "it text.\nUseful for storing files.\n",
755 0},
756 // Third entry should be improbable (it's no longer in the
757 // table!)
758 {"c0", absl::InternalError("Invalid HPACK index received"),
759 kFailureIsConnectionError},
760 }},
761 Test{"SingleByte7a", {}, {}, {{"7a", "", 0}}},
762 Test{"SingleByte60",
763 {},
764 {},
765 {{"60",
766 absl::InternalError("Incomplete header at the end of a "
767 "header/continuation sequence"),
768 kEndOfStream | kFailureIsConnectionError}}},
769 Test{"FuzzerFoundMultipleTableSizeChanges3",
770 {},
771 {},
772 {{"89", ":status: 204\n", 0},
773 {"89", ":status: 204\n", 0},
774 {"393939393939393939393939393939393939393939",
775 absl::InternalError(
776 "More than two max table size changes in a single frame"),
777 kFailureIsConnectionError}}},
778 Test{"FuzzerCoverage4005782d62696edbd94e1f7etc",
779 {},
780 {},
781 {{"4005782d62696edbd94e1f7fbbf983267e36a313fd47c9bab54d5e592f5d",
782 "", 0}}},
783 Test{"FuzzerCoverage72656672657368",
784 {},
785 {},
786 {{"72656672657368", "", 0}}},
787 Test{"FuzzerCoverage66e6645f74Then66645f74",
788 {},
789 {},
790 {{"66e6645f74", "", 0}, {"66645f74", "", 0}}},
791 Test{
792 "MixedCaseHeadersAreStreamErrors",
793 {},
794 {},
795 {{// Generated with: tools/codegen/core/gen_header_frame.py
796 // --compression inc --output hexstr --no_framing <
797 // test/core/transport/chttp2/MiXeD-CaSe.headers
798 "400a4d695865442d436153651073686f756c64206e6f74207061727365",
799 absl::InternalError("Illegal header key: MiXeD-CaSe"), 0},
800 {// Looking up with hpack indices should work, but also return
801 // error
802 "be", absl::InternalError("Illegal header key: MiXeD-CaSe"), 0}}},
803 Test{
804 "FuzzerCoverageIntegerOverflow3",
805 {},
806 {},
807 {{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
808 absl::InternalError("integer overflow in hpack integer decoding"),
809 kFailureIsConnectionError}}},
810 Test{"Dadadadadada",
811 {},
812 {},
813 {{"dadadadadadadadadadadadadadadadadadadadadadadadadadadadadadadad"
814 "adadadadadadadadadadadadadadadadadadadadadadadadadadadadadadada"
815 "dadadadadadadadadadadadadadadadadadadadadadadadadadadadadadadad"
816 "adadadadadadadadadadadadadadadadadadada",
817 absl::InternalError("Invalid HPACK index received"),
818 kWithPriority | kFailureIsConnectionError}}},
819 Test{"MaliciousVarintEncoding",
820 {},
821 {},
822 {{"1f80808080808080808080808080808080808080808080808080808080",
823 absl::InternalError(
824 "Malicious varint encoding detected in HPACK stream"),
825 kFailureIsConnectionError}}}),
826 NameFromConfig);
827
828 } // namespace
829 } // namespace grpc_core
830
main(int argc,char ** argv)831 int main(int argc, char** argv) {
832 grpc::testing::TestEnvironment env(&argc, argv);
833 ::testing::InitGoogleTest(&argc, argv);
834 return RUN_ALL_TESTS();
835 }
836