@claude-flow/security 3.0.0-alpha.1 → 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 (115) hide show
  1. package/README.md +292 -234
  2. package/__tests__/acceptance/security-compliance.test.ts +674 -674
  3. package/__tests__/authorization-propagator.test.ts +155 -0
  4. package/__tests__/credential-generator.test.ts +310 -310
  5. package/__tests__/fixtures/configurations.ts +425 -419
  6. package/__tests__/fixtures/index.ts +21 -21
  7. package/__tests__/helpers/create-mock.ts +469 -469
  8. package/__tests__/helpers/index.ts +32 -32
  9. package/__tests__/input-validator.test.ts +382 -381
  10. package/__tests__/integration/security-flow.test.ts +606 -606
  11. package/__tests__/password-hasher.test.ts +239 -239
  12. package/__tests__/path-validator.test.ts +302 -302
  13. package/__tests__/plugin-integrity-verifier.test.ts +199 -0
  14. package/__tests__/safe-executor.test.ts +292 -292
  15. package/__tests__/token-generator.test.ts +371 -371
  16. package/__tests__/tool-output-guardrail.test.ts +256 -0
  17. package/__tests__/unit/credential-generator.test.ts +182 -182
  18. package/__tests__/unit/password-hasher.test.ts +359 -359
  19. package/__tests__/unit/path-validator.test.ts +509 -509
  20. package/__tests__/unit/safe-executor.test.ts +667 -667
  21. package/__tests__/unit/token-generator.test.ts +310 -310
  22. package/dist/authorization/propagator.d.ts +148 -0
  23. package/dist/authorization/propagator.d.ts.map +1 -0
  24. package/dist/authorization/propagator.js +182 -0
  25. package/dist/authorization/propagator.js.map +1 -0
  26. package/dist/credential-generator.js +9 -9
  27. package/dist/domain/services/security-domain-service.js +1 -1
  28. package/dist/domain/services/security-domain-service.js.map +1 -1
  29. package/dist/index.d.ts +4 -1
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +11 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/password-hasher.d.ts.map +1 -1
  34. package/dist/password-hasher.js +8 -1
  35. package/dist/password-hasher.js.map +1 -1
  36. package/dist/plugins/integrity-verifier.d.ts +125 -0
  37. package/dist/plugins/integrity-verifier.d.ts.map +1 -0
  38. package/dist/plugins/integrity-verifier.js +186 -0
  39. package/dist/plugins/integrity-verifier.js.map +1 -0
  40. package/dist/safe-executor.d.ts +7 -0
  41. package/dist/safe-executor.d.ts.map +1 -1
  42. package/dist/safe-executor.js +21 -1
  43. package/dist/safe-executor.js.map +1 -1
  44. package/dist/tool-output-guardrail.d.ts +126 -0
  45. package/dist/tool-output-guardrail.d.ts.map +1 -0
  46. package/dist/tool-output-guardrail.js +257 -0
  47. package/dist/tool-output-guardrail.js.map +1 -0
  48. package/package.json +28 -28
  49. package/src/CVE-REMEDIATION.ts +251 -251
  50. package/src/application/index.ts +10 -10
  51. package/src/application/services/security-application-service.ts +193 -193
  52. package/src/authorization/propagator.ts +279 -0
  53. package/src/credential-generator.ts +368 -368
  54. package/src/domain/entities/security-context.ts +173 -173
  55. package/src/domain/index.ts +17 -17
  56. package/src/domain/services/security-domain-service.ts +296 -296
  57. package/src/index.ts +314 -271
  58. package/src/input-validator.ts +466 -466
  59. package/src/password-hasher.ts +277 -270
  60. package/src/path-validator.ts +525 -525
  61. package/src/plugins/integrity-verifier.ts +265 -0
  62. package/src/safe-executor.ts +546 -525
  63. package/src/token-generator.ts +463 -463
  64. package/src/tool-output-guardrail.ts +360 -0
  65. package/tmp.json +0 -0
  66. package/tsconfig.json +9 -9
  67. package/tsconfig.tsbuildinfo +1 -1
  68. package/vitest.config.ts +20 -0
  69. package/.agentic-flow/intelligence.json +0 -16
  70. package/__tests__/coverage/base.css +0 -224
  71. package/__tests__/coverage/block-navigation.js +0 -87
  72. package/__tests__/coverage/coverage-final.json +0 -11
  73. package/__tests__/coverage/favicon.png +0 -0
  74. package/__tests__/coverage/index.html +0 -161
  75. package/__tests__/coverage/lcov-report/base.css +0 -224
  76. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  77. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  78. package/__tests__/coverage/lcov-report/index.html +0 -161
  79. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  80. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  81. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  82. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  83. package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
  84. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  85. package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
  86. package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
  87. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  88. package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
  89. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  90. package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
  91. package/__tests__/coverage/lcov-report/src/index.html +0 -206
  92. package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
  93. package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
  94. package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
  95. package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
  96. package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
  97. package/__tests__/coverage/lcov.info +0 -3650
  98. package/__tests__/coverage/prettify.css +0 -1
  99. package/__tests__/coverage/prettify.js +0 -2
  100. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  101. package/__tests__/coverage/sorter.js +0 -210
  102. package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
  103. package/__tests__/coverage/src/application/services/index.html +0 -116
  104. package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
  105. package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
  106. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  107. package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
  108. package/__tests__/coverage/src/domain/services/index.html +0 -116
  109. package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
  110. package/__tests__/coverage/src/index.html +0 -206
  111. package/__tests__/coverage/src/input-validator.ts.html +0 -1483
  112. package/__tests__/coverage/src/password-hasher.ts.html +0 -895
  113. package/__tests__/coverage/src/path-validator.ts.html +0 -1660
  114. package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
  115. package/__tests__/coverage/src/token-generator.ts.html +0 -1474
@@ -1,302 +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
- });
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 '../src/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
+ });