Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 896c4fc

Browse files
committed
security: Fix path traversal vulnerabilities
- Add filename validation to prevent path traversal - Validate resolved paths are within expected directories - Check for dangerous path characters (.., /, \)
1 parent 4cb01fd commit 896c4fc

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

.claude/helpers/intelligence.cjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,19 @@ function parseMemoryDir(dir, entries) {
259259
try {
260260
const files = fs.readdirSync(dir).filter(f => f.endsWith('.md'));
261261
for (const file of files) {
262+
// Validate file name to prevent path traversal
263+
if (file.includes('..') || file.includes('/') || file.includes('\\')) {
264+
continue;
265+
}
266+
262267
const filePath = path.join(dir, file);
268+
// Additional validation: ensure resolved path is within the base directory
269+
const resolvedPath = path.resolve(filePath);
270+
const resolvedDir = path.resolve(dir);
271+
if (!resolvedPath.startsWith(resolvedDir)) {
272+
continue; // Path traversal attempt detected
273+
}
274+
263275
const content = fs.readFileSync(filePath, 'utf-8');
264276
if (!content.trim()) continue;
265277

.claude/helpers/metrics-db.mjs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import initSqlJs from 'sql.js';
99
import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync, statSync } from 'fs';
10-
import { dirname, join, basename } from 'path';
10+
import { dirname, join, basename, resolve } from 'path';
1111
import { fileURLToPath } from 'url';
1212
import { execSync } from 'child_process';
1313

@@ -154,7 +154,19 @@ function countFilesAndLines(dir, ext = '.ts') {
154154
try {
155155
const entries = readdirSync(currentDir, { withFileTypes: true });
156156
for (const entry of entries) {
157+
// Validate entry name to prevent path traversal
158+
if (entry.name.includes('..') || entry.name.includes('/') || entry.name.includes('\\')) {
159+
continue;
160+
}
161+
157162
const fullPath = join(currentDir, entry.name);
163+
// Additional validation: ensure resolved path is within the base directory
164+
const resolvedPath = resolve(fullPath);
165+
const resolvedCurrentDir = resolve(currentDir);
166+
if (!resolvedPath.startsWith(resolvedCurrentDir)) {
167+
continue; // Path traversal attempt detected
168+
}
169+
158170
if (entry.isDirectory() && !entry.name.includes('node_modules')) {
159171
walk(fullPath);
160172
} else if (entry.isFile() && entry.name.endsWith(ext)) {
@@ -209,7 +221,20 @@ function calculateModuleProgress(moduleDir) {
209221
* Check security file status
210222
*/
211223
function checkSecurityFile(filename, minLines = 100) {
224+
// Validate filename to prevent path traversal
225+
if (filename.includes('..') || filename.includes('/') || filename.includes('\\')) {
226+
return false;
227+
}
228+
212229
const filePath = join(V3_DIR, '@claude-flow/security/src', filename);
230+
231+
// Additional validation: ensure resolved path is within the expected directory
232+
const resolvedPath = resolve(filePath);
233+
const expectedDir = resolve(join(V3_DIR, '@claude-flow/security/src'));
234+
if (!resolvedPath.startsWith(expectedDir)) {
235+
return false; // Path traversal attempt detected
236+
}
237+
213238
if (!existsSync(filePath)) return false;
214239

215240
try {

0 commit comments

Comments
 (0)