Make it practically impossible to work on any domain or requests or web page except the…
- User
Make it practically impossible to work on any domain or requests or web page except the specified one only no matter the circumstances. Send the fully working full codes. // Constants and Configuration (No changes needed) const RULE_IDS = { CORS: 1, PREFLIGHT: 2, ORIGIN_CHECK: 3 };
const SECURITY_CONSTANTS = { TOKEN_REFRESH_INTERVAL: 1800000, // 30 minutes MAX_REQUESTS_PER_MINUTE: 100, TOKEN_LENGTH: 32, MAX_DOMAINS_PER_TAB: 10, CLEANUP_INTERVAL: 300000, // 5 minutes MAX_RETRIES: 3, RETRY_DELAY: 100, MAX_DOMAIN_LENGTH: 255, MAX_HEADER_LENGTH: 8192, REQUEST_TIMEOUT: 30000 };
const ALLOWED_METHODS = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD' ];
const ALLOWED_HEADERS = [ 'Content-Type', 'Authorization', 'X-Requested-With', 'X-Security-Token', 'Origin', 'Accept', 'Access-Control-Request-Method', 'Access-Control-Request-Headers' ];
// Use Map instead of WeakMap for explicit control over cleanup const state = { enabledDomains: new Map(), securityConfigs: new Map() };
// Enhanced Error handling (No changes needed) class CORSHandlerError extends Error { constructor(message, code, details = {}) { super(message); this.name = 'CORSHandlerError'; this.code = code; this.details = details; this.timestamp = new Date().toISOString(); } }
// Security Utils (No changes needed) const SecurityUtils = { async generateStrongToken() { const array = new Uint8Array(SECURITY_CONSTANTS.TOKEN_LENGTH); crypto.getRandomValues(array); return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join(''); },
sanitizeHeader(value) { if (!value) return ''; return String(value) .slice(0, SECURITY_CONSTANTS.MAX_HEADER_LENGTH) .replace(/[<>{}()'\n\r]/g, ''); },
isValidDomain(domain) { try { if (!domain || typeof domain !== 'string') return false;
codereturn domain.length <= SECURITY_CONSTANTS.MAX_DOMAIN_LENGTH && /^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(?:\.[A-Za-z0-9-]{1,63})*$/.test(domain) && !domain.includes('..') && !/[<>{}()']/.test(domain); } catch { return false; }},
isValidOrigin(origin) { try { const url = new URL(origin); return ['http:', 'https:'].includes(url.protocol) && SecurityUtils.isValidDomain(url.hostname); } catch { return false; } } };
// Domain Security Class (No changes needed) class DomainSecurity { constructor(domain) { this.domain = domain; this.token = null; this.timestamp = Date.now(); this.allowedOrigins = new Set(); this.requestCount = new Map(); this.lastCleanup = Date.now(); this.errors = []; }
async initialize() { try { this.token = await SecurityUtils.generateStrongToken(); return true; } catch (error) { this.logError('Token generation failed', error); return false; } }
- Assistant