Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ› Fixes header auth proxy whitelist on dual-stack IPv6 listeners

Alicia Sykes076c71f7c313
diff --git a/services/app.js b/services/app.js
index 44208467..59e3384b 100644
--- a/services/app.js
+++ b/services/app.js
@@ -40,6 +40,7 @@ const aliasTarget = require('./endpoints/alias-target'); // Resolves /<alias> to
const { apiEnabledGate, apiErrorHandler, createApiRouter } = require('./endpoints/api'); // Opt-in REST API
const { loadOidcSettings, createOidcMiddleware } = require('./utils/auth-oidc');
+const isTrustedProxy = require('./utils/trusted-proxy');
/* Service endpoint URL paths (see also serviceEndpoints in src/utils/config/defaults.js) */
const ENDPOINTS = {
@@ -167,7 +168,7 @@ function getAuthMiddleware(authConfig, oidcSettings) {
} else if (authConfig.enableHeaderAuth && authConfig.headerAuth) {
const { userHeader = 'Remote-User', proxyWhitelist = [] } = authConfig.headerAuth;
return (req, res, next) => {
- if (!proxyWhitelist.includes(req.socket.remoteAddress)) {
+ if (!isTrustedProxy(proxyWhitelist, req.socket.remoteAddress)) {
return res.status(401).json({ success: false, message: 'Unauthorized - not from trusted proxy' });
}
const user = req.headers[userHeader.toLowerCase()];
diff --git a/services/endpoints/get-user.js b/services/endpoints/get-user.js
index 9f1ac22b..2cc9a3d1 100644
--- a/services/endpoints/get-user.js
+++ b/services/endpoints/get-user.js
@@ -1,9 +1,11 @@
+const isTrustedProxy = require('../utils/trusted-proxy');
+
module.exports = (config, req) => {
try {
if (config.appConfig?.auth?.enableHeaderAuth) {
const { userHeader = 'Remote-User' } = config.appConfig.auth.headerAuth;
const { proxyWhitelist } = config.appConfig.auth.headerAuth;
- if (proxyWhitelist.includes(req.socket.remoteAddress)) {
+ if (isTrustedProxy(proxyWhitelist, req.socket.remoteAddress)) {
return { success: true, user: req.headers[userHeader.toLowerCase()] };
}
}
diff --git a/services/utils/trusted-proxy.js b/services/utils/trusted-proxy.js
new file mode 100644
index 00000000..c699ad11
--- /dev/null
+++ b/services/utils/trusted-proxy.js
@@ -0,0 +1,5 @@
+/* Dual-stack sockets see IPv4 peers as ::ffff:a.b.c.d, and proxies using localhost may arrive on ::1 */
+module.exports = (proxyWhitelist, ip) => {
+ const ipv4 = ip === '::1' ? '127.0.0.1' : ip?.replace(/^::ffff:/, '');
+ return proxyWhitelist.includes(ip) || proxyWhitelist.includes(ipv4);
+};