diff --git a/development.ini b/development.ini --- a/development.ini +++ b/development.ini @@ -114,6 +114,9 @@ cut_off_limit = 256000 ## WSGI environment variable to get the IP address of the client (default REMOTE_ADDR) #remote_addr_variable = HTTP_X_FORWARDED_FOR +## WSGI environment variable to get the protocol (http or https) of the client connection (default wsgi.url_scheme) +#url_scheme_variable = HTTP_X_FORWARDED_PROTO + ## always pretend the client connected using HTTPS (default false) #force_https = true diff --git a/docs/setup.rst b/docs/setup.rst --- a/docs/setup.rst +++ b/docs/setup.rst @@ -432,11 +432,10 @@ HTTP header, set:: Kallithea will by default rely on finding the protocol (``http`` or ``https``) in the WSGI environment as ``wsgi.url_scheme``. If the proxy server puts -the protocol of the client request in the ``X-Url-Scheme``, -``X-Forwarded-Scheme``, or ``X-Forwarded-Proto`` HTTP header, -Kallithea can be configured to trust these headers by setting:: +the protocol of the client request in the ``X-Forwarded-Proto`` HTTP header, +Kallithea can be configured to trust that header by setting:: - https_fixup = true + url_scheme_variable = HTTP_X_FORWARDED_PROTO HTTPS support @@ -447,9 +446,8 @@ Kallithea will by default generate URLs Alternatively, you can use some special configuration settings to control directly which scheme/protocol Kallithea will use when generating URLs: -- With ``https_fixup = true``, the scheme will be taken from the - ``X-Url-Scheme``, ``X-Forwarded-Scheme`` or ``X-Forwarded-Proto`` HTTP header - (default ``http``). +- With ``url_scheme_variable`` set, the scheme will be taken from that HTTP + header. - With ``force_https = true``, the scheme will be seen as ``https``. - With ``use_htsts = true``, Kallithea will set ``Strict-Transport-Security`` when using https. diff --git a/kallithea/config/application.py b/kallithea/config/application.py --- a/kallithea/config/application.py +++ b/kallithea/config/application.py @@ -35,7 +35,7 @@ def wrap_app(app): app = SimpleGit(app, config) # Enable https redirects based on HTTP_X_URL_SCHEME set by proxy - if any(asbool(config.get(x)) for x in ['https_fixup', 'force_https', 'use_htsts']): + if any(asbool(config.get(x)) for x in ['url_scheme_variable', 'force_https', 'use_htsts']): app = HttpsFixup(app, config) app = PermanentRepoUrl(app, config) diff --git a/kallithea/config/middleware/https_fixup.py b/kallithea/config/middleware/https_fixup.py --- a/kallithea/config/middleware/https_fixup.py +++ b/kallithea/config/middleware/https_fixup.py @@ -26,6 +26,7 @@ Original author and date, and relevant c """ +import kallithea from kallithea.lib.utils2 import asbool @@ -54,20 +55,17 @@ class HttpsFixup(object): middleware you should set this header inside your proxy ie. nginx, apache etc. """ - # DETECT PROTOCOL ! - if 'HTTP_X_URL_SCHEME' in environ: - proto = environ.get('HTTP_X_URL_SCHEME') - elif 'HTTP_X_FORWARDED_SCHEME' in environ: - proto = environ.get('HTTP_X_FORWARDED_SCHEME') - elif 'HTTP_X_FORWARDED_PROTO' in environ: - proto = environ.get('HTTP_X_FORWARDED_PROTO') - else: - proto = 'http' - org_proto = proto + proto = None # if we have force, just override if asbool(self.config.get('force_https')): proto = 'https' + else: + # get protocol from configured WSGI environment variable + url_scheme_variable = kallithea.CONFIG.get('url_scheme_variable') + if url_scheme_variable: + proto = environ.get(url_scheme_variable) - environ['wsgi.url_scheme'] = proto - environ['wsgi._org_proto'] = org_proto + if proto: + environ['wsgi._org_proto'] = environ.get('wsgi.url_scheme') + environ['wsgi.url_scheme'] = proto diff --git a/kallithea/templates/ini/template.ini.mako b/kallithea/templates/ini/template.ini.mako --- a/kallithea/templates/ini/template.ini.mako +++ b/kallithea/templates/ini/template.ini.mako @@ -177,6 +177,9 @@ cut_off_limit = 256000 <%text>## WSGI environment variable to get the IP address of the client (default REMOTE_ADDR) #remote_addr_variable = HTTP_X_FORWARDED_FOR +<%text>## WSGI environment variable to get the protocol (http or https) of the client connection (default wsgi.url_scheme) +#url_scheme_variable = HTTP_X_FORWARDED_PROTO + <%text>## always pretend the client connected using HTTPS (default false) #force_https = true