-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_allow_headers_module.c
368 lines (257 loc) · 7.18 KB
/
ngx_http_allow_headers_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Header files
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
// Structures
// Location configuration structure
typedef struct {
// Enabled
ngx_uint_t enabled;
// Allowed headers
ngx_array_t *allowedHeaders;
} ngx_http_allow_headers_conf_t;
// Function prototypes
// Allow header setup
static char *allowHeaderSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data);
// Postconfiguration
static ngx_int_t postconfiguration(ngx_conf_t *configuration);
// Create location configuration
static void *createLocationConfiguration(ngx_conf_t *configuration);
// Merge location configuration
static char *mergeLocationConfiguration(ngx_conf_t *configuration, void *parent, void *child);
// Header filter
static ngx_int_t headerFilter(ngx_http_request_t *request);
// Constants
// Allow headers values
static ngx_conf_enum_t allowHeadersValues[] = {
{
// Off
ngx_string("off"),
0
},
{
// On
ngx_string("on"),
1
},
{
// End of value
ngx_null_string,
0
}
};
// Directives
static ngx_command_t directives[] = {
// Allow headers
{
// Name
ngx_string("allow_headers"),
// Type
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
// Enumeration
ngx_conf_set_enum_slot,
// Configuration
NGX_HTTP_LOC_CONF_OFFSET,
// Offset
offsetof(ngx_http_allow_headers_conf_t, enabled),
// Values
&allowHeadersValues
},
// Allow header
{
// Name
ngx_string("allow_header"),
// Type
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
// Setup
allowHeaderSetup,
// Configuration
NGX_HTTP_LOC_CONF_OFFSET,
// Offset
offsetof(ngx_http_allow_headers_conf_t, allowedHeaders),
// Reserved
NULL
},
// End of directives
ngx_null_command
};
// Context
static ngx_http_module_t context = {
// Preconfiguration
NULL,
// Postconfiguration
postconfiguration,
// Create main configuration
NULL,
// Initialize main configuration
NULL,
// Create server configuration
NULL,
// Merge server configuration
NULL,
// Create location configuration
createLocationConfiguration,
// Merge location configuration
mergeLocationConfiguration
};
// Module
ngx_module_t ngx_http_allow_headers_module = {
// Version header
NGX_MODULE_V1,
// Context
&context,
// Directives
directives,
// Type
NGX_HTTP_MODULE,
// initialize master
NULL,
// Initialize module
NULL,
// Initialize process
NULL,
// Initialize thread
NULL,
// Exit thread
NULL,
// Exit process
NULL,
// Exit master
NULL,
// Version footer
NGX_MODULE_V1_PADDING
};
// Global variables
// Next header filter
static ngx_http_output_header_filter_pt nextHeaderFilter;
// Supporting function implementation
// Allow headers setup
char *allowHeaderSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data) {
// Get location configuration
const ngx_http_allow_headers_conf_t *locationConfiguration = data;
// Get arguments
const ngx_str_t *arguments = configuration->args->elts;
// Get header
const ngx_str_t *header = &arguments[1];
// Get allowed headers
ngx_array_t **allowedHeaders = (ngx_array_t **)((char *)locationConfiguration + command->offset);
// Check if allowed headers doesn't exist
if(!*allowedHeaders) {
// Check if creating allowed headers failed
*allowedHeaders = ngx_array_create(configuration->pool, 1, sizeof(ngx_str_t));
if(!*allowedHeaders) {
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Check if adding value to allowed headers failed
ngx_str_t *allowedHeader = ngx_array_push(*allowedHeaders);
if(!allowedHeader) {
// Return configuration error
return NGX_CONF_ERROR;
}
// Set allowed header
*allowedHeader = *header;
// Check if header is invalid
if(!header->len) {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", header);
// Return configuration error
return NGX_CONF_ERROR;
}
// Return configuration ok
return NGX_CONF_OK;
}
// Postconfiguration
ngx_int_t postconfiguration(ngx_conf_t *configuration) {
// Append filter to filter chain
nextHeaderFilter = ngx_http_top_header_filter;
ngx_http_top_header_filter = headerFilter;
// Return ok
return NGX_OK;
}
// Create location configuration
void *createLocationConfiguration(ngx_conf_t *configuration) {
// Check if creating location configuration failed
ngx_http_allow_headers_conf_t *locationConfiguration = ngx_pcalloc(configuration->pool, sizeof(ngx_http_allow_headers_conf_t));
if(!locationConfiguration) {
// Return null
return NULL;
}
// Configure location configuration
locationConfiguration->enabled = NGX_CONF_UNSET_UINT;
// Return location configuration
return locationConfiguration;
}
// Merge location configuration
char *mergeLocationConfiguration(ngx_conf_t *configuration, void *parent, void *child) {
// Initialize current and previous location configuration
ngx_http_allow_headers_conf_t *currentLocationConfiguration = child;
const ngx_http_allow_headers_conf_t *previousLocationConfiguration = parent;
// Merge location configuration values
ngx_conf_merge_uint_value(currentLocationConfiguration->enabled, previousLocationConfiguration->enabled, 0);
if(!currentLocationConfiguration->allowedHeaders) {
currentLocationConfiguration->allowedHeaders = previousLocationConfiguration->allowedHeaders;
}
// Return configuration ok
return NGX_CONF_OK;
}
// Header filter
ngx_int_t headerFilter(ngx_http_request_t *request) {
// Get location configuration
const ngx_http_allow_headers_conf_t *locationConfiguration = ngx_http_get_module_loc_conf(request, ngx_http_allow_headers_module);
// Check if module isn't used
if(!locationConfiguration->enabled) {
// Return next header filter
return nextHeaderFilter(request);
}
// Go through all headers
const ngx_list_part_t *part = &request->headers_out.headers.part;
ngx_table_elt_t *header = part->elts;
for(ngx_uint_t i = 0;; ++i) {
// Check if at the end of the part
if(i >= part->nelts) {
// Check if at last part
if(!part->next) {
// Break
break;
}
// Go to next part
part = part->next;
header = part->elts;
i = 0;
}
// Check if header is invaid
if(!header[i].hash) {
// Continue
continue;
}
// Initialize blocked header
ngx_uint_t blockedHeader = 1;
// Check if allowed headers exist
if(locationConfiguration->allowedHeaders) {
// Go through all allowed headers
const ngx_str_t *allowedHeaders = locationConfiguration->allowedHeaders->elts;
for(ngx_uint_t j = 0; j < locationConfiguration->allowedHeaders->nelts; ++j) {
// Get allowed headers
const ngx_str_t *allowedHeader = &allowedHeaders[j];
// Check if header is allowed
if(allowedHeader->len == header[i].key.len && !ngx_strncasecmp(allowedHeader->data, header[i].key.data, header[i].key.len)) {
// Clear blocked header
blockedHeader = 0;
// Break
break;
}
}
}
// Check if header is blocked
if(blockedHeader) {
// Log info
ngx_log_error(NGX_LOG_INFO, request->connection->log, 0, "blocked header %V", &header[i].key);
// Invalidate header
header[i].hash = 0;
}
}
// Return next header filter
return nextHeaderFilter(request);
}