File size: 3,557 Bytes
37d6cb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | /**
* 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
};
|