Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | /** * Security Context Entity - Domain Layer * * Represents security context for operations with validation and policy enforcement. * * @module v3/security/domain/entities */ import { randomUUID } from 'crypto'; /** * Permission levels */ export type PermissionLevel = 'read' | 'write' | 'execute' | 'admin'; /** * Security context properties */ export interface SecurityContextProps { id?: string; principalId: string; principalType: 'agent' | 'user' | 'system'; permissions: PermissionLevel[]; allowedPaths?: string[]; blockedPaths?: string[]; allowedCommands?: string[]; blockedCommands?: string[]; metadata?: Record<string, unknown>; expiresAt?: Date; createdAt?: Date; } /** * Security Context - Entity */ export class SecurityContext { private _id: string; private _principalId: string; private _principalType: 'agent' | 'user' | 'system'; private _permissions: Set<PermissionLevel>; private _allowedPaths: Set<string>; private _blockedPaths: Set<string>; private _allowedCommands: Set<string>; private _blockedCommands: Set<string>; private _metadata: Record<string, unknown>; private _expiresAt?: Date; private _createdAt: Date; private constructor(props: SecurityContextProps) { this._id = props.id ?? randomUUID(); this._principalId = props.principalId; this._principalType = props.principalType; this._permissions = new Set(props.permissions); this._allowedPaths = new Set(props.allowedPaths ?? []); this._blockedPaths = new Set(props.blockedPaths ?? []); this._allowedCommands = new Set(props.allowedCommands ?? []); this._blockedCommands = new Set(props.blockedCommands ?? []); this._metadata = props.metadata ?? {}; this._expiresAt = props.expiresAt; this._createdAt = props.createdAt ?? new Date(); } static create(props: SecurityContextProps): SecurityContext { return new SecurityContext(props); } static fromPersistence(props: SecurityContextProps): SecurityContext { return new SecurityContext(props); } get id(): string { return this._id; } get principalId(): string { return this._principalId; } get principalType(): string { return this._principalType; } get permissions(): PermissionLevel[] { return Array.from(this._permissions); } get allowedPaths(): string[] { return Array.from(this._allowedPaths); } get blockedPaths(): string[] { return Array.from(this._blockedPaths); } get allowedCommands(): string[] { return Array.from(this._allowedCommands); } get blockedCommands(): string[] { return Array.from(this._blockedCommands); } get metadata(): Record<string, unknown> { return { ...this._metadata }; } get expiresAt(): Date | undefined { return this._expiresAt; } get createdAt(): Date { return new Date(this._createdAt); } // Business Logic hasPermission(level: PermissionLevel): boolean { return this._permissions.has(level) || this._permissions.has('admin'); } isExpired(): boolean { if (!this._expiresAt) return false; return Date.now() > this._expiresAt.getTime(); } canAccessPath(path: string): boolean { if (this.isExpired()) return false; // Check blocked paths first for (const blocked of this._blockedPaths) { if (path.startsWith(blocked) || this.matchGlob(path, blocked)) { return false; } } // If no allowed paths specified, allow all non-blocked if (this._allowedPaths.size === 0) return true; // Check allowed paths for (const allowed of this._allowedPaths) { if (path.startsWith(allowed) || this.matchGlob(path, allowed)) { return true; } } return false; } canExecuteCommand(command: string): boolean { if (this.isExpired()) return false; const cmdBase = command.split(' ')[0]; // Check blocked commands first if (this._blockedCommands.has(cmdBase) || this._blockedCommands.has(command)) { return false; } // If no allowed commands specified, allow all non-blocked if (this._allowedCommands.size === 0) return true; // Check allowed commands return this._allowedCommands.has(cmdBase) || this._allowedCommands.has(command); } private matchGlob(path: string, pattern: string): boolean { const regex = pattern .replace(/\*\*/g, '.*') .replace(/\*/g, '[^/]*') .replace(/\?/g, '.'); return new RegExp(`^${regex}$`).test(path); } grantPermission(level: PermissionLevel): void { this._permissions.add(level); } revokePermission(level: PermissionLevel): void { this._permissions.delete(level); } addAllowedPath(path: string): void { this._allowedPaths.add(path); } addBlockedPath(path: string): void { this._blockedPaths.add(path); } toPersistence(): Record<string, unknown> { return { id: this._id, principalId: this._principalId, principalType: this._principalType, permissions: Array.from(this._permissions), allowedPaths: Array.from(this._allowedPaths), blockedPaths: Array.from(this._blockedPaths), allowedCommands: Array.from(this._allowedCommands), blockedCommands: Array.from(this._blockedCommands), metadata: this._metadata, expiresAt: this._expiresAt?.toISOString(), createdAt: this._createdAt.toISOString(), }; } } |