From 73c199c98c90b05585cd69cb9f5350fd2e5ec24a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 27 Sep 2022 13:02:39 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20a=20reverse=20proxy=20test=20?= =?UTF-8?q?page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Will show variables needed for URL generation and cookie suffix generation, both as seen by client and server, and highlight any mismatches. Also links to the reverse proxy FAQ and includes information on which request headers are relevant. Not translated, kept as simple as possible. Not behind a login since none of this information should be news to a client on a properly configured proxy setup. --- src/octoprint/server/util/flask.py | 26 ++-- src/octoprint/server/views.py | 20 +++ src/octoprint/static/js/app/client/base.js | 10 +- .../reverse_proxy_test/reverse_proxy_test.js | 43 ++++++ .../templates/reverse_proxy_test.jinja2 | 130 ++++++++++++++++++ 5 files changed, 217 insertions(+), 12 deletions(-) create mode 100644 src/octoprint/static/js/reverse_proxy_test/reverse_proxy_test.js create mode 100644 src/octoprint/templates/reverse_proxy_test.jinja2 diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index cdf9052a8..263698128 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -470,6 +470,20 @@ def decode_remember_me_cookie(value): raise ValueError("Invalid remember me cookie") +def get_cookie_suffix(request): + """ + Request specific suffix for set and read cookies + + We need this because cookies are not port-specific and we don't want to overwrite our + session and other cookies from one OctoPrint instance on our machine with those of another + one who happens to listen on the same address albeit a different port or script root. + """ + result = "_P" + request.server_port + if request.script_root: + return result + "_R" + request.script_root.replace("/", "|") + return result + + class OctoPrintFlaskRequest(flask.Request): environment_wrapper = staticmethod(lambda x: x) @@ -518,17 +532,7 @@ class OctoPrintFlaskRequest(flask.Request): @cached_property def cookie_suffix(self): - """ - Request specific suffix for set and read cookies - - We need this because cookies are not port-specific and we don't want to overwrite our - session and other cookies from one OctoPrint instance on our machine with those of another - one who happens to listen on the same address albeit a different port or script root. - """ - result = "_P" + self.server_port - if self.script_root: - return result + "_R" + self.script_root.replace("/", "|") - return result + return get_cookie_suffix(self) class OctoPrintFlaskResponse(flask.Response): diff --git a/src/octoprint/server/views.py b/src/octoprint/server/views.py index 6a9442bf8..9666e6c60 100644 --- a/src/octoprint/server/views.py +++ b/src/octoprint/server/views.py @@ -331,6 +331,26 @@ def in_cache(): return abort(404) +@app.route("/reverse_proxy_test") +@app.route("/reverse_proxy_test/") +def reverse_proxy_test(): + from octoprint.server.util.flask import get_cookie_suffix, get_remote_address + + remote_address = get_remote_address(request) + cookie_suffix = get_cookie_suffix(request) + + return render_template( + "reverse_proxy_test.jinja2", + theming=[], + client_ip=remote_address, + server_protocol=request.environ.get("wsgi.url_scheme"), + server_name=request.environ.get("SERVER_NAME"), + server_port=request.environ.get("SERVER_PORT"), + server_path=request.script_root if request.script_root else "/", + cookie_suffix=cookie_suffix, + ) + + @app.route("/") def index(): from octoprint.server import connectivityChecker, printer diff --git a/src/octoprint/static/js/app/client/base.js b/src/octoprint/static/js/app/client/base.js index 1cbf2e871..79870fd86 100644 --- a/src/octoprint/static/js/app/client/base.js +++ b/src/octoprint/static/js/app/client/base.js @@ -107,7 +107,7 @@ return url; }; - OctoPrintClient.prototype.getCookieSuffix = function () { + OctoPrintClient.prototype.getParsedBaseUrl = function () { if (!this.options.baseurl) return ""; try { @@ -121,6 +121,14 @@ var url = new URL(parsed.protocol + "//" + parsed.host + path); } + return url; + }; + + OctoPrintClient.prototype.getCookieSuffix = function () { + if (!this.options.baseurl) return ""; + + var url = this.getParsedBaseUrl(); + var port = url.port || (url.protocol === "https:" ? 443 : 80); if (url.pathname && url.pathname !== "/") { var path = url.pathname; diff --git a/src/octoprint/static/js/reverse_proxy_test/reverse_proxy_test.js b/src/octoprint/static/js/reverse_proxy_test/reverse_proxy_test.js new file mode 100644 index 000000000..fcb991d3d --- /dev/null +++ b/src/octoprint/static/js/reverse_proxy_test/reverse_proxy_test.js @@ -0,0 +1,43 @@ +$(function () { + //~~ OctoPrint client setup + + var OctoPrint = window.OctoPrint; + OctoPrint.options.baseurl = BASE_URL; + + //~~ Lodash setup + + _.mixin({sprintf: sprintf, vsprintf: vsprintf}); + + //~~ View Model + + function ReverseProxyTestViewModel() { + var self = this; + + var url = OctoPrint.getParsedBaseUrl(); + var cookieSuffix = OctoPrint.getCookieSuffix(); + var protocol = url.protocol; + var path = url.pathname || "/"; + + if (path && path !== "/") { + if (path.endsWith("/")) { + path = path.substring(0, path.length - 1); + } + } + + self.serverProtocol = protocol.substring(0, protocol.length - 1); + self.serverName = url.hostname; + self.serverPort = url.port || (url.protocol === "https:" ? 443 : 80); + self.serverPath = path; + self.cookieSuffix = cookieSuffix; + + self.serverProtocolMatch = self.serverProtocol == SERVER_PROTOCOL; + self.serverNameMatch = self.serverName == SERVER_NAME; + self.serverPortMatch = self.serverPort == SERVER_PORT; + self.serverPathMatch = self.serverPath == SERVER_PATH; + self.cookieSuffixMatch = self.cookieSuffix == COOKIE_SUFFIX; + } + + var viewModel = new ReverseProxyTestViewModel(); + + ko.applyBindings(viewModel, document.getElementById("reverse_proxy_test")); +}); diff --git a/src/octoprint/templates/reverse_proxy_test.jinja2 b/src/octoprint/templates/reverse_proxy_test.jinja2 new file mode 100644 index 000000000..a92ab3f1a --- /dev/null +++ b/src/octoprint/templates/reverse_proxy_test.jinja2 @@ -0,0 +1,130 @@ + + + + OctoPrint Reverse Proxy Test + + + + + + + + + + + + + + + {% for url in theming %} + + {% endfor %} + + + + + + + + + + + {% assets "js_client" %} + + {% endassets %} + + + + + + +
+
+ Warning! The used cookie suffix doesn't match between client and server. That means that OctoPrint's + UI won't work properly. Please check your configuration and fix it. +
+ +

+ This page helps you identify issues in your reverse proxy configuration. It will show you information about + some of the variables that OctoPrint uses to set cookies and generate fully qualified URLs. If you see + any mismatches highlighted in red below, that means that something is not configured properly and you + need to fix it. +

+ +

+ The table also shows you the client IP that your server saw you as. Make sure that this client IP is what + OctoPrint should be seeing, and not for example your reverse proxy itself. +

+ +

+ For each variable, information is displayed what influences the value the server sees and thus where to + start debugging in case of any observed mismatches. +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
VariableSourceClientServer
Client IPX-Forwarded-For-{{ client_ip }}
ProtocolX-Forwarded-Protocol, X-Scheme or config{{ server_protocol }}
HostX-Forwarded-Host, Host, X-Forwarded-Server or config{{ server_name }}
PortX-Forwarded-Host, Host, X-Forwarded-Port or config{{ server_port }}
PathX-Script-Name or config{{ server_path }}
Cookie SuffixBuilt from port & path{{ cookie_suffix }}
+ +

+ Please refer to the Reverse Proxy FAQ entry + for information on correct configuration and examples. +

+ +
+ + +