comparison src/http/modules/ngx_http_referer_module.c @ 7079:7564a919d333

Referer: fixed $invalid_referer. The variable was considered non-existent in the absence of any valid_referers directives. Given the following config snippet, location / { return 200 $invalid_referer; } location /referer { valid_referers server_names; } "location /" should work identically and independently on other "location /referer". The fix is to always add the $invalid_referer variable as long as the module is compiled in, as is done by other modules.
author Ruslan Ermilov <ru@nginx.com>
date Fri, 04 Aug 2017 08:01:55 +0300
parents 2cd019520210
children
comparison
equal deleted inserted replaced
7078:1eb753aa8e5e 7079:7564a919d333
30 ngx_uint_t referer_hash_max_size; 30 ngx_uint_t referer_hash_max_size;
31 ngx_uint_t referer_hash_bucket_size; 31 ngx_uint_t referer_hash_bucket_size;
32 } ngx_http_referer_conf_t; 32 } ngx_http_referer_conf_t;
33 33
34 34
35 static ngx_int_t ngx_http_referer_add_variables(ngx_conf_t *cf);
35 static void * ngx_http_referer_create_conf(ngx_conf_t *cf); 36 static void * ngx_http_referer_create_conf(ngx_conf_t *cf);
36 static char * ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent, 37 static char * ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent,
37 void *child); 38 void *child);
38 static char *ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, 39 static char *ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd,
39 void *conf); 40 void *conf);
75 ngx_null_command 76 ngx_null_command
76 }; 77 };
77 78
78 79
79 static ngx_http_module_t ngx_http_referer_module_ctx = { 80 static ngx_http_module_t ngx_http_referer_module_ctx = {
80 NULL, /* preconfiguration */ 81 ngx_http_referer_add_variables, /* preconfiguration */
81 NULL, /* postconfiguration */ 82 NULL, /* postconfiguration */
82 83
83 NULL, /* create main configuration */ 84 NULL, /* create main configuration */
84 NULL, /* init main configuration */ 85 NULL, /* init main configuration */
85 86
105 NULL, /* exit master */ 106 NULL, /* exit master */
106 NGX_MODULE_V1_PADDING 107 NGX_MODULE_V1_PADDING
107 }; 108 };
108 109
109 110
111 static ngx_str_t ngx_http_invalid_referer_name = ngx_string("invalid_referer");
112
113
110 static ngx_int_t 114 static ngx_int_t
111 ngx_http_referer_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, 115 ngx_http_referer_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
112 uintptr_t data) 116 uintptr_t data)
113 { 117 {
114 u_char *p, *ref, *last; 118 u_char *p, *ref, *last;
256 } 260 }
257 261
258 valid: 262 valid:
259 263
260 *v = ngx_http_variable_null_value; 264 *v = ngx_http_variable_null_value;
265
266 return NGX_OK;
267 }
268
269
270 static ngx_int_t
271 ngx_http_referer_add_variables(ngx_conf_t *cf)
272 {
273 ngx_http_variable_t *var;
274
275 var = ngx_http_add_variable(cf, &ngx_http_invalid_referer_name,
276 NGX_HTTP_VAR_CHANGEABLE);
277 if (var == NULL) {
278 return NGX_ERROR;
279 }
280
281 var->get_handler = ngx_http_referer_variable;
261 282
262 return NGX_OK; 283 return NGX_OK;
263 } 284 }
264 285
265 286
450 static char * 471 static char *
451 ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 472 ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
452 { 473 {
453 ngx_http_referer_conf_t *rlcf = conf; 474 ngx_http_referer_conf_t *rlcf = conf;
454 475
455 u_char *p; 476 u_char *p;
456 ngx_str_t *value, uri, name; 477 ngx_str_t *value, uri;
457 ngx_uint_t i; 478 ngx_uint_t i;
458 ngx_http_variable_t *var;
459
460 ngx_str_set(&name, "invalid_referer");
461
462 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
463 if (var == NULL) {
464 return NGX_CONF_ERROR;
465 }
466
467 var->get_handler = ngx_http_referer_variable;
468 479
469 if (rlcf->keys == NULL) { 480 if (rlcf->keys == NULL) {
470 rlcf->keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t)); 481 rlcf->keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t));
471 if (rlcf->keys == NULL) { 482 if (rlcf->keys == NULL) {
472 return NGX_CONF_ERROR; 483 return NGX_CONF_ERROR;