File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import fs from 'fs/promises' ;
22import http from 'http' ;
3+ import path from 'path' ;
34import rssParser from './rss-parser.js' ;
45import jsonToRss from './json-to-rss.js' ;
56
@@ -159,8 +160,13 @@ class Server {
159160 async serveStatic ( req , res ) {
160161 const [ url ] = req . url . split ( '?' ) ;
161162 const filePath = url === '/' ? '/index.html' : url ;
162- const fileExt = filePath . slice ( filePath . lastIndexOf ( '.' ) + 1 ) ;
163- this . sendResponse ( res , { contentType : this . extToMime ( fileExt ) , data : await fs . readFile ( `./public${ filePath } ` ) } ) ;
163+ const publicDir = path . resolve ( './public' ) ;
164+ const resolvedPath = path . resolve ( publicDir , filePath . replace ( / ^ \/ / , '' ) ) ;
165+ if ( ! resolvedPath . startsWith ( publicDir + path . sep ) ) {
166+ return this . sendResponse ( res , { statusCode : 404 , data : { message : 'Not found' } } ) ;
167+ }
168+ const fileExt = resolvedPath . slice ( resolvedPath . lastIndexOf ( '.' ) + 1 ) ;
169+ this . sendResponse ( res , { contentType : this . extToMime ( fileExt ) , data : await fs . readFile ( resolvedPath ) } ) ;
164170 }
165171 sendResponse ( res , { statusCode= 200 , contentType= 'application/json' , data } ) {
166172 res . writeHead ( statusCode , { 'Content-Type' : contentType } ) ;
You can’t perform that action at this time.
0 commit comments