Coverage for constants/passwords.py: 100.00%
2 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:10 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:10 +0000
1"""
2Password policy (SB-640).
4A working prod admin account had an eight-character dictionary word plus
5`123`. Nothing in the app rejected it: Supabase's own minimum is low and the
6API added nothing on top.
8The policy follows NIST SP 800-63B in shape — **length first, no composition
9rules** — because forcing a symbol turns `password` into `Password1!` and
10buys nothing. What actually stops the password that was found is a length
11floor plus a blocklist that ignores decorative digits.
13Deliberately not here: expiry, forced rotation, and a "must contain one of
14each" rule. All three push people toward predictable variants.
15"""
17MIN_PASSWORD_LENGTH = 12
19# bcrypt (what Supabase Auth uses) silently truncates past 72 bytes, so a
20# longer password is not stronger, and hashing an unbounded string is a cheap
21# way to burn CPU on a public endpoint.
22MAX_PASSWORD_LENGTH = 72
24# Bases that appear at the top of every credential list. Compared after
25# stripping trailing digits and punctuation, so `soccer123!` is caught by
26# `soccer`. Not a substitute for a breach corpus — it is the cheap half that
27# catches the shape of password this policy exists to reject.
28COMMON_PASSWORD_BASES = frozenset(
29 {
30 "password",
31 "passwd",
32 "pass",
33 "letmein",
34 "welcome",
35 "admin",
36 "administrator",
37 "qwerty",
38 "qwertyuiop",
39 "asdfgh",
40 "zxcvbn",
41 "iloveyou",
42 "monkey",
43 "dragon",
44 "football",
45 "baseball",
46 "basketball",
47 "soccer",
48 "sunshine",
49 "princess",
50 "trustno",
51 "master",
52 "shadow",
53 "superman",
54 "batman",
55 "michael",
56 "jennifer",
57 "jordan",
58 "hockey",
59 "ranger",
60 "abc",
61 "abcd",
62 "test",
63 "testing",
64 "changeme",
65 "secret",
66 "login",
67 "starwars",
68 "whatever",
69 "freedom",
70 "missingtable",
71 "mlsnext",
72 }
73)
76def _base(password: str) -> str:
77 """The word under the decoration: lowercased, trailing digits/symbols cut."""
78 return password.lower().rstrip("0123456789!@#$%^&*()_+-=.,?")
81def validate_password(password: str, *, username: str | None = None) -> str:
82 """Raise ValueError when a password is too weak to accept.
84 Applied wherever a password is *set* — signup and reset — and never at
85 login: an existing weak password must still authenticate, or rotating it
86 would be impossible. Rotating the accounts that already have one is a
87 separate, human step.
88 """
89 if len(password) < MIN_PASSWORD_LENGTH:
90 raise ValueError(f"Password must be at least {MIN_PASSWORD_LENGTH} characters")
91 if len(password.encode("utf-8")) > MAX_PASSWORD_LENGTH:
92 raise ValueError(f"Password must be at most {MAX_PASSWORD_LENGTH} bytes")
93 if _base(password) in COMMON_PASSWORD_BASES:
94 raise ValueError("Password is too common — adding digits to a common word does not make it stronger")
95 if username and len(username) >= 3 and username.lower() in password.lower():
96 raise ValueError("Password must not contain your username")
97 return password