security: Fix path traversal vulnerabilities

- Add filename validation to prevent path traversal
- Validate resolved paths are within expected directories
- Check for dangerous path characters (.., /, \)
This commit is contained in:
fr4iser
2026-02-28 20:40:13 +01:00
parent 4cb01fd482
commit 896c4fc520
2 changed files with 38 additions and 1 deletions

View File

@@ -259,7 +259,19 @@ function parseMemoryDir(dir, entries) {
try {
const files = fs.readdirSync(dir).filter(f => f.endsWith('.md'));
for (const file of files) {
// Validate file name to prevent path traversal
if (file.includes('..') || file.includes('/') || file.includes('\\')) {
continue;
}
const filePath = path.join(dir, file);
// Additional validation: ensure resolved path is within the base directory
const resolvedPath = path.resolve(filePath);
const resolvedDir = path.resolve(dir);
if (!resolvedPath.startsWith(resolvedDir)) {
continue; // Path traversal attempt detected
}
const content = fs.readFileSync(filePath, 'utf-8');
if (!content.trim()) continue;