Skip to content

Commit

Permalink
Add use_x_forwarded_for option that applies ProxyFix to correctly han…
Browse files Browse the repository at this point in the history
…dle X-Forwarded-For header (#845)
  • Loading branch information
psrok1 committed Jul 6, 2023
1 parent 57d5789 commit 23dc0f8
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docker/mwdb.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ local_plugins_autodiscover = 1
request_timeout = 20000
file_upload_timeout = 60000
statement_timeout = 15000

use_x_forwarded_for = 1

[mwdb_limiter]
4 changes: 4 additions & 0 deletions docs/setup-and-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ Web application settings:
* ``flask_config_file`` (string) - additional file containing Flask configuration (.py)
* ``admin_login`` (string) - administrator account name
* ``admin_password`` (string) - initial password for administrator account
* ``use_x_forwarded_for`` (0 or 1) - Set this to 1 if MWDB backend is behind reverse proxy, so X-Forwarded-For header is correctly translated to ``request.remote_addr`` value. Set by default to 1 in ``certpl/mwdb`` Docker image.


Plugin settings:
Expand Down Expand Up @@ -366,3 +367,6 @@ Other endpoints are limited by default limits.
.. note::

Complete list of possible rate-limit parameters is placed in ``mwdb-core\mwdb\core\templates\mwdb.ini.tmpl`` file - section ``mwdb_limiter``.

If your MWDB instance uses standalone installation and MWDB backend is behind reverse proxy, make sure that use_x_forwarded_for is set to 1
and your reverse proxy correctly sets X-Forwarded-For header with real remote IP.
1 change: 1 addition & 0 deletions mwdb/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def log_request(response):
"status": response.status_code,
"response_time": response_time,
"response_size": response_size,
"remote_addr": request.remote_addr,
},
)

Expand Down
4 changes: 4 additions & 0 deletions mwdb/core/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Blueprint, Flask
from werkzeug.middleware.proxy_fix import ProxyFix

from mwdb.core.config import app_config
from mwdb.core.rate_limit import limiter
Expand All @@ -11,4 +12,7 @@
api = Service(app, api_blueprint)
app.register_blueprint(api_blueprint)

if app_config.mwdb.use_x_forwarded_for:
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1)

limiter.init_app(app)
1 change: 1 addition & 0 deletions mwdb/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class MWDBConfig(Config):
enable_json_logger = key(cast=intbool, required=False, default=False)
enable_sql_profiler = key(cast=intbool, required=False, default=False)
log_only_slow_sql = key(cast=intbool, required=False, default=False)
use_x_forwarded_for = key(cast=intbool, required=False, default=False)


@section("karton")
Expand Down
2 changes: 1 addition & 1 deletion mwdb/core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def setup_logger():
" - %(message)s"
)
handler.setFormatter(formatter)
logger.addFilter(ContextFilter())
handler.addFilter(ContextFilter())
logger.addHandler(handler)
logger.setLevel(logging.INFO)

Expand Down

0 comments on commit 23dc0f8

Please sign in to comment.