@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,381 +1,382 @@
1
- /**
2
- * Input Validator Tests
3
- *
4
- * Tests verify:
5
- * - Zod schema validation
6
- * - Input sanitization
7
- * - Authentication schemas
8
- * - Command and path schemas
9
- */
10
-
11
- import { describe, it, expect } from 'vitest';
12
- import {
13
- InputValidator,
14
- sanitizeString,
15
- sanitizeHtml,
16
- sanitizePath,
17
- SafeStringSchema,
18
- IdentifierSchema,
19
- EmailSchema,
20
- PasswordSchema,
21
- UUIDSchema,
22
- HttpsUrlSchema,
23
- PortSchema,
24
- UserRoleSchema,
25
- PermissionSchema,
26
- LoginRequestSchema,
27
- CreateUserSchema,
28
- TaskInputSchema,
29
- CommandArgumentSchema,
30
- PathSchema,
31
- PATTERNS,
32
- LIMITS,
33
- } from '../../security/input-validator.js';
34
-
35
- describe('InputValidator', () => {
36
- describe('SafeStringSchema', () => {
37
- it('should accept safe strings', () => {
38
- expect(() => SafeStringSchema.parse('hello world')).not.toThrow();
39
- });
40
-
41
- it('should reject empty strings', () => {
42
- expect(() => SafeStringSchema.parse('')).toThrow();
43
- });
44
-
45
- it('should reject strings with shell metacharacters', () => {
46
- const dangerous = [';', '&&', '||', '|', '`', '$()', '${}', '>', '<'];
47
- for (const char of dangerous) {
48
- expect(() => SafeStringSchema.parse(`hello${char}world`)).toThrow();
49
- }
50
- });
51
- });
52
-
53
- describe('IdentifierSchema', () => {
54
- it('should accept valid identifiers', () => {
55
- expect(() => IdentifierSchema.parse('validId')).not.toThrow();
56
- expect(() => IdentifierSchema.parse('valid-id')).not.toThrow();
57
- expect(() => IdentifierSchema.parse('valid_id')).not.toThrow();
58
- expect(() => IdentifierSchema.parse('validId123')).not.toThrow();
59
- });
60
-
61
- it('should reject identifiers starting with number', () => {
62
- expect(() => IdentifierSchema.parse('123invalid')).toThrow();
63
- });
64
-
65
- it('should reject identifiers with special characters', () => {
66
- expect(() => IdentifierSchema.parse('invalid@id')).toThrow();
67
- expect(() => IdentifierSchema.parse('invalid id')).toThrow();
68
- });
69
-
70
- it('should reject empty identifiers', () => {
71
- expect(() => IdentifierSchema.parse('')).toThrow();
72
- });
73
- });
74
-
75
- describe('EmailSchema', () => {
76
- it('should accept valid emails', () => {
77
- expect(() => EmailSchema.parse('user@example.com')).not.toThrow();
78
- expect(() => EmailSchema.parse('user.name@example.co.uk')).not.toThrow();
79
- });
80
-
81
- it('should reject invalid emails', () => {
82
- expect(() => EmailSchema.parse('notanemail')).toThrow();
83
- expect(() => EmailSchema.parse('@nodomain.com')).toThrow();
84
- expect(() => EmailSchema.parse('no@')).toThrow();
85
- });
86
-
87
- it('should lowercase emails', () => {
88
- const result = EmailSchema.parse('USER@EXAMPLE.COM');
89
- expect(result).toBe('user@example.com');
90
- });
91
-
92
- it('should reject too long emails', () => {
93
- const longEmail = 'a'.repeat(300) + '@example.com';
94
- expect(() => EmailSchema.parse(longEmail)).toThrow();
95
- });
96
- });
97
-
98
- describe('PasswordSchema', () => {
99
- it('should accept valid passwords', () => {
100
- expect(() => PasswordSchema.parse('SecurePass123')).not.toThrow();
101
- });
102
-
103
- it('should reject short passwords', () => {
104
- expect(() => PasswordSchema.parse('Short1')).toThrow();
105
- });
106
-
107
- it('should reject passwords without uppercase', () => {
108
- expect(() => PasswordSchema.parse('lowercase123')).toThrow();
109
- });
110
-
111
- it('should reject passwords without lowercase', () => {
112
- expect(() => PasswordSchema.parse('UPPERCASE123')).toThrow();
113
- });
114
-
115
- it('should reject passwords without digits', () => {
116
- expect(() => PasswordSchema.parse('NoDigitsHere')).toThrow();
117
- });
118
- });
119
-
120
- describe('UUIDSchema', () => {
121
- it('should accept valid UUIDs', () => {
122
- expect(() => UUIDSchema.parse('550e8400-e29b-41d4-a716-446655440000')).not.toThrow();
123
- });
124
-
125
- it('should reject invalid UUIDs', () => {
126
- expect(() => UUIDSchema.parse('not-a-uuid')).toThrow();
127
- expect(() => UUIDSchema.parse('550e8400-e29b-41d4-a716')).toThrow();
128
- });
129
- });
130
-
131
- describe('HttpsUrlSchema', () => {
132
- it('should accept HTTPS URLs', () => {
133
- expect(() => HttpsUrlSchema.parse('https://example.com')).not.toThrow();
134
- expect(() => HttpsUrlSchema.parse('https://example.com/path')).not.toThrow();
135
- });
136
-
137
- it('should reject HTTP URLs', () => {
138
- expect(() => HttpsUrlSchema.parse('http://example.com')).toThrow();
139
- });
140
-
141
- it('should reject invalid URLs', () => {
142
- expect(() => HttpsUrlSchema.parse('not-a-url')).toThrow();
143
- });
144
- });
145
-
146
- describe('PortSchema', () => {
147
- it('should accept valid ports', () => {
148
- expect(() => PortSchema.parse(80)).not.toThrow();
149
- expect(() => PortSchema.parse(443)).not.toThrow();
150
- expect(() => PortSchema.parse(3000)).not.toThrow();
151
- expect(() => PortSchema.parse(65535)).not.toThrow();
152
- });
153
-
154
- it('should reject invalid ports', () => {
155
- expect(() => PortSchema.parse(0)).toThrow();
156
- expect(() => PortSchema.parse(-1)).toThrow();
157
- expect(() => PortSchema.parse(65536)).toThrow();
158
- expect(() => PortSchema.parse(3.14)).toThrow();
159
- });
160
- });
161
-
162
- describe('UserRoleSchema', () => {
163
- it('should accept valid roles', () => {
164
- expect(() => UserRoleSchema.parse('admin')).not.toThrow();
165
- expect(() => UserRoleSchema.parse('operator')).not.toThrow();
166
- expect(() => UserRoleSchema.parse('developer')).not.toThrow();
167
- expect(() => UserRoleSchema.parse('viewer')).not.toThrow();
168
- expect(() => UserRoleSchema.parse('service')).not.toThrow();
169
- });
170
-
171
- it('should reject invalid roles', () => {
172
- expect(() => UserRoleSchema.parse('superuser')).toThrow();
173
- expect(() => UserRoleSchema.parse('root')).toThrow();
174
- });
175
- });
176
-
177
- describe('PermissionSchema', () => {
178
- it('should accept valid permissions', () => {
179
- expect(() => PermissionSchema.parse('swarm.create')).not.toThrow();
180
- expect(() => PermissionSchema.parse('agent.spawn')).not.toThrow();
181
- expect(() => PermissionSchema.parse('system.admin')).not.toThrow();
182
- });
183
-
184
- it('should reject invalid permissions', () => {
185
- expect(() => PermissionSchema.parse('invalid.permission')).toThrow();
186
- });
187
- });
188
-
189
- describe('LoginRequestSchema', () => {
190
- it('should accept valid login request', () => {
191
- expect(() => LoginRequestSchema.parse({
192
- email: 'user@example.com',
193
- password: 'password123',
194
- })).not.toThrow();
195
- });
196
-
197
- it('should accept login with MFA code', () => {
198
- expect(() => LoginRequestSchema.parse({
199
- email: 'user@example.com',
200
- password: 'password123',
201
- mfaCode: '123456',
202
- })).not.toThrow();
203
- });
204
-
205
- it('should reject invalid MFA code length', () => {
206
- expect(() => LoginRequestSchema.parse({
207
- email: 'user@example.com',
208
- password: 'password123',
209
- mfaCode: '12345', // 5 digits instead of 6
210
- })).toThrow();
211
- });
212
- });
213
-
214
- describe('CreateUserSchema', () => {
215
- it('should accept valid user creation', () => {
216
- expect(() => CreateUserSchema.parse({
217
- email: 'user@example.com',
218
- password: 'SecurePass123',
219
- role: 'developer',
220
- })).not.toThrow();
221
- });
222
-
223
- it('should require strong password', () => {
224
- expect(() => CreateUserSchema.parse({
225
- email: 'user@example.com',
226
- password: 'weak',
227
- role: 'developer',
228
- })).toThrow();
229
- });
230
- });
231
-
232
- describe('TaskInputSchema', () => {
233
- it('should accept valid task input', () => {
234
- expect(() => TaskInputSchema.parse({
235
- taskId: '550e8400-e29b-41d4-a716-446655440000',
236
- content: 'Implement new feature',
237
- agentType: 'coder',
238
- })).not.toThrow();
239
- });
240
-
241
- it('should reject task with shell characters in content', () => {
242
- expect(() => TaskInputSchema.parse({
243
- taskId: '550e8400-e29b-41d4-a716-446655440000',
244
- content: 'Implement feature; rm -rf /',
245
- agentType: 'coder',
246
- })).toThrow();
247
- });
248
- });
249
-
250
- describe('CommandArgumentSchema', () => {
251
- it('should accept safe arguments', () => {
252
- expect(() => CommandArgumentSchema.parse('--flag')).not.toThrow();
253
- expect(() => CommandArgumentSchema.parse('value')).not.toThrow();
254
- expect(() => CommandArgumentSchema.parse('path/to/file')).not.toThrow();
255
- });
256
-
257
- it('should reject arguments with null bytes', () => {
258
- expect(() => CommandArgumentSchema.parse('arg\x00injected')).toThrow();
259
- });
260
-
261
- it('should reject arguments with shell metacharacters', () => {
262
- expect(() => CommandArgumentSchema.parse('arg;injected')).toThrow();
263
- expect(() => CommandArgumentSchema.parse('arg&&injected')).toThrow();
264
- expect(() => CommandArgumentSchema.parse('arg|injected')).toThrow();
265
- });
266
- });
267
-
268
- describe('PathSchema', () => {
269
- it('should accept valid paths', () => {
270
- expect(() => PathSchema.parse('/path/to/file.ts')).not.toThrow();
271
- expect(() => PathSchema.parse('./relative/path')).not.toThrow();
272
- });
273
-
274
- it('should reject paths with traversal', () => {
275
- expect(() => PathSchema.parse('/path/../etc/passwd')).toThrow();
276
- });
277
-
278
- it('should reject paths with null bytes', () => {
279
- expect(() => PathSchema.parse('/path/file\x00.jpg')).toThrow();
280
- });
281
- });
282
-
283
- describe('Sanitization Functions', () => {
284
- describe('sanitizeString', () => {
285
- it('should remove null bytes', () => {
286
- expect(sanitizeString('hello\x00world')).toBe('helloworld');
287
- });
288
-
289
- it('should remove HTML brackets', () => {
290
- expect(sanitizeString('<script>alert(1)</script>')).toBe('scriptalert(1)/script');
291
- });
292
-
293
- it('should remove javascript: protocol', () => {
294
- expect(sanitizeString('javascript:alert(1)')).toBe('alert(1)');
295
- });
296
-
297
- it('should trim whitespace', () => {
298
- expect(sanitizeString(' hello ')).toBe('hello');
299
- });
300
- });
301
-
302
- describe('sanitizeHtml', () => {
303
- it('should escape HTML entities', () => {
304
- expect(sanitizeHtml('<script>')).toBe('&lt;script&gt;');
305
- expect(sanitizeHtml('"quoted"')).toBe('&quot;quoted&quot;');
306
- expect(sanitizeHtml("'apostrophe'")).toBe('&#x27;apostrophe&#x27;');
307
- expect(sanitizeHtml('a & b')).toBe('a &amp; b');
308
- });
309
- });
310
-
311
- describe('sanitizePath', () => {
312
- it('should remove null bytes', () => {
313
- expect(sanitizePath('/path\x00/file')).toBe('path/file');
314
- });
315
-
316
- it('should remove traversal patterns', () => {
317
- expect(sanitizePath('../etc/passwd')).toBe('/etc/passwd');
318
- });
319
-
320
- it('should normalize slashes', () => {
321
- expect(sanitizePath('/path//to///file')).toBe('path/to/file');
322
- });
323
-
324
- it('should remove leading slash', () => {
325
- expect(sanitizePath('/absolute/path')).toBe('absolute/path');
326
- });
327
- });
328
- });
329
-
330
- describe('InputValidator Class', () => {
331
- it('should validate email', () => {
332
- expect(InputValidator.validateEmail('user@example.com')).toBe('user@example.com');
333
- });
334
-
335
- it('should validate password', () => {
336
- expect(() => InputValidator.validatePassword('SecurePass123')).not.toThrow();
337
- });
338
-
339
- it('should validate identifier', () => {
340
- expect(InputValidator.validateIdentifier('myId')).toBe('myId');
341
- });
342
-
343
- it('should validate path', () => {
344
- expect(InputValidator.validatePath('/valid/path')).toBe('/valid/path');
345
- });
346
-
347
- it('should validate command argument', () => {
348
- expect(InputValidator.validateCommandArg('--flag')).toBe('--flag');
349
- });
350
-
351
- it('should validate login request', () => {
352
- const result = InputValidator.validateLoginRequest({
353
- email: 'USER@example.com',
354
- password: 'password',
355
- });
356
- expect(result.email).toBe('user@example.com');
357
- });
358
-
359
- it('should safely parse with result', () => {
360
- const success = InputValidator.safeParse(EmailSchema, 'user@example.com');
361
- expect(success.success).toBe(true);
362
-
363
- const failure = InputValidator.safeParse(EmailSchema, 'invalid');
364
- expect(failure.success).toBe(false);
365
- });
366
- });
367
-
368
- describe('Constants', () => {
369
- it('should export PATTERNS', () => {
370
- expect(PATTERNS.SAFE_IDENTIFIER).toBeDefined();
371
- expect(PATTERNS.SAFE_FILENAME).toBeDefined();
372
- expect(PATTERNS.NO_SHELL_CHARS).toBeDefined();
373
- });
374
-
375
- it('should export LIMITS', () => {
376
- expect(LIMITS.MIN_PASSWORD_LENGTH).toBe(8);
377
- expect(LIMITS.MAX_PASSWORD_LENGTH).toBe(128);
378
- expect(LIMITS.MAX_PATH_LENGTH).toBe(4096);
379
- });
380
- });
381
- });
1
+ /**
2
+ * Input Validator Tests
3
+ *
4
+ * Tests verify:
5
+ * - Zod schema validation
6
+ * - Input sanitization
7
+ * - Authentication schemas
8
+ * - Command and path schemas
9
+ */
10
+
11
+ import { describe, it, expect } from 'vitest';
12
+ import {
13
+ InputValidator,
14
+ sanitizeString,
15
+ sanitizeHtml,
16
+ sanitizePath,
17
+ SafeStringSchema,
18
+ IdentifierSchema,
19
+ EmailSchema,
20
+ PasswordSchema,
21
+ UUIDSchema,
22
+ HttpsUrlSchema,
23
+ PortSchema,
24
+ UserRoleSchema,
25
+ PermissionSchema,
26
+ LoginRequestSchema,
27
+ CreateUserSchema,
28
+ TaskInputSchema,
29
+ CommandArgumentSchema,
30
+ PathSchema,
31
+ PATTERNS,
32
+ LIMITS,
33
+ } from '../src/input-validator.js';
34
+
35
+ describe('InputValidator', () => {
36
+ describe('SafeStringSchema', () => {
37
+ it('should accept safe strings', () => {
38
+ expect(() => SafeStringSchema.parse('hello world')).not.toThrow();
39
+ });
40
+
41
+ it('should reject empty strings', () => {
42
+ expect(() => SafeStringSchema.parse('')).toThrow();
43
+ });
44
+
45
+ it('should reject strings with shell metacharacters', () => {
46
+ const dangerous = [';', '&&', '||', '|', '`', '$()', '${}', '>', '<'];
47
+ for (const char of dangerous) {
48
+ expect(() => SafeStringSchema.parse(`hello${char}world`)).toThrow();
49
+ }
50
+ });
51
+ });
52
+
53
+ describe('IdentifierSchema', () => {
54
+ it('should accept valid identifiers', () => {
55
+ expect(() => IdentifierSchema.parse('validId')).not.toThrow();
56
+ expect(() => IdentifierSchema.parse('valid-id')).not.toThrow();
57
+ expect(() => IdentifierSchema.parse('valid_id')).not.toThrow();
58
+ expect(() => IdentifierSchema.parse('validId123')).not.toThrow();
59
+ });
60
+
61
+ it('should reject identifiers starting with number', () => {
62
+ expect(() => IdentifierSchema.parse('123invalid')).toThrow();
63
+ });
64
+
65
+ it('should reject identifiers with special characters', () => {
66
+ expect(() => IdentifierSchema.parse('invalid@id')).toThrow();
67
+ expect(() => IdentifierSchema.parse('invalid id')).toThrow();
68
+ });
69
+
70
+ it('should reject empty identifiers', () => {
71
+ expect(() => IdentifierSchema.parse('')).toThrow();
72
+ });
73
+ });
74
+
75
+ describe('EmailSchema', () => {
76
+ it('should accept valid emails', () => {
77
+ expect(() => EmailSchema.parse('user@example.com')).not.toThrow();
78
+ expect(() => EmailSchema.parse('user.name@example.co.uk')).not.toThrow();
79
+ });
80
+
81
+ it('should reject invalid emails', () => {
82
+ expect(() => EmailSchema.parse('notanemail')).toThrow();
83
+ expect(() => EmailSchema.parse('@nodomain.com')).toThrow();
84
+ expect(() => EmailSchema.parse('no@')).toThrow();
85
+ });
86
+
87
+ it('should lowercase emails', () => {
88
+ const result = EmailSchema.parse('USER@EXAMPLE.COM');
89
+ expect(result).toBe('user@example.com');
90
+ });
91
+
92
+ it('should reject too long emails', () => {
93
+ const longEmail = 'a'.repeat(300) + '@example.com';
94
+ expect(() => EmailSchema.parse(longEmail)).toThrow();
95
+ });
96
+ });
97
+
98
+ describe('PasswordSchema', () => {
99
+ it('should accept valid passwords', () => {
100
+ expect(() => PasswordSchema.parse('SecurePass123')).not.toThrow();
101
+ });
102
+
103
+ it('should reject short passwords', () => {
104
+ expect(() => PasswordSchema.parse('Short1')).toThrow();
105
+ });
106
+
107
+ it('should reject passwords without uppercase', () => {
108
+ expect(() => PasswordSchema.parse('lowercase123')).toThrow();
109
+ });
110
+
111
+ it('should reject passwords without lowercase', () => {
112
+ expect(() => PasswordSchema.parse('UPPERCASE123')).toThrow();
113
+ });
114
+
115
+ it('should reject passwords without digits', () => {
116
+ expect(() => PasswordSchema.parse('NoDigitsHere')).toThrow();
117
+ });
118
+ });
119
+
120
+ describe('UUIDSchema', () => {
121
+ it('should accept valid UUIDs', () => {
122
+ expect(() => UUIDSchema.parse('550e8400-e29b-41d4-a716-446655440000')).not.toThrow();
123
+ });
124
+
125
+ it('should reject invalid UUIDs', () => {
126
+ expect(() => UUIDSchema.parse('not-a-uuid')).toThrow();
127
+ expect(() => UUIDSchema.parse('550e8400-e29b-41d4-a716')).toThrow();
128
+ });
129
+ });
130
+
131
+ describe('HttpsUrlSchema', () => {
132
+ it('should accept HTTPS URLs', () => {
133
+ expect(() => HttpsUrlSchema.parse('https://example.com')).not.toThrow();
134
+ expect(() => HttpsUrlSchema.parse('https://example.com/path')).not.toThrow();
135
+ });
136
+
137
+ it('should reject HTTP URLs', () => {
138
+ expect(() => HttpsUrlSchema.parse('http://example.com')).toThrow();
139
+ });
140
+
141
+ it('should reject invalid URLs', () => {
142
+ expect(() => HttpsUrlSchema.parse('not-a-url')).toThrow();
143
+ });
144
+ });
145
+
146
+ describe('PortSchema', () => {
147
+ it('should accept valid ports', () => {
148
+ expect(() => PortSchema.parse(80)).not.toThrow();
149
+ expect(() => PortSchema.parse(443)).not.toThrow();
150
+ expect(() => PortSchema.parse(3000)).not.toThrow();
151
+ expect(() => PortSchema.parse(65535)).not.toThrow();
152
+ });
153
+
154
+ it('should reject invalid ports', () => {
155
+ expect(() => PortSchema.parse(0)).toThrow();
156
+ expect(() => PortSchema.parse(-1)).toThrow();
157
+ expect(() => PortSchema.parse(65536)).toThrow();
158
+ expect(() => PortSchema.parse(3.14)).toThrow();
159
+ });
160
+ });
161
+
162
+ describe('UserRoleSchema', () => {
163
+ it('should accept valid roles', () => {
164
+ expect(() => UserRoleSchema.parse('admin')).not.toThrow();
165
+ expect(() => UserRoleSchema.parse('operator')).not.toThrow();
166
+ expect(() => UserRoleSchema.parse('developer')).not.toThrow();
167
+ expect(() => UserRoleSchema.parse('viewer')).not.toThrow();
168
+ expect(() => UserRoleSchema.parse('service')).not.toThrow();
169
+ });
170
+
171
+ it('should reject invalid roles', () => {
172
+ expect(() => UserRoleSchema.parse('superuser')).toThrow();
173
+ expect(() => UserRoleSchema.parse('root')).toThrow();
174
+ });
175
+ });
176
+
177
+ describe('PermissionSchema', () => {
178
+ it('should accept valid permissions', () => {
179
+ expect(() => PermissionSchema.parse('swarm.create')).not.toThrow();
180
+ expect(() => PermissionSchema.parse('agent.spawn')).not.toThrow();
181
+ expect(() => PermissionSchema.parse('system.admin')).not.toThrow();
182
+ });
183
+
184
+ it('should reject invalid permissions', () => {
185
+ expect(() => PermissionSchema.parse('invalid.permission')).toThrow();
186
+ });
187
+ });
188
+
189
+ describe('LoginRequestSchema', () => {
190
+ it('should accept valid login request', () => {
191
+ expect(() => LoginRequestSchema.parse({
192
+ email: 'user@example.com',
193
+ password: 'password123',
194
+ })).not.toThrow();
195
+ });
196
+
197
+ it('should accept login with MFA code', () => {
198
+ expect(() => LoginRequestSchema.parse({
199
+ email: 'user@example.com',
200
+ password: 'password123',
201
+ mfaCode: '123456',
202
+ })).not.toThrow();
203
+ });
204
+
205
+ it('should reject invalid MFA code length', () => {
206
+ expect(() => LoginRequestSchema.parse({
207
+ email: 'user@example.com',
208
+ password: 'password123',
209
+ mfaCode: '12345', // 5 digits instead of 6
210
+ })).toThrow();
211
+ });
212
+ });
213
+
214
+ describe('CreateUserSchema', () => {
215
+ it('should accept valid user creation', () => {
216
+ expect(() => CreateUserSchema.parse({
217
+ email: 'user@example.com',
218
+ password: 'SecurePass123',
219
+ role: 'developer',
220
+ })).not.toThrow();
221
+ });
222
+
223
+ it('should require strong password', () => {
224
+ expect(() => CreateUserSchema.parse({
225
+ email: 'user@example.com',
226
+ password: 'weak',
227
+ role: 'developer',
228
+ })).toThrow();
229
+ });
230
+ });
231
+
232
+ describe('TaskInputSchema', () => {
233
+ it('should accept valid task input', () => {
234
+ expect(() => TaskInputSchema.parse({
235
+ taskId: '550e8400-e29b-41d4-a716-446655440000',
236
+ content: 'Implement new feature',
237
+ agentType: 'coder',
238
+ })).not.toThrow();
239
+ });
240
+
241
+ it('should reject task with shell characters in content', () => {
242
+ expect(() => TaskInputSchema.parse({
243
+ taskId: '550e8400-e29b-41d4-a716-446655440000',
244
+ content: 'Implement feature; rm -rf /',
245
+ agentType: 'coder',
246
+ })).toThrow();
247
+ });
248
+ });
249
+
250
+ describe('CommandArgumentSchema', () => {
251
+ it('should accept safe arguments', () => {
252
+ expect(() => CommandArgumentSchema.parse('--flag')).not.toThrow();
253
+ expect(() => CommandArgumentSchema.parse('value')).not.toThrow();
254
+ expect(() => CommandArgumentSchema.parse('path/to/file')).not.toThrow();
255
+ });
256
+
257
+ it('should reject arguments with null bytes', () => {
258
+ expect(() => CommandArgumentSchema.parse('arg\x00injected')).toThrow();
259
+ });
260
+
261
+ it('should reject arguments with shell metacharacters', () => {
262
+ expect(() => CommandArgumentSchema.parse('arg;injected')).toThrow();
263
+ expect(() => CommandArgumentSchema.parse('arg&&injected')).toThrow();
264
+ expect(() => CommandArgumentSchema.parse('arg|injected')).toThrow();
265
+ });
266
+ });
267
+
268
+ describe('PathSchema', () => {
269
+ it('should accept valid paths', () => {
270
+ expect(() => PathSchema.parse('/path/to/file.ts')).not.toThrow();
271
+ expect(() => PathSchema.parse('./relative/path')).not.toThrow();
272
+ });
273
+
274
+ it('should reject paths with traversal', () => {
275
+ expect(() => PathSchema.parse('/path/../etc/passwd')).toThrow();
276
+ });
277
+
278
+ it('should reject paths with null bytes', () => {
279
+ expect(() => PathSchema.parse('/path/file\x00.jpg')).toThrow();
280
+ });
281
+ });
282
+
283
+ describe('Sanitization Functions', () => {
284
+ describe('sanitizeString', () => {
285
+ it('should remove null bytes', () => {
286
+ expect(sanitizeString('hello\x00world')).toBe('helloworld');
287
+ });
288
+
289
+ it('should remove HTML brackets', () => {
290
+ expect(sanitizeString('<script>alert(1)</script>')).toBe('scriptalert(1)/script');
291
+ });
292
+
293
+ it('should remove javascript: protocol', () => {
294
+ expect(sanitizeString('javascript:alert(1)')).toBe('alert(1)');
295
+ });
296
+
297
+ it('should trim whitespace', () => {
298
+ expect(sanitizeString(' hello ')).toBe('hello');
299
+ });
300
+ });
301
+
302
+ describe('sanitizeHtml', () => {
303
+ it('should escape HTML entities', () => {
304
+ expect(sanitizeHtml('<script>')).toBe('&lt;script&gt;');
305
+ expect(sanitizeHtml('"quoted"')).toBe('&quot;quoted&quot;');
306
+ expect(sanitizeHtml("'apostrophe'")).toBe('&#x27;apostrophe&#x27;');
307
+ expect(sanitizeHtml('a & b')).toBe('a &amp; b');
308
+ });
309
+ });
310
+
311
+ describe('sanitizePath', () => {
312
+ it('should remove null bytes', () => {
313
+ expect(sanitizePath('/path\x00/file')).toBe('path/file');
314
+ });
315
+
316
+ it('should remove traversal patterns', () => {
317
+ // '../etc/passwd' → remove '..' → '/etc/passwd' → remove leading '/' → 'etc/passwd'
318
+ expect(sanitizePath('../etc/passwd')).toBe('etc/passwd');
319
+ });
320
+
321
+ it('should normalize slashes', () => {
322
+ expect(sanitizePath('/path//to///file')).toBe('path/to/file');
323
+ });
324
+
325
+ it('should remove leading slash', () => {
326
+ expect(sanitizePath('/absolute/path')).toBe('absolute/path');
327
+ });
328
+ });
329
+ });
330
+
331
+ describe('InputValidator Class', () => {
332
+ it('should validate email', () => {
333
+ expect(InputValidator.validateEmail('user@example.com')).toBe('user@example.com');
334
+ });
335
+
336
+ it('should validate password', () => {
337
+ expect(() => InputValidator.validatePassword('SecurePass123')).not.toThrow();
338
+ });
339
+
340
+ it('should validate identifier', () => {
341
+ expect(InputValidator.validateIdentifier('myId')).toBe('myId');
342
+ });
343
+
344
+ it('should validate path', () => {
345
+ expect(InputValidator.validatePath('/valid/path')).toBe('/valid/path');
346
+ });
347
+
348
+ it('should validate command argument', () => {
349
+ expect(InputValidator.validateCommandArg('--flag')).toBe('--flag');
350
+ });
351
+
352
+ it('should validate login request', () => {
353
+ const result = InputValidator.validateLoginRequest({
354
+ email: 'USER@example.com',
355
+ password: 'password',
356
+ });
357
+ expect(result.email).toBe('user@example.com');
358
+ });
359
+
360
+ it('should safely parse with result', () => {
361
+ const success = InputValidator.safeParse(EmailSchema, 'user@example.com');
362
+ expect(success.success).toBe(true);
363
+
364
+ const failure = InputValidator.safeParse(EmailSchema, 'invalid');
365
+ expect(failure.success).toBe(false);
366
+ });
367
+ });
368
+
369
+ describe('Constants', () => {
370
+ it('should export PATTERNS', () => {
371
+ expect(PATTERNS.SAFE_IDENTIFIER).toBeDefined();
372
+ expect(PATTERNS.SAFE_FILENAME).toBeDefined();
373
+ expect(PATTERNS.NO_SHELL_CHARS).toBeDefined();
374
+ });
375
+
376
+ it('should export LIMITS', () => {
377
+ expect(LIMITS.MIN_PASSWORD_LENGTH).toBe(8);
378
+ expect(LIMITS.MAX_PASSWORD_LENGTH).toBe(128);
379
+ expect(LIMITS.MAX_PATH_LENGTH).toBe(4096);
380
+ });
381
+ });
382
+ });