Add IP whitelist for SSO authentication headers (#5404) * feat: add trusted proxies configuration for SSO authentication * Refactor check to accept IP address directly * Refactor IP patterns validation * Unify warning message format

d96d1451ab468e82c1a6046bf4c6ea95c17ecdb8

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
4 files changed, +88 -23Showing whitespace changes
default/config.yaml+7 -0
@@ -127,6 +127,13 @@ sso:
127127 # as that used for authentik. (Ensure the username in authentik
128128 # is an exact match in lowercase with that in sillytavern).
129129 authentikAuth: false
130+ # List of trusted proxy IPs for SSO authentication.
131+ # Supports wildcards or CIDR notation for subnets.
132+ # Example: ['127.0.0.1', '192.168.1.1']
133+ # Set to ['*'] to trust all proxies (NOT RECOMMENDED unless you have other security measures in place)
134+ trustedProxies:
135+ - ::1
136+ - 127.0.0.1
130137
131138# Host whitelist configuration. Recommended if you're using a listen mode
132139hostWhitelist:
src/express-common.js+29 -0
@@ -1,4 +1,5 @@
11import ipaddr from 'ipaddr.js';
2+import ipMatching from 'ip-matching';
23
34const noopMiddleware = (_req, _res, next) => next();
45/** @deprecated Do not use. A global middleware is provided at the application level. */
@@ -50,3 +51,31 @@ export function isFirefox(req) {
5051 const userAgent = req.headers['user-agent'] || '';
5152 return /firefox/i.test(userAgent);
5253}
54+
55+/**
56+ * Filters and validates IP patterns.
57+ * @param {string[]} entries - The list of IP patterns to validate
58+ * @param {(entry: string, message: string) => string} formatLog - The function to format the warning message for invalid entries
59+ * @returns {string[]} The list of valid IP patterns
60+ */
61+export function filterValidIpPatterns(entries, formatLog) {
62+ const validEntries = [];
63+
64+ if (!Array.isArray(entries)) {
65+ return validEntries;
66+ }
67+
68+ for (const entry of entries) {
69+ try {
70+ // This will throw if the entry is not a valid IP or CIDR
71+ ipMatching.getMatch(entry);
72+ validEntries.push(entry);
73+ } catch (e) {
74+ if (typeof formatLog === 'function') {
75+ console.warn(formatLog(entry, e?.message || 'Unknown error'));
76+ }
77+ }
78+ }
79+
80+ return validEntries;
81+}
src/middleware/whitelist.js+4 -23
@@ -6,7 +6,7 @@ import Handlebars from 'handlebars';
66import ipMatching from 'ip-matching';
77import isDocker from 'is-docker';
88
99import { filterValidIpPatterns, getIpFromRequest } from '../express-common.js';
1010import { color, getConfigValue, safeReadFileSync } from '../util.js';
1111
1212const whitelistPath = path.join(process.cwd(), './whitelist.txt');
@@ -16,6 +16,8 @@ const whitelistDockerHosts = !!getConfigValue('whitelistDockerHosts', true, 'boo
1616let whitelist = getConfigValue('whitelist', []);
1717
1818if (fs.existsSync(whitelistPath)) {
19+ console.warn(color.yellow('whitelist.txt is deprecated and will be removed in a future release.'));
20+ console.warn(color.yellow('Please migrate its contents to the whitelist field in config.yaml. See the documentation for more details.'));
1921 try {
2022 let whitelistTxt = fs.readFileSync(whitelistPath, 'utf-8');
2123 whitelist = whitelistTxt.split('\n').filter(ip => ip).map(ip => ip.trim());
@@ -24,28 +26,7 @@ if (fs.existsSync(whitelistPath)) {
2426 }
2527}
2628
27-/**
29+whitelist = filterValidIpPatterns(whitelist, (entry, message) => `${color.red('Warning')}: Ignoring invalid whitelist entry ${color.yellow(entry)} - ${message}`);
28- * Validates and filters the whitelist, removing any invalid entries.
29- * @param {string[]} entries - The whitelist entries to validate
30- * @returns {string[]} The filtered list of valid whitelist entries
31- */
32-function validateWhitelist(entries) {
33- const validEntries = [];
34-
35- for (const entry of entries) {
36- try {
37- // This will throw if the entry is not a valid IP or CIDR
38- ipMatching.getMatch(entry);
39- validEntries.push(entry);
40- } catch (e) {
41- console.warn(`Whitelist ${color.red('Warning')}: Ignoring invalid entry ${color.yellow(entry)} - ${e.message}`);
42- }
43- }
44-
45- return validEntries;
46-}
47-
48-whitelist = validateWhitelist(whitelist);
4930
5031/**
5132 * Get the client IP address from the request headers.
src/users.js+48 -0
@@ -14,12 +14,14 @@ import archiver from 'archiver';
1414import _ from 'lodash';
1515import { sync as writeFileAtomicSync } from 'write-file-atomic';
1616import sanitize from 'sanitize-filename';
17+import ipMatching from 'ip-matching';
1718
1819import { USER_DIRECTORY_TEMPLATE, DEFAULT_USER, PUBLIC_DIRECTORIES, SETTINGS_FILE, UPLOADS_DIRECTORY } from './constants.js';
1920import { getConfigValue, color, delay, generateTimestamp, invalidateFirefoxCache, isPathUnderParent, setPermissionsSync } from './util.js';
2021import { allowKeysExposure, readSecret, writeSecret, SECRETS_FILE } from './endpoints/secrets.js';
2122import { getContentOfType } from './endpoints/content-manager.js';
2223import { serverDirectory } from './server-directory.js';
24+import { filterValidIpPatterns, getIpFromRequest } from './express-common.js';
2325
2426export const KEY_PREFIX = 'user:';
2527const AVATAR_PREFIX = 'avatar:';
@@ -28,6 +30,7 @@ const AUTHELIA_AUTH = getConfigValue('sso.autheliaAuth', false, 'boolean');
2830const AUTHENTIK_AUTH = getConfigValue('sso.authentikAuth', false, 'boolean');
2931const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false, 'boolean');
3032const ANON_CSRF_SECRET = crypto.randomBytes(64).toString('base64');
33+const TRUSTED_PROXIES = filterValidIpPatterns(getConfigValue('sso.trustedProxies', ['127.0.0.1', '::1']) ?? [], (entry, message) => `${color.red('Warning')}: Ignoring invalid sso.trustedProxies entry ${color.yellow(entry)} - ${message}`);
3134
3235/**
3336 * Cache for user directories.
@@ -812,6 +815,44 @@ async function authentikUserLogin(request) {
812815}
813816
814817/**
818+ * Check if the request can authenticate SSO users based on the trusted proxies configuration and the request's IP address.
819+ * @param {string} ip The IP address of the request
820+ * @return {boolean} If the request is from a trusted proxy based on the configuration
821+ */
822+function isRequestFromTrustedProxy(ip) {
823+ if (!Array.isArray(TRUSTED_PROXIES)) {
824+ console.warn(color.yellow('sso.trustedProxies is not an array. Please check your config.yaml. SSO auto-login will not work.'));
825+ return false;
826+ }
827+
828+ // Bypass magic value check if the user explicitly configured
829+ if (TRUSTED_PROXIES.length === 1 && TRUSTED_PROXIES[0] === '*') {
830+ console.warn(color.yellow('sso.trustedProxies is set to accept all IPs. This is not recommended for production environments.'));
831+ return true;
832+ }
833+
834+ // If the IP is missing or unknown, we can't trust it
835+ if (!ip || ip === 'unknown') {
836+ return false;
837+ }
838+
839+ // At least one entry in the trusted proxies list must match the request IP for it to be considered trusted
840+ for (const entry of TRUSTED_PROXIES) {
841+ try {
842+ // This will throw if the entry is not a valid IP or CIDR
843+ const match = ipMatching.getMatch(entry);
844+ if (ipMatching.matches(ip, match)) {
845+ return true;
846+ }
847+ } catch (e) {
848+ continue;
849+ }
850+ }
851+
852+ return false;
853+}
854+
855+/**
815856 * Tries auto-login with a given header.
816857 * @param {import('express').Request} request Request object
817858 * @param {string} [header='Remote-User'] The header to use for the trusted user
@@ -828,6 +869,13 @@ async function headerUserLogin(request, header = 'Remote-User') {
828869 }
829870 console.debug(`Attempting auto-login for user from header ${header}: ${remoteUser}`);
830871
872+ const ip = getIpFromRequest(request);
873+ const isTrusted = isRequestFromTrustedProxy(ip);
874+ if (!isTrusted) {
875+ console.warn(color.yellow(`Received ${header} header from untrusted IP ${ip}. Ignoring for auto-login.`));
876+ return false;
877+ }
878+
831879 const userHandles = await getAllUserHandles();
832880 for (const userHandle of userHandles) {
833881 if (remoteUser.toLowerCase() === userHandle) {