mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
LDAP Connection button.
Every LDAP_* setting server/authentication.js and packages/wekan-ldap read
straight from process.env can now be overridden from Admin Panel / Login,
with an explicit admin value winning over the env var and the env var
winning over nothing (models/lib/configResolver.js's resolveConfigValue(),
pure and unit-tested for the precedence and for its 'admin'/'env'/'default'
source tag). The Admin Panel LDAP section shows, next to every field,
which source is currently in effect - an env var name, "Admin Panel", or
"Unset" - so it is always clear whether a value comes from the environment
or from an admin override.
The bind password never reaches the browser: it is stored in the new
Settings.ldap.bindPassword field, which server/publications/settings.js
deliberately never publishes (only the boolean ldap.bindPasswordSet is).
The password input starts empty and an empty submission leaves the
existing value/source untouched, matching the existing mail-server
password field's pattern. hasConfigValue() is the parallel, secret-safe
resolver: it returns only a boolean and a source, never the value, and a
negative test fuzzes several secret shapes through it to prove that.
Found while wiring this up: packages/wekan-ldap/server/testConnection.js's
existing ldap_test_connection method had its isAdmin check commented out
("TODO: this needs to be fixed - security issue"), so any authenticated
user - not only an admin - could trigger a real LDAP bind attempt against
the configured directory. Fixed to require isAdmin, the same check every
other admin-only Settings method uses, before any connection is attempted.
A new "Test LDAP Connection" button in Admin Panel / Login calls this
method against whichever config (admin override or env var) is currently
resolved and shows the result - success or the directory's own error -
inline, satisfying the "get back error message visible at admin panel"
request without adding a second LDAP client dependency.
Switching between LDAP, OAuth2, SAML and password login already has its
own UI (the Login pane's "Default Authentication Method" selector plus
each method's own enabled flag) and needed no separate switcher; this only
extends "enabled" itself to be admin-overridable, the same as every other
LDAP field.
Scope: LDAP is covered end to end (override + test-connection). OAuth2/
SAML/CAS's env vars are unchanged and stay env-only for now - the resolver
is written to extend to them, but doing so was out of scope for this pass.
Tests: tests/configResolver.test.cjs (precedence, source tagging,
hasConfigValue's secret non-leak, redactCredentialsInUrl for the composite-
connection-string case); tests/ldapAdminOverrideSecurity.test.cjs (the
publication never sends ldap.bindPassword, a whole-tree negative sweep for
any OTHER publication doing the same, the isAdmin gate on
ldap_test_connection runs before any connection attempt, and the method's
response never carries the password); tests/settingPublishedFields.test.cjs
extended to understand dotted/sub-document published fields so it keeps
checking the LDAP section's currentSetting.ldap.* reads against what is
actually published.
Thanks to xet7 !
104 lines
4.1 KiB
JavaScript
104 lines
4.1 KiB
JavaScript
// Admin Panel env-var-override resolution (maintainer request: "Add all settings
|
|
// from environment variables to Admin Panel where appropriate ... as possibility
|
|
// to override"). Pure, dependency-free helpers so they can be unit-tested without
|
|
// Meteor/Mongo and reused by every env-var-backed setting (LDAP first, extensible
|
|
// to OAuth2/SAML/CAS).
|
|
//
|
|
// Precedence, for an ordinary (non-secret) setting:
|
|
// 1. an explicitly-set, non-empty Admin Panel value wins;
|
|
// 2. otherwise the environment variable's value, if set;
|
|
// 3. otherwise undefined (caller's own default applies).
|
|
// The `source` tag ('admin' | 'env' | 'default') is what lets the Admin Panel UI
|
|
// clearly show which one is actually in effect - the maintainer's explicit
|
|
// requirement ("clearly visible, is in use environment variable or admin panel
|
|
// setting").
|
|
//
|
|
// SECURITY: this module resolves ordinary values. For a password/secret-shaped
|
|
// setting, use hasConfigValue() below instead - it never returns the secret
|
|
// itself, only whether one is configured and where it came from. Nothing in this
|
|
// file reads process.env for a *_PASSWORD/*_SECRET-shaped name and hands the
|
|
// value back to a caller that publishes to the client; callers are responsible
|
|
// for not doing that with resolveConfigValue() either, but the secret-safe
|
|
// helper exists precisely so a caller never has to.
|
|
|
|
function isEmpty(value) {
|
|
return value === undefined || value === null || value === '';
|
|
}
|
|
|
|
/**
|
|
* Resolve a single configuration value between an Admin Panel override and an
|
|
* environment variable. `readEnv` defaults to reading process.env directly but
|
|
* is injectable for tests and for callers that already have the value.
|
|
*
|
|
* @param {string} envVarName - e.g. 'LDAP_HOST'
|
|
* @param {*} adminValue - the value currently stored in the Admin Panel setting
|
|
* @param {object} [options]
|
|
* @param {function(string): (string|undefined)} [options.readEnv]
|
|
* @returns {{ value: *, source: 'admin'|'env'|'default' }}
|
|
*/
|
|
function resolveConfigValue(envVarName, adminValue, options = {}) {
|
|
const readEnv = options.readEnv || (name => process.env[name]);
|
|
|
|
if (!isEmpty(adminValue)) {
|
|
return { value: adminValue, source: 'admin' };
|
|
}
|
|
|
|
const envValue = readEnv(envVarName);
|
|
if (!isEmpty(envValue)) {
|
|
return { value: envValue, source: 'env' };
|
|
}
|
|
|
|
return { value: undefined, source: 'default' };
|
|
}
|
|
|
|
/**
|
|
* The secret-safe counterpart of resolveConfigValue(): tells a caller WHETHER a
|
|
* password/secret is configured and WHERE it comes from, but never returns the
|
|
* secret's value. Use this - never resolveConfigValue() - for anything that
|
|
* ends up in code reachable by a client-facing method/publication.
|
|
*
|
|
* @param {string} envVarName
|
|
* @param {*} adminValue - the raw admin-panel secret value (e.g. Settings doc's
|
|
* ldap.bindPassword), NEVER pass this through to a client-visible result.
|
|
* @param {object} [options]
|
|
* @param {function(string): (string|undefined)} [options.readEnv]
|
|
* @returns {{ hasValue: boolean, source: 'admin'|'env'|'default' }}
|
|
*/
|
|
function hasConfigValue(envVarName, adminValue, options = {}) {
|
|
const readEnv = options.readEnv || (name => process.env[name]);
|
|
|
|
if (!isEmpty(adminValue)) {
|
|
return { hasValue: true, source: 'admin' };
|
|
}
|
|
const envValue = readEnv(envVarName);
|
|
if (!isEmpty(envValue)) {
|
|
return { hasValue: true, source: 'env' };
|
|
}
|
|
return { hasValue: false, source: 'default' };
|
|
}
|
|
|
|
/**
|
|
* Redact credentials embedded inline in a composite/connection-string-shaped
|
|
* value (e.g. "ldap://user:secret@host:389", "smtp://user:secret@host:25")
|
|
* before it is ever sent to a browser for display/debugging purposes. Applied
|
|
* server-side, always - a value must be redacted BEFORE it reaches any
|
|
* client-visible method result or publication, never after (masking client-side
|
|
* still transmits the secret first).
|
|
*
|
|
* @param {string} value
|
|
* @returns {string}
|
|
*/
|
|
function redactCredentialsInUrl(value) {
|
|
if (typeof value !== 'string' || !value) return value;
|
|
return value.replace(
|
|
/(\b[a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^\s/:@]+):([^\s@]+)@/g,
|
|
'$1:***@',
|
|
);
|
|
}
|
|
|
|
module.exports = {
|
|
resolveConfigValue,
|
|
hasConfigValue,
|
|
redactCredentialsInUrl,
|
|
};
|