import { CSPDirective, CSPManagerConfig, CSPNonce, CSPNonceValue, CSPPolicy, CSPSourceValue, CSPViolationHandler, CSPViolationReport } from './types'; /** * CSP Manager for handling Content Security Policy * * Features: * - Cryptographically secure nonce generation * - Dynamic policy building with nonce injection * - CSP violation monitoring and reporting * - Support for report-only mode * - Automatic nonce rotation * * @example * ```typescript * const cspManager = CSPManager.getInstance(); * * // Get current nonce for inline scripts * const nonce = cspManager.getCurrentNonce(); * * // Build CSP header value * const cspHeader = cspManager.buildPolicyString(); * * // Add violation handler * cspManager.addViolationHandler((violation) => { * console.error('CSP Violation:', violation); * }); * ``` */ declare class CSPManagerClass { private static instance; private config; private currentNonce; private violations; private violationHandlers; private reportDebounceTimer; private pendingReports; private initialized; private constructor(); /** * Get singleton instance of CSP Manager */ static getInstance(config?: CSPManagerConfig): CSPManagerClass; /** * Reset singleton instance (for testing) */ static resetInstance(): void; /** * Initialize the CSP Manager * Sets up violation reporting listener */ initialize(): void; /** * Cleanup resources */ cleanup(): void; /** * Generate a new nonce * @returns The generated nonce object */ regenerateNonce(): CSPNonce; /** * Get the current nonce value * Regenerates if expired * @returns Current nonce value as branded type */ getCurrentNonce(): CSPNonceValue; /** * Get nonce attribute string for inline scripts * @returns Nonce attribute for use in script tags */ getScriptNonceAttr(): string; /** * Get nonce attribute string for inline styles * @returns Nonce attribute for use in style tags */ getStyleNonceAttr(): string; /** * Mark current nonce as used * For tracking nonce usage */ markNonceUsed(): void; /** * Check if nonce is still valid */ isNonceValid(): boolean; /** * Build the complete CSP policy object * Injects nonces into script-src and style-src directives * @returns Complete CSP policy */ buildPolicy(): CSPPolicy; /** * Build CSP header string from policy * @returns CSP header value string */ buildPolicyString(): string; /** * Get the appropriate CSP header name * @returns Header name based on report-only setting */ getHeaderName(): string; /** * Add a source to a directive * @param directive - The CSP directive * @param source - The source value to add */ addSource(directive: CSPDirective, source: CSPSourceValue): void; /** * Remove a source from a directive * @param directive - The CSP directive * @param source - The source value to remove */ removeSource(directive: CSPDirective, source: CSPSourceValue): void; /** * Record a CSP violation * @param violation - The violation report */ recordViolation(violation: CSPViolationReport): void; /** * Add a violation handler * @param handler - The handler function * @returns Cleanup function to remove handler */ addViolationHandler(handler: CSPViolationHandler): () => void; /** * Remove a violation handler * @param handler - The handler to remove */ removeViolationHandler(handler: CSPViolationHandler): void; /** * Get recorded violations * @returns Array of recorded violations */ getViolations(): readonly CSPViolationReport[]; /** * Clear recorded violations */ clearViolations(): void; /** * Create a meta tag element with CSP * For client-side CSP enforcement * @returns HTMLMetaElement with CSP policy */ createMetaTag(): HTMLMetaElement; /** * Inject CSP meta tag into document head * Note: Meta tag CSP is less effective than HTTP header */ injectMetaTag(): void; /** * Get current configuration */ getConfig(): Readonly; /** * Update configuration * @param updates - Partial configuration updates */ updateConfig(updates: Partial): void; /** * Check if CSP Manager is initialized */ isInitialized(): boolean; /** * Set up listener for CSP violation events */ private setupViolationListener; /** * Handle CSP violation event from browser */ private handleViolationEvent; /** * Queue violation for debounced reporting */ private queueViolationReport; /** * Flush pending violation reports to server * * Note: This method intentionally uses raw fetch() rather than apiClient because: * 1. CSP reports have special Content-Type (application/csp-report) * 2. Security reporting must be independent of the main API client * 3. Uses keepalive for reliability when page is unloading * * @see {@link @/lib/api/api-client} for application API calls */ private flushViolationReports; } /** * CSP Manager singleton instance */ export declare const CSPManager: CSPManagerClass; /** * Generate a new cryptographic nonce * Standalone function for one-off nonce generation */ export declare function generateNonce(): CSPNonceValue; /** * Parse a CSP header string into policy object * @param cspString - The CSP header value * @returns Parsed CSP policy object */ export declare function parseCSPString(cspString: string): CSPPolicy; /** * Merge multiple CSP policies * Later policies take precedence for conflicting directives * @param policies - Array of policies to merge * @returns Merged policy */ export declare function mergeCSPPolicies(...policies: CSPPolicy[]): CSPPolicy; /** * Create a strict CSP policy for high-security contexts * @returns Strict CSP policy */ export declare function createStrictPolicy(): CSPPolicy; /** * Validate that a CSP policy doesn't contain dangerous values * @param policy - The policy to validate * @returns Validation result with warnings */ export declare function validateCSPPolicy(policy: CSPPolicy): { isSecure: boolean; warnings: string[]; }; export type { CSPManagerClass }; export type { CSPPolicy, CSPDirective, CSPSourceValue, CSPNonce, CSPManagerConfig, CSPViolationReport, CSPViolationHandler, CSPNonceValue, };