Files
wekan/models/lib/passwordPolicy.js
T
Lauri Ojansivu c99aa7f845 Admin Panel / Problems / Password length, and a changed password signs every session out.
PASSWORD_MIN_LENGTH (default 8) is now the length below which a sign-in
is REPORTED, in the new Admin Panel / Problems / Password length, and
PASSWORD_MIN_LENGTH_ENFORCE (off by default) is what makes it a rule: a
shorter new password is refused, and an account that signs in with an
older shorter one sees only "Change your password" until it has a long
enough one. Signing in is never refused, enforced or not, so nobody is
locked out by a minimum set after their password. LDAP, OAuth2, OIDC,
SAML, CAS and Sandstorm accounts are neither reported nor asked: their
password is their provider's. An administrator's Set password no longer
applies WeKan's policy to such an account.

Meteor sends a password hashed, so only the browser knows its length: it
notes it on the sign-in form and, after a sign-in that worked, says "it
was short" - never the password or its length.

The page has both settings and a table, one row per account: username,
first and last short-password login, the latest IPv4 and IPv6 address
with the country's flag and city when a CDN says where it is, the count,
and when it was fixed, with the Too short (default) / Fixed / All filter.
Fixed is a changed password not seen short since; a short one seen
again reopens the row.

Changing one's own password - Member Settings, the forced change, a
reset link - now signs EVERY session of the account out, the one that
changed it too, and only the new password signs in again. The browser
goes to the sign-in page as soon as it is signed out, since the server
closes the connection before the answer can arrive.

Documented in docs/Features/Admin-Panel/Problems/Password-length.md, the
settings on every platform. The new strings are in every locale in
English for now. Tests: passwordPolicy, passwordChangeRequired and
shortPasswordLogins (node); password-min-length-signup-login,
password-length-report and password-change-other-sessions (Playwright,
Chromium and WebKit), which change each setting on the page and check
what it does, and that sign-up with no mail server registers and signs
the user in.

Thanks to xet7 !
2026-10-31 14:54:22 +02:00

126 lines
5.7 KiB
JavaScript

