UNPKG

@andrebuzeli/advanced-memory-markdown-mcp

Version:

Advanced Memory Bank MCP v3.1.5 - Sistema avançado de gerenciamento de memória com isolamento de projetos por IDE, sincronização sob demanda, backup a cada 30min, apenas arquivos .md principais sincronizados, pasta reasoning temporária com limpeza automát

192 lines 6.17 kB
/** * Security Validator - Input validation and sanitization * Prevents injection attacks and ensures data integrity */ import { VERSION } from '../version.js'; export class SecurityValidator { version = VERSION; maxContentLength = 10000; // 10KB limit maxTagLength = 50; maxTags = 20; /** * Validate and sanitize memory content */ validateMemoryContent(content) { const errors = []; const warnings = []; // Check for null or undefined if (!content) { errors.push('Content cannot be empty'); return { isValid: false, errors, warnings }; } // Check length limits if (content.length > this.maxContentLength) { errors.push(`Content exceeds maximum length of ${this.maxContentLength} characters`); } // Check for potential injection patterns const dangerousPatterns = [ /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, /javascript:/gi, /vbscript:/gi, /on\w+\s*=/gi, /eval\s*\(/gi, /expression\s*\(/gi, ]; for (const pattern of dangerousPatterns) { if (pattern.test(content)) { warnings.push('Potentially dangerous content detected and will be sanitized'); break; } } // Sanitize content const sanitized = this.sanitizeContent(content); return { isValid: errors.length === 0, errors, warnings, sanitized, }; } /** * Validate tags array */ validateTags(tags) { const errors = []; const warnings = []; if (!Array.isArray(tags)) { errors.push('Tags must be an array'); return { isValid: false, errors, warnings }; } if (tags.length > this.maxTags) { errors.push(`Maximum ${this.maxTags} tags allowed`); } const sanitizedTags = []; for (const tag of tags) { if (typeof tag !== 'string') { warnings.push('Non-string tags will be converted to strings'); sanitizedTags.push(String(tag)); } else if (tag.length > this.maxTagLength) { warnings.push(`Tag "${tag}" exceeds maximum length and will be truncated`); sanitizedTags.push(tag.substring(0, this.maxTagLength)); } else { sanitizedTags.push(this.sanitizeTag(tag)); } } return { isValid: errors.length === 0, errors, warnings, sanitized: sanitizedTags, }; } /** * Validate importance level */ validateImportance(importance) { const errors = []; const warnings = []; if (typeof importance !== 'number') { errors.push('Importance must be a number'); return { isValid: false, errors, warnings }; } if (importance < 1 || importance > 10) { errors.push('Importance must be between 1 and 10'); } if (!Number.isInteger(importance)) { warnings.push('Importance will be rounded to nearest integer'); } const sanitized = Math.max(1, Math.min(10, Math.round(importance))); return { isValid: errors.length === 0, errors, warnings, sanitized, }; } /** * Validate project name */ validateProjectName(projectName) { const errors = []; const warnings = []; if (!projectName) { errors.push('Project name cannot be empty'); return { isValid: false, errors, warnings }; } // Check for invalid characters const invalidChars = /[<>:"/\\|?*\x00-\x1f]/g; if (invalidChars.test(projectName)) { warnings.push('Invalid characters in project name will be sanitized'); } // Check length if (projectName.length > 100) { warnings.push('Project name will be truncated to 100 characters'); } const sanitized = this.sanitizeProjectName(projectName); return { isValid: errors.length === 0, errors, warnings, sanitized, }; } /** * Sanitize content by removing dangerous patterns */ sanitizeContent(content) { return content .replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '') .replace(/javascript:/gi, '') .replace(/vbscript:/gi, '') .replace(/on\w+\s*=/gi, '') .replace(/eval\s*\(/gi, 'eval (') .replace(/expression\s*\(/gi, 'expression ('); } /** * Sanitize tag by removing special characters */ sanitizeTag(tag) { return tag .replace(/[<>:"/\\|?*\x00-\x1f]/g, '') .trim() .toLowerCase(); } /** * Sanitize project name for safe file system usage */ sanitizeProjectName(projectName) { return projectName .replace(/[<>:"/\\|?*\x00-\x1f]/g, '-') .replace(/\s+/g, '-') .replace(/-+/g, '-') .replace(/^-|-$/g, '') .toLowerCase() .substring(0, 100); } /** * Validate environment variables for security */ validateEnvironment() { const errors = []; const warnings = []; // Check for potentially dangerous environment variables const dangerousEnvVars = [ 'NODE_OPTIONS', 'NPM_CONFIG_SCRIPT_SHELL', 'SHELL', ]; for (const envVar of dangerousEnvVars) { if (process.env[envVar] && process.env.NODE_ENV !== 'development') { warnings.push(`Environment variable ${envVar} detected in production`); } } return { isValid: errors.length === 0, errors, warnings, }; } } //# sourceMappingURL=validator.js.map