@claude-flow/security 3.0.0-alpha.1 → 3.0.0-alpha.11

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 (150) 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__/keychain-adapter.test.ts +60 -0
  12. package/__tests__/oauth-browser.test.ts +13 -0
  13. package/__tests__/oauth-export-surface.test.ts +21 -0
  14. package/__tests__/oauth-pkce.test.ts +37 -0
  15. package/__tests__/password-hasher.test.ts +239 -239
  16. package/__tests__/path-validator.test.ts +302 -302
  17. package/__tests__/plugin-integrity-verifier.test.ts +199 -0
  18. package/__tests__/safe-executor.test.ts +292 -292
  19. package/__tests__/token-generator.test.ts +371 -371
  20. package/__tests__/tool-output-guardrail.test.ts +256 -0
  21. package/__tests__/unit/credential-generator.test.ts +182 -182
  22. package/__tests__/unit/password-hasher.test.ts +359 -359
  23. package/__tests__/unit/path-validator.test.ts +509 -509
  24. package/__tests__/unit/safe-executor.test.ts +667 -667
  25. package/__tests__/unit/token-generator.test.ts +310 -310
  26. package/dist/CVE-REMEDIATION.d.ts +17 -6
  27. package/dist/CVE-REMEDIATION.d.ts.map +1 -1
  28. package/dist/CVE-REMEDIATION.js +328 -12
  29. package/dist/CVE-REMEDIATION.js.map +1 -1
  30. package/dist/authorization/propagator.d.ts +148 -0
  31. package/dist/authorization/propagator.d.ts.map +1 -0
  32. package/dist/authorization/propagator.js +182 -0
  33. package/dist/authorization/propagator.js.map +1 -0
  34. package/dist/credential-generator.js +9 -9
  35. package/dist/domain/services/security-domain-service.js +1 -1
  36. package/dist/domain/services/security-domain-service.js.map +1 -1
  37. package/dist/index.d.ts +9 -1
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +21 -1
  40. package/dist/index.js.map +1 -1
  41. package/dist/keychain-adapter.d.ts +48 -0
  42. package/dist/keychain-adapter.d.ts.map +1 -0
  43. package/dist/keychain-adapter.js +0 -0
  44. package/dist/keychain-adapter.js.map +1 -0
  45. package/dist/oauth/browser.d.ts +31 -0
  46. package/dist/oauth/browser.d.ts.map +1 -0
  47. package/dist/oauth/browser.js +55 -0
  48. package/dist/oauth/browser.js.map +1 -0
  49. package/dist/oauth/callback-server.d.ts +37 -0
  50. package/dist/oauth/callback-server.d.ts.map +1 -0
  51. package/dist/oauth/callback-server.js +86 -0
  52. package/dist/oauth/callback-server.js.map +1 -0
  53. package/dist/oauth/client.d.ts +57 -0
  54. package/dist/oauth/client.d.ts.map +1 -0
  55. package/dist/oauth/client.js +120 -0
  56. package/dist/oauth/client.js.map +1 -0
  57. package/dist/oauth/pkce.d.ts +25 -0
  58. package/dist/oauth/pkce.d.ts.map +1 -0
  59. package/dist/oauth/pkce.js +36 -0
  60. package/dist/oauth/pkce.js.map +1 -0
  61. package/dist/password-hasher.d.ts.map +1 -1
  62. package/dist/password-hasher.js +8 -1
  63. package/dist/password-hasher.js.map +1 -1
  64. package/dist/plugins/integrity-verifier.d.ts +125 -0
  65. package/dist/plugins/integrity-verifier.d.ts.map +1 -0
  66. package/dist/plugins/integrity-verifier.js +186 -0
  67. package/dist/plugins/integrity-verifier.js.map +1 -0
  68. package/dist/safe-executor.d.ts +7 -0
  69. package/dist/safe-executor.d.ts.map +1 -1
  70. package/dist/safe-executor.js +21 -1
  71. package/dist/safe-executor.js.map +1 -1
  72. package/dist/tool-output-guardrail.d.ts +126 -0
  73. package/dist/tool-output-guardrail.d.ts.map +1 -0
  74. package/dist/tool-output-guardrail.js +257 -0
  75. package/dist/tool-output-guardrail.js.map +1 -0
  76. package/node_modules-incomplete-release-test/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  77. package/package.json +33 -28
  78. package/scripts/verify-oauth-exports.mjs +29 -0
  79. package/src/CVE-REMEDIATION.ts +591 -251
  80. package/src/application/index.ts +10 -10
  81. package/src/application/services/security-application-service.ts +193 -193
  82. package/src/authorization/propagator.ts +279 -0
  83. package/src/credential-generator.ts +368 -368
  84. package/src/domain/entities/security-context.ts +173 -173
  85. package/src/domain/index.ts +17 -17
  86. package/src/domain/services/security-domain-service.ts +296 -296
  87. package/src/index.ts +340 -271
  88. package/src/input-validator.ts +466 -466
  89. package/src/keychain-adapter.ts +0 -0
  90. package/src/oauth/browser.ts +58 -0
  91. package/src/oauth/callback-server.ts +97 -0
  92. package/src/oauth/client.ts +153 -0
  93. package/src/oauth/pkce.ts +45 -0
  94. package/src/password-hasher.ts +277 -270
  95. package/src/path-validator.ts +525 -525
  96. package/src/plugins/integrity-verifier.ts +265 -0
  97. package/src/safe-executor.ts +546 -525
  98. package/src/token-generator.ts +463 -463
  99. package/src/tool-output-guardrail.ts +360 -0
  100. package/tmp.json +0 -0
  101. package/tsconfig.json +9 -9
  102. package/tsconfig.tsbuildinfo +1 -1
  103. package/vitest.config.ts +20 -0
  104. package/.agentic-flow/intelligence.json +0 -16
  105. package/__tests__/coverage/base.css +0 -224
  106. package/__tests__/coverage/block-navigation.js +0 -87
  107. package/__tests__/coverage/coverage-final.json +0 -11
  108. package/__tests__/coverage/favicon.png +0 -0
  109. package/__tests__/coverage/index.html +0 -161
  110. package/__tests__/coverage/lcov-report/base.css +0 -224
  111. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  112. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  113. package/__tests__/coverage/lcov-report/index.html +0 -161
  114. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  115. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  116. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  117. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  118. package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
  119. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  120. package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
  121. package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
  122. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  123. package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
  124. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  125. package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
  126. package/__tests__/coverage/lcov-report/src/index.html +0 -206
  127. package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
  128. package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
  129. package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
  130. package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
  131. package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
  132. package/__tests__/coverage/lcov.info +0 -3650
  133. package/__tests__/coverage/prettify.css +0 -1
  134. package/__tests__/coverage/prettify.js +0 -2
  135. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  136. package/__tests__/coverage/sorter.js +0 -210
  137. package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
  138. package/__tests__/coverage/src/application/services/index.html +0 -116
  139. package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
  140. package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
  141. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  142. package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
  143. package/__tests__/coverage/src/domain/services/index.html +0 -116
  144. package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
  145. package/__tests__/coverage/src/index.html +0 -206
  146. package/__tests__/coverage/src/input-validator.ts.html +0 -1483
  147. package/__tests__/coverage/src/password-hasher.ts.html +0 -895
  148. package/__tests__/coverage/src/path-validator.ts.html +0 -1660
  149. package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
  150. 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
+ });