From 13da7f14336f101ceac0062ab5b8d31c6187864c Mon Sep 17 00:00:00 2001 From: Ilkka Myller Date: Thu, 11 Aug 2016 23:17:16 +0300 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/8064 PR-URL: https://github.com/nodejs/node/pull/8072 Reviewed-By: James M Snell Conflicts: test/parallel/test-url.js --- lib/url.js | 2 +- test/parallel/test-url.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/url.js b/lib/url.js index cdfe1221344559..5f4b1cf8ccbd78 100644 --- a/lib/url.js +++ b/lib/url.js @@ -619,7 +619,7 @@ Url.prototype.format = function() { host = ''; } - search = search.replace('#', '%23'); + search = search.replace(/#/g, '%23'); if (hash && hash.charCodeAt(0) !== 35/*#*/) hash = '#' + hash; if (search && search.charCodeAt(0) !== 63/*?*/) search = '?' + search; diff --git a/test/parallel/test-url.js b/test/parallel/test-url.js index 6e0ab8be988099..9e047ef7008bac 100644 --- a/test/parallel/test-url.js +++ b/test/parallel/test-url.js @@ -1165,6 +1165,19 @@ var formatTests = { hash: '#frag', search: '?abc=the#1?&foo=bar', pathname: '/fooA100%mBr', + }, + + // multiple `#` in search + 'http://example.com/?foo=bar%231%232%233&abc=%234%23%235#frag': { + href: 'http://example.com/?foo=bar%231%232%233&abc=%234%23%235#frag', + protocol: 'http:', + slashes: true, + host: 'example.com', + hostname: 'example.com', + hash: '#frag', + search: '?foo=bar#1#2#3&abc=#4##5', + query: {}, + pathname: '/' } }; for (const u in formatTests) {