@elevasis/core 0.11.1 → 0.12.0

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 (38) hide show
  1. package/dist/index.d.ts +2 -2
  2. package/dist/index.js +10 -11
  3. package/dist/organization-model/index.d.ts +2 -2
  4. package/dist/organization-model/index.js +10 -11
  5. package/dist/test-utils/index.d.ts +10 -3
  6. package/dist/test-utils/index.js +6 -6
  7. package/package.json +1 -1
  8. package/src/__tests__/template-core-compatibility.test.ts +6 -15
  9. package/src/_gen/__tests__/__snapshots__/contracts.md.snap +27 -270
  10. package/src/auth/multi-tenancy/credentials/server/encryption.ts +83 -39
  11. package/src/auth/multi-tenancy/credentials/server/kek-loader.ts +47 -0
  12. package/src/auth/multi-tenancy/index.ts +3 -0
  13. package/src/auth/multi-tenancy/invitations/api-schemas.ts +104 -107
  14. package/src/auth/multi-tenancy/memberships/api-schemas.ts +6 -5
  15. package/src/auth/multi-tenancy/memberships/membership.ts +130 -138
  16. package/src/auth/multi-tenancy/role-management/api-schemas.ts +78 -0
  17. package/src/auth/multi-tenancy/role-management/index.ts +16 -0
  18. package/src/execution/engine/tools/integration/server/adapters/apify/__tests__/apify-run-actor.integration.test.ts +299 -293
  19. package/src/execution/engine/tools/integration/service.test.ts +214 -0
  20. package/src/execution/engine/tools/integration/service.ts +169 -161
  21. package/src/integrations/credentials/__tests__/api-schemas.test.ts +420 -496
  22. package/src/integrations/credentials/api-schemas.ts +127 -143
  23. package/src/integrations/webhook-endpoints/__tests__/api-schemas.test.ts +327 -318
  24. package/src/integrations/webhook-endpoints/api-schemas.ts +103 -102
  25. package/src/integrations/webhook-endpoints/types.ts +58 -51
  26. package/src/operations/activities/api-schemas.ts +80 -79
  27. package/src/operations/activities/types.ts +64 -63
  28. package/src/organization-model/contracts.ts +1 -1
  29. package/src/organization-model/defaults.ts +6 -6
  30. package/src/organization-model/domains/navigation.ts +38 -37
  31. package/src/organization-model/foundation.ts +2 -3
  32. package/src/organization-model/published.ts +3 -3
  33. package/src/platform/constants/versions.ts +1 -1
  34. package/src/reference/_generated/contracts.md +27 -270
  35. package/src/scaffold-registry/__tests__/index.test.ts +72 -7
  36. package/src/scaffold-registry/index.ts +159 -26
  37. package/src/server.ts +281 -272
  38. package/src/supabase/database.types.ts +7 -3
