/** * Core security class with CSRF, rate limiting, sanitization, and encryption * @module @voilajsx/appkit/security * @file src/security/security.ts * * @llm-rule WHEN: Building apps that need security protection (CSRF, rate limiting, input sanitization, encryption) * @llm-rule AVOID: Using directly - always get instance via securityClass.get() * @llm-rule NOTE: Provides enterprise-grade security with CSRF tokens, rate limiting, XSS prevention, and AES-256-GCM encryption */ import type { SecurityConfig } from './defaults.js'; export interface ExpressRequest { method: string; session?: any; body?: any; headers?: Record; query?: any; ip?: string; connection?: { remoteAddress?: string; }; csrfToken?: () => string; [key: string]: any; } export interface ExpressResponse { setHeader?: (name: string, value: string | number) => void; [key: string]: any; } export interface ExpressNextFunction { (error?: any): void; } export type ExpressMiddleware = (req: ExpressRequest, res: ExpressResponse, next: ExpressNextFunction) => void; export interface CSRFOptions { secret?: string; tokenField?: string; headerField?: string; expiryMinutes?: number; } export interface RateLimitOptions { maxRequests?: number; windowMs?: number; message?: string; keyGenerator?: (req: ExpressRequest) => string; } export interface InputOptions { maxLength?: number; trim?: boolean; removeXSS?: boolean; } export interface HTMLOptions { allowedTags?: string[]; stripAllTags?: boolean; } /** * Security class with enterprise-grade protection functionality */ export declare class SecurityClass { config: SecurityConfig; private requestStore; private cleanupInitialized; constructor(config: SecurityConfig); /** * Creates CSRF protection middleware for forms and AJAX requests * @llm-rule WHEN: Protecting forms and state-changing requests from CSRF attacks * @llm-rule AVOID: Using without session middleware - CSRF requires sessions for token storage * @llm-rule NOTE: Automatically validates tokens on POST/PUT/DELETE/PATCH requests, adds req.csrfToken() method */ forms(options?: CSRFOptions): ExpressMiddleware; /** * Creates rate limiting middleware with configurable limits and windows * @llm-rule WHEN: Protecting endpoints from abuse and brute force attacks * @llm-rule AVOID: Using same limits for all endpoints - auth should have stricter limits than API * @llm-rule NOTE: Uses in-memory storage with automatic cleanup, sets standard rate limit headers */ requests(maxRequests?: number, windowMs?: number, options?: RateLimitOptions): ExpressMiddleware; /** * Cleans text input with XSS prevention and length limiting * @llm-rule WHEN: Processing any user text input before storage or display * @llm-rule AVOID: Storing raw user input - always clean to prevent XSS attacks * @llm-rule NOTE: Removes dangerous patterns like