Skip to content

Commit 28d7e4b

Browse files
dydeepak97jhnns
authored andcommitted
fix: Fix problem where malformed URLs threw an error (#70)
1 parent d15444c commit 28d7e4b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/parseDomain.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@ function parseDomain(url, options) {
6262

6363
const normalizedOptions = normalize.options(options);
6464

65-
// urlSplit can't be null because urlParts will always match at the third capture
6665
urlSplit = normalizedUrl.match(urlParts);
66+
67+
// urlSplit is null if the url contains certain characters like '\n', '\r'.
68+
if (urlSplit === null) {
69+
return null;
70+
}
71+
6772
domain = urlSplit[3]; // domain will now be something like sub.domain.example.com
6873

6974
tld = matchTld(domain, normalizedOptions);

test/parseDomain.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ describe("parseDomain(url)", () => {
146146
expect(parseDomain("\xa0")).to.equal(null);
147147
});
148148

149+
it("should return null if the given value contains invalid characters", () => {
150+
expect(parseDomain("http://hell.d\ne.ibm.com")).to.equal(null);
151+
expect(parseDomain("\xa0")).to.equal(null);
152+
});
153+
154+
it("should return null if the given is an empty string with a space character", () => {
155+
expect(parseDomain(" ")).to.equal(null);
156+
});
157+
149158
it("should work with domains that could match multiple tlds", () => {
150159
expect(parseDomain("http://hello.de.ibm.com")).to.eql({
151160
subdomain: "hello.de",

0 commit comments

Comments
 (0)