Skip to content
New issue

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

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

Already on GitHub? # to your account

Do not serve files when path ends with / #224

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,12 +605,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 @@ -1194,6 +1194,12 @@ describe('send(file, options)', function () {
.get('/')
.expect(200, /tobi/, done)
})

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

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