Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line breaks in filenames cause infinite loops in mkdirs & mkdirsSync #524

Closed
wrager opened this issue Dec 9, 2017 · 11 comments
Closed

Line breaks in filenames cause infinite loops in mkdirs & mkdirsSync #524

wrager opened this issue Dec 9, 2017 · 11 comments

Comments

@wrager
Copy link

wrager commented Dec 9, 2017

  • Operating System: Windows 10 Enterprise 2016
  • Node.js version: 6.10.2
  • fs-extra version: 4.0.3

It would be nice to have error handling for line breaks in the mkdirs and mkdirsSync methods.

Correct names:

> require('fs-extra').mkdirs('dir_correct');
Promise { <pending> }

> require('fs-extra').mkdirs('dir_correct', (a) => console.log('callback', a));
undefined
> callback null

> require('fs-extra').mkdirsSync('dir_correct');
null

Directories with \n in paths are not created. It's okay, but here we get an infinite loop (at least in mkdirsSync), which is unacceptable in my opinion. In mkdirs with callback, the callback is not called.

> require('fs-extra').mkdirs('dir_incorrect\n');
Promise { <pending> }

> require('fs-extra').mkdirs('dir_incorrect\n', (a) => console.log('callback', a));
undefined

> require('fs-extra').mkdirsSync('dir_incorrect\n');
RangeError: Maximum call stack size exceeded
    at Object.fs.mkdirSync (fs.js:923:18)
    at mkdirsSync (C:\projects\test1\node_modules\fs-extra\lib\mkdirs\mkdirs-sync.js:31:9)
    at mkdirsSync (C:\projects\test1\node_modules\fs-extra\lib\mkdirs\mkdirs-sync.js:37:16)
    at mkdirsSync (C:\projects\test1\node_modules\fs-extra\lib\mkdirs\mkdirs-sync.js:38:9)
    at mkdirsSync (C:\projects\test1\node_modules\fs-extra\lib\mkdirs\mkdirs-sync.js:38:9)
    at mkdirsSync (C:\projects\test1\node_modules\fs-extra\lib\mkdirs\mkdirs-sync.js:38:9)
    at mkdirsSync (C:\projects\test1\node_modules\fs-extra\lib\mkdirs\mkdirs-sync.js:38:9)
    at mkdirsSync (C:\projects\test1\node_modules\fs-extra\lib\mkdirs\mkdirs-sync.js:38:9)
    at mkdirsSync (C:\projects\test1\node_modules\fs-extra\lib\mkdirs\mkdirs-sync.js:38:9)
    at mkdirsSync (C:\projects\test1\node_modules\fs-extra\lib\mkdirs\mkdirs-sync.js:38:9)
@RyanZim
Copy link
Collaborator

RyanZim commented Dec 9, 2017

Huh, these weird cases. What's the result of calling fs.mkdir with a newline in the filename? (when the parent directories exist)

@wrager
Copy link
Author

wrager commented Dec 9, 2017

@RyanZim

> require('fs').mkdir('dir_incorrect\n');
undefined
> Error: ENOENT: no such file or directory, mkdir 'C:\projects\test1\dir_incorrect
'
    at Error (native)

@RyanZim
Copy link
Collaborator

RyanZim commented Dec 11, 2017

No wonder it crashes. ENOENT is also the error code we get when the parent doesn't exist, so we respond by trying to create the parent. That causes an infinite loop.

Not sure what's the best way to solve this, but we need to do something.

@RyanZim RyanZim added the bug label Dec 11, 2017
@RyanZim RyanZim changed the title No error handling for line breaks in mkdirs & mkdirsSync Line breaks in filenames cause infinite loops in mkdirs & mkdirsSync Dec 11, 2017
@manidlou
Copy link
Collaborator

What should happen when a newline character exists in the path? error out?

@RyanZim
Copy link
Collaborator

RyanZim commented Dec 13, 2017

@manidlou That's the ugly part. AFAIK, on Windows, newlines aren't allowed. However, they may be allowed on some Linux systems, not sure.

We could either error out on a newline, or else put logic in place that if we get ENOENT after creating the parent, we just throw it. Not sure what's best.

@wrager
Copy link
Author

wrager commented Dec 14, 2017

@manidlou @RyanZim It seems to me that you should delegate the handling of incorrect characters to the OS. Not only because operating systems are possible that allow line breaks in paths, but also because other similar characters are possible that we do not know about.

@RyanZim
Copy link
Collaborator

RyanZim commented Dec 14, 2017

@wrager I'm leaning in that direction as well. If only Windoze would throw a sensible error for invalid characters instead of ENOENT... 😬

@manidlou
Copy link
Collaborator

@wrager yes delegating to the OS makes sense. @RyanZim you are right! there is inconsistency in error codes that OSes throw!

Not only that, there is a big discussion on what valid file paths should look like! There is this Wikipedia page on Filename on reserved words and characters in different OSes. IMO, dealing with checking for valid file path (that's what I had originally in my mind) is a mess because of noisy OS differences!

put logic in place that if we get ENOENT after creating the parent, we just throw it.

@RyanZim I think that's a good and feasible approach!

So then when that happens, we should remove the parent directory that was just created, right? Since essentially it is a failed operation?!

@RyanZim
Copy link
Collaborator

RyanZim commented Dec 15, 2017

@manidlou Yeah, we probably should. That's gonna be messy, though. I didn't think about that.

@RyanZim
Copy link
Collaborator

RyanZim commented Aug 11, 2018

Well, hopefully nodejs/node#21875 solves this for newer Node versions.

@RyanZim
Copy link
Collaborator

RyanZim commented Oct 20, 2022

Should be fixed since #894.

@RyanZim RyanZim closed this as completed Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants