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",