Skip to content

Commit 465a316

Browse files
committed
Strengthen path traversal protection logic
1 parent ce49cda commit 465a316

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

server.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function handlePostRequest(req, res, parsedUrl) {
121121
// Create HTTP server
122122
const server = http.createServer((req, res) => {
123123
const parsedUrl = url.parse(req.url, true);
124-
let pathname = parsedUrl.pathname;
124+
let pathName = parsedUrl.pathname === '/' ? '/index.html' : parsedUrl.pathname;
125125

126126
// Handle POST requests
127127
if (req.method === 'POST') {
@@ -131,17 +131,16 @@ const server = http.createServer((req, res) => {
131131

132132
// In production mode, serve static files from dist directory
133133
if (isProduction) {
134-
// Serve static files from dist directory
135-
let filePath = path.join(DIST_DIR, pathname === '/' ? 'index.html' : pathname);
134+
// Strip leading slashes so path.join/resolve can't ignore DIST_DIR
135+
let filePath = path.join(DIST_DIR, pathName.replace(/^\/+/, ''));
136136

137137
// Security check - prevent directory traversal
138-
// Resolve both paths to absolute paths to handle symlinks and relative paths
139138
const resolvedDistDir = path.resolve(DIST_DIR);
140139
const resolvedFilePath = path.resolve(filePath);
141140
const relativePath = path.relative(resolvedDistDir, resolvedFilePath);
142141

143142
// Reject if path tries to traverse outside the base directory
144-
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
143+
if (relativePath.startsWith('..')) {
145144
res.writeHead(403, { 'Content-Type': 'text/plain' });
146145
res.end('Forbidden');
147146
return;
@@ -151,7 +150,7 @@ const server = http.createServer((req, res) => {
151150
} else {
152151
// Development mode - static files are served by Vite
153152
res.writeHead(404, { 'Content-Type': 'text/plain' });
154-
res.end('Not found (development mode - use Vite dev server `npm run dev`)');
153+
res.end('Not found (development mode - use Vite dev server `npm run start:dev`)');
155154
}
156155
});
157156

0 commit comments

Comments
 (0)