I mentioned that solution in the first post and said that it could be bypassed using ".."
Maybe I could remove all preceding ".." components or error if they're there…
The one I posted deals with the ".."'s. The first time it calls fs.combine it combines the root and the path, which gets rid of all of the ".."'s. The only problem is at this point it could be at a higher point in the root directory than you want them to, so that is what the second combine call is for. It checks to see if the new root path is even in the path anymore and if it isn't it adds it back.
Even if you did remove them or error if they are there, nothing says they have to be at the beginning.
"../hi" would go up a directory, but so would: "/hi/../../hi"
I urge you to at least try out the code I posted. I am fairly certain you won't be able to break out of the new root with it.
Like valithor already mentioned, fs.combine deals with "..", a single fs.combine( rootPath, userGivenPath ) is enough.
Unfortunately a single fs.combine call leaves open the possibility of them getting out of the new root.
Example:
fs.combine("this/is/the/new/root","../../../../hi")
This would return "/hi" instead of "this/is/the/new/root/hi", which wouldn't be what he is wanting.
That is why I had two combine calls in my example. One to get rid/handle all of the ..'s and then the second one to actually add the new root.