/** * Input Validation and Sanitization Utilities * * This module provides security-focused utilities for validating and sanitizing user input * to prevent common web vulnerabilities including: * - Cross-Site Scripting (XSS) * - SQL Injection * - NoSQL Injection * - Path Traversal * - Command Injection * - LDAP Injection * * Compliance: * - ISO/IEC 25010:2023 Security requirements * - OWASP Top 10 Prevention * - CWE/SANS Top 25 Mitigation */ /** * HTML/XSS Sanitization * Escapes HTML special characters to prevent XSS attacks * * @param input - String that may contain HTML * @returns Sanitized string with HTML characters escaped * * @example * ```typescript * const userInput = ''; * const safe = sanitizeHtml(userInput); * // Returns: '<script>alert("XSS")</script>' * ``` */ export declare function sanitizeHtml(input: string): string; /** * SQL Injection Prevention * Validates and sanitizes input to prevent SQL injection attacks * * Note: This is a basic defense. Always use parameterized queries/prepared statements * * @param input - String to validate for SQL injection patterns * @returns Sanitized string or throws error if malicious pattern detected * * @throws {Error} If SQL injection pattern is detected * * @example * ```typescript * const userInput = "admin' OR '1'='1"; * try { * const safe = preventSqlInjection(userInput); * } catch (error) { * console.error('SQL injection attempt detected:', error.message); * } * ``` */ export declare function preventSqlInjection(input: string): string; /** * NoSQL Injection Prevention * Validates input to prevent NoSQL injection attacks (MongoDB, etc.) * * @param input - Object or string to validate * @returns Validated input or throws error if malicious pattern detected * * @throws {Error} If NoSQL injection pattern is detected * * @example * ```typescript * const userInput = { $gt: "" }; // MongoDB injection attempt * try { * preventNoSqlInjection(userInput); * } catch (error) { * console.error('NoSQL injection detected'); * } * ``` */ export declare function preventNoSqlInjection(input: any): any; /** * Path Traversal Prevention * Validates file paths to prevent directory traversal attacks * * @param filePath - File path to validate * @returns Sanitized path or throws error if traversal detected * * @throws {Error} If path traversal pattern is detected * * @example * ```typescript * const userPath = '../../etc/passwd'; * try { * preventPathTraversal(userPath); * } catch (error) { * console.error('Path traversal attempt detected'); * } * ``` */ export declare function preventPathTraversal(filePath: string): string; /** * Email Validation * Validates email format using RFC 5322 compliant regex * * @param email - Email address to validate * @returns true if valid email format * * @example * ```typescript * isValidEmail('user@example.com'); // true * isValidEmail('invalid.email'); // false * ``` */ export declare function isValidEmail(email: string): boolean; /** * URL Validation * Validates URL format and optionally checks for allowed protocols * * @param url - URL to validate * @param allowedProtocols - Array of allowed protocols (default: ['http:', 'https:']) * @returns true if valid URL with allowed protocol * * @example * ```typescript * isValidUrl('https://example.com'); // true * isValidUrl('javascript:alert(1)'); // false * isValidUrl('ftp://example.com', ['ftp:']); // true * ``` */ export declare function isValidUrl(url: string, allowedProtocols?: string[]): boolean; /** * Alphanumeric Validation * Checks if string contains only alphanumeric characters * * @param input - String to validate * @param allowSpaces - Whether to allow spaces (default: false) * @param allowDashes - Whether to allow dashes and underscores (default: false) * @returns true if input matches allowed pattern * * @example * ```typescript * isAlphanumeric('abc123'); // true * isAlphanumeric('abc 123', true); // true * isAlphanumeric('user_name-123', false, true); // true * ``` */ export declare function isAlphanumeric(input: string, allowSpaces?: boolean, allowDashes?: boolean): boolean; /** * Integer Validation * Validates and parses integer with optional min/max bounds * * @param value - Value to validate as integer * @param min - Minimum allowed value (optional) * @param max - Maximum allowed value (optional) * @returns Parsed integer * * @throws {Error} If value is not a valid integer or out of bounds * * @example * ```typescript * validateInteger('42', 0, 100); // 42 * validateInteger('150', 0, 100); // throws Error * ``` */ export declare function validateInteger(value: string | number, min?: number, max?: number): number; /** * String Length Validation * Validates string length with min/max constraints * * @param input - String to validate * @param min - Minimum length (default: 0) * @param max - Maximum length (default: unlimited) * @returns The validated string * * @throws {Error} If string length is out of bounds * * @example * ```typescript * validateLength('hello', 1, 10); // 'hello' * validateLength('', 1, 10); // throws Error * ``` */ export declare function validateLength(input: string, min?: number, max?: number): string; /** * Comprehensive Input Sanitization * Applies multiple sanitization steps for general user input * * @param input - String to sanitize * @param options - Sanitization options * @returns Sanitized string * * @example * ```typescript * const userInput = ' '; * const safe = sanitizeInput(userInput, { trim: true, escapeHtml: true }); * // Returns: '<script>alert(1)</script>' * ``` */ export declare function sanitizeInput(input: string, options?: { trim?: boolean; escapeHtml?: boolean; maxLength?: number; allowedChars?: RegExp; }): string; /** * UUID Validation (v4) * Validates UUID v4 format * * @param uuid - UUID string to validate * @returns true if valid UUID v4 * * @example * ```typescript * isValidUuid('550e8400-e29b-41d4-a716-446655440000'); // true * isValidUuid('invalid-uuid'); // false * ``` */ export declare function isValidUuid(uuid: string): boolean; /** * Sanitize Object Keys * Recursively sanitizes all string values in an object * * @param obj - Object to sanitize * @param sanitizer - Function to apply to each string value * @returns Object with sanitized values * * @example * ```typescript * const data = { name: '', age: 25 }; * const safe = sanitizeObject(data, sanitizeHtml); * ``` */ export declare function sanitizeObject(obj: any, sanitizer?: (input: string) => string): any; //# sourceMappingURL=validation.utils.d.ts.map