/** * EDUrocks 3.0 - Security Utilities * XSS prevention and input sanitization */ /** * Escape HTML to prevent XSS attacks * @param {string} str - String to escape * @returns {string} - Escaped string */ export function escapeHtml(str) { if (typeof str !== 'string') return ''; const div = document.createElement('div'); div.textContent = str; return div.innerHTML; } /** * Sanitize URL to prevent javascript: and data: URIs * @param {string} url - URL to sanitize * @returns {string} - Sanitized URL or empty string if invalid */ export function sanitizeUrl(url) { if (typeof url !== 'string') return ''; const trimmed = url.trim(); // Block javascript: and data: URIs except safe image data URIs if (trimmed.toLowerCase().startsWith('javascript:')) { return ''; } if (trimmed.toLowerCase().startsWith('data:')) { // Only allow image data URIs if (!trimmed.toLowerCase().match(/^data:image\/(jpeg|jpg|png|gif|webp);base64,/)) { return ''; } } return trimmed; } /** * Validate and sanitize game ID * @param {string} gameId - Game ID to validate * @returns {string|null} - Sanitized game ID or null if invalid */ export function validateGameId(gameId) { if (typeof gameId !== 'string') return null; const trimmed = gameId.trim(); // Game ID should only contain alphanumeric, dash, underscore, dot if (!/^[a-zA-Z0-9._-]+$/.test(trimmed)) { return null; } // Length limit if (trimmed.length === 0 || trimmed.length > 100) { return null; } return trimmed; } /** * Validate and sanitize game name * @param {string} gameName - Game name to validate * @returns {string} - Sanitized game name */ export function validateGameName(gameName) { if (typeof gameName !== 'string') return ''; const trimmed = gameName.trim(); // Length limit if (trimmed.length > 255) { return trimmed.substring(0, 255); } return trimmed; } /** * Rate limiter for actions */ class RateLimiter { constructor(maxActions, timeWindowMs) { this.maxActions = maxActions; this.timeWindowMs = timeWindowMs; this.actions = []; } canPerformAction() { const now = Date.now(); // Remove old actions outside time window this.actions = this.actions.filter(timestamp => now - timestamp < this.timeWindowMs); // Check if under limit if (this.actions.length >= this.maxActions) { return false; } // Record action this.actions.push(now); return true; } reset() { this.actions = []; } } // Rate limiters for different actions export const favoriteRateLimiter = new RateLimiter(10, 10000); // 10 actions per 10 seconds export const uploadRateLimiter = new RateLimiter(3, 60000); // 3 uploads per minute /** * Sanitize object for localStorage * @param {any} data - Data to sanitize * @returns {string} - Sanitized JSON string */ export function sanitizeForStorage(data) { try { // Convert to JSON and back to remove functions and other non-serializable content const cleaned = JSON.parse(JSON.stringify(data)); return JSON.stringify(cleaned); } catch (e) { console.error('Failed to sanitize data for storage:', e); return '{}'; } } // Make utilities globally available window.EDUSecure = { escapeHtml, sanitizeUrl, validateGameId, validateGameName, sanitizeForStorage };