diff --git a/README b/README index 7fdf3d7..bdb1e2b 100644 --- a/README +++ b/README @@ -17,7 +17,7 @@ If-Unmodified-Since, If-Modified-Since, Date, Accept-Encoding, Accept-Language, Accept-Charset, Max-Forwards, If-Match, If-None-Match, Last-Modified, Content-Length, Expires, - Content-Language, Content-Encoding, Allow + Content-Language, Content-Encoding, Allow, Host Report Bugs Create a ticket on the issue tracking interface of GitHub: diff --git a/ngx_http_header_inspect.c b/ngx_http_header_inspect.c index 943750a..f977622 100644 --- a/ngx_http_header_inspect.c +++ b/ngx_http_header_inspect.c @@ -35,6 +35,7 @@ static ngx_int_t ngx_header_inspect_digit_header(char* header, ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); static ngx_int_t ngx_header_inspect_ifmatch_header(char* header, ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); static ngx_int_t ngx_header_inspect_allow_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); +static ngx_int_t ngx_header_inspect_host_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); static ngx_int_t ngx_header_inspect_ifrange_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); static ngx_int_t ngx_header_inspect_date_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, char *header, ngx_str_t value); static ngx_int_t ngx_header_inspect_process_request(ngx_http_request_t *r); @@ -1297,6 +1298,57 @@ return rc; } +static ngx_int_t ngx_header_inspect_host_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value) { + u_char d = '\0'; + ngx_uint_t i = 0; + + if ( value.len == 0 ) { + return NGX_OK; + } + + while ( i < value.len ) { + d = value.data[i]; + + if ( + ((d < '0') || (d > '9')) + && ((d < 'a') || (d > 'z')) + && ((d < 'A') || (d > 'Z')) + && (d != '.') && (d != '-') + && ((d != ':') || (i == 0)) + ) { + if ( conf->log ) { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: illegal char at position %d in Host header \"%s\"", i, value.data); + } + return NGX_ERROR; + break; + } + if ( d == ':' ) { + break; + } + i++; + } + if ( (d == ':') && (i+1 < value.len) ) { + i++; + for ( ; i < value.len ; i++ ) { + if ( (value.data[i] < '0') || (value.data[i] > '9') ) { + if ( conf->log ) { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: illegal char at position %d in Host header \"%s\"", i, value.data); + } + return NGX_ERROR; + } + } + } + + if ( i != value.len ) { + if ( conf->log ) { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: unexpected end of Host header \"%s\"", value.data); + } + return NGX_ERROR; + } + + return NGX_OK; +} + static ngx_int_t ngx_header_inspect_allow_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value) { ngx_int_t rc = NGX_AGAIN; ngx_uint_t i = 0; @@ -1489,6 +1541,11 @@ if ((rc != NGX_OK) && conf->block) { return NGX_HTTP_BAD_REQUEST; } + } else if ((h[i].key.len == 4) && (ngx_strcmp("Host", h[i].key.data) == 0) ) { + rc = ngx_header_inspect_host_header(conf, r->connection->log, h[i].value); + if ((rc != NGX_OK) && conf->block) { + return NGX_HTTP_BAD_REQUEST; + } } else { /* TODO: support for other headers */ if (conf->log_uninspected) {