Skip to content

Commit 6eda259

Browse files
authored
fix: Check parseUrl for correct input type (#1190)
1 parent ddd1200 commit 6eda259

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ function urlencode(o) {
185185
// intentionally using regex and not <a/> href parsing trick because React Native and other
186186
// environments where DOM might not be available
187187
function parseUrl(url) {
188+
if (typeof url !== 'string') return {};
188189
var match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
189-
if (!match) return {};
190190

191191
// coerce to undefined values to empty string so we don't get 'undefined'
192192
var query = match[6] || '';

test/utils.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,15 @@ describe('utils', function() {
353353
// from example.com => example.com/example.com/foo (valid url).
354354
});
355355
});
356+
357+
it('should return an empty object for invalid input', function() {
358+
assert.deepEqual(parseUrl(), {});
359+
assert.deepEqual(parseUrl(42), {});
360+
assert.deepEqual(parseUrl([]), {});
361+
assert.deepEqual(parseUrl({}), {});
362+
assert.deepEqual(parseUrl(null), {});
363+
assert.deepEqual(parseUrl(undefined), {});
364+
});
356365
});
357366

358367
describe('wrappedCallback', function() {

0 commit comments

Comments
 (0)