Only trust Authelia identity headers from TRUSTED_PROXIES
Security / JS Security (npm audit) (push) Failing after 17s
Lint / JS (eslint) (push) Successful in 18s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Lint / JS (eslint) (pull_request) Successful in 18s
Test / JS Tests (jest) (push) Successful in 20s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / JS Security (npm audit) (pull_request) Failing after 17s
Test / JS Tests (jest) (pull_request) Successful in 17s

authenticateSSO and authenticatePage took the user and groups straight
from the Remote-* headers, and the server listens on every interface, so
any LAN host could connect directly, claim any user and group, and run
commands on the workers. It could also write whatever it claimed into the
users table.

Add TRUSTED_PROXIES (addresses or CIDR ranges) and ignore the identity
headers unless the TCP peer is on the list. The peer address is used
rather than a forwarding header, which a client could forge. It fails
closed: unset means no SSO login succeeds, with a startup warning.
Rejected attempts are logged with their source. Worker and Gandalf
API-key routes are unchanged.

Closes #37

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-22 01:00:44 -05:00
1 parent dc47e335aa
commit e302234187
4 files changed
+256 -17

No files matched your search

+77 -7
View File
@@ -2,9 +2,79 @@
// and the HTML page middleware (authenticatePage).
const crypto = require('crypto');
const net = require('net');
const { renderError } = require('./render');
const ALLOWED_GROUPS = ['admin', 'employee'];
const IDENTITY_HEADERS = ['remote-user', 'remote-name', 'remote-email', 'remote-groups'];
// Parse TRUSTED_PROXIES ("a.b.c.d, a.b.c.0/24, ...") into a BlockList of the
// addresses allowed to send Authelia identity headers. Invalid entries are logged
// and skipped, never widened: "10.0.0.1/" must not become a /0 that trusts everyone.
function parseTrustedProxies(value) {
const list = new net.BlockList();
let count = 0;
for (const raw of String(value || '').split(',')) {
const entry = raw.trim();
if (!entry) continue;
const slash = entry.indexOf('/');
const addr = slash === -1 ? entry : entry.slice(0, slash);
const bits = slash === -1 ? null : entry.slice(slash + 1);
const type = net.isIPv4(addr) ? 'ipv4' : (net.isIPv6(addr) ? 'ipv6' : null);
const maxBits = type === 'ipv4' ? 32 : 128;
if (!type || (bits !== null && !(/^\d{1,3}$/.test(bits) && Number(bits) <= maxBits))) {
console.error(`[Auth] Ignoring invalid TRUSTED_PROXIES entry ${JSON.stringify(entry)}`);
continue;
}
if (bits === null) list.addAddress(addr, type);
else list.addSubnet(addr, Number(bits), type);
count++;
}
return { list, count };
}
let _trusted = null;
// Parsed on first use, after server.js has loaded .env.
function trustedProxies() {
if (!_trusted) _trusted = parseTrustedProxies(process.env.TRUSTED_PROXIES);
return _trusted;
}
// The TCP peer that sent the request. Pulse is reached directly (no local
// reverse proxy), so this is NPM for SSO traffic, and can't be forged by a header.
function requestSource(req) {
let addr = (req.socket && req.socket.remoteAddress) || '';
if (addr.startsWith('::ffff:') && net.isIPv4(addr.slice(7))) addr = addr.slice(7);
return addr;
}
function isTrustedProxy(req, trusted = trustedProxies()) {
const addr = requestSource(req);
const type = net.isIPv4(addr) ? 'ipv4' : (net.isIPv6(addr) ? 'ipv6' : null);
return type !== null && trusted.list.check(addr, type);
}
// The Remote-* identity headers are only real when NPM sent the request, because
// Authelia's forward auth overwrites them; any other LAN host could set them itself.
// Returns null when the request carries no trusted identity. Fails closed: with
// TRUSTED_PROXIES unset, nobody is trusted.
function ssoIdentity(req, trusted = trustedProxies()) {
const h = req.headers;
if (!isTrustedProxy(req, trusted)) {
if (IDENTITY_HEADERS.some(name => h[name])) {
console.warn(`[Auth] Ignored identity headers from untrusted source ${JSON.stringify(requestSource(req))}; ` +
'only TRUSTED_PROXIES may send them');
}
return null;
}
if (!h['remote-user']) return null;
return {
username: h['remote-user'],
name: h['remote-name'],
email: h['remote-email'],
groups: h['remote-groups'] ? h['remote-groups'].split(',').map(g => g.trim()) : []
};
}
// Upsert the SSO user into the `users` table. Extracted verbatim from authenticateSSO.
async function upsertUser(pool, headers) {
@@ -31,17 +101,14 @@ async function upsertUser(pool, headers) {
// but failures render a themed HTML page instead of JSON.
function makeAuthenticatePage(pool) {
return async function authenticatePage(req, res, next) {
const remoteUser = req.headers['remote-user'];
const remoteName = req.headers['remote-name'];
const remoteEmail = req.headers['remote-email'];
const remoteGroups = req.headers['remote-groups'];
const identity = ssoIdentity(req);
if (!remoteUser) {
if (!identity) {
return renderError(req, res, 401, 'Not authenticated',
'Not authenticated — access via Authelia SSO (auth.lotusguild.org).');
}
const groups = remoteGroups ? remoteGroups.split(',').map(g => g.trim()) : [];
const { username: remoteUser, name: remoteName, email: remoteEmail, groups } = identity;
const hasAccess = groups.some(g => ALLOWED_GROUPS.includes(g));
if (!hasAccess) {
@@ -67,4 +134,7 @@ function makeAuthenticatePage(pool) {
};
}
module.exports = { upsertUser, makeAuthenticatePage, ALLOWED_GROUPS };
module.exports = {
upsertUser, makeAuthenticatePage, ALLOWED_GROUPS,
parseTrustedProxies, trustedProxies, requestSource, isTrustedProxy, ssoIdentity
};