Files
wekan/server/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

89 lines
3.6 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
import { authEnv } from '/server/lib/authConfig';
// The server's half of the password policy: what it is, why the browser
// checks most of it, and what "breached" means are in models/lib/passwordPolicy.js.
const {
passwordPolicyConfig, passwordProblem, parsePwnedRange, isPwned, breachLookupDigest, PWNED_PREFIX,
} = require('/models/lib/passwordPolicy');
const PWNED_RANGE_URL = 'https://api.pwnedpasswords.com/range/';
const PWNED_TIMEOUT_MS = 5000;
const REASONS = {
'password-too-short': p => `The password must be at least ${p.min} characters long.`,
'password-too-long': p => `The password must be at most ${p.max} characters long.`,
'password-same-as-username': () => 'The password must not be the username or the email address.',
'password-invalid': () => 'The password is not valid.',
'password-pwned': () => 'This password has appeared in a data breach. Choose another one.',
};
export function currentPasswordPolicy() {
return passwordPolicyConfig(authEnv);
}
// The suffixes of one SHA-1 range, or null when the service cannot answer.
export async function fetchPwnedRange(prefix) {
if (!PWNED_PREFIX.test(prefix)) return null;
try {
const response = await fetch(PWNED_RANGE_URL + prefix, {
// Padding makes every answer about the same size, so the size of the
// answer says nothing about which range was asked for.
headers: { 'Add-Padding': 'true', 'User-Agent': 'WeKan' },
signal: AbortSignal.timeout(PWNED_TIMEOUT_MS),
});
if (!response.ok) return null;
return parsePwnedRange(await response.text());
} catch (e) {
console.warn('[password-policy] breached-password lookup failed:', e && e.message);
return null;
}
}
async function pwned(password) {
let digest;
try {
digest = await breachLookupDigest(password);
} catch (e) {
// Like an unreachable service: the check never stops a password.
return false;
}
const suffixes = await fetchPwnedRange(digest.slice(0, 5));
return suffixes ? isPwned(digest, suffixes) : false;
}
// Throws a Meteor.Error when a password that reached the server as text may
// not be set. `who` is { username, email } of the account it is for.
export async function assertNewPassword(password, who = {}) {
const config = currentPasswordPolicy();
const problem = passwordProblem(password, config, who);
if (problem) throw new Meteor.Error(problem.error, REASONS[problem.error](problem), problem);
if (config.checkPwned && await pwned(password)) {
throw new Meteor.Error('password-pwned', REASONS['password-pwned']());
}
}
Meteor.methods({
// What the sign-up, Change Password and reset forms check before they send
// the password, which they send hashed. Public: the sign-up form asks it.
getPasswordPolicy() {
const { minLength, enforce, checkPwned } = currentPasswordPolicy();
return { minLength, enforce, checkPwned };
},
// The breached suffixes of one 5-hex-digit SHA-1 range, for the browser to
// compare with its own password's hash; the password never leaves it. Only
// while PASSWORD_CHECK_PWNED is on; null when the service cannot answer.
async passwordPwnedRange(prefix) {
check(prefix, Match.Where(p => typeof p === 'string' && PWNED_PREFIX.test(p)));
if (!currentPasswordPolicy().checkPwned) return null;
return fetchPwnedRange(prefix);
},
});
// A sign-up page may ask, so there is no login to limit by; one connection
// asks once per form submit.
DDPRateLimiter.addRule({ type: 'method', name: 'passwordPwnedRange' }, 20, 60 * 1000);