Repository navigation
Expand file tree
/
Copy pathpasswordStrength.ts
More file actions
143 lines (127 loc) · 4.95 KB
/
Copy pathpasswordStrength.ts
File metadata and controls
143 lines (127 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Password Strength Policies
*
* Enforces production-grade password requirements to prevent weak passwords
* that could be easily cracked via dictionary attacks, brute force, or rainbow tables.
*
* Security assumptions:
* - Minimum length prevents short passwords vulnerable to brute force.
* - Character class requirements ensure complexity against dictionary attacks.
* - No common passwords list blocks well-known weak passwords.
* - All checks are performed client-side first, but enforced server-side.
*
* Abuse/failure paths:
* - Short passwords: rejected with clear message.
* - Missing character classes: rejected with specific feedback.
* - Common passwords: rejected without revealing the list.
* - Empty/null passwords: rejected.
*/
export enum PasswordStrengthErrorCode {
PASSWORD_REQUIRED = 'PASSWORD_REQUIRED',
PASSWORD_TOO_SHORT = 'PASSWORD_TOO_SHORT',
PASSWORD_TOO_LONG = 'PASSWORD_TOO_LONG',
PASSWORD_MISSING_UPPERCASE = 'PASSWORD_MISSING_UPPERCASE',
PASSWORD_MISSING_LOWERCASE = 'PASSWORD_MISSING_LOWERCASE',
PASSWORD_MISSING_NUMBER = 'PASSWORD_MISSING_NUMBER',
PASSWORD_MISSING_SPECIAL = 'PASSWORD_MISSING_SPECIAL',
PASSWORD_TOO_COMMON = 'PASSWORD_TOO_COMMON',
PASSWORD_EXCESSIVE_REPEATED = 'PASSWORD_EXCESSIVE_REPEATED',
PASSWORD_SEQUENTIAL_CHARACTERS = 'PASSWORD_SEQUENTIAL_CHARACTERS',
}
export interface PasswordStrengthError {
code: PasswordStrengthErrorCode;
message: string;
}
export interface PasswordStrengthResult {
isValid: boolean;
errors: PasswordStrengthError[];
}
/**
* Validates password strength against security policies.
*
* @param password - Plain text password to validate
* @returns Validation result with boolean and structured error codes
*/
export function validatePasswordStrength(password: string): PasswordStrengthResult {
const errors: PasswordStrengthError[] = [];
if (!password || typeof password !== 'string') {
errors.push({
code: PasswordStrengthErrorCode.PASSWORD_REQUIRED,
message: 'Password is required'
});
return { isValid: false, errors };
}
// Minimum length: 12 characters (NIST recommends 8+, but 12 for better security)
if (password.length < 12) {
errors.push({
code: PasswordStrengthErrorCode.PASSWORD_TOO_SHORT,
message: 'Password must be at least 12 characters long'
});
}
// Maximum length: prevent DoS via extremely long passwords
if (password.length > 128) {
errors.push({
code: PasswordStrengthErrorCode.PASSWORD_TOO_LONG,
message: 'Password must be no more than 128 characters long'
});
}
// Require at least one uppercase letter
if (!/[A-Z]/.test(password)) {
errors.push({
code: PasswordStrengthErrorCode.PASSWORD_MISSING_UPPERCASE,
message: 'Password must contain at least one uppercase letter'
});
}
// Require at least one lowercase letter
if (!/[a-z]/.test(password)) {
errors.push({
code: PasswordStrengthErrorCode.PASSWORD_MISSING_LOWERCASE,
message: 'Password must contain at least one lowercase letter'
});
}
// Require at least one digit
if (!/\d/.test(password)) {
errors.push({
code: PasswordStrengthErrorCode.PASSWORD_MISSING_NUMBER,
message: 'Password must contain at least one number'
});
}
// Require at least one special character
if (!/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) {
errors.push({
code: PasswordStrengthErrorCode.PASSWORD_MISSING_SPECIAL,
message: 'Password must contain at least one special character'
});
}
// Check against common passwords (basic list; in production, use a larger dataset)
const commonPasswords = [
'password', '123456', '123456789', 'qwerty', 'abc123', 'password123',
'admin', 'letmein', 'welcome', 'monkey', '1234567890', 'iloveyou',
'princess', 'rockyou', '1234567', '12345678', 'password1', '123123',
'football', 'baseball', 'welcome1', 'admin123', 'root', 'user'
];
if (commonPasswords.includes(password.toLowerCase())) {
errors.push({
code: PasswordStrengthErrorCode.PASSWORD_TOO_COMMON,
message: 'Password is too common; please choose a stronger password'
});
}
// Check for repeated characters (e.g., 'aaaaa')
if (/(.)\1{4,}/.test(password)) {
errors.push({
code: PasswordStrengthErrorCode.PASSWORD_EXCESSIVE_REPEATED,
message: 'Password must not contain excessive repeated characters'
});
}
// Check for sequential characters (e.g., 'abcde' or '12345')
if (/(.)\1{4,}|(?:012|123|234|345|456|567|678|789|890|abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz)/i.test(password)) {
errors.push({
code: PasswordStrengthErrorCode.PASSWORD_SEQUENTIAL_CHARACTERS,
message: 'Password must not contain sequential characters'
});
}
return {
isValid: errors.length === 0,
errors
};
}