From a8e09543c86769073927a79779cb9e1641c0580c Mon Sep 17 00:00:00 2001 From: dydeepak97 Date: Thu, 23 May 2019 01:47:27 +0530 Subject: [PATCH] Added check to see if urlSplit is null or not --- src/parseDomain.js | 7 ++++++- test/parseDomain.test.js | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/parseDomain.js b/src/parseDomain.js index aef72f2..b7181d3 100644 --- a/src/parseDomain.js +++ b/src/parseDomain.js @@ -62,8 +62,13 @@ function parseDomain(url, options) { const normalizedOptions = normalize.options(options); - // urlSplit can't be null because urlParts will always match at the third capture urlSplit = normalizedUrl.match(urlParts); + + // urlSplit is null if the url contains certain characters like '\n', '\r'. + if (urlSplit === null) { + return null; + } + domain = urlSplit[3]; // domain will now be something like sub.domain.example.com tld = matchTld(domain, normalizedOptions); diff --git a/test/parseDomain.test.js b/test/parseDomain.test.js index afa927a..9efd281 100644 --- a/test/parseDomain.test.js +++ b/test/parseDomain.test.js @@ -144,6 +144,15 @@ describe("parseDomain(url)", () => { expect(parseDomain("")).to.equal(null); }); + it("should return null if the given value contains invalid characters", () => { + expect(parseDomain("http://hell.d\ne.ibm.com")).to.equal(null); + expect(parseDomain("\xa0")).to.equal(null); + }); + + it("should return null if the given is an empty string with a space character", () => { + expect(parseDomain(" ")).to.equal(null); + }); + it("should work with domains that could match multiple tlds", () => { expect(parseDomain("http://hello.de.ibm.com")).to.eql({ subdomain: "hello.de",