Skip to content

Commit b8daaaa

Browse files
committed
Parser: remove leading slashes before extracting path
1 parent d10b23c commit b8daaaa

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

CHANGELOG.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
unreleased:
2+
fixed bugs:
3+
- >-
4+
GH-130 Fixed a bug where extra slashes and backslashes in the protocol are
5+
not handled correctly
26
chores:
37
- Added secure codecov publish script
48
- Updated dependencies

parser/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ function parse (urlString) {
215215

216216
// 4. url.path
217217
urlString = urlString.replace(/\\/g, '/'); // sanitize path
218+
urlString = urlString.replace(/^\/+/, ''); // remove leading slashes
219+
218220
if ((index = urlString.indexOf(PATH_SEPARATOR)) !== -1) {
219221
// extract from the back
220222
url.path.value = urlString.slice(index + 1).split(PATH_SEPARATOR);

test/unit/parser/parser.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,42 @@ describe('parser', function () {
228228
});
229229
});
230230

231+
it('should handle extra slashes after protocol', function () {
232+
expect(parser.parse('http:////localhost')).to.deep.include({
233+
raw: 'http:////localhost',
234+
protocol: 'http',
235+
host: ['localhost'],
236+
path: undefined
237+
});
238+
});
239+
240+
it('should handle extra backslashes after protocol', function () {
241+
expect(parser.parse('http:\\\\\\\\localhost')).to.deep.include({
242+
raw: 'http:\\\\\\\\localhost',
243+
protocol: 'http',
244+
host: ['localhost'],
245+
path: undefined
246+
});
247+
});
248+
249+
it('should handle leading slashes', function () {
250+
expect(parser.parse('//localhost/foo')).to.deep.include({
251+
raw: '//localhost/foo',
252+
protocol: undefined,
253+
host: ['localhost'],
254+
path: ['foo']
255+
});
256+
});
257+
258+
it('should handle leading backslashes', function () {
259+
expect(parser.parse('\\\\localhost\\foo')).to.deep.include({
260+
raw: '\\\\localhost\\foo',
261+
protocol: undefined,
262+
host: ['localhost'],
263+
path: ['foo']
264+
});
265+
});
266+
231267
it('should return default object for empty string input', function () {
232268
expect(parser.parse('')).to.deep.include(defaultObject);
233269
expect(parser.parse(' ')).to.deep.include(defaultObject);

test/unit/toNodeUrl.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,5 +705,49 @@ describe('.toNodeUrl', function () {
705705
hostname: 'postman.com`f.society.org'
706706
});
707707
});
708+
709+
// Refer: https://huntr.dev/bounties/1625732310186-postmanlabs/postman-url-encoder/
710+
it('should handle extra backslashes in protocol', function () {
711+
expect(toNodeUrl('https:////example.com/foo/bar')).to.include({
712+
protocol: 'https:',
713+
host: 'example.com',
714+
hostname: 'example.com',
715+
pathname: '/foo/bar',
716+
href: 'https://example.com/foo/bar'
717+
});
718+
719+
expect(toNodeUrl('https:\\\\\\example.com/foo/bar')).to.include({
720+
protocol: 'https:',
721+
host: 'example.com',
722+
hostname: 'example.com',
723+
pathname: '/foo/bar',
724+
href: 'https://example.com/foo/bar'
725+
});
726+
727+
expect(toNodeUrl('https:///\\example.com/foo/bar')).to.include({
728+
protocol: 'https:',
729+
host: 'example.com',
730+
hostname: 'example.com',
731+
pathname: '/foo/bar',
732+
href: 'https://example.com/foo/bar'
733+
});
734+
735+
expect(toNodeUrl('////example.com/foo/bar')).to.include({
736+
protocol: 'http:',
737+
host: 'example.com',
738+
hostname: 'example.com',
739+
pathname: '/foo/bar',
740+
href: 'http://example.com/foo/bar'
741+
});
742+
743+
// eslint-disable-next-line no-useless-escape
744+
expect(toNodeUrl('https:/\/\/\example.com/foo/bar')).to.include({
745+
protocol: 'https:',
746+
host: 'example.com',
747+
hostname: 'example.com',
748+
pathname: '/foo/bar',
749+
href: 'https://example.com/foo/bar'
750+
});
751+
});
708752
});
709753
});

0 commit comments

Comments
 (0)