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

fix: readdir should check for access rights, fixes #294 #295

Merged
merged 3 commits into from
Apr 21, 2020
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
3 changes: 3 additions & 0 deletions lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,9 @@ Binding.prototype.readdir = function(
if (!(dir instanceof Directory)) {
throw new FSError('ENOTDIR', dirpath);
}
if (!dir.canRead()) {
throw new FSError('EACCES', dirpath);
}

let list = dir.list();
if (encoding === 'buffer') {
Expand Down
41 changes: 40 additions & 1 deletion test/lib/fs.readdir.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ describe('fs.readdir(path, callback)', function() {
empty: {}
}
}
}
},
denied: mock.directory({
mode: 0o000,
items: [
{
'one.txt': 'content'
}
]
})
});
});
afterEach(mock.restore);
Expand Down Expand Up @@ -92,6 +100,31 @@ describe('fs.readdir(path, callback)', function() {
);
});

it('calls with an error for restricted path', function(done) {
fs.readdir('denied', function(err, items) {
assert.instanceOf(err, Error);
3cp marked this conversation as resolved.
Show resolved Hide resolved
assert.equal(err.code, 'EACCES');
assert.isUndefined(items);
done();
});
});

withPromise.it('promise calls with an error for restricted path', function(
done
) {
fs.promises.readdir('denied').then(
function() {
assert.fail('should not succeed.');
done();
},
function(err) {
assert.instanceOf(err, Error);
assert.equal(err.code, 'EACCES');
done();
}
);
});

inVersion('>=10.10').it('should support "withFileTypes" option', function(
done
) {
Expand Down Expand Up @@ -216,4 +249,10 @@ describe('fs.readdirSync(path)', function() {
fs.readdirSync('bogus');
});
});

it('throws when access refused', function() {
assert.throws(function() {
fs.readdirSync('denied');
});
});
});