Line data Source code
1 :
2 : /*
3 : * Copyright (C) agentzh
4 : */
5 :
6 : #ifndef DDEBUG
7 : #define DDEBUG 0
8 : #endif
9 : #include "ddebug.h"
10 :
11 :
12 : #include "ngx_http_xss_filter_module.h"
13 : #include "ngx_http_xss_util.h"
14 :
15 : #include <nginx.h>
16 : #include <ngx_config.h>
17 :
18 :
19 : #define ngx_http_xss_default_output_type "application/x-javascript"
20 :
21 :
22 : static ngx_str_t ngx_http_xss_default_types[] = {
23 : ngx_string("application/json"),
24 : ngx_null_string
25 : };
26 :
27 :
28 : static void *ngx_http_xss_create_loc_conf(ngx_conf_t *cf);
29 : static char *ngx_http_xss_merge_loc_conf(ngx_conf_t *cf, void *parent,
30 : void *child);
31 : static ngx_int_t ngx_http_xss_filter_init(ngx_conf_t *cf);
32 : static void *ngx_http_xss_create_main_conf(ngx_conf_t *cf);
33 : static char *ngx_http_xss_get(ngx_conf_t *cf, ngx_command_t *cmd,
34 : void *conf);
35 :
36 :
37 : static volatile ngx_cycle_t *ngx_http_xss_prev_cycle = NULL;
38 :
39 :
40 : static ngx_command_t ngx_http_xss_commands[] = {
41 :
42 : { ngx_string("xss_get"),
43 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
44 : |NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
45 : ngx_http_xss_get,
46 : NGX_HTTP_LOC_CONF_OFFSET,
47 : offsetof(ngx_http_xss_loc_conf_t, get_enabled),
48 : NULL },
49 :
50 : { ngx_string("xss_callback_arg"),
51 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
52 : |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
53 : ngx_conf_set_str_slot,
54 : NGX_HTTP_LOC_CONF_OFFSET,
55 : offsetof(ngx_http_xss_loc_conf_t, callback_arg),
56 : NULL },
57 :
58 : { ngx_string("xss_input_types"),
59 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
60 : |NGX_CONF_1MORE|NGX_HTTP_LIF_CONF,
61 : ngx_http_types_slot,
62 : NGX_HTTP_LOC_CONF_OFFSET,
63 : offsetof(ngx_http_xss_loc_conf_t, input_types_keys),
64 : &ngx_http_xss_default_types[0] },
65 :
66 : { ngx_string("xss_check_status"),
67 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
68 : |NGX_CONF_FLAG|NGX_HTTP_LIF_CONF,
69 : ngx_conf_set_flag_slot,
70 : NGX_HTTP_LOC_CONF_OFFSET,
71 : offsetof(ngx_http_xss_loc_conf_t, check_status),
72 : NULL },
73 :
74 : { ngx_string("xss_override_status"),
75 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
76 : |NGX_CONF_FLAG|NGX_HTTP_LIF_CONF,
77 : ngx_conf_set_flag_slot,
78 : NGX_HTTP_LOC_CONF_OFFSET,
79 : offsetof(ngx_http_xss_loc_conf_t, override_status),
80 : NULL },
81 :
82 : { ngx_string("xss_output_type"),
83 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
84 : |NGX_CONF_1MORE|NGX_HTTP_LIF_CONF,
85 : ngx_conf_set_str_slot,
86 : NGX_HTTP_LOC_CONF_OFFSET,
87 : offsetof(ngx_http_xss_loc_conf_t, output_type),
88 : NULL },
89 :
90 : ngx_null_command
91 : };
92 :
93 :
94 : static ngx_http_module_t ngx_http_xss_filter_module_ctx = {
95 : NULL, /* preconfiguration */
96 : ngx_http_xss_filter_init, /* postconfiguration */
97 :
98 : ngx_http_xss_create_main_conf, /* create main configuration */
99 : NULL, /* init main configuration */
100 :
101 : NULL, /* create server configuration */
102 : NULL, /* merge server configuration */
103 :
104 : ngx_http_xss_create_loc_conf, /* create location configuration */
105 : ngx_http_xss_merge_loc_conf /* merge location configuration */
106 : };
107 :
108 :
109 : ngx_module_t ngx_http_xss_filter_module = {
110 : NGX_MODULE_V1,
111 : &ngx_http_xss_filter_module_ctx, /* module context */
112 : ngx_http_xss_commands, /* module directives */
113 : NGX_HTTP_MODULE, /* module type */
114 : NULL, /* init master */
115 : NULL, /* init module */
116 : NULL, /* init process */
117 : NULL, /* init thread */
118 : NULL, /* exit thread */
119 : NULL, /* exit process */
120 : NULL, /* exit master */
121 : NGX_MODULE_V1_PADDING
122 : };
123 :
124 :
125 : static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
126 : static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
127 :
128 :
129 : static ngx_int_t
130 0 : ngx_http_xss_header_filter(ngx_http_request_t *r)
131 : {
132 : ngx_http_xss_ctx_t *ctx;
133 : ngx_http_xss_loc_conf_t *xlcf;
134 : ngx_str_t callback;
135 : u_char *p, *src, *dst;
136 :
137 0 : if (r != r->main) {
138 0 : ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
139 : "xss skipped in subrequests");
140 :
141 0 : return ngx_http_next_header_filter(r);
142 : }
143 :
144 0 : xlcf = ngx_http_get_module_loc_conf(r, ngx_http_xss_filter_module);
145 :
146 0 : if (!xlcf->get_enabled) {
147 0 : return ngx_http_next_header_filter(r);
148 : }
149 :
150 0 : if (r->method != NGX_HTTP_GET) {
151 0 : ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
152 : "xss skipped due to the unmatched request method: %V",
153 : &r->method_name);
154 :
155 0 : return ngx_http_next_header_filter(r);
156 : }
157 :
158 0 : if (xlcf->check_status) {
159 :
160 0 : if (r->headers_out.status != NGX_HTTP_OK
161 0 : && r->headers_out.status != NGX_HTTP_CREATED)
162 : {
163 0 : ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
164 : "xss skipped due to unmatched response status "
165 : "\"%ui\"", r->headers_out.status);
166 :
167 0 : return ngx_http_next_header_filter(r);
168 : }
169 : }
170 :
171 0 : if (xlcf->callback_arg.len == 0) {
172 :
173 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
174 : "xss: xss_get is enabled but no xss_callback_arg "
175 : "specified");
176 :
177 0 : return ngx_http_next_header_filter(r);
178 : }
179 :
180 0 : if (ngx_http_test_content_type(r, &xlcf->input_types) == NULL) {
181 :
182 0 : ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
183 : "xss skipped due to unmatched Content-Type response "
184 : "header");
185 :
186 0 : return ngx_http_next_header_filter(r);
187 : }
188 :
189 0 : if (ngx_http_arg(r, xlcf->callback_arg.data, xlcf->callback_arg.len,
190 : &callback)
191 : != NGX_OK)
192 : {
193 0 : ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
194 : "xss skipped: no GET argument \"%V\" specified in "
195 : "the request", &xlcf->callback_arg);
196 :
197 0 : return ngx_http_next_header_filter(r);
198 : }
199 :
200 0 : p = ngx_palloc(r->pool, callback.len);
201 0 : if (p == NULL) {
202 0 : return NGX_ERROR;
203 : }
204 :
205 0 : src = callback.data; dst = p;
206 :
207 0 : ngx_unescape_uri(&dst, &src, callback.len, NGX_UNESCAPE_URI_COMPONENT);
208 :
209 0 : if (src != callback.data + callback.len) {
210 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
211 : "xss: unescape uri: input data not consumed completely");
212 :
213 0 : return NGX_ERROR;
214 : }
215 :
216 0 : callback.data = p;
217 0 : callback.len = dst - p;
218 :
219 0 : if (ngx_http_xss_test_callback(callback.data, callback.len) != NGX_OK) {
220 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
221 : "xss: bad callback argument: \"%V\"", &callback);
222 :
223 0 : return ngx_http_next_header_filter(r);
224 : }
225 :
226 0 : ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_xss_ctx_t));
227 0 : if (ctx == NULL) {
228 0 : return NGX_ERROR;
229 : }
230 :
231 : /*
232 : * set by ngx_pcalloc():
233 : *
234 : * ctx->callback = { 0, NULL };
235 : * ctx->before_body_sent = 0;
236 : */
237 :
238 0 : ctx->callback = callback;
239 :
240 0 : ngx_http_set_ctx(r, ctx, ngx_http_xss_filter_module);
241 :
242 0 : r->headers_out.content_type = xlcf->output_type;
243 0 : r->headers_out.content_type_len = xlcf->output_type.len;
244 0 : r->headers_out.content_type_lowcase = NULL;
245 :
246 0 : ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
247 : "xss output Content-Type header \"%V\"",
248 : &xlcf->output_type);
249 :
250 0 : ngx_http_clear_content_length(r);
251 0 : ngx_http_clear_accept_ranges(r);
252 :
253 0 : if (xlcf->override_status
254 0 : && r->headers_out.status >= NGX_HTTP_SPECIAL_RESPONSE)
255 : {
256 0 : r->headers_out.status = NGX_HTTP_OK;
257 : }
258 :
259 0 : return ngx_http_next_header_filter(r);
260 : }
261 :
262 :
263 : static ngx_int_t
264 0 : ngx_http_xss_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
265 : {
266 : ngx_uint_t last;
267 : ngx_chain_t *cl, *orig_in;
268 0 : ngx_chain_t **ll = NULL;
269 : ngx_http_xss_ctx_t *ctx;
270 : size_t len;
271 : ngx_buf_t *b;
272 :
273 0 : if (in == NULL || r->header_only) {
274 0 : return ngx_http_next_body_filter(r, in);
275 : }
276 :
277 0 : ctx = ngx_http_get_module_ctx(r, ngx_http_xss_filter_module);
278 :
279 0 : if (ctx == NULL) {
280 0 : return ngx_http_next_body_filter(r, in);
281 : }
282 :
283 0 : orig_in = in;
284 :
285 0 : if (!ctx->before_body_sent) {
286 0 : ctx->before_body_sent = 1;
287 :
288 : dd("callback: %.*s", (int) ctx->callback.len,
289 : ctx->callback.data);
290 :
291 0 : len = ctx->callback.len + sizeof("(") - 1;
292 :
293 0 : b = ngx_create_temp_buf(r->pool, len);
294 0 : if (b == NULL) {
295 0 : return NGX_ERROR;
296 : }
297 :
298 0 : b->last = ngx_copy(b->last, ctx->callback.data, ctx->callback.len);
299 :
300 0 : *b->last++ = '(';
301 :
302 0 : cl = ngx_alloc_chain_link(r->pool);
303 0 : if (cl == NULL) {
304 0 : return NGX_ERROR;
305 : }
306 :
307 0 : cl->buf = b;
308 0 : cl->next = in;
309 0 : in = cl;
310 : }
311 :
312 0 : last = 0;
313 :
314 0 : for (cl = orig_in; cl; cl = cl->next) {
315 0 : if (cl->buf->last_buf) {
316 0 : cl->buf->last_buf = 0;
317 0 : cl->buf->sync = 1;
318 0 : ll = &cl->next;
319 0 : last = 1;
320 : }
321 : }
322 :
323 0 : if (last) {
324 0 : len = sizeof(");") - 1;
325 :
326 0 : b = ngx_create_temp_buf(r->pool, len);
327 0 : if (b == NULL) {
328 0 : return NGX_ERROR;
329 : }
330 :
331 0 : *b->last++ = ')';
332 0 : *b->last++ = ';';
333 :
334 0 : b->last_buf = 1;
335 :
336 0 : cl = ngx_alloc_chain_link(r->pool);
337 0 : if (cl == NULL) {
338 0 : return NGX_ERROR;
339 : }
340 :
341 0 : cl->buf = b;
342 0 : cl->next = NULL;
343 0 : *ll = cl;
344 :
345 0 : ngx_http_set_ctx(r, NULL, ngx_http_xss_filter_module);
346 : }
347 :
348 0 : return ngx_http_next_body_filter(r, in);
349 : }
350 :
351 :
352 : static ngx_int_t
353 18 : ngx_http_xss_filter_init(ngx_conf_t *cf)
354 : {
355 : int multi_http_blocks;
356 : ngx_http_xss_main_conf_t *xmcf;
357 :
358 18 : xmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_xss_filter_module);
359 :
360 18 : if (ngx_http_xss_prev_cycle != ngx_cycle) {
361 18 : ngx_http_xss_prev_cycle = ngx_cycle;
362 18 : multi_http_blocks = 0;
363 :
364 : } else {
365 0 : multi_http_blocks = 1;
366 : }
367 :
368 18 : if (multi_http_blocks || xmcf->requires_filter) {
369 0 : ngx_http_next_header_filter = ngx_http_top_header_filter;
370 0 : ngx_http_top_header_filter = ngx_http_xss_header_filter;
371 :
372 0 : ngx_http_next_body_filter = ngx_http_top_body_filter;
373 0 : ngx_http_top_body_filter = ngx_http_xss_body_filter;
374 : }
375 :
376 18 : return NGX_OK;
377 : }
378 :
379 :
380 : static void *
381 108 : ngx_http_xss_create_loc_conf(ngx_conf_t *cf)
382 : {
383 : ngx_http_xss_loc_conf_t *conf;
384 :
385 108 : conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_xss_loc_conf_t));
386 108 : if (conf == NULL) {
387 0 : return NULL;
388 : }
389 :
390 : /*
391 : * set by ngx_pcalloc():
392 : *
393 : * conf->callback_arg = { 0, NULL };
394 : * conf->input_types = { NULL };
395 : * conf->input_types_keys = NULL;
396 : * conf->output_type = { 0, NULL };
397 : */
398 :
399 108 : conf->get_enabled = NGX_CONF_UNSET;
400 108 : conf->check_status = NGX_CONF_UNSET;
401 108 : conf->override_status = NGX_CONF_UNSET;
402 :
403 108 : return conf;
404 : }
405 :
406 :
407 : static char *
408 90 : ngx_http_xss_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
409 : {
410 90 : ngx_http_xss_loc_conf_t *prev = parent;
411 90 : ngx_http_xss_loc_conf_t *conf = child;
412 :
413 90 : ngx_conf_merge_str_value(conf->callback_arg, prev->callback_arg, "");
414 :
415 90 : ngx_conf_merge_value(conf->get_enabled, prev->get_enabled, 0);
416 :
417 90 : ngx_conf_merge_value(conf->check_status, prev->check_status, 1);
418 :
419 90 : ngx_conf_merge_value(conf->override_status, prev->override_status, 1);
420 :
421 : #if defined(nginx_version) && nginx_version >= 8029
422 90 : if (ngx_http_merge_types(cf, &conf->input_types_keys, &conf->input_types,
423 : &prev->input_types_keys, &prev->input_types,
424 : ngx_http_xss_default_types)
425 : != NGX_OK)
426 : #else /* 0.7.x or 0.8.x < 0.8.29 */
427 : if (ngx_http_merge_types(cf, conf->input_types_keys, &conf->input_types,
428 : prev->input_types_keys, &prev->input_types,
429 : ngx_http_xss_default_types)
430 : != NGX_OK)
431 : #endif
432 : {
433 0 : return NGX_CONF_ERROR;
434 : }
435 :
436 90 : ngx_conf_merge_str_value(conf->output_type, prev->output_type,
437 : ngx_http_xss_default_output_type);
438 :
439 90 : return NGX_CONF_OK;
440 : }
441 :
442 :
443 : static void *
444 18 : ngx_http_xss_create_main_conf(ngx_conf_t *cf)
445 : {
446 : ngx_http_xss_main_conf_t *conf;
447 :
448 18 : conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_xss_main_conf_t));
449 18 : if (conf == NULL) {
450 0 : return NULL;
451 : }
452 :
453 : /* set by ngx_pcalloc:
454 : * conf->requires_filter = 0;
455 : */
456 :
457 18 : return conf;
458 : }
459 :
460 :
461 : static char *
462 0 : ngx_http_xss_get(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
463 : {
464 : ngx_http_xss_main_conf_t *xmcf;
465 :
466 0 : xmcf = ngx_http_conf_get_module_main_conf(cf,
467 : ngx_http_xss_filter_module);
468 :
469 0 : xmcf->requires_filter = 1;
470 :
471 0 : return ngx_conf_set_flag_slot(cf, cmd, conf);
472 : }
|