From d960f0cef937ae8a4a19e4dfaf3c617f1e93b10c Mon Sep 17 00:00:00 2001 From: B Stack Date: Fri, 24 Sep 2021 15:17:35 -0400 Subject: fix x-forwarded-prefix header consumption Now that I have experimented with X-Forwarded-Prefix and reverse proxies, this logic works correctly now! --- fuss.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'fuss.py') diff --git a/fuss.py b/fuss.py index d3f915e..b67cbef 100644 --- a/fuss.py +++ b/fuss.py @@ -21,7 +21,7 @@ from flask_script import Manager, Server # python3-flask-script import magic # python3-magic, see below from hashlib import sha256 from mimetypes import guess_extension -import os, sys, time, json, base64 +import os, sys, time, json, base64, re from datetime import datetime, date from uwsgidecorators import * # python3-uwsgidecorators from logging.config import dictConfig @@ -48,6 +48,9 @@ def trim_dict(a,max_length=40): b[i] = a[i] return b +def normalize(instring): + return re.sub("//","/",instring) + # load config file # Load it from the current directory, which is not FHS-compliant #conf_file = os.path.join(os.path.dirname(os.path.realpath(__file__)),"fuss.conf") @@ -266,9 +269,10 @@ def print_files(path=None,format="text",header=False): _server = "http://" + request.headers["Host"] try: _prefix = request.headers["X-Forwarded-Prefix"] + #app.logger.warn("This is x-forwarded-prefix: {0}".format(request.headers["X-Forwarded-Prefix"])) # REMOVEME _temp = _prefix.replace(", ", "\n").split('\n') - if len(_temp) > 1: - _prefix = _temp[0] + _temp = normalize("/".join(_temp)) + _prefix = _temp except: _prefix = "/" app.logger.warn("Failed to get x-forwarded-prefix") @@ -453,9 +457,10 @@ def html_template(filename="",full=False): _server = "http://" + request.headers["Host"] try: _prefix = request.headers["X-Forwarded-Prefix"] + #app.logger.warn("This is x-forwarded-prefix: {0}".format(request.headers["X-Forwarded-Prefix"])) # REMOVEME _temp = _prefix.replace(", ", "\n").split('\n') - if len(_temp) > 1: - _prefix = _temp[0] + _temp = normalize("/".join(_temp)) + _prefix = _temp except: _prefix = "/" if full: -- cgit