node-fs-extra icon indicating copy to clipboard operation
node-fs-extra copied to clipboard

Bug in onSymlink

Open znewsham opened this issue 2 years ago • 2 comments

There is a bug when copying a directory containing a symlink onto itself (e.g., if you were making an rsync type utility):

If options.dereference is not set, then resolvedSrc and resolvedDest will always be equal - since they are the value of the symlink.

        if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
          return cb(new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`))
        }

this code needs to be changed to

if (options.dereference && stat.isSrcSubdir(resolvedSrc, resolvedDest)) {

Probably a similar change is required here:

if (destStat.isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) {

znewsham avatar Nov 15 '23 17:11 znewsham

Confirmed bug; but we need something more complicated than your proposed fix. We can't simply ignore these checks when we're not dereferencing; just one example case: https://github.com/jprichardson/node-fs-extra/blob/a277cbbdf630424482b1e7418749ccaac0db98fe/lib/copy/tests/copy-prevent-copying-into-itself.test.js#L351

RyanZim avatar Nov 20 '23 21:11 RyanZim