From 2844d454d1ad8765bfa4813dccec13200d94364d Mon Sep 17 00:00:00 2001 From: Hernan Silva Date: Wed, 15 Dec 2021 17:20:24 -0500 Subject: [PATCH] Check if a pathname containing dots is a directory before serving it as static file --- servor.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/servor.js b/servor.js index bc3b52b..e858d0b 100644 --- a/servor.js +++ b/servor.js @@ -70,6 +70,7 @@ module.exports = async ({ // Server utility functions const isRouteRequest = (pathname) => !~pathname.split('/').pop().indexOf('.'); + const isDir = (pathname) => fs.existsSync(pathname.split('/').pop()) && fs.lstatSync(pathname.split('/').pop()).isDirectory(); const utf8 = (file) => Buffer.from(file, 'binary').toString('utf8'); const baseDoc = (pathname = '', base = path.join('/', pathname, '/')) => @@ -156,6 +157,7 @@ module.exports = async ({ const pathname = path.normalize(decodePathname).replace(/^(\.\.(\/|\\|$))+/, ''); res.setHeader('access-control-allow-origin', '*'); if (reload && pathname === '/livereload') return serveReload(res); + if (!isRouteRequest(pathname) && isDir(pathname)) return serveRoute(res, pathname); if (!isRouteRequest(pathname)) return serveStaticFile(res, pathname); return serveRoute(res, pathname); }).listen(parseInt(port, 10));