@sparkleideas/security 3.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +234 -0
  2. package/__tests__/acceptance/security-compliance.test.ts +674 -0
  3. package/__tests__/credential-generator.test.ts +310 -0
  4. package/__tests__/fixtures/configurations.ts +419 -0
  5. package/__tests__/fixtures/index.ts +21 -0
  6. package/__tests__/helpers/create-mock.ts +469 -0
  7. package/__tests__/helpers/index.ts +32 -0
  8. package/__tests__/input-validator.test.ts +381 -0
  9. package/__tests__/integration/security-flow.test.ts +606 -0
  10. package/__tests__/password-hasher.test.ts +239 -0
  11. package/__tests__/path-validator.test.ts +302 -0
  12. package/__tests__/safe-executor.test.ts +292 -0
  13. package/__tests__/token-generator.test.ts +371 -0
  14. package/__tests__/unit/credential-generator.test.ts +182 -0
  15. package/__tests__/unit/password-hasher.test.ts +359 -0
  16. package/__tests__/unit/path-validator.test.ts +509 -0
  17. package/__tests__/unit/safe-executor.test.ts +667 -0
  18. package/__tests__/unit/token-generator.test.ts +310 -0
  19. package/package.json +28 -0
  20. package/src/CVE-REMEDIATION.ts +251 -0
  21. package/src/application/index.ts +10 -0
  22. package/src/application/services/security-application-service.ts +193 -0
  23. package/src/credential-generator.ts +368 -0
  24. package/src/domain/entities/security-context.ts +173 -0
  25. package/src/domain/index.ts +17 -0
  26. package/src/domain/services/security-domain-service.ts +296 -0
  27. package/src/index.ts +271 -0
  28. package/src/input-validator.ts +466 -0
  29. package/src/password-hasher.ts +270 -0
  30. package/src/path-validator.ts +525 -0
  31. package/src/safe-executor.ts +525 -0
  32. package/src/token-generator.ts +463 -0
  33. package/tmp.json +0 -0
  34. package/tsconfig.json +9 -0
