diff --git a/lib/core/index.js b/lib/core/index.js index 920e55b9..23f273c7 100644 --- a/lib/core/index.js +++ b/lib/core/index.js @@ -343,11 +343,28 @@ module.exports = function createMiddleware(_dir, _options) { }) } + function statWithAccess (file, cb) { + fs.stat(file, (err, stat) => { + if (err) { + cb(err); + return; + } + fs.access(file, fs.constants.R_OK, (err) => { + stat.readable = !err; + cb(err, stat); + }); + }); + } + function statFile() { try { - fs.stat(file, (err, stat) => { - if (err && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) { + statWithAccess(file, (err, stat) => { + const effectively404 = + (err && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) || + (!stat || !stat.readable); + + if (effectively404) { if (req.statusCode === 404) { // This means we're already trying ./404.html and can not find it. // So send plain text response with 404 status code