'use strict';
// Which new passwords WeKan accepts (PASSWORD_MIN_LENGTH, PASSWORD_CHECK_PWNED;
// Admin Panel / People / Login overrides each).
//
// WHERE IT CAN BE CHECKED. The browser sends a password to Meteor as its
// SHA-256, so the server never sees the length or the text of a password a
// user types into sign-up, Change Password, a reset or an enrolment. Those
// forms are checked in the browser (config/accounts.js gives useraccounts the
// minimum; client/lib/newPasswordCheck.js asks about breaches). The server
// checks every password that reaches it as text: an administrator's Set
// password and Add user, and POST /users/register. A directory's password
// (LDAP) is the directory's policy, and is not checked here.
//
// THE MINIMUM IS REPORTED BY DEFAULT, AND ENFORCED ONLY WHEN ASKED.
// PASSWORD_MIN_LENGTH (default 8) is the length below which a sign-in is
// listed in Admin Panel / Problems / Password length - nothing is refused for
// it. PASSWORD_MIN_LENGTH_ENFORCE (off by default) is what makes it a rule: a
// shorter NEW password is refused, and an account that signs in with an older
// shorter one is asked to change it before anything else
// (server/passwordChangeRequired.js). Signing in itself is never refused, so
// nobody is locked out by a minimum set after their password. Only the browser
// that typed a password knows how long it is, so it is the browser that tells.
// It covers WeKan's own password accounts: an LDAP, OAuth2, OIDC, SAML, CAS or
// Sandstorm account's password is its provider's, and so is the policy.
//
// BREACHED PASSWORDS (off by default: it asks a service on the Internet). Only
// the first 5 hex digits of the password's SHA-1 leave WeKan - the "range" of
// Have I Been Pwned's k-anonymity API, shared by hundreds of passwords - and
// the comparison with the returned suffixes is done here. A failed lookup
// lets the password through: an unreachable service must not stop anybody
// from setting one.
//
// Pure: no Meteor, no network. Loaded by the server, the client and tests.
// The length below which a sign-in is reported; enforced only with
// PASSWORD_MIN_LENGTH_ENFORCE.
const DEFAULT_MIN_LENGTH = 8;
const MAX_MIN_LENGTH = 256;
// Hashing a password costs time in proportion to its length; nobody types more.
const MAX_LENGTH = 512;
function passwordPolicyConfig(get) {
const raw = get('PASSWORD_MIN_LENGTH');
let minLength = DEFAULT_MIN_LENGTH;
if (raw !== undefined && raw !== null && String(raw).trim() !== '') {
const n = Number(String(raw).trim());
// A broken value keeps the default.
if (Number.isInteger(n) && n >= 1) minLength = Math.min(n, MAX_MIN_LENGTH);
}
return {
minLength,
enforce: String(get('PASSWORD_MIN_LENGTH_ENFORCE') || '').trim() === 'true',
checkPwned: String(get('PASSWORD_CHECK_PWNED') || '').trim() === 'true',
};
}
// Is this password shorter than the minimum - enforced or only reported?
// Length is counted in characters (code points), as a person counts them.
function isTooShort(password, config) {
return !!(config && config.minLength > 0 && typeof password === 'string'
&& [...password].length < config.minLength);
}
// null when the password may be set, else { error, min? }. Unless the minimum
// is enforced, only the length cap applies, as it always did.
function passwordProblem(password, config, { username, email } = {}) {
if (typeof password !== 'string') return { error: 'password-invalid' };
if (config.enforce && isTooShort(password, config)) return { error: 'password-too-short', min: config.minLength };
if ([...password].length > MAX_LENGTH) return { error: 'password-too-long', max: MAX_LENGTH };
if (!config.enforce) return null;
const lower = password.toLowerCase();
for (const name of [username, email, email && String(email).split('@')[0]]) {
if (name && String(name).length >= 3 && lower === String(name).toLowerCase()) {
return { error: 'password-same-as-username' };
}
}
return null;
}
const PWNED_PREFIX = /^[0-9A-F]{5}$/;
// The digest the breach lookup compares, as 40 upper-case hex digits. This is
// NOT how WeKan stores passwords - accounts-password keeps those with bcrypt -
// and it is never stored or logged: Have I Been Pwned's range API is keyed by
// SHA-1, so its first 5 digits are the question and the rest is compared with
// the answer, in memory, and dropped. One implementation for the browser and
// the server (Web Crypto, in both), so the two cannot drift apart
// (docs/Security/Password-Breach-Check-Digest-2026-10-31.md, CodeQL #599).
async function breachLookupDigest(text, subtle = globalThis.crypto && globalThis.crypto.subtle) {
if (!subtle) throw new Error('Web Crypto is not available');
const digest = await subtle.digest('SHA-1', new TextEncoder().encode(String(text)));
return [...new Uint8Array(digest)].map(b => b.toString(16).padStart(2, '0')).join('').toUpperCase();
}
// The suffixes in a range answer ("SUFFIX:COUNT" lines). Padding lines, which
// the API adds with a count of 0, are not breaches.
function parsePwnedRange(body) {
const suffixes = [];
for (const line of String(body || '').split(/\r?\n/)) {
const [suffix, count] = line.trim().split(':');
if (/^[0-9A-F]{35}$/i.test(suffix || '') && Number(count) > 0) suffixes.push(suffix.toUpperCase());
}
return suffixes;
}
// Is the password with this SHA-1 (40 hex digits) among the range's suffixes?
function isPwned(sha1Hex, suffixes) {
const hex = String(sha1Hex || '').toUpperCase();
if (!/^[0-9A-F]{40}$/.test(hex)) return false;
return suffixes.includes(hex.slice(5));
}
module.exports = {
passwordPolicyConfig,
passwordProblem,
isTooShort,
parsePwnedRange,
isPwned,
breachLookupDigest,
PWNED_PREFIX,
DEFAULT_MIN_LENGTH,
MAX_MIN_LENGTH,
MAX_LENGTH,
};