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

Don't allow files in hidden directories to be served (backported) #13

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion lib/directory.js
Expand Up @@ -229,7 +229,7 @@ internals.generateListing = function (path, resource, selection, hasTrailingSlas

internals.isFileHidden = function (path) {

return /^\./.test(Path.basename(path));
return /(^|[\\\/])\.([^\\\/]|[\\\/]?$)/.test(path); // Starts with a '.' or contains '/.' or '\.', and not followed by a '/' or '\' or end
};


Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "inert",
"description": "Static file and directory handlers for hapi.js",
"version": "1.1.0",
"version": "1.1.1",
"repository": "git://github.com/hapijs/inert",
"main": "index",
"keywords": [
Expand Down
52 changes: 50 additions & 2 deletions test/directory.js
Expand Up @@ -274,7 +274,7 @@ describe('handler()', function () {
});
});

it('returns the index when found in hidden folder', function (done) {
it('returns the index when served from a hidden folder', function (done) {

var server = provisionServer({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/{path*}', handler: { directoryTest: { path: './directory/.dot' } } });
Expand All @@ -293,7 +293,7 @@ describe('handler()', function () {
});
});

it('returns listing when found in hidden folder', function (done) {
it('returns listing when served from a hidden folder', function (done) {

var server = provisionServer({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/{path*}', handler: { directoryTest: { path: './directory/.dot', index: false, listing: true } } });
Expand Down Expand Up @@ -373,6 +373,35 @@ describe('handler()', function () {
});
});

it('returns a 404 response when requesting a file in a hidden directory when showHidden is disabled', function (done) {

var server = provisionServer({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/noshowhidden/{path*}', handler: { directoryTest: { path: './directory', listing: true } } });

server.inject('/noshowhidden/.dot/index.html', function (res) {

expect(res.statusCode).to.equal(404);

server.inject('/noshowhidden/.dot/', function (res) {

expect(res.statusCode).to.equal(404);
done();
});
});
});

it('returns a 404 response when requesting a hidden directory listing when showHidden is disabled', function (done) {

var server = provisionServer({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/noshowhidden/{path*}', handler: { directoryTest: { path: './directory', listing: true, index: false } } });

server.inject('/noshowhidden/.dot/', function (res) {

expect(res.statusCode).to.equal(404);
done();
});
});

it('returns a file when requesting a hidden file when showHidden is enabled', function (done) {

var server = provisionServer({ files: { relativeTo: __dirname } });
Expand All @@ -385,6 +414,25 @@ describe('handler()', function () {
});
});

it('returns a a file when requesting a file in a hidden directory when showHidden is enabled', function (done) {

var server = provisionServer({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/noshowhidden/{path*}', handler: { directoryTest: { path: './directory', showHidden: true, listing: true } } });

server.inject('/noshowhidden/.dot/index.html', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.payload).to.contain('test');

server.inject('/noshowhidden/.dot/', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.payload).to.contain('test');
done();
});
});
});

it('redirects to the same path with / appended if asking for a directory', function (done) {

var server = provisionServer({ files: { relativeTo: __dirname } });
Expand Down