Skip to content

Commit b5a798a

Browse files
committed
url: update encodePathChars to handle special characters properly
1 parent a21af4b commit b5a798a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/internal/url.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,11 +1518,20 @@ const carriageReturnRegEx = /\r/g;
15181518
const tabRegEx = /\t/g;
15191519
const questionRegex = /\?/g;
15201520
const hashRegex = /#/g;
1521+
const leftBracketRegEx = /\[/g;
1522+
const rightBracketRegEx = /\]/g;
1523+
const caretRegEx = /\^/g;
15211524

15221525
function encodePathChars(filepath, options = kEmptyObject) {
15231526
const windows = options?.windows;
15241527
if (StringPrototypeIndexOf(filepath, '%') !== -1)
15251528
filepath = RegExpPrototypeSymbolReplace(percentRegEx, filepath, '%25');
1529+
if (StringPrototypeIndexOf(filepath, '[') !== -1)
1530+
filepath = RegExpPrototypeSymbolReplace(leftBracketRegEx, filepath, '%5B');
1531+
if (StringPrototypeIndexOf(filepath, ']') !== -1)
1532+
filepath = RegExpPrototypeSymbolReplace(rightBracketRegEx, filepath, '%5D');
1533+
if (StringPrototypeIndexOf(filepath, '^') !== -1)
1534+
filepath = RegExpPrototypeSymbolReplace(caretRegEx, filepath, '%5E');
15261535
// In posix, backslash is a valid character in paths:
15271536
if (!(windows ?? isWindows) && StringPrototypeIndexOf(filepath, '\\') !== -1)
15281537
filepath = RegExpPrototypeSymbolReplace(backslashRegEx, filepath, '%5C');

test/parallel/test-url-pathtofileurl.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ const windowsTestCases = [
8888
{ path: 'C:\\foo;bar', expected: 'file:///C:/foo;bar' },
8989
// percent
9090
{ path: 'C:\\foo%bar', expected: 'file:///C:/foo%25bar' },
91+
// caret
92+
{ path: 'C:\\foo^bar', expected: 'file:///C:/foo%5Ebar' },
93+
// left bracket
94+
{ path: 'C:\\foo[bar', expected: 'file:///C:/foo%5Bbar' },
95+
// right bracket
96+
{ path: 'C:\\foo]bar', expected: 'file:///C:/foo%5Dbar' },
9197
// backslash
9298
{ path: 'C:\\foo\\bar', expected: 'file:///C:/foo/bar' },
9399
// backspace
@@ -138,6 +144,12 @@ const posixTestCases = [
138144
{ path: '/foo;bar', expected: 'file:///foo;bar' },
139145
// percent
140146
{ path: '/foo%bar', expected: 'file:///foo%25bar' },
147+
// caret
148+
{ path: '/foo^bar', expected: 'file:///foo%5Ebar' },
149+
// left bracket
150+
{ path: '/foo[bar', expected: 'file:///foo%5Bbar' },
151+
// right bracket
152+
{ path: '/foo]bar', expected: 'file:///foo%5Dbar' },
141153
// backslash
142154
{ path: '/foo\\bar', expected: 'file:///foo%5Cbar' },
143155
// backspace

0 commit comments

Comments
 (0)