Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | /** * Password Hasher - CVE-2 Remediation * * Fixes weak password hashing by replacing SHA-256 with hardcoded salt * with bcrypt using 12 rounds (configurable). * * Security Properties: * - bcrypt with adaptive cost factor (12 rounds) * - Automatic salt generation per password * - Timing-safe comparison * - Minimum password length enforcement * * @module v3/security/password-hasher */ import * as bcrypt from 'bcrypt'; export interface PasswordHasherConfig { /** * Number of bcrypt rounds (cost factor). * Default: 12 (recommended minimum for production) * Each increment doubles the computation time. */ rounds?: number; /** * Minimum password length. * Default: 8 characters */ minLength?: number; /** * Maximum password length. * Default: 128 characters (bcrypt limit is 72 bytes) */ maxLength?: number; /** * Require at least one uppercase letter. * Default: true */ requireUppercase?: boolean; /** * Require at least one lowercase letter. * Default: true */ requireLowercase?: boolean; /** * Require at least one digit. * Default: true */ requireDigit?: boolean; /** * Require at least one special character. * Default: false */ requireSpecial?: boolean; } export interface PasswordValidationResult { isValid: boolean; errors: string[]; } export class PasswordHashError extends Error { constructor( message: string, public readonly code: string, ) { super(message); this.name = 'PasswordHashError'; } } /** * Secure password hasher using bcrypt. * * This class replaces the vulnerable SHA-256 + hardcoded salt implementation * with industry-standard bcrypt hashing. * * @example * ```typescript * const hasher = new PasswordHasher({ rounds: 12 }); * const hash = await hasher.hash('securePassword123'); * const isValid = await hasher.verify('securePassword123', hash); * ``` */ export class PasswordHasher { private readonly config: Required<PasswordHasherConfig>; constructor(config: PasswordHasherConfig = {}) { this.config = { rounds: config.rounds ?? 12, minLength: config.minLength ?? 8, maxLength: config.maxLength ?? 128, requireUppercase: config.requireUppercase ?? true, requireLowercase: config.requireLowercase ?? true, requireDigit: config.requireDigit ?? true, requireSpecial: config.requireSpecial ?? false, }; // Validate configuration if (this.config.rounds < 10 || this.config.rounds > 20) { throw new PasswordHashError( 'Bcrypt rounds must be between 10 and 20 for security and performance balance', 'INVALID_ROUNDS' ); } if (this.config.minLength < 8) { throw new PasswordHashError( 'Minimum password length must be at least 8 characters', 'INVALID_MIN_LENGTH' ); } } /** * Validates password against configured requirements. * * @param password - The password to validate * @returns Validation result with errors if any */ validate(password: string): PasswordValidationResult { const errors: string[] = []; if (!password) { errors.push('Password is required'); return { isValid: false, errors }; } if (password.length < this.config.minLength) { errors.push(`Password must be at least ${this.config.minLength} characters`); } if (password.length > this.config.maxLength) { errors.push(`Password must not exceed ${this.config.maxLength} characters`); } if (this.config.requireUppercase && !/[A-Z]/.test(password)) { errors.push('Password must contain at least one uppercase letter'); } if (this.config.requireLowercase && !/[a-z]/.test(password)) { errors.push('Password must contain at least one lowercase letter'); } if (this.config.requireDigit && !/\d/.test(password)) { errors.push('Password must contain at least one digit'); } if (this.config.requireSpecial && !/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) { errors.push('Password must contain at least one special character'); } return { isValid: errors.length === 0, errors, }; } /** * Hashes a password using bcrypt. * * @param password - The plaintext password to hash * @returns The bcrypt hash * @throws PasswordHashError if password is invalid */ async hash(password: string): Promise<string> { const validation = this.validate(password); if (!validation.isValid) { throw new PasswordHashError( validation.errors.join('; '), 'VALIDATION_FAILED' ); } try { // bcrypt automatically generates a random salt per hash return await bcrypt.hash(password, this.config.rounds); } catch (error) { throw new PasswordHashError( 'Failed to hash password', 'HASH_FAILED' ); } } /** * Verifies a password against a bcrypt hash. * Uses timing-safe comparison internally. * * @param password - The plaintext password to verify * @param hash - The bcrypt hash to compare against * @returns True if password matches, false otherwise */ async verify(password: string, hash: string): Promise<boolean> { if (!password || !hash) { return false; } // Validate hash format (bcrypt hashes start with $2a$, $2b$, or $2y$) if (!this.isValidBcryptHash(hash)) { return false; } try { // bcrypt.compare uses timing-safe comparison return await bcrypt.compare(password, hash); } catch (error) { // Return false on any error to prevent timing attacks return false; } } /** * Checks if a hash needs to be rehashed with updated parameters. * Useful for upgrading hash strength over time. * * @param hash - The bcrypt hash to check * @returns True if hash should be updated */ needsRehash(hash: string): boolean { if (!this.isValidBcryptHash(hash)) { return true; } // Extract rounds from hash (format: $2b$XX$...) const match = hash.match(/^\$2[aby]\$(\d{2})\$/); if (!match) { return true; } const hashRounds = parseInt(match[1], 10); return hashRounds < this.config.rounds; } /** * Validates bcrypt hash format. * * @param hash - The hash to validate * @returns True if valid bcrypt hash format */ private isValidBcryptHash(hash: string): boolean { // bcrypt hash format: $2a$XX$22charsSalt31charsHash // Total length: 60 characters return /^\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53}$/.test(hash); } /** * Returns current configuration (without sensitive defaults). */ getConfig(): Readonly<Omit<Required<PasswordHasherConfig>, never>> { return { ...this.config }; } } /** * Factory function to create a production-ready password hasher. * * @param rounds - Bcrypt rounds (default: 12) * @returns Configured PasswordHasher instance */ export function createPasswordHasher(rounds = 12): PasswordHasher { return new PasswordHasher({ rounds }); } |