/** * StatePersistenceGuard (L22) * * Detects and prevents unauthorized state persistence and corruption. * Implements ASI08 from OWASP Agentic Applications 2026. * * Threat Model: * - ASI08: State Corruption * - Unauthorized state persistence * - Cross-session state leakage * - Malicious state injection * - State tampering and replay attacks * * Protection Capabilities: * - State integrity verification * - Persistence authorization * - Cross-session isolation * - State encryption validation * - Tampering detection */ export interface StatePersistenceGuardConfig { /** Enable state integrity checking */ enableIntegrityCheck?: boolean; /** Enable encryption validation */ requireEncryption?: boolean; /** Maximum state size in bytes */ maxStateSize?: number; /** Maximum state age in milliseconds */ maxStateAge?: number; /** Enable cross-session isolation */ enforceSessionIsolation?: boolean; /** Allowed persistence targets */ allowedTargets?: string[]; /** Sensitive state keys that require extra protection */ sensitiveKeys?: string[]; /** Enable state tampering detection */ detectTampering?: boolean; /** State signing secret (for integrity) */ signingSecret?: string; } export interface StateItem { /** Unique state identifier */ state_id: string; /** Session that owns this state */ session_id: string; /** State key/name */ key: string; /** State value */ value: any; /** Creation timestamp */ created_at: number; /** Last modified timestamp */ modified_at: number; /** State version */ version: number; /** Integrity hash */ integrity_hash?: string; /** Is state encrypted */ encrypted?: boolean; /** Persistence target */ target?: string; /** State metadata */ metadata?: Record; } export interface StateOperation { /** Operation type */ operation: "read" | "write" | "delete" | "restore" | "migrate"; /** State key */ key: string; /** State value (for write operations) */ value?: any; /** Session requesting the operation */ session_id: string; /** Target session (for cross-session operations) */ target_session_id?: string; /** Persistence target */ target?: string; /** Provided integrity hash */ integrity_hash?: string; /** State version (for optimistic locking) */ expected_version?: number; /** Additional metadata */ metadata?: Record; } export interface StatePersistenceResult { allowed: boolean; reason: string; violations: string[]; request_id: string; analysis: { operation: string; state_key: string; integrity_valid: boolean; encryption_valid: boolean; session_authorized: boolean; size_valid: boolean; age_valid: boolean; tampering_detected: boolean; }; state_item?: StateItem; recommendations: string[]; } export declare class StatePersistenceGuard { private config; private stateStore; private sessionStates; private readonly INJECTION_PATTERNS; private readonly DEFAULT_SENSITIVE_KEYS; constructor(config?: StatePersistenceGuardConfig); /** * Validate a state operation */ validateOperation(operation: StateOperation, requestId?: string): StatePersistenceResult; /** * Store state with integrity protection */ storeState(sessionId: string, key: string, value: any, options?: { target?: string; encrypted?: boolean; metadata?: Record; }): StatePersistenceResult; /** * Retrieve state with integrity verification */ retrieveState(sessionId: string, key: string, options?: { integrity_hash?: string; }): StatePersistenceResult; /** * Delete state */ deleteState(sessionId: string, key: string): StatePersistenceResult; /** * Verify state integrity */ verifyIntegrity(sessionId: string, key: string): boolean; /** * Get all states for a session */ getSessionStates(sessionId: string): StateItem[]; /** * Clean up expired states */ cleanupExpiredStates(): number; /** * Reset all states for a session */ resetSession(sessionId: string): void; private getStateKey; private computeIntegrityHash; private isSensitiveKey; private generateRecommendations; }