From 28d7e4b63d564a2b450ed431f7ef45cb9e39c1ee Mon Sep 17 00:00:00 2001 From: Deepak Yadav Date: Tue, 28 May 2019 18:00:52 +0530 Subject: [PATCH] fix: Fix problem where malformed URLs threw an error (#70) --- 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 3dbbd06..de42ebd 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 5d5154c..e87d287 100644 --- a/test/parseDomain.test.js +++ b/test/parseDomain.test.js @@ -146,6 +146,15 @@ describe("parseDomain(url)", () => { expect(parseDomain("\xa0")).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",