@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.
- package/README.md +234 -0
- package/__tests__/acceptance/security-compliance.test.ts +674 -0
- package/__tests__/credential-generator.test.ts +310 -0
- package/__tests__/fixtures/configurations.ts +419 -0
- package/__tests__/fixtures/index.ts +21 -0
- package/__tests__/helpers/create-mock.ts +469 -0
- package/__tests__/helpers/index.ts +32 -0
- package/__tests__/input-validator.test.ts +381 -0
- package/__tests__/integration/security-flow.test.ts +606 -0
- package/__tests__/password-hasher.test.ts +239 -0
- package/__tests__/path-validator.test.ts +302 -0
- package/__tests__/safe-executor.test.ts +292 -0
- package/__tests__/token-generator.test.ts +371 -0
- package/__tests__/unit/credential-generator.test.ts +182 -0
- package/__tests__/unit/password-hasher.test.ts +359 -0
- package/__tests__/unit/path-validator.test.ts +509 -0
- package/__tests__/unit/safe-executor.test.ts +667 -0
- package/__tests__/unit/token-generator.test.ts +310 -0
- package/package.json +28 -0
- package/src/CVE-REMEDIATION.ts +251 -0
- package/src/application/index.ts +10 -0
- package/src/application/services/security-application-service.ts +193 -0
- package/src/credential-generator.ts +368 -0
- package/src/domain/entities/security-context.ts +173 -0
- package/src/domain/index.ts +17 -0
- package/src/domain/services/security-domain-service.ts +296 -0
- package/src/index.ts +271 -0
- package/src/input-validator.ts +466 -0
- package/src/password-hasher.ts +270 -0
- package/src/path-validator.ts +525 -0
- package/src/safe-executor.ts +525 -0
- package/src/token-generator.ts +463 -0
- package/tmp.json +0 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { CredentialGenerator } from '../../../security/credential-generator.js';
|
|
3
|
+
|
|
4
|
+
describe('CredentialGenerator', () => {
|
|
5
|
+
describe('generatePassword', () => {
|
|
6
|
+
it('should generate passwords meeting complexity requirements', () => {
|
|
7
|
+
const generator = new CredentialGenerator();
|
|
8
|
+
const password = generator.generatePassword();
|
|
9
|
+
|
|
10
|
+
expect(password.length).toBeGreaterThanOrEqual(32);
|
|
11
|
+
expect(/[A-Z]/.test(password)).toBe(true);
|
|
12
|
+
expect(/[a-z]/.test(password)).toBe(true);
|
|
13
|
+
expect(/\d/.test(password)).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should generate unique passwords each time', () => {
|
|
17
|
+
const generator = new CredentialGenerator();
|
|
18
|
+
const passwords = new Set(Array.from({ length: 100 }, () => generator.generatePassword()));
|
|
19
|
+
expect(passwords.size).toBe(100);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should respect custom length parameter', () => {
|
|
23
|
+
const generator = new CredentialGenerator();
|
|
24
|
+
const password = generator.generatePassword(64);
|
|
25
|
+
expect(password.length).toBe(64);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should include special characters by default', () => {
|
|
29
|
+
const generator = new CredentialGenerator();
|
|
30
|
+
const password = generator.generatePassword();
|
|
31
|
+
expect(/[!@#$%^&*()_+\-=\[\]{}|;:,.<>?]/.test(password)).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('generateApiKey', () => {
|
|
36
|
+
it('should generate API keys with prefix', () => {
|
|
37
|
+
const generator = new CredentialGenerator();
|
|
38
|
+
const key = generator.generateApiKey('test_');
|
|
39
|
+
expect(key.key.startsWith('test_')).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should generate API keys with default prefix', () => {
|
|
43
|
+
const generator = new CredentialGenerator();
|
|
44
|
+
const key = generator.generateApiKey();
|
|
45
|
+
expect(key.key.startsWith('cf_')).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should include keyId as UUID', () => {
|
|
49
|
+
const generator = new CredentialGenerator();
|
|
50
|
+
const key = generator.generateApiKey();
|
|
51
|
+
expect(key.keyId).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should include createdAt timestamp', () => {
|
|
55
|
+
const generator = new CredentialGenerator();
|
|
56
|
+
const before = new Date();
|
|
57
|
+
const key = generator.generateApiKey();
|
|
58
|
+
const after = new Date();
|
|
59
|
+
expect(key.createdAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
60
|
+
expect(key.createdAt.getTime()).toBeLessThanOrEqual(after.getTime());
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should generate unique keys each time', () => {
|
|
64
|
+
const generator = new CredentialGenerator();
|
|
65
|
+
const keys = new Set(Array.from({ length: 100 }, () => generator.generateApiKey().key));
|
|
66
|
+
expect(keys.size).toBe(100);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('generateSecret', () => {
|
|
71
|
+
it('should generate hex-encoded secrets', () => {
|
|
72
|
+
const generator = new CredentialGenerator();
|
|
73
|
+
const secret = generator.generateSecret();
|
|
74
|
+
expect(/^[0-9a-f]+$/i.test(secret)).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should generate secrets with default length', () => {
|
|
78
|
+
const generator = new CredentialGenerator();
|
|
79
|
+
const secret = generator.generateSecret();
|
|
80
|
+
expect(secret.length).toBe(64);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should generate secrets with custom length', () => {
|
|
84
|
+
const generator = new CredentialGenerator();
|
|
85
|
+
const secret = generator.generateSecret(128);
|
|
86
|
+
expect(secret.length).toBe(128);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('generateEncryptionKey', () => {
|
|
91
|
+
it('should generate 64-character hex key (32 bytes)', () => {
|
|
92
|
+
const generator = new CredentialGenerator();
|
|
93
|
+
const key = generator.generateEncryptionKey();
|
|
94
|
+
expect(key.length).toBe(64);
|
|
95
|
+
expect(/^[0-9a-f]+$/i.test(key)).toBe(true);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe('generateInstallationCredentials', () => {
|
|
100
|
+
it('should generate complete credential set', () => {
|
|
101
|
+
const generator = new CredentialGenerator();
|
|
102
|
+
const creds = generator.generateInstallationCredentials();
|
|
103
|
+
|
|
104
|
+
expect(creds.adminPassword).toBeDefined();
|
|
105
|
+
expect(creds.servicePassword).toBeDefined();
|
|
106
|
+
expect(creds.jwtSecret).toBeDefined();
|
|
107
|
+
expect(creds.sessionSecret).toBeDefined();
|
|
108
|
+
expect(creds.encryptionKey).toBeDefined();
|
|
109
|
+
expect(creds.generatedAt).toBeInstanceOf(Date);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should set expiration when specified', () => {
|
|
113
|
+
const generator = new CredentialGenerator();
|
|
114
|
+
const creds = generator.generateInstallationCredentials(30);
|
|
115
|
+
|
|
116
|
+
expect(creds.expiresAt).toBeDefined();
|
|
117
|
+
const expectedExpiry = new Date(creds.generatedAt.getTime() + 30 * 24 * 60 * 60 * 1000);
|
|
118
|
+
expect(creds.expiresAt!.getTime()).toBeCloseTo(expectedExpiry.getTime(), -3);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('configuration validation', () => {
|
|
123
|
+
it('should reject password length below 16', () => {
|
|
124
|
+
expect(() => new CredentialGenerator({ passwordLength: 8 }))
|
|
125
|
+
.toThrow('Password length must be at least 16 characters');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should reject API key length below 32', () => {
|
|
129
|
+
expect(() => new CredentialGenerator({ apiKeyLength: 16 }))
|
|
130
|
+
.toThrow('API key length must be at least 32 characters');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should reject secret length below 32', () => {
|
|
134
|
+
expect(() => new CredentialGenerator({ secretLength: 16 }))
|
|
135
|
+
.toThrow('Secret length must be at least 32 characters');
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('utility methods', () => {
|
|
140
|
+
it('should generate session tokens', () => {
|
|
141
|
+
const generator = new CredentialGenerator();
|
|
142
|
+
const token = generator.generateSessionToken();
|
|
143
|
+
expect(token.length).toBe(64);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should generate CSRF tokens', () => {
|
|
147
|
+
const generator = new CredentialGenerator();
|
|
148
|
+
const token = generator.generateCsrfToken();
|
|
149
|
+
expect(token).toBeDefined();
|
|
150
|
+
expect(token.length).toBeGreaterThan(0);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should generate nonces', () => {
|
|
154
|
+
const generator = new CredentialGenerator();
|
|
155
|
+
const nonce = generator.generateNonce();
|
|
156
|
+
expect(nonce.length).toBe(32);
|
|
157
|
+
expect(/^[0-9a-f]+$/i.test(nonce)).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('output generation', () => {
|
|
162
|
+
it('should create env script format', () => {
|
|
163
|
+
const generator = new CredentialGenerator();
|
|
164
|
+
const creds = generator.generateInstallationCredentials();
|
|
165
|
+
const script = generator.createEnvScript(creds);
|
|
166
|
+
|
|
167
|
+
expect(script).toContain('CLAUDE_FLOW_ADMIN_PASSWORD');
|
|
168
|
+
expect(script).toContain('CLAUDE_FLOW_JWT_SECRET');
|
|
169
|
+
expect(script).toContain('export');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should create JSON config format', () => {
|
|
173
|
+
const generator = new CredentialGenerator();
|
|
174
|
+
const creds = generator.generateInstallationCredentials();
|
|
175
|
+
const json = generator.createJsonConfig(creds);
|
|
176
|
+
|
|
177
|
+
const parsed = JSON.parse(json);
|
|
178
|
+
expect(parsed['claude-flow/admin-password']).toBeDefined();
|
|
179
|
+
expect(parsed['claude-flow/jwt-secret']).toBeDefined();
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V3 Claude-Flow Password Hasher Unit Tests
|
|
3
|
+
*
|
|
4
|
+
* London School TDD - Behavior Verification
|
|
5
|
+
* Tests password hashing and verification behavior
|
|
6
|
+
*/
|
|
7
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
8
|
+
import { createMock, createMockWithBehavior, type MockedInterface } from '../../helpers/create-mock';
|
|
9
|
+
import { securityConfigs } from '../../fixtures/configurations';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Password hasher interface (to be implemented)
|
|
13
|
+
*/
|
|
14
|
+
interface IPasswordHasher {
|
|
15
|
+
hash(password: string): Promise<string>;
|
|
16
|
+
verify(password: string, hash: string): Promise<boolean>;
|
|
17
|
+
needsRehash(hash: string): boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Crypto provider interface (collaborator)
|
|
22
|
+
*/
|
|
23
|
+
interface ICryptoProvider {
|
|
24
|
+
argon2Hash(password: string, options: Argon2Options): Promise<string>;
|
|
25
|
+
argon2Verify(hash: string, password: string): Promise<boolean>;
|
|
26
|
+
generateSalt(length: number): Promise<string>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface Argon2Options {
|
|
30
|
+
memoryCost: number;
|
|
31
|
+
timeCost: number;
|
|
32
|
+
parallelism: number;
|
|
33
|
+
salt?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Password hasher implementation for testing
|
|
38
|
+
*/
|
|
39
|
+
class PasswordHasher implements IPasswordHasher {
|
|
40
|
+
constructor(
|
|
41
|
+
private readonly cryptoProvider: ICryptoProvider,
|
|
42
|
+
private readonly config: typeof securityConfigs.strict.hashing
|
|
43
|
+
) {}
|
|
44
|
+
|
|
45
|
+
async hash(password: string): Promise<string> {
|
|
46
|
+
if (!password || password.length < 8) {
|
|
47
|
+
throw new Error('Password must be at least 8 characters');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const salt = await this.cryptoProvider.generateSalt(16);
|
|
51
|
+
|
|
52
|
+
return this.cryptoProvider.argon2Hash(password, {
|
|
53
|
+
memoryCost: this.config.memoryCost ?? 65536,
|
|
54
|
+
timeCost: this.config.timeCost ?? 3,
|
|
55
|
+
parallelism: this.config.parallelism ?? 4,
|
|
56
|
+
salt,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async verify(password: string, hash: string): Promise<boolean> {
|
|
61
|
+
if (!password || !hash) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return this.cryptoProvider.argon2Verify(hash, password);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
needsRehash(hash: string): boolean {
|
|
69
|
+
// Check if hash uses current algorithm and parameters
|
|
70
|
+
const currentVersion = `$argon2id$v=19$m=${this.config.memoryCost},t=${this.config.timeCost},p=${this.config.parallelism}`;
|
|
71
|
+
return !hash.startsWith(currentVersion);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
describe('PasswordHasher', () => {
|
|
76
|
+
let mockCryptoProvider: MockedInterface<ICryptoProvider>;
|
|
77
|
+
let passwordHasher: PasswordHasher;
|
|
78
|
+
const hashingConfig = securityConfigs.strict.hashing;
|
|
79
|
+
|
|
80
|
+
beforeEach(() => {
|
|
81
|
+
mockCryptoProvider = createMock<ICryptoProvider>();
|
|
82
|
+
|
|
83
|
+
// Configure default mock behavior
|
|
84
|
+
mockCryptoProvider.generateSalt.mockResolvedValue('randomsalt16byte');
|
|
85
|
+
mockCryptoProvider.argon2Hash.mockResolvedValue(
|
|
86
|
+
'$argon2id$v=19$m=65536,t=3,p=4$cmFuZG9tc2FsdA$hashedpassword'
|
|
87
|
+
);
|
|
88
|
+
mockCryptoProvider.argon2Verify.mockResolvedValue(true);
|
|
89
|
+
|
|
90
|
+
passwordHasher = new PasswordHasher(mockCryptoProvider, hashingConfig);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('hash', () => {
|
|
94
|
+
it('should generate salt before hashing', async () => {
|
|
95
|
+
// Given
|
|
96
|
+
const password = 'securePassword123!';
|
|
97
|
+
|
|
98
|
+
// When
|
|
99
|
+
await passwordHasher.hash(password);
|
|
100
|
+
|
|
101
|
+
// Then - verify interaction
|
|
102
|
+
expect(mockCryptoProvider.generateSalt).toHaveBeenCalledWith(16);
|
|
103
|
+
expect(mockCryptoProvider.generateSalt).toHaveBeenCalledBefore(
|
|
104
|
+
mockCryptoProvider.argon2Hash
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should hash password with configured parameters', async () => {
|
|
109
|
+
// Given
|
|
110
|
+
const password = 'securePassword123!';
|
|
111
|
+
|
|
112
|
+
// When
|
|
113
|
+
await passwordHasher.hash(password);
|
|
114
|
+
|
|
115
|
+
// Then - verify interaction with correct parameters
|
|
116
|
+
expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledWith(
|
|
117
|
+
password,
|
|
118
|
+
expect.objectContaining({
|
|
119
|
+
memoryCost: 65536,
|
|
120
|
+
timeCost: 3,
|
|
121
|
+
parallelism: 4,
|
|
122
|
+
})
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('should return hash from crypto provider', async () => {
|
|
127
|
+
// Given
|
|
128
|
+
const password = 'securePassword123!';
|
|
129
|
+
const expectedHash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
|
|
130
|
+
mockCryptoProvider.argon2Hash.mockResolvedValue(expectedHash);
|
|
131
|
+
|
|
132
|
+
// When
|
|
133
|
+
const result = await passwordHasher.hash(password);
|
|
134
|
+
|
|
135
|
+
// Then
|
|
136
|
+
expect(result).toBe(expectedHash);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should reject passwords shorter than 8 characters', async () => {
|
|
140
|
+
// Given
|
|
141
|
+
const shortPassword = 'short';
|
|
142
|
+
|
|
143
|
+
// When/Then
|
|
144
|
+
await expect(passwordHasher.hash(shortPassword)).rejects.toThrow(
|
|
145
|
+
'Password must be at least 8 characters'
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
// Verify no interaction with crypto provider
|
|
149
|
+
expect(mockCryptoProvider.generateSalt).not.toHaveBeenCalled();
|
|
150
|
+
expect(mockCryptoProvider.argon2Hash).not.toHaveBeenCalled();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should reject empty passwords', async () => {
|
|
154
|
+
// Given
|
|
155
|
+
const emptyPassword = '';
|
|
156
|
+
|
|
157
|
+
// When/Then
|
|
158
|
+
await expect(passwordHasher.hash(emptyPassword)).rejects.toThrow(
|
|
159
|
+
'Password must be at least 8 characters'
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('should use generated salt in hash options', async () => {
|
|
164
|
+
// Given
|
|
165
|
+
const password = 'securePassword123!';
|
|
166
|
+
const generatedSalt = 'unique-salt-value';
|
|
167
|
+
mockCryptoProvider.generateSalt.mockResolvedValue(generatedSalt);
|
|
168
|
+
|
|
169
|
+
// When
|
|
170
|
+
await passwordHasher.hash(password);
|
|
171
|
+
|
|
172
|
+
// Then
|
|
173
|
+
expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledWith(
|
|
174
|
+
password,
|
|
175
|
+
expect.objectContaining({
|
|
176
|
+
salt: generatedSalt,
|
|
177
|
+
})
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe('verify', () => {
|
|
183
|
+
it('should delegate verification to crypto provider', async () => {
|
|
184
|
+
// Given
|
|
185
|
+
const password = 'securePassword123!';
|
|
186
|
+
const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
|
|
187
|
+
|
|
188
|
+
// When
|
|
189
|
+
await passwordHasher.verify(password, hash);
|
|
190
|
+
|
|
191
|
+
// Then - verify interaction
|
|
192
|
+
expect(mockCryptoProvider.argon2Verify).toHaveBeenCalledWith(hash, password);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should return true for valid password', async () => {
|
|
196
|
+
// Given
|
|
197
|
+
const password = 'securePassword123!';
|
|
198
|
+
const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
|
|
199
|
+
mockCryptoProvider.argon2Verify.mockResolvedValue(true);
|
|
200
|
+
|
|
201
|
+
// When
|
|
202
|
+
const result = await passwordHasher.verify(password, hash);
|
|
203
|
+
|
|
204
|
+
// Then
|
|
205
|
+
expect(result).toBe(true);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('should return false for invalid password', async () => {
|
|
209
|
+
// Given
|
|
210
|
+
const password = 'wrongPassword123!';
|
|
211
|
+
const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
|
|
212
|
+
mockCryptoProvider.argon2Verify.mockResolvedValue(false);
|
|
213
|
+
|
|
214
|
+
// When
|
|
215
|
+
const result = await passwordHasher.verify(password, hash);
|
|
216
|
+
|
|
217
|
+
// Then
|
|
218
|
+
expect(result).toBe(false);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('should return false for empty password', async () => {
|
|
222
|
+
// Given
|
|
223
|
+
const emptyPassword = '';
|
|
224
|
+
const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
|
|
225
|
+
|
|
226
|
+
// When
|
|
227
|
+
const result = await passwordHasher.verify(emptyPassword, hash);
|
|
228
|
+
|
|
229
|
+
// Then
|
|
230
|
+
expect(result).toBe(false);
|
|
231
|
+
expect(mockCryptoProvider.argon2Verify).not.toHaveBeenCalled();
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('should return false for empty hash', async () => {
|
|
235
|
+
// Given
|
|
236
|
+
const password = 'securePassword123!';
|
|
237
|
+
const emptyHash = '';
|
|
238
|
+
|
|
239
|
+
// When
|
|
240
|
+
const result = await passwordHasher.verify(password, emptyHash);
|
|
241
|
+
|
|
242
|
+
// Then
|
|
243
|
+
expect(result).toBe(false);
|
|
244
|
+
expect(mockCryptoProvider.argon2Verify).not.toHaveBeenCalled();
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe('needsRehash', () => {
|
|
249
|
+
it('should return false for hash with current parameters', () => {
|
|
250
|
+
// Given
|
|
251
|
+
const currentHash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
|
|
252
|
+
|
|
253
|
+
// When
|
|
254
|
+
const result = passwordHasher.needsRehash(currentHash);
|
|
255
|
+
|
|
256
|
+
// Then
|
|
257
|
+
expect(result).toBe(false);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('should return true for hash with different memory cost', () => {
|
|
261
|
+
// Given
|
|
262
|
+
const oldHash = '$argon2id$v=19$m=32768,t=3,p=4$salt$hash';
|
|
263
|
+
|
|
264
|
+
// When
|
|
265
|
+
const result = passwordHasher.needsRehash(oldHash);
|
|
266
|
+
|
|
267
|
+
// Then
|
|
268
|
+
expect(result).toBe(true);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('should return true for hash with different time cost', () => {
|
|
272
|
+
// Given
|
|
273
|
+
const oldHash = '$argon2id$v=19$m=65536,t=2,p=4$salt$hash';
|
|
274
|
+
|
|
275
|
+
// When
|
|
276
|
+
const result = passwordHasher.needsRehash(oldHash);
|
|
277
|
+
|
|
278
|
+
// Then
|
|
279
|
+
expect(result).toBe(true);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('should return true for hash with different parallelism', () => {
|
|
283
|
+
// Given
|
|
284
|
+
const oldHash = '$argon2id$v=19$m=65536,t=3,p=2$salt$hash';
|
|
285
|
+
|
|
286
|
+
// When
|
|
287
|
+
const result = passwordHasher.needsRehash(oldHash);
|
|
288
|
+
|
|
289
|
+
// Then
|
|
290
|
+
expect(result).toBe(true);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('should return true for bcrypt hash', () => {
|
|
294
|
+
// Given
|
|
295
|
+
const bcryptHash = '$2b$10$salt.hash';
|
|
296
|
+
|
|
297
|
+
// When
|
|
298
|
+
const result = passwordHasher.needsRehash(bcryptHash);
|
|
299
|
+
|
|
300
|
+
// Then
|
|
301
|
+
expect(result).toBe(true);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
describe('error handling', () => {
|
|
306
|
+
it('should propagate crypto provider errors on hash', async () => {
|
|
307
|
+
// Given
|
|
308
|
+
const password = 'securePassword123!';
|
|
309
|
+
const cryptoError = new Error('Crypto operation failed');
|
|
310
|
+
mockCryptoProvider.argon2Hash.mockRejectedValue(cryptoError);
|
|
311
|
+
|
|
312
|
+
// When/Then
|
|
313
|
+
await expect(passwordHasher.hash(password)).rejects.toThrow('Crypto operation failed');
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('should propagate crypto provider errors on verify', async () => {
|
|
317
|
+
// Given
|
|
318
|
+
const password = 'securePassword123!';
|
|
319
|
+
const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
|
|
320
|
+
const cryptoError = new Error('Verification failed');
|
|
321
|
+
mockCryptoProvider.argon2Verify.mockRejectedValue(cryptoError);
|
|
322
|
+
|
|
323
|
+
// When/Then
|
|
324
|
+
await expect(passwordHasher.verify(password, hash)).rejects.toThrow(
|
|
325
|
+
'Verification failed'
|
|
326
|
+
);
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
describe('interaction verification', () => {
|
|
331
|
+
it('should not call argon2Hash if salt generation fails', async () => {
|
|
332
|
+
// Given
|
|
333
|
+
const password = 'securePassword123!';
|
|
334
|
+
mockCryptoProvider.generateSalt.mockRejectedValue(new Error('Salt generation failed'));
|
|
335
|
+
|
|
336
|
+
// When
|
|
337
|
+
try {
|
|
338
|
+
await passwordHasher.hash(password);
|
|
339
|
+
} catch {
|
|
340
|
+
// Expected to fail
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Then
|
|
344
|
+
expect(mockCryptoProvider.argon2Hash).not.toHaveBeenCalled();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it('should only call crypto provider once per operation', async () => {
|
|
348
|
+
// Given
|
|
349
|
+
const password = 'securePassword123!';
|
|
350
|
+
|
|
351
|
+
// When
|
|
352
|
+
await passwordHasher.hash(password);
|
|
353
|
+
|
|
354
|
+
// Then
|
|
355
|
+
expect(mockCryptoProvider.generateSalt).toHaveBeenCalledTimes(1);
|
|
356
|
+
expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledTimes(1);
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
});
|