ModSecurity icon indicating copy to clipboard operation
ModSecurity copied to clipboard

Nginx with Modsecurity: POST request gives 500 error

Open pijnack opened this issue 13 years ago • 141 comments

Nginx with Modsecurity gives 500 errors after a POST request.

In error.log:

2013/10/23 13:21:07 [alert] 29432#0: *4 no upstream configuration, client: 88.159.16.136, server: test.huygenscollege.nl, request: "POST /proefwerk/prog/index.php?day=28&month=10&year=2013&klas=2 HTTP/1.1", host: "test.huygenscollege.nl", referrer: "http://test.huygenscollege.nl/proefwerk/prog/index.php?day=4&month=11&year=2013&klas=2"

In nginx.conf:

upstream smoothrac { server 88.159.13.153; } server { listen 80; server_name test.huygenscollege.nl; location / { ModSecurityEnabled on; ModSecurityConfig modsecurity.conf; proxy_pass http://smoothrac/; proxy_set_header Host test.huygenscollege.nl; proxy_read_timeout 150s; } }

After changing ModSecurityEnabled on to off, the problem is gone. I guess something is missing in my upstream configuration?

Piet Pijnacker, Eindhoven, Netherlands

pijnack avatar Oct 23 '13 11:10 pijnack

Hi @pijnack, I am reopening this issue, I'm investigating it.

zimmerle avatar Oct 23 '13 20:10 zimmerle

Wordpress set multiple cookies in the very same request, which is a issue in our Nginx implementation as report at #154. It is not directly related to this issue, but it is somehow related in the sense that it does affect the behavior of Wordpress.

zimmerle avatar Oct 23 '13 20:10 zimmerle

I also had two issues regarding "POST request 500 error" and only one set-cookie. The Fix #154 solved only one set-cookie problem. But "POST request 500 error" still remains.

yumahil avatar Dec 02 '13 06:12 yumahil

Same issue here , no upstream or proxy setting used. just nginx php-fpm.

Even more weird is this only triggers when 'Host: ' header is set - using browser or curl etc So using this http://wiki.nginx.org/HttpHeadersMoreModule - "more_clear_input_headers 'Host';" is one workaround for some folks where the application doesn't need the host headers.

jijojv avatar Feb 04 '14 06:02 jijojv

I've got the same issue, any ideas?

shiguti avatar Mar 21 '14 12:03 shiguti

Same issue for me with IPB. Latest NGINX and ModSecurity version and ASL rules. If SecRequestBodyAccess is enabled, it will give a 500 error with POST requests while writing nothing useful on the ModSecurity logs, even with debug level 9 and in the NGINX logs it only produces the lines mention in OP. This issue was reported in #115 10 months ago and nobody took care of it? I can provide server access to one of the devs if no one is able to reproduce it - it's a non-production environment.

infinitnet avatar Apr 09 '14 17:04 infinitnet

Hi @infinitnet, there are some problems with the way that ModSecurity is handling the post requests on nginx. If you look at #142 you will be able to see that contents over 8k are causing problems as well. Our version 2.8.0-RC1 contain a fix related to the issue #645. But we still have problems while the request body is bigger than a certain amount of data. The main reason for the problem is that ModSecrurity is not assembling Nginx chunks right, the function is here:

https://github.com/SpiderLabs/ModSecurity/blob/master/nginx/modsecurity/ngx_http_modsecurity.c#L1011-L1020

In certain cases (e.g. when the request body content is not complete) this function should return NGX_AGAIN, so that nginx will call it again with the rest of the body data.

zimmerle avatar Apr 10 '14 14:04 zimmerle

@zimmerle I'm actually using 2.8.0-RC1 already. I didn't check the POST data size, but the issue appears even with simple login forms, rendering ModSecurity pretty much unusable with NGINX.

[10/Apr/2014:16:19:03 +0200] "POST /index.php?app=core&module=global&section=login&do=process HTTP/1.1" 500

[alert] 19480#0: *4 no upstream configuration, client: 1.2.3.4, server: domain.com, request: "POST /index.php?app=core&module=global&section=login&do=process HTTP/1.1"

And nothing useful in the audit or debug log.

infinitnet avatar Apr 10 '14 14:04 infinitnet

@zimmerle I should have checked dmesg earlier, actually, it's indeed somehow related to APR (my version of libapr1(-dev) is 1.4.6-3+deb7u1), as I'm getting segfaults for each NGINX worker when starting NGINX:

Apr 10 17:01:13 a143 kernel: [787456.369365] nginx[19482]: segfault at 7fe879c61098 ip 00007fe88484792d sp 00007fff04fc4b90 error 4 in libapr-1.so.0.4.6[7fe88482d000+30000] Apr 10 17:01:13 a143 kernel: [787456.374651] nginx[19480]: segfault at 7fe879c61098 ip 00007fe88484792d sp 00007fff04fc4b90 error 4 in libapr-1.so.0.4.6[7fe88482d000+30000] Apr 10 17:01:13 a143 kernel: [787456.375342] nginx[19481]: segfault at 7fe879c61098 ip 00007fe88484792d sp 00007fff04fc4b90 error 4 in libapr-1.so.0.4.6[7fe88482d000+30000] Apr 10 17:01:13 a143 kernel: [787456.377620] nginx[19483]: segfault at 7fe879c61098 ip 00007fe88484792d sp 00007fff04fc4b90 error 4 in libapr-1.so.0.4.6[7fe88482d000+30000]

infinitnet avatar Apr 10 '14 15:04 infinitnet

@infinitnet This happens when you start nginx or whenever you receive a new request?

zimmerle avatar Apr 10 '14 15:04 zimmerle

@zimmerle Updated my comment, it happens for each NGINX worker process when starting NGINX, not with each request and also not when I get the 500 error, only when starting it.

infinitnet avatar Apr 10 '14 15:04 infinitnet

it just happens if SecRequestBodyAccess is marked as on ?

Can you run into inside gdb and share a stack trace? https://github.com/SpiderLabs/ModSecurity/wiki/Debugging-ModSecurity

Thanks.

zimmerle avatar Apr 10 '14 15:04 zimmerle

@zimmerle Actually it happens also with SecRequestBodyAccess Off, so I'm not sure if the segfault issue is even related. Even though I compiled NGINX --with-debug and CFlags -g -O0 for both NGINX and mod_sec and both daemon and master_process off in my NGINX config, I can't get any useful info (stack trace) with GDB. If you could add me on Skype (infinitnet) or let me know an IRC channel, I can provide you access. Here's a normal strace if that helps: http://pastebin.com/3DcEni6h

infinitnet avatar Apr 10 '14 15:04 infinitnet

helpful

brennantom avatar Apr 10 '14 15:04 brennantom

Update: We figured that the 500 error with POST requests is related to the missing NGX_AGAIN and a fix is in progress apparently. Maybe this statement should be put in all the bug reports related to this, like #115 #142 #645 etc.

Edit: The segfault is not related to ModSecurity.

infinitnet avatar Apr 10 '14 16:04 infinitnet

Is there any ETA on the error 500 issue when processing POST requests yet (NGX_AGAIN)?

infinitnet avatar Apr 15 '14 22:04 infinitnet

This was reported 6 months ago and makes a huge part of ModSecurity useless with NGINX and yet it doesn't have anyone assigned to fix it or anything. I'd very much like to know about the progress on this.

infinitnet avatar Apr 22 '14 12:04 infinitnet

Hi @pijnack large POSTs with SecRequestBodyAccess On, should be functional in the following branch:

https://github.com/SpiderLabs/ModSecurity/tree/nginx_refactoring

Can you test and confirm it?

Thanks, F.

zimmerle avatar May 06 '14 12:05 zimmerle

Hello,

I'm having this problem too, my scenario:

  • FreeBSD 9.1
  • nginx 1.5.6 & nginx 1.7.0
  • modsecurity: 2.7.7 & 2.8.0

I've tried the nginx_refactoring tree (I just switched the "nginx" folder within modsecurity) with nginx 1.7.0 and it doesn't even give error 500, it core dumped, here is a gdb run of a core:

http://pastebin.com/winjWMY7

badcrc avatar May 28 '14 07:05 badcrc

Hi again,

I'm trying a clean, hand-compiled (i was using ports before) version of nginx 1.7.1 against nginx_refactoring and I'm still getting "no upstream configuration".

 # ./nginx -V 
nginx version: nginx/1.7.1
built by gcc 4.2.1 20070831 patched [FreeBSD]
TLS SNI support enabled
configure arguments: --prefix=/usr/local/etc/nginx --with-cc-opt='-I /usr/local/include' --with-ld-opt='-L /usr/local/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --sbin-path=/usr/local/sbin/nginx --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx-error.log --user=www --group=www --with-ipv6 --http-client-body-temp-path=/var/tmp/nginx/client_body_temp --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi_temp --http-proxy-temp-path=/var/tmp/nginx/proxy_temp --http-scgi-temp-path=/var/tmp/nginx/scgi_temp --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi_temp --http-log-path=/var/log/nginx-access.log --add-module=/expert/software/agentzh-headers-more-nginx-module-0c6e05d --with-http_geoip_module --with-http_stub_status_module --add-module=/expert/software/ModSecurity-nginx_refactoring/nginx/modsecurity --with-pcre --with-http_ssl_module

And I've compiled ModSecurity with:

--enable-standalone-module --disable-apache2-module"

I get the error with and without an Upstream configured:

location {
        ModSecurityEnabled on;
        ModSecurityConfig modsecurity/web-modsecurity.conf;

        proxy_pass        http://AAA.BBB.CCC.DDD:80;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Protocol $scheme;

        proxy_set_header Host $http_host;

    } 
upstream my_server{
    server AAA.BBB.CCC.DDD:80;
}

location {
        ModSecurityEnabled on;
        ModSecurityConfig modsecurity/web-modsecurity.conf;

        proxy_pass        http://myserver;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Protocol $scheme;

        proxy_set_header Host $http_host;

    } 

badcrc avatar May 28 '14 14:05 badcrc