@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.
- package/README.md +292 -234
- package/__tests__/acceptance/security-compliance.test.ts +674 -674
- package/__tests__/authorization-propagator.test.ts +155 -0
- package/__tests__/credential-generator.test.ts +310 -310
- package/__tests__/fixtures/configurations.ts +425 -419
- package/__tests__/fixtures/index.ts +21 -21
- package/__tests__/helpers/create-mock.ts +469 -469
- package/__tests__/helpers/index.ts +32 -32
- package/__tests__/input-validator.test.ts +382 -381
- package/__tests__/integration/security-flow.test.ts +606 -606
- package/__tests__/keychain-adapter.test.ts +60 -0
- package/__tests__/oauth-browser.test.ts +13 -0
- package/__tests__/oauth-export-surface.test.ts +21 -0
- package/__tests__/oauth-pkce.test.ts +37 -0
- package/__tests__/password-hasher.test.ts +239 -239
- package/__tests__/path-validator.test.ts +302 -302
- package/__tests__/plugin-integrity-verifier.test.ts +199 -0
- package/__tests__/safe-executor.test.ts +292 -292
- package/__tests__/token-generator.test.ts +371 -371
- package/__tests__/tool-output-guardrail.test.ts +256 -0
- package/__tests__/unit/credential-generator.test.ts +182 -182
- package/__tests__/unit/password-hasher.test.ts +359 -359
- package/__tests__/unit/path-validator.test.ts +509 -509
- package/__tests__/unit/safe-executor.test.ts +667 -667
- package/__tests__/unit/token-generator.test.ts +310 -310
- package/dist/CVE-REMEDIATION.d.ts +17 -6
- package/dist/CVE-REMEDIATION.d.ts.map +1 -1
- package/dist/CVE-REMEDIATION.js +328 -12
- package/dist/CVE-REMEDIATION.js.map +1 -1
- package/dist/authorization/propagator.d.ts +148 -0
- package/dist/authorization/propagator.d.ts.map +1 -0
- package/dist/authorization/propagator.js +182 -0
- package/dist/authorization/propagator.js.map +1 -0
- package/dist/credential-generator.js +9 -9
- package/dist/domain/services/security-domain-service.js +1 -1
- package/dist/domain/services/security-domain-service.js.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -1
- package/dist/index.js.map +1 -1
- package/dist/keychain-adapter.d.ts +48 -0
- package/dist/keychain-adapter.d.ts.map +1 -0
- package/dist/keychain-adapter.js +0 -0
- package/dist/keychain-adapter.js.map +1 -0
- package/dist/oauth/browser.d.ts +31 -0
- package/dist/oauth/browser.d.ts.map +1 -0
- package/dist/oauth/browser.js +55 -0
- package/dist/oauth/browser.js.map +1 -0
- package/dist/oauth/callback-server.d.ts +37 -0
- package/dist/oauth/callback-server.d.ts.map +1 -0
- package/dist/oauth/callback-server.js +86 -0
- package/dist/oauth/callback-server.js.map +1 -0
- package/dist/oauth/client.d.ts +57 -0
- package/dist/oauth/client.d.ts.map +1 -0
- package/dist/oauth/client.js +120 -0
- package/dist/oauth/client.js.map +1 -0
- package/dist/oauth/pkce.d.ts +25 -0
- package/dist/oauth/pkce.d.ts.map +1 -0
- package/dist/oauth/pkce.js +36 -0
- package/dist/oauth/pkce.js.map +1 -0
- package/dist/password-hasher.d.ts.map +1 -1
- package/dist/password-hasher.js +8 -1
- package/dist/password-hasher.js.map +1 -1
- package/dist/plugins/integrity-verifier.d.ts +125 -0
- package/dist/plugins/integrity-verifier.d.ts.map +1 -0
- package/dist/plugins/integrity-verifier.js +186 -0
- package/dist/plugins/integrity-verifier.js.map +1 -0
- package/dist/safe-executor.d.ts +7 -0
- package/dist/safe-executor.d.ts.map +1 -1
- package/dist/safe-executor.js +21 -1
- package/dist/safe-executor.js.map +1 -1
- package/dist/tool-output-guardrail.d.ts +126 -0
- package/dist/tool-output-guardrail.d.ts.map +1 -0
- package/dist/tool-output-guardrail.js +257 -0
- package/dist/tool-output-guardrail.js.map +1 -0
- package/node_modules-incomplete-release-test/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
- package/package.json +33 -28
- package/scripts/verify-oauth-exports.mjs +29 -0
- package/src/CVE-REMEDIATION.ts +591 -251
- package/src/application/index.ts +10 -10
- package/src/application/services/security-application-service.ts +193 -193
- package/src/authorization/propagator.ts +279 -0
- package/src/credential-generator.ts +368 -368
- package/src/domain/entities/security-context.ts +173 -173
- package/src/domain/index.ts +17 -17
- package/src/domain/services/security-domain-service.ts +296 -296
- package/src/index.ts +340 -271
- package/src/input-validator.ts +466 -466
- package/src/keychain-adapter.ts +0 -0
- package/src/oauth/browser.ts +58 -0
- package/src/oauth/callback-server.ts +97 -0
- package/src/oauth/client.ts +153 -0
- package/src/oauth/pkce.ts +45 -0
- package/src/password-hasher.ts +277 -270
- package/src/path-validator.ts +525 -525
- package/src/plugins/integrity-verifier.ts +265 -0
- package/src/safe-executor.ts +546 -525
- package/src/token-generator.ts +463 -463
- package/src/tool-output-guardrail.ts +360 -0
- package/tmp.json +0 -0
- package/tsconfig.json +9 -9
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +20 -0
- package/.agentic-flow/intelligence.json +0 -16
- package/__tests__/coverage/base.css +0 -224
- package/__tests__/coverage/block-navigation.js +0 -87
- package/__tests__/coverage/coverage-final.json +0 -11
- package/__tests__/coverage/favicon.png +0 -0
- package/__tests__/coverage/index.html +0 -161
- package/__tests__/coverage/lcov-report/base.css +0 -224
- package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
- package/__tests__/coverage/lcov-report/favicon.png +0 -0
- package/__tests__/coverage/lcov-report/index.html +0 -161
- package/__tests__/coverage/lcov-report/prettify.css +0 -1
- package/__tests__/coverage/lcov-report/prettify.js +0 -2
- package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/lcov-report/sorter.js +0 -210
- package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
- package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
- package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
- package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
- package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
- package/__tests__/coverage/lcov-report/src/index.html +0 -206
- package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
- package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
- package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
- package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
- package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
- package/__tests__/coverage/lcov.info +0 -3650
- package/__tests__/coverage/prettify.css +0 -1
- package/__tests__/coverage/prettify.js +0 -2
- package/__tests__/coverage/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/sorter.js +0 -210
- package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
- package/__tests__/coverage/src/application/services/index.html +0 -116
- package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
- package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
- package/__tests__/coverage/src/domain/entities/index.html +0 -116
- package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
- package/__tests__/coverage/src/domain/services/index.html +0 -116
- package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
- package/__tests__/coverage/src/index.html +0 -206
- package/__tests__/coverage/src/input-validator.ts.html +0 -1483
- package/__tests__/coverage/src/password-hasher.ts.html +0 -895
- package/__tests__/coverage/src/path-validator.ts.html +0 -1660
- package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
- 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 '
|
|
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
|
+
});
|