Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not serve files when path ends with / #224

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,12 +715,14 @@ SendStream.prototype.sendFile = function sendFile (path) {

debug('stat "%s"', path)
fs.stat(path, function onstat (err, stat) {
if (err && err.code === 'ENOENT' && !extname(path) && path[path.length - 1] !== sep) {
var pathEndsWithSep = path[path.length - 1] === sep
if (err && err.code === 'ENOENT' && !extname(path) && !pathEndsWithSep) {
// not found, check extensions
return next(err)
}
if (err) return self.onStatError(err)
if (stat.isDirectory()) return self.redirect(path)
if (pathEndsWithSep) return self.error(404)
self.emit('file', path, stat)
self.send(path, stat)
})
Expand Down
6 changes: 6 additions & 0 deletions test/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,12 @@ describe('send(file, options)', function () {
.get('/')
.expect(200, /tobi/, done)
})

it('should 404 if file path contains tralling slash (windows)', function (done) {
request(createServer({ root: fixtures, index: false }))
.get('/tobi.html/')
.expect(404, done)
})
})

describe('root', function () {
Expand Down