mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
CodeQL #599 (js/insufficient-password-hash, CWE-916) flagged the SHA-1 the server computed for PASSWORD_CHECK_PWNED. That digest is not password storage - accounts-password keeps passwords with bcrypt - and it is never stored or logged: Have I Been Pwned's range API is keyed by SHA-1, its first 5 hex digits are the question and the rest is compared in memory. A slow hash cannot be used, because the service could not match it. The browser and the server now compute it with one implementation, breachLookupDigest in models/lib/passwordPolicy.js, which says what it is and is not; the server no longer hashes a password with a hash of its own, and a digest that cannot be computed lets the password through. Tests pin that the digest is SHA-1 for ASCII and UTF-8, that it reaches only the lookup and the comparison, and that nothing in WeKan hashes a password-named value with a fast hash. Unreleased hardening, so no Hall of Fame entry: docs/Security/Password-Breach-Check-Digest-2026-10-31.md. Thanks to GitHub CodeQL and xet7 !
56 lines
2.3 KiB
JavaScript
56 lines
2.3 KiB
JavaScript
// Breached-password check for the forms that set a NEW password - sign-up,
|
|
// Change Password, reset and enrolment (models/lib/passwordPolicy.js says why
|
|
// it is done in the browser). It captures the submit before useraccounts sees
|
|
// it, asks the server for the SHA-1 range of the password - five hex digits,
|
|
// never the password - and replays the submit when the password is not in it.
|
|
//
|
|
// A form that sets a new password is the one with a "password again" or a
|
|
// "current password" field; the sign-in form has neither and is left alone.
|
|
|
|
const { isPwned, breachLookupDigest } = require('../../models/lib/passwordPolicy');
|
|
|
|
function isNewPasswordForm(form) {
|
|
return !!(form && form.id === 'at-pwd-form' && form.querySelector('#at-field-password')
|
|
&& (form.querySelector('#at-field-password_again') || form.querySelector('#at-field-current_password')));
|
|
}
|
|
|
|
// policy() -> { checkPwned } (cached by the caller); range(prefix) -> Promise
|
|
// of suffixes or null; subtle is crypto.subtle (absent outside a secure
|
|
// context, where the check is skipped); showError(message); replay(form).
|
|
function createNewPasswordSubmitHandler({ policy, range, subtle, showError, replay, pwnedMessage }) {
|
|
let replaying = false;
|
|
let pending = false;
|
|
return async function submit(event) {
|
|
const form = event.target;
|
|
if (replaying || !isNewPasswordForm(form) || !policy().checkPwned || !subtle) return;
|
|
const password = form.querySelector('#at-field-password').value;
|
|
if (!password) return;
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
if (pending) return;
|
|
pending = true;
|
|
try {
|
|
let breached = false;
|
|
try {
|
|
const hex = await breachLookupDigest(password, subtle);
|
|
const suffixes = await range(hex.slice(0, 5));
|
|
breached = Array.isArray(suffixes) && isPwned(hex, suffixes);
|
|
} catch (e) {
|
|
// An unreachable service must not stop anybody from setting a password.
|
|
breached = false;
|
|
}
|
|
if (breached) {
|
|
showError(pwnedMessage());
|
|
return;
|
|
}
|
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
replaying = true;
|
|
try { replay(form); } finally { replaying = false; }
|
|
} finally {
|
|
pending = false;
|
|
}
|
|
};
|
|
}
|
|
|
|
module.exports = { createNewPasswordSubmitHandler, isNewPasswordForm };
|