@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,239 @@
1
+ /**
2
+ * Password Hasher Tests - CVE-2 Remediation Validation
3
+ *
4
+ * Tests verify:
5
+ * - bcrypt is used instead of SHA-256
6
+ * - 12 rounds minimum
7
+ * - Password validation rules
8
+ * - Timing-safe comparison
9
+ */
10
+
11
+ import { describe, it, expect, beforeEach } from 'vitest';
12
+ import {
13
+ PasswordHasher,
14
+ PasswordHashError,
15
+ createPasswordHasher,
16
+ } from '../../security/password-hasher.js';
17
+
18
+ describe('PasswordHasher', () => {
19
+ let hasher: PasswordHasher;
20
+
21
+ beforeEach(() => {
22
+ hasher = new PasswordHasher({ rounds: 12 });
23
+ });
24
+
25
+ describe('Configuration', () => {
26
+ it('should create hasher with default 12 rounds', () => {
27
+ const defaultHasher = new PasswordHasher();
28
+ expect(defaultHasher.getConfig().rounds).toBe(12);
29
+ });
30
+
31
+ it('should reject rounds below 10', () => {
32
+ expect(() => new PasswordHasher({ rounds: 8 })).toThrow(PasswordHashError);
33
+ });
34
+
35
+ it('should reject rounds above 20', () => {
36
+ expect(() => new PasswordHasher({ rounds: 22 })).toThrow(PasswordHashError);
37
+ });
38
+
39
+ it('should reject minimum password length below 8', () => {
40
+ expect(() => new PasswordHasher({ minLength: 4 })).toThrow(PasswordHashError);
41
+ });
42
+ });
43
+
44
+ describe('Password Validation', () => {
45
+ it('should reject empty password', () => {
46
+ const result = hasher.validate('');
47
+ expect(result.isValid).toBe(false);
48
+ expect(result.errors).toContain('Password is required');
49
+ });
50
+
51
+ it('should reject password shorter than minimum', () => {
52
+ const result = hasher.validate('short');
53
+ expect(result.isValid).toBe(false);
54
+ expect(result.errors.some(e => e.includes('at least'))).toBe(true);
55
+ });
56
+
57
+ it('should reject password without uppercase', () => {
58
+ const result = hasher.validate('password123');
59
+ expect(result.isValid).toBe(false);
60
+ expect(result.errors.some(e => e.includes('uppercase'))).toBe(true);
61
+ });
62
+
63
+ it('should reject password without lowercase', () => {
64
+ const result = hasher.validate('PASSWORD123');
65
+ expect(result.isValid).toBe(false);
66
+ expect(result.errors.some(e => e.includes('lowercase'))).toBe(true);
67
+ });
68
+
69
+ it('should reject password without digit', () => {
70
+ const result = hasher.validate('PasswordNoDigit');
71
+ expect(result.isValid).toBe(false);
72
+ expect(result.errors.some(e => e.includes('digit'))).toBe(true);
73
+ });
74
+
75
+ it('should accept valid password', () => {
76
+ const result = hasher.validate('SecurePass123');
77
+ expect(result.isValid).toBe(true);
78
+ expect(result.errors).toHaveLength(0);
79
+ });
80
+
81
+ it('should optionally require special character', () => {
82
+ const strictHasher = new PasswordHasher({ requireSpecial: true });
83
+ const result = strictHasher.validate('SecurePass123');
84
+ expect(result.isValid).toBe(false);
85
+ expect(result.errors.some(e => e.includes('special'))).toBe(true);
86
+ });
87
+
88
+ it('should accept password with special character when required', () => {
89
+ const strictHasher = new PasswordHasher({ requireSpecial: true });
90
+ const result = strictHasher.validate('SecurePass123!');
91
+ expect(result.isValid).toBe(true);
92
+ });
93
+ });
94
+
95
+ describe('Password Hashing', () => {
96
+ it('should hash password with bcrypt', async () => {
97
+ const hash = await hasher.hash('SecurePass123');
98
+
99
+ // bcrypt hashes start with $2a$, $2b$, or $2y$
100
+ expect(hash).toMatch(/^\$2[aby]\$\d{2}\$/);
101
+ });
102
+
103
+ it('should produce different hashes for same password', async () => {
104
+ const hash1 = await hasher.hash('SecurePass123');
105
+ const hash2 = await hasher.hash('SecurePass123');
106
+
107
+ expect(hash1).not.toBe(hash2); // Different salts
108
+ });
109
+
110
+ it('should include rounds in hash', async () => {
111
+ const hash = await hasher.hash('SecurePass123');
112
+
113
+ // Hash format: $2b$12$...
114
+ expect(hash).toContain('$12$');
115
+ });
116
+
117
+ it('should throw for invalid password', async () => {
118
+ await expect(hasher.hash('short')).rejects.toThrow(PasswordHashError);
119
+ });
120
+
121
+ it('should throw for empty password', async () => {
122
+ await expect(hasher.hash('')).rejects.toThrow(PasswordHashError);
123
+ });
124
+ });
125
+
126
+ describe('Password Verification', () => {
127
+ it('should verify correct password', async () => {
128
+ const password = 'SecurePass123';
129
+ const hash = await hasher.hash(password);
130
+
131
+ const isValid = await hasher.verify(password, hash);
132
+ expect(isValid).toBe(true);
133
+ });
134
+
135
+ it('should reject incorrect password', async () => {
136
+ const hash = await hasher.hash('SecurePass123');
137
+
138
+ const isValid = await hasher.verify('WrongPass123', hash);
139
+ expect(isValid).toBe(false);
140
+ });
141
+
142
+ it('should return false for empty password', async () => {
143
+ const hash = await hasher.hash('SecurePass123');
144
+
145
+ const isValid = await hasher.verify('', hash);
146
+ expect(isValid).toBe(false);
147
+ });
148
+
149
+ it('should return false for empty hash', async () => {
150
+ const isValid = await hasher.verify('SecurePass123', '');
151
+ expect(isValid).toBe(false);
152
+ });
153
+
154
+ it('should return false for invalid hash format', async () => {
155
+ const isValid = await hasher.verify('SecurePass123', 'invalid-hash');
156
+ expect(isValid).toBe(false);
157
+ });
158
+
159
+ it('should return false for SHA-256 hash (old format)', async () => {
160
+ // SHA-256 hash format (what we're replacing)
161
+ const sha256Hash = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3';
162
+
163
+ const isValid = await hasher.verify('password', sha256Hash);
164
+ expect(isValid).toBe(false);
165
+ });
166
+ });
167
+
168
+ describe('Rehash Detection', () => {
169
+ it('should not need rehash for current rounds', async () => {
170
+ const hash = await hasher.hash('SecurePass123');
171
+ expect(hasher.needsRehash(hash)).toBe(false);
172
+ });
173
+
174
+ it('should need rehash for lower rounds', () => {
175
+ // Hash with 10 rounds
176
+ const lowRoundsHash = '$2b$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy';
177
+ expect(hasher.needsRehash(lowRoundsHash)).toBe(true);
178
+ });
179
+
180
+ it('should need rehash for invalid format', () => {
181
+ expect(hasher.needsRehash('invalid-hash')).toBe(true);
182
+ });
183
+
184
+ it('should need rehash for SHA-256 hash', () => {
185
+ const sha256Hash = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3';
186
+ expect(hasher.needsRehash(sha256Hash)).toBe(true);
187
+ });
188
+ });
189
+
190
+ describe('Factory Function', () => {
191
+ it('should create hasher with specified rounds', () => {
192
+ const hasher12 = createPasswordHasher(12);
193
+ expect(hasher12.getConfig().rounds).toBe(12);
194
+
195
+ const hasher14 = createPasswordHasher(14);
196
+ expect(hasher14.getConfig().rounds).toBe(14);
197
+ });
198
+
199
+ it('should use default 12 rounds', () => {
200
+ const hasher = createPasswordHasher();
201
+ expect(hasher.getConfig().rounds).toBe(12);
202
+ });
203
+ });
204
+
205
+ describe('CVE-2 Security Verification', () => {
206
+ it('should NOT use hardcoded salt', async () => {
207
+ // Generate multiple hashes and verify they have different salts
208
+ const hashes = await Promise.all([
209
+ hasher.hash('SecurePass123'),
210
+ hasher.hash('SecurePass123'),
211
+ hasher.hash('SecurePass123'),
212
+ ]);
213
+
214
+ // Extract salts (22 chars after $2b$12$)
215
+ const salts = hashes.map(h => h.substring(7, 29));
216
+
217
+ // All salts should be unique
218
+ const uniqueSalts = new Set(salts);
219
+ expect(uniqueSalts.size).toBe(3);
220
+ });
221
+
222
+ it('should NOT produce same hash for same input (unlike SHA-256)', async () => {
223
+ // SHA-256 with hardcoded salt would produce identical output
224
+ const hash1 = await hasher.hash('SecurePass123');
225
+ const hash2 = await hasher.hash('SecurePass123');
226
+
227
+ expect(hash1).not.toBe(hash2);
228
+ });
229
+
230
+ it('should produce hash that takes time to compute', async () => {
231
+ const start = Date.now();
232
+ await hasher.hash('SecurePass123');
233
+ const duration = Date.now() - start;
234
+
235
+ // bcrypt with 12 rounds should take measurable time (>10ms typically)
236
+ expect(duration).toBeGreaterThan(5);
237
+ });
238
+ });
239
+ });
@@ -0,0 +1,302 @@
1
+ /**
2
+ * Path Validator Tests - HIGH-2 Remediation Validation
3
+ *
4
+ * Tests verify:
5
+ * - Path traversal prevention
6
+ * - Prefix validation
7
+ * - Symlink handling
8
+ * - Blocked file detection
9
+ */
10
+
11
+ import { describe, it, expect, beforeEach } from 'vitest';
12
+ import * as path from 'path';
13
+ import {
14
+ PathValidator,
15
+ PathValidatorError,
16
+ createProjectPathValidator,
17
+ createFullProjectPathValidator,
18
+ } from '../../security/path-validator.js';
19
+
20
+ describe('PathValidator', () => {
21
+ let validator: PathValidator;
22
+ const projectRoot = '/workspaces/project';
23
+
24
+ beforeEach(() => {
25
+ validator = new PathValidator({
26
+ allowedPrefixes: [projectRoot],
27
+ allowHidden: false,
28
+ });
29
+ });
30
+
31
+ describe('Configuration', () => {
32
+ it('should require at least one prefix', () => {
33
+ expect(() => new PathValidator({
34
+ allowedPrefixes: [],
35
+ })).toThrow(PathValidatorError);
36
+ });
37
+
38
+ it('should resolve prefixes to absolute paths', () => {
39
+ const relativeValidator = new PathValidator({
40
+ allowedPrefixes: ['./src'],
41
+ });
42
+
43
+ const prefixes = relativeValidator.getAllowedPrefixes();
44
+ expect(prefixes[0]).toMatch(/^\//);
45
+ });
46
+ });
47
+
48
+ describe('Path Traversal Prevention', () => {
49
+ it('should block ../ traversal', async () => {
50
+ const result = await validator.validate('/workspaces/project/../etc/passwd');
51
+ expect(result.isValid).toBe(false);
52
+ expect(result.errors).toContain('Path traversal pattern detected');
53
+ });
54
+
55
+ it('should block ..\\ traversal (Windows)', async () => {
56
+ const result = await validator.validate('/workspaces/project\\..\\..\\etc\\passwd');
57
+ expect(result.isValid).toBe(false);
58
+ });
59
+
60
+ it('should block URL-encoded traversal (%2e%2e)', async () => {
61
+ const result = await validator.validate('/workspaces/project/%2e%2e/etc/passwd');
62
+ expect(result.isValid).toBe(false);
63
+ expect(result.errors).toContain('Path traversal pattern detected');
64
+ });
65
+
66
+ it('should block double URL-encoded traversal', async () => {
67
+ const result = await validator.validate('/workspaces/project/%252e%252e/etc/passwd');
68
+ expect(result.isValid).toBe(false);
69
+ });
70
+
71
+ it('should block mixed encoding traversal', async () => {
72
+ const result = await validator.validate('/workspaces/project/.%2e/etc/passwd');
73
+ expect(result.isValid).toBe(false);
74
+ });
75
+
76
+ it('should block null byte injection', async () => {
77
+ const result = await validator.validate('/workspaces/project/file.txt\x00.jpg');
78
+ expect(result.isValid).toBe(false);
79
+ expect(result.errors).toContain('Path traversal pattern detected');
80
+ });
81
+
82
+ it('should block URL-encoded null byte', async () => {
83
+ const result = await validator.validate('/workspaces/project/file.txt%00.jpg');
84
+ expect(result.isValid).toBe(false);
85
+ });
86
+ });
87
+
88
+ describe('Prefix Validation', () => {
89
+ it('should allow paths within prefix', async () => {
90
+ const result = await validator.validate('/workspaces/project/src/file.ts');
91
+ expect(result.isValid).toBe(true);
92
+ expect(result.matchedPrefix).toBe(projectRoot);
93
+ });
94
+
95
+ it('should block paths outside prefix', async () => {
96
+ const result = await validator.validate('/etc/passwd');
97
+ expect(result.isValid).toBe(false);
98
+ expect(result.errors).toContain('Path is outside allowed directories');
99
+ });
100
+
101
+ it('should block paths that start with prefix but escape', async () => {
102
+ const result = await validator.validate('/workspaces/project-other/file.ts');
103
+ expect(result.isValid).toBe(false);
104
+ });
105
+
106
+ it('should handle exact prefix match', async () => {
107
+ const result = await validator.validate(projectRoot);
108
+ expect(result.isValid).toBe(true);
109
+ });
110
+
111
+ it('should calculate relative path correctly', async () => {
112
+ const result = await validator.validate('/workspaces/project/src/deep/file.ts');
113
+ expect(result.relativePath).toBe('src/deep/file.ts');
114
+ });
115
+ });
116
+
117
+ describe('Hidden File Handling', () => {
118
+ it('should block hidden files by default', async () => {
119
+ const result = await validator.validate('/workspaces/project/.env');
120
+ expect(result.isValid).toBe(false);
121
+ expect(result.errors.some(e => e.includes('Hidden'))).toBe(true);
122
+ });
123
+
124
+ it('should block hidden directories by default', async () => {
125
+ const result = await validator.validate('/workspaces/project/.git/config');
126
+ expect(result.isValid).toBe(false);
127
+ });
128
+
129
+ it('should allow hidden files when configured', async () => {
130
+ const hiddenValidator = new PathValidator({
131
+ allowedPrefixes: [projectRoot],
132
+ allowHidden: true,
133
+ blockedNames: [], // Remove .git from blocked names for this test
134
+ blockedExtensions: [],
135
+ });
136
+
137
+ const result = await hiddenValidator.validate('/workspaces/project/.gitignore');
138
+ expect(result.isValid).toBe(true);
139
+ });
140
+ });
141
+
142
+ describe('Blocked Files', () => {
143
+ it('should block .env files', async () => {
144
+ const hiddenValidator = new PathValidator({
145
+ allowedPrefixes: [projectRoot],
146
+ allowHidden: true,
147
+ });
148
+
149
+ const result = await hiddenValidator.validate('/workspaces/project/config/.env');
150
+ expect(result.isValid).toBe(false);
151
+ expect(result.errors.some(e => e.includes('.env'))).toBe(true);
152
+ });
153
+
154
+ it('should block .pem files', async () => {
155
+ const result = await validator.validate('/workspaces/project/certs/key.pem');
156
+ expect(result.isValid).toBe(false);
157
+ });
158
+
159
+ it('should block private key files', async () => {
160
+ const hiddenValidator = new PathValidator({
161
+ allowedPrefixes: [projectRoot],
162
+ allowHidden: true,
163
+ });
164
+
165
+ const result = await hiddenValidator.validate('/workspaces/project/.ssh/id_rsa');
166
+ expect(result.isValid).toBe(false);
167
+ });
168
+
169
+ it('should block .htpasswd files', async () => {
170
+ const hiddenValidator = new PathValidator({
171
+ allowedPrefixes: [projectRoot],
172
+ allowHidden: true,
173
+ });
174
+
175
+ const result = await hiddenValidator.validate('/workspaces/project/.htpasswd');
176
+ expect(result.isValid).toBe(false);
177
+ });
178
+ });
179
+
180
+ describe('Path Length', () => {
181
+ it('should block paths exceeding max length', async () => {
182
+ const longPath = '/workspaces/project/' + 'a'.repeat(5000);
183
+ const result = await validator.validate(longPath);
184
+ expect(result.isValid).toBe(false);
185
+ expect(result.errors.some(e => e.includes('maximum length'))).toBe(true);
186
+ });
187
+
188
+ it('should allow paths within max length', async () => {
189
+ const result = await validator.validate('/workspaces/project/src/file.ts');
190
+ expect(result.isValid).toBe(true);
191
+ });
192
+ });
193
+
194
+ describe('Empty Path', () => {
195
+ it('should reject empty path', async () => {
196
+ const result = await validator.validate('');
197
+ expect(result.isValid).toBe(false);
198
+ expect(result.errors).toContain('Path is empty');
199
+ });
200
+
201
+ it('should reject whitespace-only path', async () => {
202
+ const result = await validator.validate(' ');
203
+ expect(result.isValid).toBe(false);
204
+ expect(result.errors).toContain('Path is empty');
205
+ });
206
+ });
207
+
208
+ describe('Synchronous Validation', () => {
209
+ it('should validate synchronously', () => {
210
+ const result = validator.validateSync('/workspaces/project/src/file.ts');
211
+ expect(result.isValid).toBe(true);
212
+ });
213
+
214
+ it('should detect traversal synchronously', () => {
215
+ const result = validator.validateSync('/workspaces/project/../etc/passwd');
216
+ expect(result.isValid).toBe(false);
217
+ });
218
+ });
219
+
220
+ describe('validateOrThrow', () => {
221
+ it('should return path when valid', async () => {
222
+ const resolved = await validator.validateOrThrow('/workspaces/project/src/file.ts');
223
+ expect(resolved).toBe('/workspaces/project/src/file.ts');
224
+ });
225
+
226
+ it('should throw when invalid', async () => {
227
+ await expect(
228
+ validator.validateOrThrow('/etc/passwd')
229
+ ).rejects.toThrow(PathValidatorError);
230
+ });
231
+ });
232
+
233
+ describe('securePath', () => {
234
+ it('should join paths securely', async () => {
235
+ const resolved = await validator.securePath(projectRoot, 'src', 'file.ts');
236
+ expect(resolved).toBe('/workspaces/project/src/file.ts');
237
+ });
238
+
239
+ it('should block traversal in segments', async () => {
240
+ await expect(
241
+ validator.securePath(projectRoot, '..', 'etc', 'passwd')
242
+ ).rejects.toThrow(PathValidatorError);
243
+ });
244
+ });
245
+
246
+ describe('isWithinAllowed', () => {
247
+ it('should return true for allowed paths', () => {
248
+ expect(validator.isWithinAllowed('/workspaces/project/src')).toBe(true);
249
+ });
250
+
251
+ it('should return false for disallowed paths', () => {
252
+ expect(validator.isWithinAllowed('/etc/passwd')).toBe(false);
253
+ });
254
+ });
255
+
256
+ describe('Factory Functions', () => {
257
+ it('should create project path validator', () => {
258
+ const projectValidator = createProjectPathValidator('/workspaces/project');
259
+ const prefixes = projectValidator.getAllowedPrefixes();
260
+
261
+ expect(prefixes).toContain('/workspaces/project/src');
262
+ expect(prefixes).toContain('/workspaces/project/tests');
263
+ expect(prefixes).toContain('/workspaces/project/docs');
264
+ });
265
+
266
+ it('should create full project path validator', () => {
267
+ const fullValidator = createFullProjectPathValidator('/workspaces/project');
268
+ const prefixes = fullValidator.getAllowedPrefixes();
269
+
270
+ expect(prefixes).toContain('/workspaces/project');
271
+ });
272
+ });
273
+
274
+ describe('HIGH-2 Security Verification', () => {
275
+ it('should prevent access to system files via traversal', async () => {
276
+ const attacks = [
277
+ '../../../etc/passwd',
278
+ '..\\..\\..\\windows\\system32\\config\\sam',
279
+ '....//....//....//etc/passwd',
280
+ '..%252f..%252f..%252fetc/passwd',
281
+ '..%c0%af..%c0%af..%c0%afetc/passwd',
282
+ ];
283
+
284
+ for (const attack of attacks) {
285
+ const result = await validator.validate(`/workspaces/project/${attack}`);
286
+ expect(result.isValid).toBe(false);
287
+ }
288
+ });
289
+
290
+ it('should resolve symlink-like path attempts', async () => {
291
+ // Even if the path looks like it's within bounds, resolution should catch escapes
292
+ const result = await validator.validate('/workspaces/project/symlink/../../../etc/passwd');
293
+ expect(result.isValid).toBe(false);
294
+ });
295
+
296
+ it('should not allow prefix manipulation', async () => {
297
+ // Path starts with project root but escapes via traversal
298
+ const result = await validator.validate('/workspaces/project/../../etc/passwd');
299
+ expect(result.isValid).toBe(false);
300
+ });
301
+ });
302
+ });