✨ Add a reverse proxy test page

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.
This commit is contained in:
Gina Häußge committed 2022-09-27 13:20:27 +02:00
1 parent f334decb33
commit 73c199c98c
5 files changed
+217 -12

No files matched your search

+15 -11
View File
@@ -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):
+20
View File
@@ -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
+9 -1
View File
@@ -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;
@@ -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"));
});
@@ -0,0 +1,130 @@
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>OctoPrint Reverse Proxy Test</title>
<link rel="shortcut icon" href="{{ url_for('static', filename='img/tentacle-32x32.png') }}">
<link rel="mask-icon" href="{{ url_for('static', filename='img/mask.svg') }}" color="#56BE37">
<link rel="mask-icon-theme" href="{{ url_for('static', filename='img/mask-theme.svg') }}" color="#56BE37">
<link rel="apple-touch-icon" sizes="114x114" href="{{ url_for('static', filename='img/apple-touch-icon-114x114.png') }}">
<link rel="apple-touch-icon" sizes="144x144" href="{{ url_for('static', filename='img/apple-touch-icon-144x144.png') }}">
<!-- le CSS -->
<link rel="stylesheet" href="{{ url_for("static", filename="css/bootstrap.min.css") }}">
<link rel="stylesheet" href="{{ url_for("static", filename="css/bootstrap-responsive.min.css") }}">
<link rel="stylesheet" href="{{ url_for("static", filename="vendor/font-awesome-5.15.1/css/all.min.css") }}">
<link rel="stylesheet" href="{{ url_for("static", filename="vendor/font-awesome-5.15.1/css/v4-shims.min") }}">
{% for url in theming %}
<link rel="stylesheet" href="{{ url }}">
{% endfor %}
<!-- le javascript -->
<script>
var BASE_URL = "{{ url_for('index') }}";
var CLIENT_IP = "{{ client_ip }}";
var SERVER_PROTOCOL = "{{ server_protocol }}";
var SERVER_NAME = "{{ server_name }}";
var SERVER_PORT = {{ server_port }};
var SERVER_PATH = "{{ server_path }}";
var COOKIE_SUFFIX = "{{ cookie_suffix }}";
</script>
<script src="{{ url_for("static", filename="js/lib/jquery/jquery.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/knockout.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/bootstrap/bootstrap.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/lodash.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/sprintf.min.js") }}"></script>
{% assets "js_client" %}
<script type="text/javascript" src="{{ ASSET_URL }}"></script>
{% endassets %}
<script src="{{ url_for("static", filename="js/app/helpers.js") }}"></script>
<script src="{{ url_for("static", filename="js/reverse_proxy_test/reverse_proxy_test.js") }}"></script>
</head>
<body>
<div id="navbar" class="navbar navbar-inverse navbar-static-top">
<div class="navbar-inner">
<span class="brand">OctoPrint Reverse Proxy Test</span>
</div>
</div>
<div id="reverse_proxy_test" class="container" style="padding-top: 1em">
<div class="alert alert-error" data-bind="visible: !cookieSuffixMatch">
<strong>Warning!</strong> 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.
</div>
<p>
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.
</p>
<p>
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.
</p>
<p>
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.
</p>
<table class="table table-bordered table-hover">
<thead>
<th>Variable</th>
<th>Source</th>
<th>Client</th>
<th>Server</th>
</thead>
<tr>
<td>Client IP</td>
<td><code>X-Forwarded-For</code></td>
<td>-</td>
<td>{{ client_ip }}</td>
</tr>
<tr data-bind="css: { success: serverProtocolMatch, error: !serverProtocolMatch }">
<td>Protocol</td>
<td><code>X-Forwarded-Protocol</code>, <code>X-Scheme</code> or config</td>
<td data-bind="text: serverProtocol"></td>
<td>{{ server_protocol }}</td>
</tr>
<tr data-bind="css: { success: serverNameMatch, error: !serverNameMatch }">
<td>Host</td>
<td><code>X-Forwarded-Host</code>, <code>Host</code>, <code>X-Forwarded-Server</code> or config</td>
<td data-bind="text: serverName"></td>
<td>{{ server_name }}</td>
</tr>
<tr data-bind="css: { success: serverPortMatch, error: !serverPortMatch }">
<td>Port</td>
<td><code>X-Forwarded-Host</code>, <code>Host</code>, <code>X-Forwarded-Port</code> or config</td>
<td data-bind="text: serverPort"></td>
<td>{{ server_port }}</td>
</tr>
<tr data-bind="css: { success: serverPathMatch, error: !serverPathMatch }">
<td>Path</td>
<td><code>X-Script-Name</code> or config</td>
<td data-bind="text: serverPath"></td>
<td>{{ server_path }}</td>
</tr>
<tr data-bind="css: { success: cookieSuffixMatch, error: !cookieSuffixMatch }">
<td>Cookie Suffix</td>
<td>Built from port & path</td>
<td data-bind="text: cookieSuffix"></td>
<td>{{ cookie_suffix }}</td>
</tr>
</table>
<p>
Please refer to <a href="https://faq.octoprint.org/reverse-proxy" target="_blank" rel="noopener noreferrer">the Reverse Proxy FAQ entry</a>
for information on correct configuration and examples.
</p>
</div>
</body>
</html>