Allow file system paths (possibly with symbolic links) using Node.js

I need a way to check if a file is under a specific path. If the path /var/wwwand file /var/www/something/something-else/file, I need to return true. If the file /var/www/../../etc/passwd, I need to return false.

In the field of PHP, I usually use a function called realpath():

realpath () expands all symbolic links and resolves references to "/./", "/../" and additional "/" characters in the input path and returns the canonical absolute path name.

I start realpath()both the desired path and the full path to the file, then determine whether the desired path is a substring of the result of the fully resolved path to the file. If so, I return true.

Is there a function for Node.js that accomplishes the same goal? path.resolve()gets me halfway, but doesn't process any symbolic links that actually reside on the file system. fs.readlink()absolutely does not contain any documentation and may not be applied directly, since I will not always have links to characters.

Do I have to go through every part of the path resolving symbolic links when I go, or is there a more canonical method?

+4
source share
1 answer

Node also has fs.realpath(), so it should work just like in PHP.

+6
source

All Articles