Skip to content

Commit

Permalink
Merge pull request #295 from warpdesign/fix_readdir_check_rights
Browse files Browse the repository at this point in the history
fix: readdir should check for access rights, fixes #294
  • Loading branch information
tschaub committed Apr 21, 2020
2 parents bc04178 + f0ce803 commit 1d13c92
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/binding.js
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
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);
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');
});
});
});

0 comments on commit 1d13c92

Please sign in to comment.