File tree Expand file tree Collapse file tree 1 file changed +14
-2
lines changed
Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -364,8 +364,20 @@ const httpServer = http.createServer((req, res) => {
364364 }
365365
366366 // Serve static files
367- let filePath = req . url === "/" ? "/index.html" : req . url ;
368- filePath = path . join ( PUBLIC_DIR , filePath ) ;
367+ let requestedPath = req . url === "/" ? "/index.html" : req . url ;
368+
369+ // Sanitize: remove query strings and decode
370+ requestedPath = decodeURIComponent ( requestedPath . split ( "?" ) [ 0 ] ) ;
371+
372+ // Resolve to absolute path and verify it's within PUBLIC_DIR (prevent path traversal)
373+ const filePath = path . resolve ( PUBLIC_DIR , "." + requestedPath ) ;
374+ const normalizedPublicDir = path . resolve ( PUBLIC_DIR ) ;
375+
376+ if ( ! filePath . startsWith ( normalizedPublicDir + path . sep ) && filePath !== normalizedPublicDir ) {
377+ res . writeHead ( 403 ) ;
378+ res . end ( "Forbidden" ) ;
379+ return ;
380+ }
369381
370382 const ext = path . extname ( filePath ) ;
371383 const contentType = MIME_TYPES [ ext ] || "application/octet-stream" ;
You can’t perform that action at this time.
0 commit comments