Skip to content

Commit ddfcaf0

Browse files
committed
waf feature: use $proxy_pass_uri
1 parent 3bc8136 commit ddfcaf0

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

src/ngx_http_modsecurity_module.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,14 @@ char *ngx_str_to_char(ngx_str_t a, ngx_pool_t *p)
130130
{
131131
char *str = NULL;
132132

133-
if (a.len == 0) {
133+
str = ngx_pnalloc(p, a.len+1);
134+
if (str == NULL) {
134135
return NULL;
135136
}
136137

137-
str = ngx_pnalloc(p, a.len+1);
138-
if (str == NULL) {
139-
dd("failed to allocate memory to convert space ngx_string to C string");
140-
/* We already returned NULL for an empty string, so return -1 here to indicate allocation error */
141-
return (char *)-1;
138+
if (a.len > 0) {
139+
ngx_memcpy(str, a.data, a.len);
142140
}
143-
ngx_memcpy(str, a.data, a.len);
144141
str[a.len] = '\0';
145142

146143
return str;

src/ngx_http_modsecurity_rewrite.c

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
ngx_int_t ngx_http_modsecurity_process_connection(ngx_http_request_t *r,
2525
ngx_http_modsecurity_ctx_t *ctx);
2626
ngx_int_t ngx_http_modsecurity_process_url(ngx_http_request_t *r,
27-
ngx_http_modsecurity_ctx_t *ctx);
27+
ngx_http_modsecurity_ctx_t *ctx,
28+
const char *uri, const char *method, const char *http_version);
2829
ngx_int_t ngx_http_modsecurity_process_req_header(ngx_http_request_t *r,
2930
ngx_http_modsecurity_ctx_t *ctx);
3031
ngx_int_t ngx_http_modsecurity_process_empty_req_body(ngx_http_request_t *r,
@@ -61,7 +62,7 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
6162
return rc;
6263
}
6364

64-
rc = ngx_http_modsecurity_process_url(r, ctx);
65+
rc = ngx_http_modsecurity_process_url(r, ctx, NULL, NULL, NULL);
6566
if (rc > 0) {
6667
return rc;
6768
}
@@ -137,36 +138,47 @@ ngx_http_modsecurity_process_connection(ngx_http_request_t *r,
137138

138139
ngx_int_t
139140
ngx_http_modsecurity_process_url(ngx_http_request_t *r,
140-
ngx_http_modsecurity_ctx_t *ctx)
141+
ngx_http_modsecurity_ctx_t *ctx,
142+
const char *uri, const char *method, const char *http_version)
141143
{
142144
ngx_pool_t *old_pool;
143-
const char *http_version, *n_uri, *n_method;
144-
145-
switch (r->http_version) {
146-
case NGX_HTTP_VERSION_9 :
147-
http_version = "0.9";
148-
break;
149-
case NGX_HTTP_VERSION_10 :
150-
http_version = "1.0";
151-
break;
152-
case NGX_HTTP_VERSION_11 :
153-
http_version = "1.1";
154-
break;
155-
case NGX_HTTP_VERSION_20 :
156-
http_version = "2.0";
157-
break;
158-
default :
159-
http_version = "1.0";
160-
break;
145+
146+
if (http_version == NULL) {
147+
switch (r->http_version) {
148+
case NGX_HTTP_VERSION_9 :
149+
http_version = "0.9";
150+
break;
151+
case NGX_HTTP_VERSION_10 :
152+
http_version = "1.0";
153+
break;
154+
case NGX_HTTP_VERSION_11 :
155+
http_version = "1.1";
156+
break;
157+
case NGX_HTTP_VERSION_20 :
158+
http_version = "2.0";
159+
break;
160+
default :
161+
http_version = "1.0";
162+
break;
163+
}
161164
}
162165

163-
n_uri = ngx_str_to_char(r->unparsed_uri, r->pool);
164-
n_method = ngx_str_to_char(r->method_name, r->pool);
165-
if (n_uri == (char*)-1 || n_method == (char*)-1) {
166-
return NGX_HTTP_INTERNAL_SERVER_ERROR;
166+
if (uri == NULL) {
167+
uri = ngx_str_to_char(r->unparsed_uri, r->pool);
168+
if (uri == NULL) {
169+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
170+
}
167171
}
172+
173+
if (method == NULL) {
174+
method = ngx_str_to_char(r->method_name, r->pool);
175+
if (method == NULL) {
176+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
177+
}
178+
}
179+
168180
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);
169-
msc_process_uri(ctx->modsec_transaction, n_uri, n_method, http_version);
181+
msc_process_uri(ctx->modsec_transaction, uri, method, http_version);
170182
ngx_http_modsecurity_pcre_malloc_done(old_pool);
171183

172184
dd("Processing intervention with the transaction information filled in (uri, method and version)");

0 commit comments

Comments
 (0)