Skip to content

Commit

Permalink
path: fix normalize on directories with two dots
Browse files Browse the repository at this point in the history
PR-URL: #14107
Fixes: #14105
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information
targos authored and MylesBorins committed Oct 25, 2017
1 parent a0b3805 commit f87a626
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function normalizeStringWin32(path, allowAboveRoot) {
var lastSlash = -1;
var dots = 0;
var code;
var isAboveRoot = false;
for (var i = 0; i <= path.length; ++i) {
if (i < path.length)
code = path.charCodeAt(i);
Expand All @@ -25,7 +26,7 @@ function normalizeStringWin32(path, allowAboveRoot) {
if (lastSlash === i - 1 || dots === 1) {
// NOOP
} else if (lastSlash !== i - 1 && dots === 2) {
if (res.length < 2 ||
if (res.length < 2 || !isAboveRoot ||
res.charCodeAt(res.length - 1) !== 46/*.*/ ||
res.charCodeAt(res.length - 2) !== 46/*.*/) {
if (res.length > 2) {
Expand All @@ -42,12 +43,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
res = res.slice(0, j);
lastSlash = i;
dots = 0;
isAboveRoot = false;
continue;
}
} else if (res.length === 2 || res.length === 1) {
res = '';
lastSlash = i;
dots = 0;
isAboveRoot = false;
continue;
}
}
Expand All @@ -56,12 +59,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
res += '\\..';
else
res = '..';
isAboveRoot = true;
}
} else {
if (res.length > 0)
res += '\\' + path.slice(lastSlash + 1, i);
else
res = path.slice(lastSlash + 1, i);
isAboveRoot = false;
}
lastSlash = i;
dots = 0;
Expand All @@ -80,6 +85,7 @@ function normalizeStringPosix(path, allowAboveRoot) {
var lastSlash = -1;
var dots = 0;
var code;
var isAboveRoot = false;
for (var i = 0; i <= path.length; ++i) {
if (i < path.length)
code = path.charCodeAt(i);
Expand All @@ -91,7 +97,7 @@ function normalizeStringPosix(path, allowAboveRoot) {
if (lastSlash === i - 1 || dots === 1) {
// NOOP
} else if (lastSlash !== i - 1 && dots === 2) {
if (res.length < 2 ||
if (res.length < 2 || !isAboveRoot ||
res.charCodeAt(res.length - 1) !== 46/*.*/ ||
res.charCodeAt(res.length - 2) !== 46/*.*/) {
if (res.length > 2) {
Expand All @@ -108,12 +114,14 @@ function normalizeStringPosix(path, allowAboveRoot) {
res = res.slice(0, j);
lastSlash = i;
dots = 0;
isAboveRoot = false;
continue;
}
} else if (res.length === 2 || res.length === 1) {
res = '';
lastSlash = i;
dots = 0;
isAboveRoot = false;
continue;
}
}
Expand All @@ -122,12 +130,14 @@ function normalizeStringPosix(path, allowAboveRoot) {
res += '/..';
else
res = '..';
isAboveRoot = true;
}
} else {
if (res.length > 0)
res += '/' + path.slice(lastSlash + 1, i);
else
res = path.slice(lastSlash + 1, i);
isAboveRoot = false;
}
lastSlash = i;
dots = 0;
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ assert.strictEqual(path.win32.normalize('C:..\\..\\abc\\..\\def'),
'C:..\\..\\def');
assert.strictEqual(path.win32.normalize('C:\\.'), 'C:\\');
assert.strictEqual(path.win32.normalize('file:stream'), 'file:stream');
assert.strictEqual(path.win32.normalize('bar\\foo..\\..\\'), 'bar\\');
assert.strictEqual(path.win32.normalize('bar\\foo..\\..'), 'bar');
assert.strictEqual(path.win32.normalize('bar\\foo..\\..\\baz'), 'bar\\baz');
assert.strictEqual(path.win32.normalize('bar\\foo..\\'), 'bar\\foo..\\');
assert.strictEqual(path.win32.normalize('bar\\foo..'), 'bar\\foo..');

assert.strictEqual(path.posix.normalize('./fixtures///b/../b/c.js'),
'fixtures/b/c.js');
Expand All @@ -420,6 +425,11 @@ assert.strictEqual(path.posix.normalize('a//b//./c'), 'a/b/c');
assert.strictEqual(path.posix.normalize('a//b//.'), 'a/b');
assert.strictEqual(path.posix.normalize('/a/b/c/../../../x/y/z'), '/x/y/z');
assert.strictEqual(path.posix.normalize('///..//./foo/.//bar'), '/foo/bar');
assert.strictEqual(path.posix.normalize('bar/foo../../'), 'bar/');
assert.strictEqual(path.posix.normalize('bar/foo../..'), 'bar');
assert.strictEqual(path.posix.normalize('bar/foo../../baz'), 'bar/baz');
assert.strictEqual(path.posix.normalize('bar/foo../'), 'bar/foo../');
assert.strictEqual(path.posix.normalize('bar/foo..'), 'bar/foo..');


// path.resolve tests
Expand Down

0 comments on commit f87a626

Please sign in to comment.