UNPKG

vibe-coder-mcp

Version:

Production-ready MCP server with complete agent integration, multi-transport support, and comprehensive development automation tools for AI-assisted workflows.

58 lines (57 loc) 2.11 kB
import * as path from 'path'; import { fileURLToPath } from 'url'; import logger from '../../../logger.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const PROJECT_ROOT = path.resolve(__dirname, '../../../..'); export function getProjectRoot() { return PROJECT_ROOT; } export function resolveProjectPath(relativePath) { return path.join(PROJECT_ROOT, relativePath); } export function normalizePath(inputPath) { if (path.isAbsolute(inputPath)) { return inputPath; } return resolveProjectPath(inputPath); } export function validatePathSecurity(inputPath, allowedDirectory) { try { const normalizedAllowedDir = path.resolve(allowedDirectory); const normalizedPath = path.resolve(inputPath); if (!isPathWithin(normalizedPath, normalizedAllowedDir)) { return { isValid: false, error: `Path '${inputPath}' is outside the allowed directory '${allowedDirectory}'` }; } return { isValid: true, normalizedPath }; } catch (error) { return { isValid: false, error: `Error validating path security: ${error instanceof Error ? error.message : String(error)}` }; } } export function isPathWithin(childPath, parentPath) { const normalizedChild = path.resolve(childPath).replace(/\\/g, '/'); const normalizedParent = path.resolve(parentPath).replace(/\\/g, '/'); return normalizedChild.startsWith(normalizedParent + '/') || normalizedChild === normalizedParent; } export function createSecurePath(inputPath, allowedDirectory) { const validationResult = validatePathSecurity(inputPath, allowedDirectory); if (!validationResult.isValid) { logger.error({ inputPath, allowedDirectory, error: validationResult.error }, 'Security violation: Attempted to access path outside allowed directory'); throw new Error(validationResult.error); } return validationResult.normalizedPath; }