Skip to content

Prevent copying a directory into itself #83

Closed
@iwege

Description

@iwege

fse.copy('src','src/dest') will create the dest continues to recurse, and finally it throws an error ENAMETOOLONG

It links to AvianFlu/ncp#4

Activity

danyshaanan

danyshaanan commented on Sep 9, 2014

@danyshaanan

How does require('fs').copy handle this case?

modified the milestone: 1.0 on Jul 2, 2015
jprichardson

jprichardson commented on Jul 13, 2015

@jprichardson
Owner

Off the cuff, I'd say that I could just resolve the paths and check if one is a substring of the other. But I'd be concerned about introducing regressions. Thoughts?

iwege

iwege commented on Jul 14, 2015

@iwege
Author

I have collected some cases in this pr AvianFlu/ncp#61, maybe you can use it as a reference.

jprichardson

jprichardson commented on Oct 28, 2015

@jprichardson
Owner

@lwege are you still interested in this getting fixed? I'd like to tackle this - it'd be awesome to take your list of cases and turn them into tests for both *nix and Windows.

iwege

iwege commented on Nov 15, 2015

@iwege
Author

@jimhigson I copied testcase to https://github.com/iwege/node-fs-extra/tree/feature/prevent-copy-to-self , but how can I test it first? Or do I need to fix this issue with my old code?

jprichardson

jprichardson commented on Nov 15, 2015

@jprichardson
Owner

Would you link directly to the test case so that I can take a look? Thanks.

iwege

iwege commented on Nov 15, 2015

@iwege
Author
jprichardson

jprichardson commented on Dec 17, 2015

@jprichardson
Owner

Thank you. Tests look good. Any thoughts on a reliable fix?

iwege

iwege commented on Dec 24, 2015

@iwege
Author

I have used the AvianFlu/ncp#61 in my project and no one reports relative issue to me. But I don't know it reliable or not. Maybe my user doesn't do this in my product.

DaneEveritt

DaneEveritt commented on Oct 3, 2016

@DaneEveritt

Sorry to bring this back up, but any progress on implementing this, or should I add my own checks?

jprichardson

jprichardson commented on Oct 3, 2016

@jprichardson
Owner

or should I add my own checks

What checks would you add?

DaneEveritt

DaneEveritt commented on Oct 3, 2016

@DaneEveritt

I built off @iwege's changes, and made the following function that I'm using before I call the move function.

isSelf(moveTo, moveFrom) {
    const target = this.server.path(moveTo); // these two lines are simply building a path to the files
    const source = this.server.path(moveFrom);

    if (!_.startsWith(target, source)) {
        return false;
    }

    const end = target.slice(source.length);
    if (!end) {
        return true;
    }

    return _.startsWith(end, '/');
}

Prevents moving folders into themselves, but doesn't block moving a folder into another folder that starts with the same name (so src can still move into src-dest, but not into src).

Renaming files to the same name also triggers the catch, but that doesn't bother me so much.

43 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jprichardson@DaneEveritt@iwege@danyshaanan@manidlou

        Issue actions

          Prevent copying a directory into itself · Issue #83 · jprichardson/node-fs-extra