diff --git a/README b/README index d44dd0d..ab36bdb 100644 --- a/README +++ b/README @@ -19,7 +19,7 @@ If-Match, If-None-Match, Last-Modified, Content-Length, Expires, Content-Language, Content-Encoding, Allow, Host, Accept, Connection, Content-Range, User-Agent, Upgrade, Via, - From, Pragma, Content-Type + From, Pragma, Content-Type, Content-MD5 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 e86eda6..9f201b6 100644 --- a/ngx_http_header_inspect.c +++ b/ngx_http_header_inspect.c @@ -46,6 +46,7 @@ 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_pragma_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); static ngx_int_t ngx_header_inspect_contenttype_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); +static ngx_int_t ngx_header_inspect_contentmd5_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); @@ -1440,6 +1441,54 @@ return rc; } +static ngx_int_t ngx_header_inspect_contentmd5_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value) { + ngx_uint_t i; + u_char d; + + if ( value.len == 0 ) { + if ( conf->log ) { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: empty Content-MD5 header"); + } + return NGX_ERROR; + } + + for ( i = 0; i < value.len; i++ ) { + d = value.data[i]; + + if ( (d >= '0') && (d <= '9') ) { + continue; + } + if ( (d >= 'a') && (d <= 'z') ) { + continue; + } + if ( (d >= 'A') && (d <= 'Z') ) { + continue; + } + if ( (d == '+') || (d == '/') ) { + continue; + } + if ( d == '=' ) { + i++; + while ( i < value.len ) { + if ( value.data[i] != '=' ) { + if ( conf->log ) { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: trailing characters at position %d in Content-MD5 header \"%s\"", i, value.data); + } + return NGX_ERROR; + } + i++; + } + break; + } + if ( conf->log ) { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: illegal character at position %d in Content-MD5 header \"%s\"", i, value.data); + } + return NGX_ERROR; + } + + return NGX_OK; +} + static ngx_int_t ngx_header_inspect_contenttype_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value) { ngx_uint_t v; @@ -2505,6 +2554,11 @@ if ((rc != NGX_OK) && conf->block) { return NGX_HTTP_BAD_REQUEST; } + } else if ((h[i].key.len == 11) && (ngx_strcmp("Content-MD5", h[i].key.data) == 0) ) { + rc = ngx_header_inspect_contentmd5_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) {