From 17c5b9357201bddf684fb6d07f23155385273582 Mon Sep 17 00:00:00 2001 From: ADARSH <64017655+0x0806@users.noreply.github.com> Date: Sat, 5 Jul 2025 21:10:38 +0530 Subject: [PATCH] Update file-server.js --- file-server.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/file-server.js b/file-server.js index 3a083e2..68174f3 100644 --- a/file-server.js +++ b/file-server.js @@ -18,22 +18,25 @@ function getAllMarkdownFiles(dirPath, arrayOfFiles = []) { getAllMarkdownFiles(fullPath, arrayOfFiles); } else if (file.endsWith('.md')) { const stats = fs.statSync(fullPath); + const relativePath = path.relative('.', fullPath); + const folderPath = path.dirname(relativePath); + arrayOfFiles.push({ name: file, path: fullPath, - relativePath: path.relative('.', fullPath), - folder: path.dirname(path.relative('.', fullPath)), + relativePath: relativePath, + folder: folderPath === '.' ? 'Root' : folderPath, size: stats.size, type: 'md', lastModified: stats.mtime }); } } catch (error) { - // Skip files that can't be accessed + console.error(`Error processing file ${fullPath}:`, error.message); } }); } catch (error) { - // Skip directories that can't be accessed + console.error(`Error reading directory ${dirPath}:`, error.message); } return arrayOfFiles; @@ -61,13 +64,23 @@ const server = http.createServer((req, res) => { return; } + // Security check to prevent directory traversal + const resolvedPath = path.resolve(filePath); + const projectRoot = path.resolve('.'); + if (!resolvedPath.startsWith(projectRoot)) { + res.writeHead(403, { 'Content-Type': 'text/plain' }); + res.end('Access denied'); + return; + } + try { - const content = fs.readFileSync(filePath, 'utf8'); + const content = fs.readFileSync(resolvedPath, 'utf8'); res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); res.end(content); } catch (error) { + console.error(`Error reading file ${resolvedPath}:`, error.message); res.writeHead(404, { 'Content-Type': 'text/plain' }); - res.end('File not found'); + res.end('File not found: ' + error.message); } return; }