@@ -1,496 +1,420 @@
1
- /**
2
- * Credential API schemas tests
3
- * Tests all validation schemas for credentials endpoints
4
- * Focus: Security (path traversal, DoS, mass assignment, type coercion)
5
- */
6
-
7
- import { describe, it, expect } from 'vitest'
8
- import {
9
- CredentialTypeSchema,
10
- CreateCredentialRequestSchema,
11
- UpdateCredentialParamsSchema,
12
- UpdateCredentialRequestSchema,
13
- DeleteCredentialParamsSchema,
14
- DecryptCredentialParamsSchema,
15
- ListCredentialsResponseSchema
16
- } from '../api-schemas'
17
-
18
- describe('CredentialTypeSchema', () => {
19
- it('accepts valid credential types', () => {
20
- // These are the actual types stored in the database
21
- expect(CredentialTypeSchema.parse('oauth')).toBe('oauth')
22
- expect(CredentialTypeSchema.parse('api-key')).toBe('api-key')
23
- expect(CredentialTypeSchema.parse('webhook-secret')).toBe('webhook-secret')
24
- })
25
-
26
- it('rejects invalid credential types', () => {
27
- expect(() => CredentialTypeSchema.parse('invalid-type')).toThrow()
28
- expect(() => CredentialTypeSchema.parse('')).toThrow()
29
- })
30
-
31
- it('rejects provider names (these are CREDENTIAL_SCHEMAS keys, not stored types)', () => {
32
- // OAuth providers store type='oauth', not their provider name
33
- expect(() => CredentialTypeSchema.parse('notion')).toThrow()
34
- expect(() => CredentialTypeSchema.parse('google-sheets')).toThrow()
35
- })
36
- })
37
-
38
- describe('CreateCredentialRequestSchema', () => {
39
- const validPayload = {
40
- name: 'gmail-prod',
41
- type: 'api-key' as const,
42
- value: { apiKey: 'test-key-123' }
43
- }
44
-
45
- describe('valid requests', () => {
46
- it('accepts valid credential creation request', () => {
47
- const result = CreateCredentialRequestSchema.parse(validPayload)
48
- expect(result).toEqual(validPayload)
49
- })
50
-
51
- it('accepts oauth type credentials', () => {
52
- const payload = {
53
- name: 'notion-dev',
54
- type: 'oauth' as const,
55
- value: { accessToken: 'token', refreshToken: 'refresh' }
56
- }
57
- const result = CreateCredentialRequestSchema.parse(payload)
58
- expect(result).toEqual(payload)
59
- })
60
-
61
- it('accepts webhook-secret type credentials', () => {
62
- const payload = {
63
- name: 'stripe-webhook',
64
- type: 'webhook-secret' as const,
65
- value: { signingSecret: 'whsec_abc123' }
66
- }
67
- const result = CreateCredentialRequestSchema.parse(payload)
68
- expect(result).toEqual(payload)
69
- })
70
- })
71
-
72
- describe('SECURITY: mass assignment prevention', () => {
73
- it('rejects unknown fields (strict mode)', () => {
74
- const payload = {
75
- ...validPayload,
76
- organizationId: 'attacker-org-id', // Injected field
77
- createdBy: null // Override creator
78
- }
79
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
80
- })
81
-
82
- it('rejects extra top-level fields', () => {
83
- const payload = {
84
- ...validPayload,
85
- maliciousField: 'value'
86
- }
87
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
88
- })
89
- })
90
-
91
- describe('SECURITY: credential name validation', () => {
92
- it('rejects invalid credential names', () => {
93
- const payload = { ...validPayload, name: 'gmail prod' }
94
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must be lowercase/)
95
- })
96
-
97
- it('rejects path traversal in name', () => {
98
- const payload = { ...validPayload, name: '../admin-cred' }
99
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must be lowercase/)
100
- })
101
-
102
- it('rejects special characters in name', () => {
103
- const payload = { ...validPayload, name: 'gmail@prod' }
104
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must be lowercase/)
105
- })
106
-
107
- it('rejects names without hyphens', () => {
108
- const payload = { ...validPayload, name: 'gmailprod' }
109
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must be lowercase/)
110
- })
111
-
112
- it('rejects underscores', () => {
113
- const payload = { ...validPayload, name: 'gmail_prod' }
114
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must be lowercase/)
115
- })
116
-
117
- it('auto-lowercases uppercase input', () => {
118
- const payload = { ...validPayload, name: 'Gmail-Prod' }
119
- const result = CreateCredentialRequestSchema.parse(payload)
120
- expect(result.name).toBe('gmail-prod')
121
- })
122
- })
123
-
124
- describe('SECURITY: credential type validation', () => {
125
- it('rejects invalid credential types', () => {
126
- const payload = { ...validPayload, type: 'invalid-type' }
127
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
128
- })
129
-
130
- it('rejects null type', () => {
131
- const payload = { ...validPayload, type: null }
132
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
133
- })
134
-
135
- it('rejects array as type', () => {
136
- const payload = { ...validPayload, type: ['api-key'] }
137
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
138
- })
139
- })
140
-
141
- describe('SECURITY: credential value validation', () => {
142
- it('rejects empty credential value', () => {
143
- const payload = { ...validPayload, value: {} }
144
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must not be empty/)
145
- })
146
-
147
- it('rejects non-object value', () => {
148
- const payload = { ...validPayload, value: 'string-instead-of-object' }
149
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
150
- })
151
-
152
- it('rejects null value', () => {
153
- const payload = { ...validPayload, value: null }
154
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
155
- })
156
-
157
- it('rejects array as value', () => {
158
- const payload = { ...validPayload, value: ['array', 'as', 'value'] }
159
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
160
- })
161
- })
162
-
163
- describe('SECURITY: DoS prevention - credential value size', () => {
164
- it('rejects credential value with too many keys (over 50)', () => {
165
- const largeValue = Object.fromEntries(
166
- Array.from({ length: 51 }, (_, i) => [`key${i}`, 'value'])
167
- )
168
- const payload = { ...validPayload, value: largeValue }
169
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/too many keys/)
170
- })
171
-
172
- it('accepts credential value with max keys (50)', () => {
173
- const maxValue = Object.fromEntries(
174
- Array.from({ length: 50 }, (_, i) => [`key${i}`, 'value'])
175
- )
176
- const payload = { ...validPayload, value: maxValue }
177
- const result = CreateCredentialRequestSchema.parse(payload)
178
- expect(Object.keys(result.value).length).toBe(50)
179
- })
180
-
181
- it('rejects individual string values over 10KB', () => {
182
- const hugeString = 'a'.repeat(10241)
183
- const payload = { ...validPayload, value: { apiKey: hugeString } }
184
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/too large/)
185
- })
186
-
187
- it('accepts string values at max size (10KB)', () => {
188
- const maxString = 'a'.repeat(10240)
189
- const payload = { ...validPayload, value: { apiKey: maxString } }
190
- const result = CreateCredentialRequestSchema.parse(payload)
191
- expect(result.value.apiKey).toBe(maxString)
192
- })
193
-
194
- it('allows non-string values without size checks', () => {
195
- const payload = {
196
- ...validPayload,
197
- value: {
198
- apiKey: 'test',
199
- number: 12345,
200
- boolean: true,
201
- nested: { key: 'value' }
202
- }
203
- }
204
- const result = CreateCredentialRequestSchema.parse(payload)
205
- expect(result.value).toEqual(payload.value)
206
- })
207
- })
208
-
209
- describe('required fields', () => {
210
- it('rejects missing name', () => {
211
- const { name: _name, ...payload } = validPayload
212
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
213
- })
214
-
215
- it('rejects missing type', () => {
216
- const { type: _type, ...payload } = validPayload
217
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
218
- })
219
-
220
- it('rejects missing value', () => {
221
- const { value: _value, ...payload } = validPayload
222
- expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
223
- })
224
- })
225
-
226
- describe('provider field (OAuth provider identification)', () => {
227
- it('accepts OAuth credential with provider', () => {
228
- const payload = {
229
- name: 'my-dropbox',
230
- type: 'oauth' as const,
231
- value: { accessToken: 'token', refreshToken: 'refresh' },
232
- provider: 'dropbox'
233
- }
234
- const result = CreateCredentialRequestSchema.parse(payload)
235
- expect(result.provider).toBe('dropbox')
236
- })
237
-
238
- it('accepts request without provider (optional field)', () => {
239
- const result = CreateCredentialRequestSchema.parse(validPayload)
240
- expect(result.provider).toBeUndefined()
241
- })
242
-
243
- it('accepts various OAuth provider values', () => {
244
- const providers = ['dropbox', 'notion', 'google-sheets']
245
- for (const provider of providers) {
246
- const payload = {
247
- name: `my-${provider}`,
248
- type: 'oauth' as const,
249
- value: { accessToken: 'token' },
250
- provider
251
- }
252
- const result = CreateCredentialRequestSchema.parse(payload)
253
- expect(result.provider).toBe(provider)
254
- }
255
- })
256
- })
257
- })
258
-
259
- describe('UpdateCredentialParamsSchema', () => {
260
- const validUuid = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
261
-
262
- it('accepts valid UUID', () => {
263
- const result = UpdateCredentialParamsSchema.parse({ credentialId: validUuid })
264
- expect(result.credentialId).toBe(validUuid)
265
- })
266
-
267
- it('rejects invalid UUID format', () => {
268
- expect(() => UpdateCredentialParamsSchema.parse({ credentialId: 'not-a-uuid' })).toThrow()
269
- })
270
-
271
- it('rejects empty string', () => {
272
- expect(() => UpdateCredentialParamsSchema.parse({ credentialId: '' })).toThrow()
273
- })
274
-
275
- it('rejects number instead of UUID', () => {
276
- expect(() => UpdateCredentialParamsSchema.parse({ credentialId: 12345 })).toThrow()
277
- })
278
- })
279
-
280
- describe('UpdateCredentialRequestSchema', () => {
281
- const validValue = { apiKey: 'updated-key' }
282
- const validName = 'updated-name'
283
-
284
- describe('valid requests', () => {
285
- it('accepts update with value only', () => {
286
- const result = UpdateCredentialRequestSchema.parse({ value: validValue })
287
- expect(result.value).toEqual(validValue)
288
- expect(result.name).toBeUndefined()
289
- })
290
-
291
- it('accepts update with name only', () => {
292
- const result = UpdateCredentialRequestSchema.parse({ name: validName })
293
- expect(result.name).toBe(validName)
294
- expect(result.value).toBeUndefined()
295
- })
296
-
297
- it('accepts update with both value and name', () => {
298
- const result = UpdateCredentialRequestSchema.parse({ value: validValue, name: validName })
299
- expect(result.value).toEqual(validValue)
300
- expect(result.name).toBe(validName)
301
- })
302
- })
303
-
304
- describe('SECURITY: strict mode', () => {
305
- it('rejects unknown fields', () => {
306
- const payload = { value: validValue, unknownField: 'test' }
307
- expect(() => UpdateCredentialRequestSchema.parse(payload)).toThrow()
308
- })
309
- })
310
-
311
- describe('validation: at least one field required', () => {
312
- it('rejects empty update (no fields)', () => {
313
- expect(() => UpdateCredentialRequestSchema.parse({})).toThrow(/At least one field/)
314
- })
315
-
316
- it('rejects update with undefined fields only', () => {
317
- expect(() => UpdateCredentialRequestSchema.parse({ value: undefined, name: undefined })).toThrow(/At least one field/)
318
- })
319
- })
320
-
321
- describe('SECURITY: credential value validation', () => {
322
- it('rejects empty value object', () => {
323
- expect(() => UpdateCredentialRequestSchema.parse({ value: {} })).toThrow(/must not be empty/)
324
- })
325
-
326
- it('rejects value with too many keys', () => {
327
- const largeValue = Object.fromEntries(
328
- Array.from({ length: 51 }, (_, i) => [`key${i}`, 'value'])
329
- )
330
- expect(() => UpdateCredentialRequestSchema.parse({ value: largeValue })).toThrow(/too many keys/)
331
- })
332
-
333
- it('rejects individual string values over 10KB', () => {
334
- const hugeString = 'a'.repeat(10241)
335
- expect(() => UpdateCredentialRequestSchema.parse({ value: { apiKey: hugeString } })).toThrow(/too large/)
336
- })
337
- })
338
-
339
- describe('SECURITY: credential name validation', () => {
340
- it('rejects invalid name format', () => {
341
- expect(() => UpdateCredentialRequestSchema.parse({ name: 'gmail prod' })).toThrow(/must be lowercase/)
342
- })
343
-
344
- it('rejects path traversal in name', () => {
345
- expect(() => UpdateCredentialRequestSchema.parse({ name: '../admin' })).toThrow(/must be lowercase/)
346
- })
347
- })
348
- })
349
-
350
- describe('DeleteCredentialParamsSchema', () => {
351
- const validUuid = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
352
-
353
- it('accepts valid UUID', () => {
354
- const result = DeleteCredentialParamsSchema.parse({ credentialId: validUuid })
355
- expect(result.credentialId).toBe(validUuid)
356
- })
357
-
358
- it('rejects invalid UUID format', () => {
359
- expect(() => DeleteCredentialParamsSchema.parse({ credentialId: 'not-a-uuid' })).toThrow()
360
- })
361
-
362
- it('rejects empty string', () => {
363
- expect(() => DeleteCredentialParamsSchema.parse({ credentialId: '' })).toThrow()
364
- })
365
- })
366
-
367
- describe('ListCredentialsResponseSchema - Provider Field', () => {
368
- const validUuid = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
369
-
370
- it('validates response with provider set', () => {
371
- const response = {
372
- credentials: [{
373
- id: validUuid,
374
- name: 'dropbox-cred',
375
- type: 'oauth',
376
- provider: 'dropbox',
377
- createdAt: '2026-02-02T00:00:00.000Z'
378
- }]
379
- }
380
- const result = ListCredentialsResponseSchema.parse(response)
381
- expect(result.credentials[0].provider).toBe('dropbox')
382
- })
383
-
384
- it('validates response with null provider (non-OAuth credentials)', () => {
385
- const response = {
386
- credentials: [{
387
- id: validUuid,
388
- name: 'api-key',
389
- type: 'api-key',
390
- provider: null,
391
- createdAt: '2026-02-02T00:00:00.000Z'
392
- }]
393
- }
394
- const result = ListCredentialsResponseSchema.parse(response)
395
- expect(result.credentials[0].provider).toBeNull()
396
- })
397
-
398
- it('validates response with mixed provider values', () => {
399
- const response = {
400
- credentials: [
401
- {
402
- id: validUuid,
403
- name: 'dropbox-cred',
404
- type: 'oauth',
405
- provider: 'dropbox',
406
- createdAt: '2026-02-02T00:00:00.000Z'
407
- },
408
- {
409
- id: 'b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a22',
410
- name: 'api-key',
411
- type: 'api-key',
412
- provider: null,
413
- createdAt: '2026-02-02T00:00:00.000Z'
414
- }
415
- ]
416
- }
417
- const result = ListCredentialsResponseSchema.parse(response)
418
- expect(result.credentials[0].provider).toBe('dropbox')
419
- expect(result.credentials[1].provider).toBeNull()
420
- })
421
- })
422
-
423
- describe('DecryptCredentialParamsSchema - CRITICAL ENDPOINT', () => {
424
- describe('valid credential names', () => {
425
- it('accepts valid credential name', () => {
426
- const result = DecryptCredentialParamsSchema.parse({ credentialName: 'gmail-prod' })
427
- expect(result.credentialName).toBe('gmail-prod')
428
- })
429
-
430
- it('accepts multi-segment names', () => {
431
- const result = DecryptCredentialParamsSchema.parse({ credentialName: 'notion-dev-2024' })
432
- expect(result.credentialName).toBe('notion-dev-2024')
433
- })
434
-
435
- it('trims whitespace', () => {
436
- const result = DecryptCredentialParamsSchema.parse({ credentialName: ' gmail-prod ' })
437
- expect(result.credentialName).toBe('gmail-prod')
438
- })
439
-
440
- it('auto-lowercases input', () => {
441
- const result = DecryptCredentialParamsSchema.parse({ credentialName: 'Gmail-Prod' })
442
- expect(result.credentialName).toBe('gmail-prod')
443
- })
444
- })
445
-
446
- describe('CRITICAL SECURITY: path traversal prevention', () => {
447
- it('rejects path traversal attempts', () => {
448
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: '../admin-cred' })).toThrow(/must be lowercase/)
449
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: '../../secrets' })).toThrow(/must be lowercase/)
450
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: './../config' })).toThrow(/must be lowercase/)
451
- })
452
-
453
- it('rejects relative path characters', () => {
454
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: './local-cred' })).toThrow(/must be lowercase/)
455
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: '../parent' })).toThrow(/must be lowercase/)
456
- })
457
- })
458
-
459
- describe('CRITICAL SECURITY: SQL injection prevention', () => {
460
- it('rejects SQL injection attempts', () => {
461
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: "' OR '1'='1" })).toThrow(/must be lowercase/)
462
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: "admin'; DROP TABLE credentials;--" })).toThrow(/must be lowercase/)
463
- })
464
- })
465
-
466
- describe('CRITICAL SECURITY: special character prevention', () => {
467
- it('rejects names with spaces', () => {
468
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: 'gmail prod' })).toThrow(/must be lowercase/)
469
- })
470
-
471
- it('rejects names with special characters', () => {
472
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: 'gmail@prod' })).toThrow(/must be lowercase/)
473
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: 'notion#dev' })).toThrow(/must be lowercase/)
474
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: 'slack$prod' })).toThrow(/must be lowercase/)
475
- })
476
- })
477
-
478
- describe('CRITICAL SECURITY: DoS prevention', () => {
479
- it('rejects empty names', () => {
480
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: '' })).toThrow(/required/)
481
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: ' ' })).toThrow(/required/)
482
- })
483
-
484
- it('rejects names too long (over 100 chars)', () => {
485
- const longName = 'a-' + 'b'.repeat(99)
486
- expect(() => DecryptCredentialParamsSchema.parse({ credentialName: longName })).toThrow(/too long/)
487
- })
488
-
489
- it('accepts names at max length (100 chars)', () => {
490
- const maxName = 'a'.repeat(49) + '-' + 'b'.repeat(49) + 'c'
491
- const result = DecryptCredentialParamsSchema.parse({ credentialName: maxName })
492
- expect(result.credentialName).toBe(maxName)
493
- })
494
- })
495
- })
496
-
1
+ /**
2
+ * Credential API schemas tests
3
+ * Tests all validation schemas for credentials endpoints
4
+ * Focus: Security (path traversal, DoS, mass assignment, type coercion)
5
+ */
6
+
7
+ import { describe, it, expect } from 'vitest'
8
+ import {
9
+ CredentialTypeSchema,
10
+ CreateCredentialRequestSchema,
11
+ UpdateCredentialParamsSchema,
12
+ UpdateCredentialRequestSchema,
13
+ DeleteCredentialParamsSchema,
14
+ ListCredentialsResponseSchema
15
+ } from '../api-schemas'
16
+
17
+ describe('CredentialTypeSchema', () => {
18
+ it('accepts valid credential types', () => {
19
+ // These are the actual types stored in the database
20
+ expect(CredentialTypeSchema.parse('oauth')).toBe('oauth')
21
+ expect(CredentialTypeSchema.parse('api-key')).toBe('api-key')
22
+ expect(CredentialTypeSchema.parse('webhook-secret')).toBe('webhook-secret')
23
+ })
24
+
25
+ it('rejects invalid credential types', () => {
26
+ expect(() => CredentialTypeSchema.parse('invalid-type')).toThrow()
27
+ expect(() => CredentialTypeSchema.parse('')).toThrow()
28
+ })
29
+
30
+ it('rejects provider names (these are CREDENTIAL_SCHEMAS keys, not stored types)', () => {
31
+ // OAuth providers store type='oauth', not their provider name
32
+ expect(() => CredentialTypeSchema.parse('notion')).toThrow()
33
+ expect(() => CredentialTypeSchema.parse('google-sheets')).toThrow()
34
+ })
35
+ })
36
+
37
+ describe('CreateCredentialRequestSchema', () => {
38
+ const validPayload = {
39
+ name: 'gmail-prod',
40
+ type: 'api-key' as const,
41
+ value: { apiKey: 'test-key-123' }
42
+ }
43
+
44
+ describe('valid requests', () => {
45
+ it('accepts valid credential creation request', () => {
46
+ const result = CreateCredentialRequestSchema.parse(validPayload)
47
+ expect(result).toEqual(validPayload)
48
+ })
49
+
50
+ it('accepts oauth type credentials', () => {
51
+ const payload = {
52
+ name: 'notion-dev',
53
+ type: 'oauth' as const,
54
+ value: { accessToken: 'token', refreshToken: 'refresh' }
55
+ }
56
+ const result = CreateCredentialRequestSchema.parse(payload)
57
+ expect(result).toEqual(payload)
58
+ })
59
+
60
+ it('accepts webhook-secret type credentials', () => {
61
+ const payload = {
62
+ name: 'stripe-webhook',
63
+ type: 'webhook-secret' as const,
64
+ value: { signingSecret: 'whsec_abc123' }
65
+ }
66
+ const result = CreateCredentialRequestSchema.parse(payload)
67
+ expect(result).toEqual(payload)
68
+ })
69
+ })
70
+
71
+ describe('SECURITY: mass assignment prevention', () => {
72
+ it('rejects unknown fields (strict mode)', () => {
73
+ const payload = {
74
+ ...validPayload,
75
+ organizationId: 'attacker-org-id', // Injected field
76
+ createdBy: null // Override creator
77
+ }
78
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
79
+ })
80
+
81
+ it('rejects extra top-level fields', () => {
82
+ const payload = {
83
+ ...validPayload,
84
+ maliciousField: 'value'
85
+ }
86
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
87
+ })
88
+ })
89
+
90
+ describe('SECURITY: credential name validation', () => {
91
+ it('rejects invalid credential names', () => {
92
+ const payload = { ...validPayload, name: 'gmail prod' }
93
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must be lowercase/)
94
+ })
95
+
96
+ it('rejects path traversal in name', () => {
97
+ const payload = { ...validPayload, name: '../admin-cred' }
98
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must be lowercase/)
99
+ })
100
+
101
+ it('rejects special characters in name', () => {
102
+ const payload = { ...validPayload, name: 'gmail@prod' }
103
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must be lowercase/)
104
+ })
105
+
106
+ it('rejects names without hyphens', () => {
107
+ const payload = { ...validPayload, name: 'gmailprod' }
108
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must be lowercase/)
109
+ })
110
+
111
+ it('rejects underscores', () => {
112
+ const payload = { ...validPayload, name: 'gmail_prod' }
113
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must be lowercase/)
114
+ })
115
+
116
+ it('auto-lowercases uppercase input', () => {
117
+ const payload = { ...validPayload, name: 'Gmail-Prod' }
118
+ const result = CreateCredentialRequestSchema.parse(payload)
119
+ expect(result.name).toBe('gmail-prod')
120
+ })
121
+ })
122
+
123
+ describe('SECURITY: credential type validation', () => {
124
+ it('rejects invalid credential types', () => {
125
+ const payload = { ...validPayload, type: 'invalid-type' }
126
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
127
+ })
128
+
129
+ it('rejects null type', () => {
130
+ const payload = { ...validPayload, type: null }
131
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
132
+ })
133
+
134
+ it('rejects array as type', () => {
135
+ const payload = { ...validPayload, type: ['api-key'] }
136
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
137
+ })
138
+ })
139
+
140
+ describe('SECURITY: credential value validation', () => {
141
+ it('rejects empty credential value', () => {
142
+ const payload = { ...validPayload, value: {} }
143
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/must not be empty/)
144
+ })
145
+
146
+ it('rejects non-object value', () => {
147
+ const payload = { ...validPayload, value: 'string-instead-of-object' }
148
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
149
+ })
150
+
151
+ it('rejects null value', () => {
152
+ const payload = { ...validPayload, value: null }
153
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
154
+ })
155
+
156
+ it('rejects array as value', () => {
157
+ const payload = { ...validPayload, value: ['array', 'as', 'value'] }
158
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
159
+ })
160
+ })
161
+
162
+ describe('SECURITY: DoS prevention - credential value size', () => {
163
+ it('rejects credential value with too many keys (over 50)', () => {
164
+ const largeValue = Object.fromEntries(Array.from({ length: 51 }, (_, i) => [`key${i}`, 'value']))
165
+ const payload = { ...validPayload, value: largeValue }
166
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/too many keys/)
167
+ })
168
+
169
+ it('accepts credential value with max keys (50)', () => {
170
+ const maxValue = Object.fromEntries(Array.from({ length: 50 }, (_, i) => [`key${i}`, 'value']))
171
+ const payload = { ...validPayload, value: maxValue }
172
+ const result = CreateCredentialRequestSchema.parse(payload)
173
+ expect(Object.keys(result.value).length).toBe(50)
174
+ })
175
+
176
+ it('rejects individual string values over 10KB', () => {
177
+ const hugeString = 'a'.repeat(10241)
178
+ const payload = { ...validPayload, value: { apiKey: hugeString } }
179
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow(/too large/)
180
+ })
181
+
182
+ it('accepts string values at max size (10KB)', () => {
183
+ const maxString = 'a'.repeat(10240)
184
+ const payload = { ...validPayload, value: { apiKey: maxString } }
185
+ const result = CreateCredentialRequestSchema.parse(payload)
186
+ expect(result.value.apiKey).toBe(maxString)
187
+ })
188
+
189
+ it('allows non-string values without size checks', () => {
190
+ const payload = {
191
+ ...validPayload,
192
+ value: {
193
+ apiKey: 'test',
194
+ number: 12345,
195
+ boolean: true,
196
+ nested: { key: 'value' }
197
+ }
198
+ }
199
+ const result = CreateCredentialRequestSchema.parse(payload)
200
+ expect(result.value).toEqual(payload.value)
201
+ })
202
+ })
203
+
204
+ describe('required fields', () => {
205
+ it('rejects missing name', () => {
206
+ const { name: _name, ...payload } = validPayload
207
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
208
+ })
209
+
210
+ it('rejects missing type', () => {
211
+ const { type: _type, ...payload } = validPayload
212
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
213
+ })
214
+
215
+ it('rejects missing value', () => {
216
+ const { value: _value, ...payload } = validPayload
217
+ expect(() => CreateCredentialRequestSchema.parse(payload)).toThrow()
218
+ })
219
+ })
220
+
221
+ describe('provider field (OAuth provider identification)', () => {
222
+ it('accepts OAuth credential with provider', () => {
223
+ const payload = {
224
+ name: 'my-dropbox',
225
+ type: 'oauth' as const,
226
+ value: { accessToken: 'token', refreshToken: 'refresh' },
227
+ provider: 'dropbox'
228
+ }
229
+ const result = CreateCredentialRequestSchema.parse(payload)
230
+ expect(result.provider).toBe('dropbox')
231
+ })
232
+
233
+ it('accepts request without provider (optional field)', () => {
234
+ const result = CreateCredentialRequestSchema.parse(validPayload)
235
+ expect(result.provider).toBeUndefined()
236
+ })
237
+
238
+ it('accepts various OAuth provider values', () => {
239
+ const providers = ['dropbox', 'notion', 'google-sheets']
240
+ for (const provider of providers) {
241
+ const payload = {
242
+ name: `my-${provider}`,
243
+ type: 'oauth' as const,
244
+ value: { accessToken: 'token' },
245
+ provider
246
+ }
247
+ const result = CreateCredentialRequestSchema.parse(payload)
248
+ expect(result.provider).toBe(provider)
249
+ }
250
+ })
251
+ })
252
+ })
253
+
254
+ describe('UpdateCredentialParamsSchema', () => {
255
+ const validUuid = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
256
+
257
+ it('accepts valid UUID', () => {
258
+ const result = UpdateCredentialParamsSchema.parse({ credentialId: validUuid })
259
+ expect(result.credentialId).toBe(validUuid)
260
+ })
261
+
262
+ it('rejects invalid UUID format', () => {
263
+ expect(() => UpdateCredentialParamsSchema.parse({ credentialId: 'not-a-uuid' })).toThrow()
264
+ })
265
+
266
+ it('rejects empty string', () => {
267
+ expect(() => UpdateCredentialParamsSchema.parse({ credentialId: '' })).toThrow()
268
+ })
269
+
270
+ it('rejects number instead of UUID', () => {
271
+ expect(() => UpdateCredentialParamsSchema.parse({ credentialId: 12345 })).toThrow()
272
+ })
273
+ })
274
+
275
+ describe('UpdateCredentialRequestSchema', () => {
276
+ const validValue = { apiKey: 'updated-key' }
277
+ const validName = 'updated-name'
278
+
279
+ describe('valid requests', () => {
280
+ it('accepts update with value only', () => {
281
+ const result = UpdateCredentialRequestSchema.parse({ value: validValue })
282
+ expect(result.value).toEqual(validValue)
283
+ expect(result.name).toBeUndefined()
284
+ })
285
+
286
+ it('accepts update with name only', () => {
287
+ const result = UpdateCredentialRequestSchema.parse({ name: validName })
288
+ expect(result.name).toBe(validName)
289
+ expect(result.value).toBeUndefined()
290
+ })
291
+
292
+ it('accepts update with both value and name', () => {
293
+ const result = UpdateCredentialRequestSchema.parse({ value: validValue, name: validName })
294
+ expect(result.value).toEqual(validValue)
295
+ expect(result.name).toBe(validName)
296
+ })
297
+ })
298
+
299
+ describe('SECURITY: strict mode', () => {
300
+ it('rejects unknown fields', () => {
301
+ const payload = { value: validValue, unknownField: 'test' }
302
+ expect(() => UpdateCredentialRequestSchema.parse(payload)).toThrow()
303
+ })
304
+ })
305
+
306
+ describe('validation: at least one field required', () => {
307
+ it('rejects empty update (no fields)', () => {
308
+ expect(() => UpdateCredentialRequestSchema.parse({})).toThrow(/At least one field/)
309
+ })
310
+
311
+ it('rejects update with undefined fields only', () => {
312
+ expect(() => UpdateCredentialRequestSchema.parse({ value: undefined, name: undefined })).toThrow(
313
+ /At least one field/
314
+ )
315
+ })
316
+ })
317
+
318
+ describe('SECURITY: credential value validation', () => {
319
+ it('rejects empty value object', () => {
320
+ expect(() => UpdateCredentialRequestSchema.parse({ value: {} })).toThrow(/must not be empty/)
321
+ })
322
+
323
+ it('rejects value with too many keys', () => {
324
+ const largeValue = Object.fromEntries(Array.from({ length: 51 }, (_, i) => [`key${i}`, 'value']))
325
+ expect(() => UpdateCredentialRequestSchema.parse({ value: largeValue })).toThrow(/too many keys/)
326
+ })
327
+
328
+ it('rejects individual string values over 10KB', () => {
329
+ const hugeString = 'a'.repeat(10241)
330
+ expect(() => UpdateCredentialRequestSchema.parse({ value: { apiKey: hugeString } })).toThrow(/too large/)
331
+ })
332
+ })
333
+
334
+ describe('SECURITY: credential name validation', () => {
335
+ it('rejects invalid name format', () => {
336
+ expect(() => UpdateCredentialRequestSchema.parse({ name: 'gmail prod' })).toThrow(/must be lowercase/)
337
+ })
338
+
339
+ it('rejects path traversal in name', () => {
340
+ expect(() => UpdateCredentialRequestSchema.parse({ name: '../admin' })).toThrow(/must be lowercase/)
341
+ })
342
+ })
343
+ })
344
+
345
+ describe('DeleteCredentialParamsSchema', () => {
346
+ const validUuid = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
347
+
348
+ it('accepts valid UUID', () => {
349
+ const result = DeleteCredentialParamsSchema.parse({ credentialId: validUuid })
350
+ expect(result.credentialId).toBe(validUuid)
351
+ })
352
+
353
+ it('rejects invalid UUID format', () => {
354
+ expect(() => DeleteCredentialParamsSchema.parse({ credentialId: 'not-a-uuid' })).toThrow()
355
+ })
356
+
357
+ it('rejects empty string', () => {
358
+ expect(() => DeleteCredentialParamsSchema.parse({ credentialId: '' })).toThrow()
359
+ })
360
+ })
361
+
362
+ describe('ListCredentialsResponseSchema - Provider Field', () => {
363
+ const validUuid = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
364
+
365
+ it('validates response with provider set', () => {
366
+ const response = {
367
+ credentials: [
368
+ {
369
+ id: validUuid,
370
+ name: 'dropbox-cred',
371
+ type: 'oauth',
372
+ provider: 'dropbox',
373
+ createdAt: '2026-02-02T00:00:00.000Z'
374
+ }
375
+ ]
376
+ }
377
+ const result = ListCredentialsResponseSchema.parse(response)
378
+ expect(result.credentials[0].provider).toBe('dropbox')
379
+ })
380
+
381
+ it('validates response with null provider (non-OAuth credentials)', () => {
382
+ const response = {
383
+ credentials: [
384
+ {
385
+ id: validUuid,
386
+ name: 'api-key',
387
+ type: 'api-key',
388
+ provider: null,
389
+ createdAt: '2026-02-02T00:00:00.000Z'
390
+ }
391
+ ]
392
+ }
393
+ const result = ListCredentialsResponseSchema.parse(response)
394
+ expect(result.credentials[0].provider).toBeNull()
395
+ })
396
+
397
+ it('validates response with mixed provider values', () => {
398
+ const response = {
399
+ credentials: [
400
+ {
401
+ id: validUuid,
402
+ name: 'dropbox-cred',
403
+ type: 'oauth',
404
+ provider: 'dropbox',
405
+ createdAt: '2026-02-02T00:00:00.000Z'
406
+ },
407
+ {
408
+ id: 'b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a22',
409
+ name: 'api-key',
410
+ type: 'api-key',
411
+ provider: null,
412
+ createdAt: '2026-02-02T00:00:00.000Z'
413
+ }
414
+ ]
415
+ }
416
+ const result = ListCredentialsResponseSchema.parse(response)
417
+ expect(result.credentials[0].provider).toBe('dropbox')
418
+ expect(result.credentials[1].provider).toBeNull()
419
+ })
420
+ })