From 8c4de99a9886cbdd8cd1ff2bfddd4dbaddce87d8 Mon Sep 17 00:00:00 2001 From: Gideon Thomas Date: Sat, 6 Dec 2014 01:16:22 -0500 Subject: [PATCH] Fix #334 - Do not allow fs.link on directories --- src/filesystem/implementation.js | 2 ++ tests/spec/fs.link.spec.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index 5bed6e5..20a8f52 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -942,6 +942,8 @@ function link_node(context, oldpath, newpath, callback) { oldDirectoryData = result; if(!_(oldDirectoryData).has(oldname)) { callback(new Errors.ENOENT('a component of either path prefix does not exist', oldname)); + } else if(oldDirectoryData[oldname].type === 'DIRECTORY') { + callback(new Errors.EPERM('oldpath refers to a directory')); } else { find_node(context, newParentPath, read_new_directory_data); } diff --git a/tests/spec/fs.link.spec.js b/tests/spec/fs.link.spec.js index c1c68d2..e1c8e1a 100644 --- a/tests/spec/fs.link.spec.js +++ b/tests/spec/fs.link.spec.js @@ -69,4 +69,18 @@ describe('fs.link', function() { }); }); }); + + it('should not allow links to a directory', function(done) { + var fs = util.fs(); + + fs.mkdir('/mydir', function(error) { + if(error) throw error; + + fs.link('/mydir', '/mydirlink', function(error) { + expect(error).to.exist; + expect(error.code).to.equal('EPERM'); + done(); + }); + }); + }); });