1
#pragma once
2

            
3
#include <cstdint>
4

            
5
#include "source/common/http/character_set_validation.h"
6

            
7
namespace Envoy {
8
namespace Extensions {
9
namespace Http {
10
namespace HeaderValidators {
11
namespace EnvoyDefault {
12

            
13
// Header value character table.
14
// From RFC 9110, https://www.rfc-editor.org/rfc/rfc9110.html#section-5.5:
15
//
16
// SPELLCHECKER(off)
17
// header-field   = field-name ":" OWS field-value OWS
18
// field-value    = *field-content
19
// field-content  = field-vchar
20
//                  [ 1*( SP / HTAB / field-vchar ) field-vchar ]
21
// field-vchar    = VCHAR / obs-text
22
// obs-text       = %x80-FF
23
//
24
// VCHAR          =  %x21-7E
25
//                   ; visible (printing) characters
26
// SPELLCHECKER(on)
27
inline constexpr std::array<uint32_t, 8> kGenericHeaderValueCharTable = {
28
    // control characters
29
    0b00000000010000000000000000000000,
30
    // !"#$%&'()*+,-./0123456789:;<=>?
31
    0b11111111111111111111111111111111,
32
    //@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
33
    0b11111111111111111111111111111111,
34
    //`abcdefghijklmnopqrstuvwxyz{|}~
35
    0b11111111111111111111111111111110,
36
    // extended ascii
37
    0b11111111111111111111111111111111,
38
    0b11111111111111111111111111111111,
39
    0b11111111111111111111111111111111,
40
    0b11111111111111111111111111111111,
41
};
42

            
43
// :method header character table.
44
// From RFC 9110: https://www.rfc-editor.org/rfc/rfc9110.html#section-9.1
45
//
46
// SPELLCHECKER(off)
47
// method = token
48
// token  = 1*tchar
49
// tchar  = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "."
50
//        /  "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
51
// SPELLCHECKER(on)
52
inline constexpr std::array<uint32_t, 8> kMethodHeaderCharTable = {
53
    // control characters
54
    0b00000000000000000000000000000000,
55
    // !"#$%&'()*+,-./0123456789:;<=>?
56
    0b01011111001101101111111111000000,
57
    //@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
58
    0b01111111111111111111111111100011,
59
    //`abcdefghijklmnopqrstuvwxyz{|}~
60
    0b11111111111111111111111111101010,
61
    // extended ascii
62
    0b00000000000000000000000000000000,
63
    0b00000000000000000000000000000000,
64
    0b00000000000000000000000000000000,
65
    0b00000000000000000000000000000000,
66
};
67

            
68
// :path header character table.
69
// From RFC 3986: https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
70
//
71
// SPELLCHECKER(off)
72
// path          = path-abempty    ; begins with "/" or is empty
73
//               / path-absolute   ; begins with "/" but not "//"
74
//               / path-noscheme   ; begins with a non-colon segment
75
//               / path-rootless   ; begins with a segment
76
//               / path-empty      ; zero characters
77
//
78
// path-abempty  = *( "/" segment )
79
// path-absolute = "/" [ segment-nz *( "/" segment ) ]
80
// path-noscheme = segment-nz-nc *( "/" segment )
81
// path-rootless = segment-nz *( "/" segment )
82
// path-empty    = 0<pchar>
83
//
84
// segment       = *pchar
85
// segment-nz    = 1*pchar
86
// segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
87
//               ; non-zero-length segment without any colon ":"
88
//
89
// pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
90
// SPELLCHECKER(on)
91
inline constexpr std::array<uint32_t, 8> kPathHeaderCharTable = {
92
    // control characters
93
    0b00000000000000000000000000000000,
94
    // !"#$%&'()*+,-./0123456789:;<=>?
95
    0b01001111111111111111111111110100,
96
    //@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
97
    0b11111111111111111111111111100001,
98
    //`abcdefghijklmnopqrstuvwxyz{|}~
99
    0b01111111111111111111111111100010,
100
    // extended ascii
101
    0b00000000000000000000000000000000,
102
    0b00000000000000000000000000000000,
103
    0b00000000000000000000000000000000,
104
    0b00000000000000000000000000000000,
105
};
106

            
107
// Unreserved characters.
108
// From RFC 3986: https://datatracker.ietf.org/doc/html/rfc3986#section-2.3
109
//
110
// unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
111
inline constexpr std::array<uint32_t, 8> kUnreservedCharTable = {
112
    // control characters
113
    0b00000000000000000000000000000000,
114
    // !"#$%&'()*+,-./0123456789:;<=>?
115
    0b00000000000001101111111111000000,
116
    //@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
117
    0b01111111111111111111111111100001,
118
    //`abcdefghijklmnopqrstuvwxyz{|}~
119
    0b01111111111111111111111111100010,
120
    // extended ascii
121
    0b00000000000000000000000000000000,
122
    0b00000000000000000000000000000000,
123
    0b00000000000000000000000000000000,
124
    0b00000000000000000000000000000000,
125
};
126

            
127
// Transfer-Encoding HTTP/1.1 header character table.
128
// From RFC 9110: https://www.rfc-editor.org/rfc/rfc9110.html#section-10.1.4
129
//
130
// SPELLCHECKER(off)
131
// Transfer-Encoding   = #transfer-coding
132
// transfer-coding     = token *( OWS ";" OWS transfer-parameter )
133
// transfer-parameter  = token BWS "=" BWS ( token / quoted-string )
134
// SPELLCHECKER(on)
135
inline constexpr std::array<uint32_t, 8> kTransferEncodingHeaderCharTable = {
136
    // control characters
137
    0b00000000010000000000000000000000,
138
    // !"#$%&'()*+,-./0123456789:;<=>?
139
    0b11111111001111101111111111010100,
140
    //@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
141
    0b01111111111111111111111111100011,
142
    //`abcdefghijklmnopqrstuvwxyz{|}~
143
    0b11111111111111111111111111101010,
144
    // extended ascii
145
    0b00000000000000000000000000000000,
146
    0b00000000000000000000000000000000,
147
    0b00000000000000000000000000000000,
148
    0b00000000000000000000000000000000,
149
};
150

            
151
// An IPv6 address, excluding the surrounding "[" and "]" characters. This is based on RFC 3986,
152
// https://www.rfc-editor.org/rfc/rfc3986.html#section-3.2.2, that only allows hex digits and the
153
// ":" separator.
154
inline constexpr std::array<uint32_t, 8> kHostIPv6AddressCharTable = {
155
    // control characters
156
    0b00000000000000000000000000000000,
157
    // !"#$%&'()*+,-./0123456789:;<=>?
158
    0b00000000000000001111111111100000,
159
    //@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
160
    0b01111110000000000000000000000000,
161
    //`abcdefghijklmnopqrstuvwxyz{|}~
162
    0b01111110000000000000000000000000,
163
    // extended ascii
164
    0b00000000000000000000000000000000,
165
    0b00000000000000000000000000000000,
166
    0b00000000000000000000000000000000,
167
    0b00000000000000000000000000000000,
168
};
169

            
170
// A host reg-name character table, which covers both IPv4 addresses and hostnames.
171
// From RFC 3986: https://www.rfc-editor.org/rfc/rfc3986.html#section-3.2.2
172
//
173
// SPELLCHECKER(off)
174
// reg-name    = *( unreserved / pct-encoded / sub-delims )
175
// SPELLCHECKER(on)
176
inline constexpr std::array<uint32_t, 8> kHostRegNameCharTable = {
177
    // control characters
178
    0b00000000000000000000000000000000,
179
    // !"#$%&'()*+,-./0123456789:;<=>?
180
    0b01001111111111101111111111010100,
181
    //@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
182
    0b01111111111111111111111111100001,
183
    //`abcdefghijklmnopqrstuvwxyz{|}~
184
    0b01111111111111111111111111100010,
185
    // extended ascii
186
    0b00000000000000000000000000000000,
187
    0b00000000000000000000000000000000,
188
    0b00000000000000000000000000000000,
189
    0b00000000000000000000000000000000,
190
};
191

            
192
} // namespace EnvoyDefault
193
} // namespace HeaderValidators
194
} // namespace Http
195
} // namespace Extensions
196
} // namespace Envoy