Skip to content

Commit 1d0385f

Browse files
imyllerjasnell
authored andcommitted
url: url.format() encodes all # in search
This commit fixes an error where only the first occurrence of `#` in `search` parameter is URL encoded, and subsequent occurrences are not. Also added a test for the case. Fixes: #8064 PR-URL: #8072 Reviewed-By: James M Snell <[email protected]>
1 parent 0abdf59 commit 1d0385f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ Url.prototype.format = function() {
624624
}
625625
}
626626

627-
search = search.replace('#', '%23');
627+
search = search.replace(/#/g, '%23');
628628

629629
if (hash && hash.charCodeAt(0) !== 35/*#*/) hash = '#' + hash;
630630
if (search && search.charCodeAt(0) !== 63/*?*/) search = '?' + search;

test/parallel/test-url.js

+13
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,19 @@ var formatTests = {
11761176
pathname: '/fooA100%mBr',
11771177
},
11781178

1179+
// multiple `#` in search
1180+
'http://example.com/?foo=bar%231%232%233&abc=%234%23%235#frag': {
1181+
href: 'http://example.com/?foo=bar%231%232%233&abc=%234%23%235#frag',
1182+
protocol: 'http:',
1183+
slashes: true,
1184+
host: 'example.com',
1185+
hostname: 'example.com',
1186+
hash: '#frag',
1187+
search: '?foo=bar#1#2#3&abc=#4##5',
1188+
query: {},
1189+
pathname: '/'
1190+
},
1191+
11791192
// https://github.com/nodejs/node/issues/3361
11801193
'file:///home/user': {
11811194
href: 'file:///home/user',

0 commit comments

Comments
 (0)