Python & Ruby webserver config – the great misunderstanding

Two months ago I ran a huge global scan for unintentionally exposed .git repositories. I was surprised to find many Python and Ruby applications with this issue. The total number wasn’t very high – around two thousand, but when I normalized it according to the market share of these programming languages, the situation was worse than the case of PHP.

How could it even be possible that this kind of app returns the static files outside its own directory?

So, I did some research of the affected sites and tried to contact their owners for more details. I suspected a misconfigured web server. It is a common technique to let the web server manage the static files. If done incorrectly and your app is placed in the webroot folder, then the web server will serve all existing files – plain source codes included.

And I was right.

The reason

In the Nginx servers configs, I found these 2 common snippets (the issue is the same for Apache):

try_files $uri @app;

and

if (!-f $request_filename) {
proxy_pass @app;
break;
}

The first one is how Nginx checks to see if the file exists (https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#check-if-file-exists). The second one is probably a direct transcription from Apache’s .htaccess rules with the same functionality:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ http://app/$1 [P]

All these rules work in a similar manner:

if requested file doesn’t exists => send request to the app => else serve the requested existing file = big trouble if your application is placed in webroot

Example app:

/static/logo.png
/flaskapp.py (running on port 5000)

Config:

try_files $uri @app;

location @app {
proxy_pass http://127.0.0.1:5000;
...
}

Requests:

  • /static/logo.png => file logo.png exists => logo.png returned
  • /contacts => file contacts doesn’t exists => request is routed to the application on port 5000
  • /flaskapp.py => file exists => the source of flaskapp.py is returned

Examples of tutorials with bad config:

It is easy to find many more of these tutorials and repositories with this bad configuration.

How to do it right?

The golden rule is to not place you application files inside the webroot folder, but, it isn’t always possible. Either way, you can use the following draft of the config:

# serve static files directly from its folder
location /static/ {
alias /.../app/static;
}
# let the app process the rest
location / {
proxy_pass http://127.0.0.1:5000;
}

This is a very serious issue and there are a huge number of affected sites. As I ran more scans focusing on this issue I found more than ten thousand sites in total. The scans weren’t complex, so the actual numbers could be much higher. I tried to contact the authors of the tutorial and examples, but many of them abandoned their projects years ago. My final recommendation: don’t blindly follow every tutorial you find and try to adapt it to your current situation.


Discover more from Vladimir Smitka

Subscribe to get the latest posts sent to your email.

4 responses to “Python & Ruby webserver config – the great misunderstanding”

  1. Globální scan otevřených .git repozitářů – lynt.stage.wp5.cz

    […] další kritický problém s chybně nakonfigurovanými webovými servery, postihující tisíce dalších […]

    Like

  2. valentine

    Kindly need your help someone hacked my account

    Like

  3. Globální scan otevřených .git repozitářů – WP by LYNT

    […] další kritický problém s chybně nakonfigurovanými webovými servery, postihující tisíce dalších […]

    Like

  4. Open .git scan – the results – Vladimir Smitka

    […] But after normalization the numbers according to the market share, the worst situation is among the Python: […]

    Like

Leave a comment

About Me

My name is Vladimir Smitka and I’m a security researcher/hobbyist from the Czech Republic. I’m also the owner of Lynt, a PPC Agency. I’m also an active member of the Czech WordPress community and one of the WordCamp Prague organizers.

OPEN .GIT GLOBAL SCAN

  • 230 000 000 sites scanned 🔍
  • 390 000 sites affected 😥
  • 100 000 mail send to the developers 📧
  • 150 000+ sites fixed 
  • 100+ possitive comments 🗨️
  • 3500+ thankyou mails ❤️
  • Thousands and thousands sites with another serious issue found 😑

For my research I use affordable Virtual Private Servers from Digital Ocean (they have a great infrascruture), Linode (they have a great understanding for my work) and dedicted servers from Hetzner.

If you like my research, you can make a small donation for coffee and VPS – two basic ingredients for my future security scans.

Follow me

Our Projects

Latest Articles