Skip to content

Commit d6b859d

Browse files
laravelcms-zzjonathanong
authored andcommitted
when .hidden option is set, also check hidden directories
closes #17
1 parent 9252046 commit d6b859d

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function send(ctx, path, opts) {
5858
path = resolvePath(root, path);
5959

6060
// hidden file support, ignore
61-
if (!hidden && leadingDot(path)) return;
61+
if (!hidden && isHidden(root, path)) return;
6262

6363
// serve gzipped file when possible
6464
if (encoding === 'gzip' && gzip && (yield fs.exists(path + '.gz'))) {
@@ -93,8 +93,12 @@ function send(ctx, path, opts) {
9393
* Check if it's hidden.
9494
*/
9595

96-
function leadingDot(path) {
97-
return '.' == basename(path)[0];
96+
function isHidden(root, path) {
97+
path = path.substr(root.length).split('/');
98+
for(var i = 0; i < path.length; i++) {
99+
if(path[i][0] === '.') return true;
100+
}
101+
return false;
98102
}
99103

100104
/**

test/fixtures/.hidden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You should never get here

test/fixtures/.private/id_rsa.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You should never get here

test/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,49 @@ describe('send(ctx, file)', function(){
311311
})
312312
})
313313
})
314+
describe('.hidden option', function() {
315+
describe('when trying to get a hidden file', function(){
316+
it('should 404', function(done){
317+
var app = koa();
318+
319+
app.use(function *(){
320+
yield send(this, 'test/fixtures/.hidden');
321+
});
322+
323+
request(app.listen())
324+
.get('/')
325+
.expect(404, done);
326+
})
327+
})
328+
329+
describe('when trying to get a file from a hidden directory', function(){
330+
it('should 404', function(done){
331+
var app = koa();
332+
333+
app.use(function *(){
334+
yield send(this, 'test/fixtures/.private/id_rsa.txt');
335+
});
336+
337+
request(app.listen())
338+
.get('/')
339+
.expect(404, done);
340+
})
341+
})
342+
343+
describe('when trying to get a hidden file and .hidden check is turned off', function(){
344+
it('should 200', function(done){
345+
var app = koa();
346+
347+
app.use(function *(){
348+
yield send(this, 'test/fixtures/.hidden', {hidden: true});
349+
});
350+
351+
request(app.listen())
352+
.get('/')
353+
.expect(200, done);
354+
})
355+
})
356+
});
314357

315358
it('should set the Content-Type', function(done){
316359
var app = koa();

0 commit comments

Comments
 (0)