Skip to main content
Glama
ahmetshbz1

Filesystem MCP Server

by ahmetshbz1
path-validation.ts1.47 kB
import path from 'path'; export function isPathWithinAllowedDirectories(absolutePath: string, allowedDirectories: string[]): boolean { if (typeof absolutePath !== 'string' || !Array.isArray(allowedDirectories)) return false; if (!absolutePath || allowedDirectories.length === 0) return false; if (absolutePath.includes('\x00')) return false; let normalizedPath: string; try { normalizedPath = path.resolve(path.normalize(absolutePath)); } catch { return false; } if (!path.isAbsolute(normalizedPath)) throw new Error('Path must be absolute after normalization'); return allowedDirectories.some(dir => { if (typeof dir !== 'string' || !dir) return false; if (dir.includes('\x00')) return false; let normalizedDir: string; try { normalizedDir = path.resolve(path.normalize(dir)); } catch { return false; } if (!path.isAbsolute(normalizedDir)) throw new Error('Allowed directories must be absolute paths after normalization'); if (normalizedPath === normalizedDir) return true; if (normalizedDir === path.sep) return normalizedPath.startsWith(path.sep); if (path.sep === '\\' && normalizedDir.match(/^[A-Za-z]:\\?$/)) { const dirDrive = normalizedDir.charAt(0).toLowerCase(); const pathDrive = normalizedPath.charAt(0).toLowerCase(); return pathDrive === dirDrive && normalizedPath.startsWith(normalizedDir.replace(/\\?$/, '\\')); } return normalizedPath.startsWith(normalizedDir + path.sep); }); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ahmetshbz1/filesystem-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server