Cleanup - Bug Fixes
This commit is contained in:
@@ -14,6 +14,7 @@ const process = require('process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { parseMinutesToMs, parseSecondsToMs, secondsToMs, loadOrCreateKeypair, setupCache, getPersistentPublicKey, isSecureHolesailKey } = require('../includes/infrastructure/utils');
|
||||
const { validateDomainDetailed } = require('../includes/infrastructure/validation');
|
||||
|
||||
// ============================================================================
|
||||
// Configuration
|
||||
@@ -49,6 +50,8 @@ const config = {
|
||||
|
||||
// Domain validation
|
||||
MAX_DOMAIN_LENGTH: 253,
|
||||
MAX_DOMAIN_CACHE_SIZE: parseInt(process.env.MAX_DOMAIN_CACHE_SIZE || '5000', 10),
|
||||
MAX_METRIC_MAP_SIZE: parseInt(process.env.MAX_METRIC_MAP_SIZE || '1000', 10),
|
||||
DOMAIN_REGEX: /^[a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?)*$/i
|
||||
};
|
||||
|
||||
@@ -137,15 +140,25 @@ function recordMetric(type, value = 1) {
|
||||
}
|
||||
}
|
||||
|
||||
function evictOldestMapEntry(map, maxSize) {
|
||||
if (map.size <= maxSize) return;
|
||||
const firstKey = map.keys().next().value;
|
||||
if (firstKey !== undefined) {
|
||||
map.delete(firstKey);
|
||||
}
|
||||
}
|
||||
|
||||
function recordError(type, message) {
|
||||
const count = metrics.errorsByType.get(type) || 0;
|
||||
metrics.errorsByType.set(type, count + 1);
|
||||
evictOldestMapEntry(metrics.errorsByType, config.MAX_METRIC_MAP_SIZE);
|
||||
recordMetric('error');
|
||||
}
|
||||
|
||||
function recordDomainRequest(domain) {
|
||||
const count = metrics.requestsByDomain.get(domain) || 0;
|
||||
metrics.requestsByDomain.set(domain, count + 1);
|
||||
evictOldestMapEntry(metrics.requestsByDomain, config.MAX_METRIC_MAP_SIZE);
|
||||
}
|
||||
|
||||
function getMetrics() {
|
||||
@@ -237,24 +250,7 @@ setInterval(() => {
|
||||
// Input Validation
|
||||
// ============================================================================
|
||||
function validateDomain(domain) {
|
||||
if (!domain || typeof domain !== 'string') {
|
||||
return { valid: false, error: 'Domain must be a non-empty string' };
|
||||
}
|
||||
|
||||
if (domain.length > config.MAX_DOMAIN_LENGTH) {
|
||||
return { valid: false, error: `Domain exceeds maximum length of ${config.MAX_DOMAIN_LENGTH}` };
|
||||
}
|
||||
|
||||
if (!config.DOMAIN_REGEX.test(domain)) {
|
||||
return { valid: false, error: 'Domain contains invalid characters' };
|
||||
}
|
||||
|
||||
// Check for path traversal attempts
|
||||
if (domain.includes('..') || domain.includes('/') || domain.includes('\\')) {
|
||||
return { valid: false, error: 'Domain contains invalid path characters' };
|
||||
}
|
||||
|
||||
return { valid: true };
|
||||
return validateDomainDetailed(domain, { maxLength: config.MAX_DOMAIN_LENGTH });
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -274,6 +270,12 @@ function getCachedHash(domain) {
|
||||
}
|
||||
|
||||
function setCachedHash(domain, hash) {
|
||||
if (domainCache.size >= config.MAX_DOMAIN_CACHE_SIZE) {
|
||||
const firstKey = domainCache.keys().next().value;
|
||||
if (firstKey !== undefined) {
|
||||
domainCache.delete(firstKey);
|
||||
}
|
||||
}
|
||||
domainCache.set(domain, {
|
||||
hash,
|
||||
expires: Date.now() + config.CACHE_TTL
|
||||
|
||||
Reference in New Issue
Block a user