Skip to content

Commit

Permalink
fix: Fix problem where malformed URLs threw an error (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
dydeepak97 authored and jhnns committed May 28, 2019
1 parent d15444c commit 28d7e4b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/parseDomain.js
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions test/parseDomain.test.js
Expand Up @@ -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",
Expand Down

0 comments on commit 28d7e4b

Please sign in to comment.