Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a header from nginx to uWSGI backend running a Flask application

I have a nginx server running on Debian. The same server is also running uWSGI with a Flask Python application. One thing I'm trying to do is pass an HTTP header to the Flask application from nginx that's determined by a nginx condition, namely which virtual-host is running the request.

I'm trying to pass an HTTP header from nginx (not from the client) to the uWSGI backend but it isn't showing up in Flask's DebugToolbar under HTTP request.

Relevant section from nginx config

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri =404 @flaskapplication;
}
        location @flaskapplication {
            include uwsgi_params;
            uwsgi_pass unix:/tmp/flaskapplication.sock;
            uwsgi_param HTTP_X_TESTING 'bar';
            uwsgi_modifier1 30;
        }

While running said nginx config said header doesn't show up under Flask-DebugToolbar HTTP request section, although I'm not sure how reliable this is because I injected a HTTP header I know influences application behavior using Burp Suite X-Foo-Bar: 1 and it didn't show up under the HTTP request section but it did influence the application behavior (A certain if statement in @app.before_request) Obviously I would delete the header if the client added it for security reasons.

The above screenshot is HTTP Headers with Flask-DebugToolbar trying to do uwsgi_param for HTTP headers.

Thanks

like image 466
jtl999 Avatar asked Jun 29 '17 03:06

jtl999


1 Answers

Solved it.

I enabled Flask's debug mode and raised an exception to gain access to the REPL.

With uwsgi_param HTTP_X_FOO 'Foo bar lorem ipsum'; in the nginx configuration in the relevant location block, request.headers looks like this (private and unrelated information has been redacted)

So request.headers != Flask-DebugToolbar request panel.

like image 96
jtl999 Avatar answered Nov 15 '22 01:11

jtl999