Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Don't let path.normalize get above the root.
Browse files Browse the repository at this point in the history
Any path.join or path.normalize that starts with a / will not go "above" that after normalization.  This is important because /../foo is almost *always* some sort of error, and doesn't match the corollary in sh: `cd $p; pwd`

At the worse, this can be a vector for exploits, since a static file server might do path.join(docroot, path.normalize("/"+req)) to get the file.  If the normalized request path could be something like "/../../../etc/passwd" then bad things could happen.
  • Loading branch information
isaacs authored and ry committed Aug 2, 2010
1 parent dc8c079 commit 65037ee
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/path.js
Expand Up @@ -14,6 +14,10 @@ exports.normalizeArray = function (parts, keepBlanks) {
// if it's a dot, and there was some previous dir already, then skip it. // if it's a dot, and there was some previous dir already, then skip it.
if (directory === "." && prev !== undefined) continue; if (directory === "." && prev !== undefined) continue;


// if it starts with "", and is a . or .., then skip it.
if (directories.length === 1 && directories[0] === "" && (
directory === "." || directory === "..")) continue;

if ( if (
directory === ".." directory === ".."
&& directories.length && directories.length
Expand Down
2 changes: 2 additions & 0 deletions test/simple/test-path.js
Expand Up @@ -38,9 +38,11 @@ assert.equal(path.extname("file.ext.ext"), ".ext");
assert.equal(path.extname("file."), "."); assert.equal(path.extname("file."), ".");


assert.equal(path.join(".", "fixtures/b", "..", "/b/c.js"), "fixtures/b/c.js"); assert.equal(path.join(".", "fixtures/b", "..", "/b/c.js"), "fixtures/b/c.js");
assert.equal(path.join("/foo", "../../../bar"), "/bar");


assert.equal(path.normalize("./fixtures///b/../b/c.js"), "fixtures/b/c.js"); assert.equal(path.normalize("./fixtures///b/../b/c.js"), "fixtures/b/c.js");
assert.equal(path.normalize("./fixtures///b/../b/c.js",true), "fixtures///b/c.js"); assert.equal(path.normalize("./fixtures///b/../b/c.js",true), "fixtures///b/c.js");
assert.equal(path.normalize("/foo/../../../bar"), "/bar");


assert.deepEqual(path.normalizeArray(["fixtures","b","","..","b","c.js"]), ["fixtures","b","c.js"]); assert.deepEqual(path.normalizeArray(["fixtures","b","","..","b","c.js"]), ["fixtures","b","c.js"]);
assert.deepEqual(path.normalizeArray(["fixtures","","b","..","b","c.js"], true), ["fixtures","","b","c.js"]); assert.deepEqual(path.normalizeArray(["fixtures","","b","..","b","c.js"], true), ["fixtures","","b","c.js"]);
Expand Down

0 comments on commit 65037ee

Please sign in to comment.