File tree Expand file tree Collapse file tree 1 file changed +10
-3
lines changed
Expand file tree Collapse file tree 1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change @@ -15,14 +15,21 @@ function createServer(rootDir) {
1515 res . end ( '<html><body><div id="content">Draft V2</div></body></html>' ) ;
1616 return ;
1717 }
18- const filePath = path . join ( rootDir , decodeURIComponent ( req . url . split ( '?' ) [ 0 ] ) ) ;
19- fs . readFile ( filePath , ( err , data ) => {
18+ // Normalize and validate the file path
19+ const requestedPath = decodeURIComponent ( req . url . split ( '?' ) [ 0 ] ) ;
20+ const safePath = path . resolve ( rootDir , "." + requestedPath ) ; // ensure requestedPath is not absolute
21+ if ( ! safePath . startsWith ( rootDir ) ) {
22+ res . writeHead ( 403 ) ;
23+ res . end ( 'Forbidden' ) ;
24+ return ;
25+ }
26+ fs . readFile ( safePath , ( err , data ) => {
2027 if ( err ) {
2128 res . writeHead ( 404 ) ;
2229 res . end ( 'Not Found' ) ;
2330 return ;
2431 }
25- const ext = path . extname ( filePath ) ;
32+ const ext = path . extname ( safePath ) ;
2633 const types = { '.html' : 'text/html' , '.js' : 'application/javascript' , '.css' : 'text/css' } ;
2734 res . writeHead ( 200 , { 'Content-Type' : types [ ext ] || 'application/octet-stream' } ) ;
2835 res . end ( data ) ;
You can’t perform that action at this time.
0 commit comments