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 @@ + +
+ ++ 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. +
+ +| Variable | +Source | +Client | +Server | + +
|---|---|---|---|
| Client IP | +X-Forwarded-For |
+ - | +{{ client_ip }} | +
| Protocol | +X-Forwarded-Protocol, X-Scheme or config |
+ + | {{ server_protocol }} | +
| Host | +X-Forwarded-Host, Host, X-Forwarded-Server or config |
+ + | {{ server_name }} | +
| Port | +X-Forwarded-Host, Host, X-Forwarded-Port or config |
+ + | {{ server_port }} | +
| Path | +X-Script-Name or config |
+ + | {{ server_path }} | +
| Cookie Suffix | +Built from port & path | ++ | {{ cookie_suffix }} | +
+ Please refer to the Reverse Proxy FAQ entry + for information on correct configuration and examples. +
+ +