-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsecurity.ts
More file actions
1094 lines (943 loc) · 31.4 KB
/
security.ts
File metadata and controls
1094 lines (943 loc) · 31.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* SwarmOrchestrator Security Module
*
* This module addresses security vulnerabilities in the multi-agent system:
*
* 1. Token Security - HMAC-signed tokens with expiration
* 2. Input Sanitization - Prevent injection attacks
* 3. Rate Limiting - Prevent DoS from rogue agents
* 4. Audit Integrity - Cryptographically signed audit logs
* 5. Data Encryption - Encrypt sensitive blackboard entries
* 6. Permission Hardening - Prevent privilege escalation
* 7. Path Traversal Protection - Sanitize file paths
*
* @module SwarmSecurity
* @version 1.0.0
*/
import { createHmac, createCipheriv, createDecipheriv, randomBytes, scryptSync } from 'crypto';
import { readFileSync, appendFileSync } from 'fs';
import { join, normalize, isAbsolute } from 'path';
// ============================================================================
// SECURITY CONFIGURATION
// ============================================================================
interface SecurityConfig {
// Token settings
tokenSecret: string;
tokenAlgorithm: 'sha256' | 'sha512';
maxTokenAge: number; // milliseconds
// Rate limiting
maxRequestsPerMinute: number;
maxFailedAuthAttempts: number;
lockoutDuration: number; // milliseconds
// Encryption
encryptionKey: string;
encryptSensitiveData: boolean;
// Audit
signAuditLogs: boolean;
auditLogPath: string;
// Paths
allowedBasePath: string;
}
const DEFAULT_CONFIG: SecurityConfig = {
tokenSecret: process.env.SWARM_TOKEN_SECRET || randomBytes(32).toString('hex'),
tokenAlgorithm: 'sha256',
maxTokenAge: 300000, // 5 minutes
maxRequestsPerMinute: 100,
maxFailedAuthAttempts: 5,
lockoutDuration: 900000, // 15 minutes
encryptionKey: process.env.SWARM_ENCRYPTION_KEY || randomBytes(32).toString('hex'),
encryptSensitiveData: true,
signAuditLogs: true,
auditLogPath: './security-audit.log',
allowedBasePath: process.cwd(),
};
// ============================================================================
// 1. SECURE TOKEN MANAGER
// ============================================================================
interface SecureToken {
tokenId: string;
agentId: string;
resourceType: string;
scope: string;
issuedAt: number;
expiresAt: number;
signature: string;
}
/**
* Cryptographically signed token manager using HMAC.
*
* Generates, validates, and revokes tokens with configurable expiration.
* Uses constant-time comparison to prevent timing attacks.
*
* @example
* ```typescript
* const mgr = new SecureTokenManager({ maxTokenAge: 60000 });
* const token = mgr.generateToken('agent-1', 'DATABASE', 'read');
* const { valid } = mgr.validateToken(token);
* ```
*/
export class SecureTokenManager {
private config: SecurityConfig;
private revokedTokens: Set<string> = new Set();
constructor(config: Partial<SecurityConfig> = {}) {
this.config = { ...DEFAULT_CONFIG, ...config };
}
/**
* Generate a cryptographically signed token
*/
generateToken(agentId: string, resourceType: string, scope: string): SecureToken {
const tokenId = randomBytes(16).toString('hex');
const issuedAt = Date.now();
const expiresAt = issuedAt + this.config.maxTokenAge;
// Create token payload
const payload = `${tokenId}:${agentId}:${resourceType}:${scope}:${issuedAt}:${expiresAt}`;
// Sign the payload
const signature = this.sign(payload);
return {
tokenId,
agentId,
resourceType,
scope,
issuedAt,
expiresAt,
signature,
};
}
/**
* Validate a token's authenticity and expiration
*/
validateToken(token: SecureToken): { valid: boolean; reason?: string } {
// Check if revoked
if (this.revokedTokens.has(token.tokenId)) {
return { valid: false, reason: 'Token has been revoked' };
}
// Check expiration
if (Date.now() > token.expiresAt) {
return { valid: false, reason: 'Token has expired' };
}
// Verify signature
const payload = `${token.tokenId}:${token.agentId}:${token.resourceType}:${token.scope}:${token.issuedAt}:${token.expiresAt}`;
const expectedSignature = this.sign(payload);
if (!this.constantTimeCompare(token.signature, expectedSignature)) {
return { valid: false, reason: 'Invalid token signature' };
}
return { valid: true };
}
/**
* Revoke a token
*/
revokeToken(tokenId: string): void {
this.revokedTokens.add(tokenId);
}
/**
* HMAC sign a payload
*/
private sign(payload: string): string {
return createHmac(this.config.tokenAlgorithm, this.config.tokenSecret)
.update(payload)
.digest('hex');
}
/**
* Constant-time string comparison to prevent timing attacks
*/
private constantTimeCompare(a: string, b: string): boolean {
if (a.length !== b.length) return false;
let result = 0;
for (let i = 0; i < a.length; i++) {
result |= a.charCodeAt(i) ^ b.charCodeAt(i);
}
return result === 0;
}
}
// ============================================================================
// 2. INPUT SANITIZER
// ============================================================================
/**
* Static utility for sanitizing user-supplied strings, objects, agent IDs,
* and file paths. Strips XSS payloads, template injection, command injection
* characters, and prototype pollution attempts.
*
* All methods are static — no instantiation required.
*
* @example
* ```typescript
* const safe = InputSanitizer.sanitizeString(userInput, 2000);
* const safeObj = InputSanitizer.sanitizeObject(payload);
* const safeId = InputSanitizer.sanitizeAgentId(rawId);
* ```
*/
export class InputSanitizer {
// Dangerous patterns that could indicate injection attempts
private static DANGEROUS_PATTERNS = [
/\$\{.*\}/g, // Template injection
/<script[\s\S]*?>[\s\S]*?<\/script>/gi, // XSS
/javascript:/gi, // JavaScript protocol
/on\w+\s*=/gi, // Event handlers
/\.\.\//g, // Path traversal
/[;&|`$]/g, // Command injection chars
/__proto__/gi, // Prototype pollution
/constructor/gi, // Prototype pollution
];
/**
* Sanitize a string input
*/
static sanitizeString(input: string, maxLength: number = 10000): string {
if (typeof input !== 'string') {
throw new SecurityError('Input must be a string', 'INVALID_INPUT_TYPE');
}
// Truncate to max length
let sanitized = input.slice(0, maxLength);
// Remove dangerous patterns
for (const pattern of this.DANGEROUS_PATTERNS) {
sanitized = sanitized.replace(pattern, '');
}
// Encode special characters
sanitized = sanitized
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
return sanitized;
}
/**
* Sanitize an object recursively
*/
static sanitizeObject(obj: unknown, depth: number = 0, maxDepth: number = 10): unknown {
if (depth > maxDepth) {
throw new SecurityError('Object nesting too deep', 'MAX_DEPTH_EXCEEDED');
}
if (obj === null || obj === undefined) {
return obj;
}
if (typeof obj === 'string') {
return this.sanitizeString(obj);
}
if (typeof obj === 'number' || typeof obj === 'boolean') {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(item => this.sanitizeObject(item, depth + 1, maxDepth));
}
if (typeof obj === 'object') {
const sanitized: Record<string, unknown> = {};
for (const [key, value] of Object.entries(obj)) {
// Sanitize keys too
const sanitizedKey = this.sanitizeString(key, 100);
// Block prototype pollution attempts
if (sanitizedKey === '__proto__' || sanitizedKey === 'constructor' || sanitizedKey === 'prototype') {
continue;
}
sanitized[sanitizedKey] = this.sanitizeObject(value, depth + 1, maxDepth);
}
return sanitized;
}
return undefined; // Unknown types are dropped
}
/**
* Validate and sanitize an agent ID
*/
static sanitizeAgentId(agentId: string): string {
if (typeof agentId !== 'string' || agentId.length === 0) {
throw new SecurityError('Invalid agent ID', 'INVALID_AGENT_ID');
}
// Agent IDs should be alphanumeric with underscores/hyphens only
const sanitized = agentId.replace(/[^a-zA-Z0-9_-]/g, '');
if (sanitized.length === 0 || sanitized.length > 64) {
throw new SecurityError('Agent ID format invalid', 'INVALID_AGENT_ID_FORMAT');
}
return sanitized;
}
/**
* Validate and sanitize a file path
*/
static sanitizePath(inputPath: string, basePath: string): string {
// Normalize the path
const normalized = normalize(inputPath);
// Resolve to absolute
const absolute = isAbsolute(normalized)
? normalized
: join(basePath, normalized);
// Ensure it's within the allowed base path
const resolvedBase = normalize(basePath);
const resolvedPath = normalize(absolute);
if (!resolvedPath.startsWith(resolvedBase)) {
throw new SecurityError('Path traversal attempt detected', 'PATH_TRAVERSAL');
}
return resolvedPath;
}
}
// ============================================================================
// 3. RATE LIMITER
// ============================================================================
interface RateLimitEntry {
count: number;
windowStart: number;
failedAttempts: number;
lockedUntil: number | null;
}
/**
* Per-agent rate limiter with sliding window and lockout on repeated
* authentication failures. Prevents DoS from rogue agents.
*
* @example
* ```typescript
* const limiter = new RateLimiter({ maxRequestsPerMinute: 50 });
* const { limited } = limiter.isRateLimited('agent-1');
* if (limited) { /* back off *\/ }
* ```
*/
export class RateLimiter {
private limits: Map<string, RateLimitEntry> = new Map();
private config: SecurityConfig;
constructor(config: Partial<SecurityConfig> = {}) {
this.config = { ...DEFAULT_CONFIG, ...config };
}
/**
* Check if an agent is rate limited
*/
isRateLimited(agentId: string): { limited: boolean; retryAfter?: number } {
const entry = this.limits.get(agentId);
const now = Date.now();
if (!entry) {
this.limits.set(agentId, {
count: 1,
windowStart: now,
failedAttempts: 0,
lockedUntil: null,
});
return { limited: false };
}
// Check if locked out
if (entry.lockedUntil && now < entry.lockedUntil) {
return {
limited: true,
retryAfter: Math.ceil((entry.lockedUntil - now) / 1000)
};
}
// Reset window if expired (1 minute)
if (now - entry.windowStart > 60000) {
entry.count = 1;
entry.windowStart = now;
entry.lockedUntil = null;
return { limited: false };
}
// Increment counter
entry.count++;
// Check if over limit
if (entry.count > this.config.maxRequestsPerMinute) {
return {
limited: true,
retryAfter: Math.ceil((entry.windowStart + 60000 - now) / 1000)
};
}
return { limited: false };
}
/**
* Record a failed authentication attempt
*/
recordFailedAuth(agentId: string): { locked: boolean; attemptsRemaining?: number } {
const entry = this.limits.get(agentId) || {
count: 0,
windowStart: Date.now(),
failedAttempts: 0,
lockedUntil: null,
};
entry.failedAttempts++;
if (entry.failedAttempts >= this.config.maxFailedAuthAttempts) {
entry.lockedUntil = Date.now() + this.config.lockoutDuration;
this.limits.set(agentId, entry);
return { locked: true };
}
this.limits.set(agentId, entry);
return {
locked: false,
attemptsRemaining: this.config.maxFailedAuthAttempts - entry.failedAttempts
};
}
/**
* Reset failed attempts after successful auth
*/
resetFailedAttempts(agentId: string): void {
const entry = this.limits.get(agentId);
if (entry) {
entry.failedAttempts = 0;
entry.lockedUntil = null;
}
}
/**
* Get rate limit status for an agent
*/
getStatus(agentId: string): RateLimitEntry | null {
return this.limits.get(agentId) || null;
}
}
// ============================================================================
// 4. SECURE AUDIT LOGGER
// ============================================================================
interface AuditEntry {
timestamp: string;
eventId: string;
eventType: string;
agentId: string;
action: string;
resource?: string;
outcome: 'success' | 'failure' | 'denied';
details: Record<string, unknown>;
signature?: string;
}
/**
* Append-only audit logger with HMAC-chained integrity verification.
*
* Each entry is signed with a hash that includes the previous entry's
* signature, forming a tamper-evident chain. Supports verification
* across process restarts.
*
* @example
* ```typescript
* const logger = new SecureAuditLogger();
* logger.log('ACCESS', 'agent-1', 'read_file', 'success', { path: '/data' });
* const { valid } = logger.verifyLogIntegrity();
* ```
*/
export class SecureAuditLogger {
private config: SecurityConfig;
private previousHash: string = '';
constructor(config: Partial<SecurityConfig> = {}) {
this.config = { ...DEFAULT_CONFIG, ...config };
this.initializeLog();
}
private initializeLog(): void {
const logPath = this.config.auditLogPath;
// appendFileSync creates the file if it doesn't exist — atomic, no TOCTOU
appendFileSync(logPath, '');
// Continue the hash chain from the last entry so integrity
// verification works across process restarts.
try {
const content = readFileSync(logPath, 'utf-8').trim();
if (content) {
const lines = content.split('\n').filter((l: string) => l);
const lastLine = lines[lines.length - 1];
const lastEntry = JSON.parse(lastLine) as AuditEntry;
if (lastEntry.signature) {
this.previousHash = lastEntry.signature;
}
}
} catch {
// If we can't read the last entry, start fresh chain
}
}
/**
* Log a security event with cryptographic integrity
*/
log(
eventType: string,
agentId: string,
action: string,
outcome: 'success' | 'failure' | 'denied',
details: Record<string, unknown> = {},
resource?: string
): AuditEntry {
const entry: AuditEntry = {
timestamp: new Date().toISOString(),
eventId: randomBytes(8).toString('hex'),
eventType,
agentId: InputSanitizer.sanitizeAgentId(agentId),
action,
resource,
outcome,
details: InputSanitizer.sanitizeObject(details) as Record<string, unknown>,
};
// Sign the entry if configured
if (this.config.signAuditLogs) {
const payload = JSON.stringify({
...entry,
previousHash: this.previousHash,
});
entry.signature = createHmac('sha256', this.config.tokenSecret)
.update(payload)
.digest('hex');
this.previousHash = entry.signature ?? '';
}
// Append to log file
const logLine = JSON.stringify(entry) + '\n';
appendFileSync(this.config.auditLogPath, logLine);
return entry;
}
/**
* Log a permission request
*/
logPermissionRequest(
agentId: string,
resourceType: string,
scope: string,
granted: boolean,
reason?: string
): void {
this.log(
'PERMISSION_REQUEST',
agentId,
`request_${resourceType}`,
granted ? 'success' : 'denied',
{ resourceType, scope, reason },
resourceType
);
}
/**
* Log a security violation
*/
logViolation(
agentId: string,
violationType: string,
details: Record<string, unknown>
): void {
this.log(
'SECURITY_VIOLATION',
agentId,
violationType,
'denied',
details
);
}
/**
* Verify audit log integrity
*/
verifyLogIntegrity(): { valid: boolean; invalidEntries: number[] } {
const logContent = readFileSync(this.config.auditLogPath, 'utf-8');
const lines = logContent.trim().split('\n').filter((l: string) => l);
let previousHash = '';
const invalidEntries: number[] = [];
for (let i = 0; i < lines.length; i++) {
try {
const entry = JSON.parse(lines[i]) as AuditEntry;
if (entry.signature) {
const { signature, ...rest } = entry;
const payload = JSON.stringify({
...rest,
previousHash,
});
const expectedSignature = createHmac('sha256', this.config.tokenSecret)
.update(payload)
.digest('hex');
if (signature !== expectedSignature) {
invalidEntries.push(i);
}
previousHash = signature;
}
} catch {
invalidEntries.push(i);
}
}
return {
valid: invalidEntries.length === 0,
invalidEntries,
};
}
}
// ============================================================================
// 5. DATA ENCRYPTION
// ============================================================================
/**
* AES-256-GCM encryptor for sensitive blackboard entries.
*
* Uses `scryptSync` key derivation with a unique salt per instance.
* The salt is required for decryption and can be retrieved via {@link getSalt}.
*
* @example
* ```typescript
* const enc = new DataEncryptor('my-secret-key');
* const cipher = enc.encrypt('sensitive data');
* const plain = enc.decrypt(cipher);
* ```
*/
export class DataEncryptor {
private key: Buffer;
private algorithm = 'aes-256-gcm' as const;
private salt: Buffer;
constructor(encryptionKey: string, salt?: string | Buffer) {
// Use provided salt or generate a random one
this.salt = salt
? (typeof salt === 'string' ? Buffer.from(salt, 'hex') : salt)
: randomBytes(16);
// Derive a proper key from the provided key with unique salt
this.key = scryptSync(encryptionKey, this.salt, 32);
}
/**
* Get the salt (needed to recreate the same encryptor for decryption)
*/
getSalt(): string {
return this.salt.toString('hex');
}
/**
* Encrypt sensitive data
*/
encrypt(data: string): string {
const iv = randomBytes(16);
const cipher = createCipheriv(this.algorithm, this.key, iv);
let encrypted = cipher.update(data, 'utf8', 'hex');
encrypted += cipher.final('hex');
const authTag = cipher.getAuthTag();
// Return iv:authTag:encryptedData
return `${iv.toString('hex')}:${authTag.toString('hex')}:${encrypted}`;
}
/**
* Decrypt sensitive data
*/
decrypt(encryptedData: string): string {
const parts = encryptedData.split(':');
if (parts.length !== 3) {
throw new SecurityError('Invalid encrypted data format', 'INVALID_ENCRYPTED_FORMAT');
}
const [ivHex, authTagHex, encrypted] = parts;
const iv = Buffer.from(ivHex, 'hex');
const authTag = Buffer.from(authTagHex, 'hex');
const decipher = createDecipheriv(this.algorithm, this.key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
/**
* Encrypt an object
*/
encryptObject(obj: unknown): string {
return this.encrypt(JSON.stringify(obj));
}
/**
* Decrypt to object
*/
decryptObject<T = unknown>(encryptedData: string): T {
return JSON.parse(this.decrypt(encryptedData));
}
}
// ============================================================================
// 6. PERMISSION HARDENING
// ============================================================================
interface TrustPolicy {
agentId: string;
trustLevel: number;
allowedResources: string[];
maxScope: string[];
createdBy: string;
immutable: boolean;
}
/**
* Trust-policy-based permission hardener with privilege escalation prevention.
*
* Manages per-agent trust policies that control which resources and scopes
* an agent can access. Prevents agents from granting trust levels higher
* than their own.
*
* @example
* ```typescript
* const hardener = new PermissionHardener(auditLogger);
* hardener.registerPolicy({ agentId: 'bot', trustLevel: 0.6, allowedResources: ['DATABASE'] });
* const { allowed } = hardener.canAccess('bot', 'DATABASE', 'read');
* ```
*/
export class PermissionHardener {
private trustPolicies: Map<string, TrustPolicy> = new Map();
private auditLogger: SecureAuditLogger;
constructor(auditLogger: SecureAuditLogger, defaultPolicies?: Array<{
agentId: string;
trustLevel: number;
allowedResources: string[];
maxScope?: string[];
immutable?: boolean;
}>) {
this.auditLogger = auditLogger;
this.initializeDefaultPolicies(defaultPolicies);
}
private initializeDefaultPolicies(customPolicies?: Array<{
agentId: string;
trustLevel: number;
allowedResources: string[];
maxScope?: string[];
immutable?: boolean;
}>): void {
if (customPolicies && customPolicies.length > 0) {
for (const policy of customPolicies) {
this.trustPolicies.set(policy.agentId, {
agentId: policy.agentId,
trustLevel: policy.trustLevel,
allowedResources: policy.allowedResources,
maxScope: policy.maxScope ?? ['read'],
createdBy: 'SYSTEM',
immutable: policy.immutable ?? false,
});
}
return;
}
// Fallback: universal defaults that cover common domains
this.trustPolicies.set('orchestrator', {
agentId: 'orchestrator',
trustLevel: 0.9,
allowedResources: ['*'],
maxScope: ['read', 'write', 'execute', 'delegate'],
createdBy: 'SYSTEM',
immutable: true,
});
}
/**
* Register or update a trust policy for an agent at runtime.
*/
registerPolicy(policy: {
agentId: string;
trustLevel: number;
allowedResources: string[];
maxScope?: string[];
immutable?: boolean;
}): void {
const existing = this.trustPolicies.get(policy.agentId);
if (existing?.immutable) return; // Cannot overwrite immutable policies
this.trustPolicies.set(policy.agentId, {
agentId: policy.agentId,
trustLevel: policy.trustLevel,
allowedResources: policy.allowedResources,
maxScope: policy.maxScope ?? ['read'],
createdBy: 'RUNTIME',
immutable: policy.immutable ?? false,
});
}
/**
* Check if an agent can access a resource
*/
canAccess(agentId: string, resourceType: string, requestedScope: string): {
allowed: boolean;
reason?: string;
} {
const policy = this.trustPolicies.get(agentId);
if (!policy) {
this.auditLogger.logViolation(agentId, 'UNKNOWN_AGENT', { resourceType, requestedScope });
return { allowed: false, reason: 'Agent has no trust policy' };
}
// Check resource access (support '*' wildcard)
if (!policy.allowedResources.includes('*') && !policy.allowedResources.includes(resourceType)) {
this.auditLogger.logViolation(agentId, 'RESOURCE_NOT_ALLOWED', {
resourceType,
allowedResources: policy.allowedResources
});
return { allowed: false, reason: `Agent not allowed to access ${resourceType}` };
}
// Check scope
const scopeMatch = policy.maxScope.some(s => requestedScope.startsWith(s));
if (!scopeMatch) {
this.auditLogger.logViolation(agentId, 'SCOPE_EXCEEDED', {
requestedScope,
maxScope: policy.maxScope,
});
return { allowed: false, reason: 'Requested scope exceeds allowed scope' };
}
return { allowed: true };
}
/**
* Attempt to modify trust level (with escalation prevention)
*/
modifyTrustLevel(
requestingAgent: string,
targetAgent: string,
newTrustLevel: number
): { success: boolean; reason?: string } {
const requestorPolicy = this.trustPolicies.get(requestingAgent);
const targetPolicy = this.trustPolicies.get(targetAgent);
// Only orchestrator can modify trust
if (requestingAgent !== 'orchestrator') {
this.auditLogger.logViolation(requestingAgent, 'UNAUTHORIZED_TRUST_MODIFICATION', {
targetAgent,
attemptedTrustLevel: newTrustLevel,
});
return { success: false, reason: 'Only orchestrator can modify trust levels' };
}
// Cannot modify immutable policies
if (targetPolicy?.immutable) {
return { success: false, reason: 'Cannot modify immutable policy' };
}
// Cannot set trust higher than your own
if (requestorPolicy && newTrustLevel > requestorPolicy.trustLevel) {
this.auditLogger.logViolation(requestingAgent, 'PRIVILEGE_ESCALATION_ATTEMPT', {
targetAgent,
attemptedTrustLevel: newTrustLevel,
requestorTrustLevel: requestorPolicy.trustLevel,
});
return { success: false, reason: 'Cannot grant trust level higher than your own' };
}
// Apply the modification
if (targetPolicy) {
targetPolicy.trustLevel = newTrustLevel;
} else {
this.trustPolicies.set(targetAgent, {
agentId: targetAgent,
trustLevel: newTrustLevel,
allowedResources: [],
maxScope: ['read'],
createdBy: requestingAgent,
immutable: false,
});
}
return { success: true };
}
/**
* Get policy for an agent
*/
getPolicy(agentId: string): TrustPolicy | undefined {
return this.trustPolicies.get(agentId);
}
}
// ============================================================================
// 7. SECURITY ERROR CLASS
// ============================================================================
/**
* Custom error class for security-related failures.
*
* Includes a machine-readable `code` field for programmatic handling.
*/
export class SecurityError extends Error {
code: string;
constructor(message: string, code: string) {
super(message);
this.name = 'SecurityError';
this.code = code;
}
}
// ============================================================================
// 8. SECURE SWARM GATEWAY (Integration Point)
// ============================================================================
/**
* Unified security gateway that integrates all security modules:
* token management, rate limiting, input sanitization, audit logging,
* permission hardening, and data encryption.
*
* The SwarmOrchestrator routes every request through this gateway
* before processing.
*
* @example
* ```typescript
* const gw = new SecureSwarmGateway();
* const { allowed, sanitizedParams } = await gw.handleSecureRequest(
* 'agent-1', 'delegate_task', { targetAgent: 'bot' }
* );
* ```
*/
export class SecureSwarmGateway {
private tokenManager: SecureTokenManager;
private rateLimiter: RateLimiter;
private auditLogger: SecureAuditLogger;
private permissionHardener: PermissionHardener;
private encryptor: DataEncryptor;
constructor(config: Partial<SecurityConfig> = {}) {
const fullConfig = { ...DEFAULT_CONFIG, ...config };
this.tokenManager = new SecureTokenManager(fullConfig);
this.rateLimiter = new RateLimiter(fullConfig);
this.auditLogger = new SecureAuditLogger(fullConfig);
this.permissionHardener = new PermissionHardener(this.auditLogger);
this.encryptor = new DataEncryptor(fullConfig.encryptionKey);
}
/**
* Secure request handler - validates all security requirements
*/
async handleSecureRequest(
agentId: string,
action: string,
params: Record<string, unknown>,
token?: SecureToken
): Promise<{ allowed: boolean; reason?: string; sanitizedParams?: Record<string, unknown> }> {
// 1. Sanitize agent ID
let sanitizedAgentId: string;
try {
sanitizedAgentId = InputSanitizer.sanitizeAgentId(agentId);
} catch (error) {
this.auditLogger.logViolation(agentId, 'INVALID_AGENT_ID', { error: String(error) });
return { allowed: false, reason: 'Invalid agent ID' };
}
// 2. Check rate limit
const rateLimit = this.rateLimiter.isRateLimited(sanitizedAgentId);
if (rateLimit.limited) {
this.auditLogger.log('RATE_LIMITED', sanitizedAgentId, action, 'denied', {
retryAfter: rateLimit.retryAfter,
});
return { allowed: false, reason: `Rate limited. Retry after ${rateLimit.retryAfter}s` };
}
// 3. Validate token if provided
if (token) {
const tokenValidation = this.tokenManager.validateToken(token);
if (!tokenValidation.valid) {
const failedAuth = this.rateLimiter.recordFailedAuth(sanitizedAgentId);
this.auditLogger.log('TOKEN_VALIDATION_FAILED', sanitizedAgentId, action, 'denied', {