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 <stdlib.h>
22 #include <string.h>
23
24 #include <grpc/grpc.h>
25 #include <grpc/support/alloc.h>
26
27 #include "src/core/lib/gprpp/memory.h"
28 #include "src/core/lib/iomgr/error.h"
29 #include "src/core/lib/slice/slice_internal.h"
30 #include "src/core/lib/slice/slice_string_helpers.h"
31 #include "src/core/lib/surface/validate_metadata.h"
32
conforms_to(const grpc_slice & slice,const uint8_t * legal_bits,const char * err_desc)33 static grpc_error* conforms_to(const grpc_slice& slice,
34 const uint8_t* legal_bits,
35 const char* err_desc) {
36 const uint8_t* p = GRPC_SLICE_START_PTR(slice);
37 const uint8_t* e = GRPC_SLICE_END_PTR(slice);
38 for (; p != e; p++) {
39 int idx = *p;
40 int byte = idx / 8;
41 int bit = idx % 8;
42 if ((legal_bits[byte] & (1 << bit)) == 0) {
43 grpc_error* error = grpc_error_set_str(
44 grpc_error_set_int(GRPC_ERROR_CREATE_FROM_COPIED_STRING(err_desc),
45 GRPC_ERROR_INT_OFFSET,
46 p - GRPC_SLICE_START_PTR(slice)),
47 GRPC_ERROR_STR_RAW_BYTES,
48 grpc_dump_slice_to_slice(slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
49 return error;
50 }
51 }
52 return GRPC_ERROR_NONE;
53 }
54
error2int(grpc_error * error)55 static int error2int(grpc_error* error) {
56 int r = (error == GRPC_ERROR_NONE);
57 GRPC_ERROR_UNREF(error);
58 return r;
59 }
60
grpc_validate_header_key_is_legal(const grpc_slice & slice)61 grpc_error* grpc_validate_header_key_is_legal(const grpc_slice& slice) {
62 static const uint8_t legal_header_bits[256 / 8] = {
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xff, 0x03, 0x00, 0x00, 0x00,
64 0x80, 0xfe, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
66 if (GRPC_SLICE_LENGTH(slice) == 0) {
67 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
68 "Metadata keys cannot be zero length");
69 }
70 if (GRPC_SLICE_LENGTH(slice) > UINT32_MAX) {
71 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
72 "Metadata keys cannot be larger than UINT32_MAX");
73 }
74 if (GRPC_SLICE_START_PTR(slice)[0] == ':') {
75 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
76 "Metadata keys cannot start with :");
77 }
78 return conforms_to(slice, legal_header_bits, "Illegal header key");
79 }
80
grpc_header_key_is_legal(grpc_slice slice)81 int grpc_header_key_is_legal(grpc_slice slice) {
82 return error2int(grpc_validate_header_key_is_legal(slice));
83 }
84
grpc_validate_header_nonbin_value_is_legal(const grpc_slice & slice)85 grpc_error* grpc_validate_header_nonbin_value_is_legal(
86 const grpc_slice& slice) {
87 static const uint8_t legal_header_bits[256 / 8] = {
88 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
91 return conforms_to(slice, legal_header_bits, "Illegal header value");
92 }
93
grpc_header_nonbin_value_is_legal(grpc_slice slice)94 int grpc_header_nonbin_value_is_legal(grpc_slice slice) {
95 return error2int(grpc_validate_header_nonbin_value_is_legal(slice));
96 }
97
grpc_is_binary_header_internal(const grpc_slice & slice)98 int grpc_is_binary_header_internal(const grpc_slice& slice) {
99 return grpc_key_is_binary_header(GRPC_SLICE_START_PTR(slice),
100 GRPC_SLICE_LENGTH(slice));
101 }
102
grpc_is_binary_header(grpc_slice slice)103 int grpc_is_binary_header(grpc_slice slice) {
104 return grpc_is_binary_header_internal(slice);
105 }
106