Debian Codegraph @

src/core/lib/surface/validate_metadata.cc

raw
1 /*
2 *
3 * Copyright 2016 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/support/port_platform.h>
20
21 #include "src/core/lib/surface/validate_metadata.h"
22
23 #include "absl/status/status.h"
24 #include "absl/strings/string_view.h"
25
26 #include <grpc/grpc.h>
27
28 #include "src/core/lib/gpr/string.h"
29 #include "src/core/lib/gprpp/bitset.h"
30 #include "src/core/lib/gprpp/memory.h"
31 #include "src/core/lib/gprpp/status_helper.h"
32 #include "src/core/lib/iomgr/error.h"
33
34 static grpc_error_handle conforms_to(const grpc_slice& slice,
35 const grpc_core::BitSet<256>& legal_bits,
36 const char* err_desc) {
37 const uint8_t* p = GRPC_SLICE_START_PTR(slice);
38 const uint8_t* e = GRPC_SLICE_END_PTR(slice);
39 for (; p != e; p++) {
40 if (!legal_bits.is_set(*p)) {
41 size_t len;
42 grpc_core::UniquePtr<char> ptr(gpr_dump_return_len(
43 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(slice),
44 GRPC_SLICE_LENGTH(slice), GPR_DUMP_HEX | GPR_DUMP_ASCII, &len));
45 grpc_error_handle error = grpc_error_set_str(
46 grpc_error_set_int(GRPC_ERROR_CREATE(err_desc),
47 grpc_core::StatusIntProperty::kOffset,
48 p - GRPC_SLICE_START_PTR(slice)),
49 grpc_core::StatusStrProperty::kRawBytes,
50 absl::string_view(ptr.get(), len));
51 return error;
52 }
53 }
54 return absl::OkStatus();
55 }
56
57 static int error2int(grpc_error_handle error) {
58 int r = (error.ok());
59 return r;
60 }
61
62 namespace {
63 class LegalHeaderKeyBits : public grpc_core::BitSet<256> {
64 public:
65 constexpr LegalHeaderKeyBits() {
66 for (int i = 'a'; i <= 'z'; i++) set(i);
67 for (int i = '0'; i <= '9'; i++) set(i);
68 set('-');
69 set('_');
70 set('.');
71 }
72 };
73 constexpr LegalHeaderKeyBits g_legal_header_key_bits;
74 } // namespace
75
76 grpc_error_handle grpc_validate_header_key_is_legal(const grpc_slice& slice) {
77 if (GRPC_SLICE_LENGTH(slice) == 0) {
78 return GRPC_ERROR_CREATE("Metadata keys cannot be zero length");
79 }
80 if (GRPC_SLICE_LENGTH(slice) > UINT32_MAX) {
81 return GRPC_ERROR_CREATE("Metadata keys cannot be larger than UINT32_MAX");
82 }
83 if (GRPC_SLICE_START_PTR(slice)[0] == ':') {
84 return GRPC_ERROR_CREATE("Metadata keys cannot start with :");
85 }
86 return conforms_to(slice, g_legal_header_key_bits, "Illegal header key");
87 }
88
89 int grpc_header_key_is_legal(grpc_slice slice) {
90 return error2int(grpc_validate_header_key_is_legal(slice));
91 }
92
93 namespace {
94 class LegalHeaderNonBinValueBits : public grpc_core::BitSet<256> {
95 public:
96 constexpr LegalHeaderNonBinValueBits() {
97 for (int i = 32; i <= 126; i++) {
98 set(i);
99 }
100 }
101 };
102 constexpr LegalHeaderNonBinValueBits g_legal_header_non_bin_value_bits;
103 } // namespace
104
105 grpc_error_handle grpc_validate_header_nonbin_value_is_legal(
106 const grpc_slice& slice) {
107 return conforms_to(slice, g_legal_header_non_bin_value_bits,
108 "Illegal header value");
109 }
110
111 int grpc_header_nonbin_value_is_legal(grpc_slice slice) {
112 return error2int(grpc_validate_header_nonbin_value_is_legal(slice));
113 }
114
115 int grpc_is_binary_header_internal(const grpc_slice& slice) {
116 return grpc_key_is_binary_header(GRPC_SLICE_START_PTR(slice),
117 GRPC_SLICE_LENGTH(slice));
118 }
119
120 int grpc_is_binary_header(grpc_slice slice) {
121 return grpc_is_binary_header_internal(slice);
122 }