Skip to content

Commit 07c9a7b

Browse files
Fix handling of encoded backslash (#192)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 4869023 commit 07c9a7b

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export default function normalizeUrl(urlString, options) {
181181
// Decode URI octets
182182
if (urlObject.pathname) {
183183
try {
184-
urlObject.pathname = decodeURI(urlObject.pathname);
184+
urlObject.pathname = decodeURI(urlObject.pathname).replace(/\\/g, '%5C');
185185
} catch {}
186186
}
187187

test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,11 @@ test('ignore custom schemes', t => {
417417
t.is(normalizeUrl('sindre://www.sindresorhus.com'), 'sindre://www.sindresorhus.com');
418418
t.is(normalizeUrl('foo:bar'), 'foo:bar');
419419
});
420+
421+
test('encoded backslashes do not get decoded', t => {
422+
t.is(normalizeUrl('https://foo.com/some%5Bthing%5Celse/that-is%40great@coding'), 'https://foo.com/some[thing%5Celse/that-is%40great@coding');
423+
t.is(normalizeUrl('https://foo.com/something%5Celse/great'), 'https://foo.com/something%5Celse/great');
424+
425+
// Non-encoded backslashes should remain as-is.
426+
t.is(normalizeUrl('https://foo.com/something\\else/great'), 'https://foo.com/something/else/great');
427+
});

0 commit comments

Comments
 (0)