// The strength rule for a new web password, kept in step with // scripts/password.py (which enforces it). The proxy only uses it to mark a // migrated plaintext password as weak, so Settings can warn about it once. export const PASSWORD_RULE = "Use at least 16 characters, or a passphrase of 4 or more random words " + "separated by spaces or dashes (15 characters or more in all). Common words, " + "keyboard runs and repeated characters do not count towards the length."; const COMMON = [ "password", "passwort", "pipelines", "pipeline", "qwerty", "azerty", "letmein", "welcome", "admin", "administrator", "iloveyou", "monkey", "dragon", "master", "secret", "login", "changeme", "default", "football", "baseball", "sunshine", "princess", "shadow", "trustno", "superman", "hello", "test", "guest", "root", ].sort((a, b) => b.length - a.length); const ROWS = ["qwertyuiop", "asdfghjkl", "zxcvbnm", "1234567890"]; const SEPARATORS = /[\s\-_.,;:+/]+/g; /** Removes runs of 3+ repeated characters and 4+ consecutive characters. */ function withoutRuns(text) { const chars = [...text]; const out = []; let i = 0; while (i < chars.length) { let j = i + 1; while (j < chars.length && chars[j] === chars[i]) j++; if (j - i >= 3) { i = j; continue; } let run = 0; for (const step of [1, -1]) { j = i + 1; while (j < chars.length && chars[j].codePointAt(0) - chars[j - 1].codePointAt(0) === step) j++; if (j - i >= 4) { run = j; break; } } if (run) { i = run; continue; } out.push(chars[i]); i++; } return out.join(""); } function substance(password) { let text = password.toLowerCase(); for (const word of COMMON) text = text.split(word).join(""); for (const row of ROWS) for (let size = row.length; size > 3; size--) for (let start = 0; start + size <= row.length; start++) text = text.split(row.slice(start, start + size)).join(""); return withoutRuns(text).replace(SEPARATORS, ""); } /** Why a new password is too weak, or null. Lengths count code points. */ export function passwordProblem(password) { const value = String(password ?? ""); const length = [...value].length; if (length > 1024) return "Choose a password of at most 1024 characters."; const words = value.split(SEPARATORS).filter(Boolean).map((word) => word.toLowerCase()); const passphrase = words.length >= 4 && new Set(words).size >= 4 && words.every((word) => [...word].length >= 3) && length >= 15 && [...substance(value)].length >= 12; if (passphrase) return null; if (length < 16) return `Too short. ${PASSWORD_RULE}`; if (new Set(value).size < 8 || [...substance(value)].length < 12) return `Too predictable. ${PASSWORD_RULE}`; return null; }