@@ -0,0 +1,270 @@
1
+ /**
2
+ * Password Hasher - CVE-2 Remediation
3
+ *
4
+ * Fixes weak password hashing by replacing SHA-256 with hardcoded salt
5
+ * with bcrypt using 12 rounds (configurable).
6
+ *
7
+ * Security Properties:
8
+ * - bcrypt with adaptive cost factor (12 rounds)
9
+ * - Automatic salt generation per password
10
+ * - Timing-safe comparison
11
+ * - Minimum password length enforcement
12
+ *
13
+ * @module v3/security/password-hasher
14
+ */
15
+
16
+ import * as bcrypt from 'bcrypt';
17
+
18
+ export interface PasswordHasherConfig {
19
+ /**
20
+ * Number of bcrypt rounds (cost factor).
21
+ * Default: 12 (recommended minimum for production)
22
+ * Each increment doubles the computation time.
23
+ */
24
+ rounds?: number;
25
+
26
+ /**
27
+ * Minimum password length.
28
+ * Default: 8 characters
29
+ */
30
+ minLength?: number;
31
+
32
+ /**
33
+ * Maximum password length.
34
+ * Default: 128 characters (bcrypt limit is 72 bytes)
35
+ */
36
+ maxLength?: number;
37
+
38
+ /**
39
+ * Require at least one uppercase letter.
40
+ * Default: true
41
+ */
42
+ requireUppercase?: boolean;
43
+
44
+ /**
45
+ * Require at least one lowercase letter.
46
+ * Default: true
47
+ */
48
+ requireLowercase?: boolean;
49
+
50
+ /**
51
+ * Require at least one digit.
52
+ * Default: true
53
+ */
54
+ requireDigit?: boolean;
55
+
56
+ /**
57
+ * Require at least one special character.
58
+ * Default: false
59
+ */
60
+ requireSpecial?: boolean;
61
+ }
62
+
63
+ export interface PasswordValidationResult {
64
+ isValid: boolean;
65
+ errors: string[];
66
+ }
67
+
68
+ export class PasswordHashError extends Error {
69
+ constructor(
70
+ message: string,
71
+ public readonly code: string,
72
+ ) {
73
+ super(message);
74
+ this.name = 'PasswordHashError';
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Secure password hasher using bcrypt.
80
+ *
81
+ * This class replaces the vulnerable SHA-256 + hardcoded salt implementation
82
+ * with industry-standard bcrypt hashing.
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * const hasher = new PasswordHasher({ rounds: 12 });
87
+ * const hash = await hasher.hash('securePassword123');
88
+ * const isValid = await hasher.verify('securePassword123', hash);
89
+ * ```
90
+ */
91
+ export class PasswordHasher {
92
+ private readonly config: Required<PasswordHasherConfig>;
93
+
94
+ constructor(config: PasswordHasherConfig = {}) {
95
+ this.config = {
96
+ rounds: config.rounds ?? 12,
97
+ minLength: config.minLength ?? 8,
98
+ maxLength: config.maxLength ?? 128,
99
+ requireUppercase: config.requireUppercase ?? true,
100
+ requireLowercase: config.requireLowercase ?? true,
101
+ requireDigit: config.requireDigit ?? true,
102
+ requireSpecial: config.requireSpecial ?? false,
103
+ };
104
+
105
+ // Validate configuration
106
+ if (this.config.rounds < 10 || this.config.rounds > 20) {
107
+ throw new PasswordHashError(
108
+ 'Bcrypt rounds must be between 10 and 20 for security and performance balance',
109
+ 'INVALID_ROUNDS'
110
+ );
111
+ }
112
+
113
+ if (this.config.minLength < 8) {
114
+ throw new PasswordHashError(
115
+ 'Minimum password length must be at least 8 characters',
116
+ 'INVALID_MIN_LENGTH'
117
+ );
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Validates password against configured requirements.
123
+ *
124
+ * @param password - The password to validate
125
+ * @returns Validation result with errors if any
126
+ */
127
+ validate(password: string): PasswordValidationResult {
128
+ const errors: string[] = [];
129
+
130
+ if (!password) {
131
+ errors.push('Password is required');
132
+ return { isValid: false, errors };
133
+ }
134
+
135
+ if (password.length < this.config.minLength) {
136
+ errors.push(`Password must be at least ${this.config.minLength} characters`);
137
+ }
138
+
139
+ if (password.length > this.config.maxLength) {
140
+ errors.push(`Password must not exceed ${this.config.maxLength} characters`);
141
+ }
142
+
143
+ if (this.config.requireUppercase && !/[A-Z]/.test(password)) {
144
+ errors.push('Password must contain at least one uppercase letter');
145
+ }
146
+
147
+ if (this.config.requireLowercase && !/[a-z]/.test(password)) {
148
+ errors.push('Password must contain at least one lowercase letter');
149
+ }
150
+
151
+ if (this.config.requireDigit && !/\d/.test(password)) {
152
+ errors.push('Password must contain at least one digit');
153
+ }
154
+
155
+ if (this.config.requireSpecial && !/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) {
156
+ errors.push('Password must contain at least one special character');
157
+ }
158
+
159
+ return {
160
+ isValid: errors.length === 0,
161
+ errors,
162
+ };
163
+ }
164
+
165
+ /**
166
+ * Hashes a password using bcrypt.
167
+ *
168
+ * @param password - The plaintext password to hash
169
+ * @returns The bcrypt hash
170
+ * @throws PasswordHashError if password is invalid
171
+ */
172
+ async hash(password: string): Promise<string> {
173
+ const validation = this.validate(password);
174
+
175
+ if (!validation.isValid) {
176
+ throw new PasswordHashError(
177
+ validation.errors.join('; '),
178
+ 'VALIDATION_FAILED'
179
+ );
180
+ }
181
+
182
+ try {
183
+ // bcrypt automatically generates a random salt per hash
184
+ return await bcrypt.hash(password, this.config.rounds);
185
+ } catch (error) {
186
+ throw new PasswordHashError(
187
+ 'Failed to hash password',
188
+ 'HASH_FAILED'
189
+ );
190
+ }
191
+ }
192
+
193
+ /**
194
+ * Verifies a password against a bcrypt hash.
195
+ * Uses timing-safe comparison internally.
196
+ *
197
+ * @param password - The plaintext password to verify
198
+ * @param hash - The bcrypt hash to compare against
199
+ * @returns True if password matches, false otherwise
200
+ */
201
+ async verify(password: string, hash: string): Promise<boolean> {
202
+ if (!password || !hash) {
203
+ return false;
204
+ }
205
+
206
+ // Validate hash format (bcrypt hashes start with $2a$, $2b$, or $2y$)
207
+ if (!this.isValidBcryptHash(hash)) {
208
+ return false;
209
+ }
210
+
211
+ try {
212
+ // bcrypt.compare uses timing-safe comparison
213
+ return await bcrypt.compare(password, hash);
214
+ } catch (error) {
215
+ // Return false on any error to prevent timing attacks
216
+ return false;
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Checks if a hash needs to be rehashed with updated parameters.
222
+ * Useful for upgrading hash strength over time.
223
+ *
224
+ * @param hash - The bcrypt hash to check
225
+ * @returns True if hash should be updated
226
+ */
227
+ needsRehash(hash: string): boolean {
228
+ if (!this.isValidBcryptHash(hash)) {
229
+ return true;
230
+ }
231
+
232
+ // Extract rounds from hash (format: $2b$XX$...)
233
+ const match = hash.match(/^\$2[aby]\$(\d{2})\$/);
234
+ if (!match) {
235
+ return true;
236
+ }
237
+
238
+ const hashRounds = parseInt(match[1], 10);
239
+ return hashRounds < this.config.rounds;
240
+ }
241
+
242
+ /**
243
+ * Validates bcrypt hash format.
244
+ *
245
+ * @param hash - The hash to validate
246
+ * @returns True if valid bcrypt hash format
247
+ */
248
+ private isValidBcryptHash(hash: string): boolean {
249
+ // bcrypt hash format: $2a$XX$22charsSalt31charsHash
250
+ // Total length: 60 characters
251
+ return /^\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53}$/.test(hash);
252
+ }
253
+
254
+ /**
255
+ * Returns current configuration (without sensitive defaults).
256
+ */
257
+ getConfig(): Readonly<Omit<Required<PasswordHasherConfig>, never>> {
258
+ return { ...this.config };
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Factory function to create a production-ready password hasher.
264
+ *
265
+ * @param rounds - Bcrypt rounds (default: 12)
266
+ * @returns Configured PasswordHasher instance
267
+ */
268
+ export function createPasswordHasher(rounds = 12): PasswordHasher {
269
+ return new PasswordHasher({ rounds });
270
+ }