未加星标

Python & Ruby webserver config the great misunderstanding

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Two months ago I ran ahuge 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 ofphp.

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

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:

https://tutos.readthedocs.io/en/latest/source/ndg.html https://django-best-practices.readthedocs.io/en/latest/deployment/servers.html

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.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:Python & Ruby webserver config the great misunderstanding
本站链接:https://www.codesec.net/view/611854.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(11)