From 3e00d4a425efcce6b32c6969b5aa8fc5f7d0315f Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Jul 04 2025 22:38:57 +0000 Subject: Use the proxy middleware from werkzeug Signed-off-by: Aurélien Bompard --- diff --git a/fedocal/__init__.py b/fedocal/__init__.py index d4da302..c9ce03d 100644 --- a/fedocal/__init__.py +++ b/fedocal/__init__.py @@ -49,6 +49,7 @@ from functools import wraps from pytz import common_timezones from six.moves.urllib.parse import urlparse, urljoin from sqlalchemy.exc import SQLAlchemyError +from werkzeug.middleware.proxy_fix import ProxyFix from werkzeug.utils import secure_filename from fedocal.fedocal_babel import Babel @@ -58,7 +59,6 @@ from fedocal.fedocal_babel import format_datetime import fedocal.forms as forms import fedocal.fedocallib as fedocallib import fedocal.mail_logging -import fedocal.proxy from fedocal.fedocallib.exceptions import FedocalException from fedocal.fedocallib.model import (Calendar, Meeting) @@ -96,7 +96,7 @@ APP.static_folder = [ OIDC = OpenIDConnect(APP, credentials_store=flask.session ) -APP.wsgi_app = fedocal.proxy.ReverseProxied(APP.wsgi_app) +APP.wsgi_app = ProxyFix(APP.wsgi_app, x_proto=1, x_host=1) SESSION = fedocallib.create_session(APP.config['DB_URL']) if not APP.debug: diff --git a/fedocal/proxy.py b/fedocal/proxy.py deleted file mode 100644 index 89bc75d..0000000 --- a/fedocal/proxy.py +++ /dev/null @@ -1,66 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright © 2014 Red Hat, Inc. -# -# This copyrighted material is made available to anyone wishing to use, -# modify, copy, or redistribute it subject to the terms and conditions -# of the GNU General Public License v.2, or (at your option) any later -# version. This program is distributed in the hope that it will be -# useful, but WITHOUT ANY WARRANTY expressed or implied, including the -# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR -# PURPOSE. See the GNU General Public License for more details. You -# should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, -# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -# -# Any Red Hat trademarks that are incorporated in the source -# code or documentation are not subject to the GNU General Public -# License and may only be used or replicated with the express permission -# of Red Hat, Inc. -# -from __future__ import unicode_literals, absolute_import, print_function - -''' -Makes fedocal an application behind a reverse proxy and thus ensure the -redirects are using ``https``. - -Source: http://flask.pocoo.org/snippets/35/ by Peter Hansen -''' - - -class ReverseProxied(object): - '''Wrap the application in this middleware and configure the - front-end server to add these headers, to let you quietly bind - this to a URL other than / and to an HTTP scheme that is - different than what is used locally. - - In nginx: - location /myprefix { - proxy_pass http://192.168.0.1:5001; - proxy_set_header Host $host; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Scheme $scheme; - proxy_set_header X-Script-Name /myprefix; - } - - :param app: the WSGI application - ''' - def __init__(self, app): - self.app = app - - def __call__(self, environ, start_response): - script_name = environ.get('HTTP_X_SCRIPT_NAME', '') - if script_name: - environ['SCRIPT_NAME'] = script_name - path_info = environ['PATH_INFO'] - if path_info.startswith(script_name): - environ['PATH_INFO'] = path_info[len(script_name):] - - server = environ.get('HTTP_X_FORWARDED_HOST', '') - if server: - environ['HTTP_HOST'] = server - - scheme = environ.get('HTTP_X_SCHEME', '') - if scheme: - environ['wsgi.url_scheme'] = scheme - return self.app(environ, start_response)