Skip to content
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
7 changes: 7 additions & 0 deletions .labrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
'coverage-predicates': {
win32: process.platform === 'win32'
}
};
6 changes: 6 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ exports.File = class {
throw Boom.forbidden(null, { code: 'EISDIR', path: this.path });
}

// $lab:coverage:off$ $if:win32$
if (!stat.isFile()) {
throw Boom.forbidden(null, { code: 'EINVAL', path: this.path });
}
// $lab:coverage:on$

return stat;
}
catch (err) {
Expand Down
32 changes: 32 additions & 0 deletions test/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,38 @@ describe('file', () => {
expect(res.request.response._error.data.path).to.equal(Path.join(__dirname, '..', 'lib'));
});

it('returns a 403 when the file is not a regular file (eg. device)', { skip: process.platform === 'win32' }, async () => {

const filename = '/dev/null'; // a character device, not a regular file

const server = await provisionServer();
server.route({ method: 'GET', path: '/', handler: { file: { path: filename, confine: false } } });

const res = await server.inject('/');

expect(res.statusCode).to.equal(403);
expect(res.request.response._error.data.code).to.equal('EINVAL');
expect(res.request.response._error.data.path).to.equal(filename);
});

it('returns a file through a symbolic link', { skip: process.platform === 'win32' }, async () => {

const link = File.uniqueFilename(Os.tmpdir()) + '.package.json';
Fs.symlinkSync(Path.join(__dirname, '..', 'package.json'), link);

const server = await provisionServer();
server.route({ method: 'GET', path: '/', handler: { file: { path: link, confine: false } } });

const res = await server.inject('/');

Fs.unlinkSync(link);

expect(res.statusCode).to.equal(200);
expect(res.payload).to.contain('hapi');
expect(res.headers['content-type']).to.equal('application/json; charset=utf-8');
expect(res.headers['content-length']).to.exist();
});

it('returns a file using the built-in handler config', async () => {

const server = await provisionServer({ routes: { files: { relativeTo: Path.join(__dirname, '..') } } });
Expand Down
Loading