Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Improve URL parsing (#411)
Browse files Browse the repository at this point in the history
* Check hostname is valid in getDomain

* fix linting

* Update noop implementation

* Fix tests

* Fix tests
  • Loading branch information
eliykat committed Jun 22, 2021
1 parent 18bf616 commit 9ee31ad
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions common/src/misc/tldjs.noop.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function getDomain(host: string): string | null {
return null;
}

export function isValid(host: string): boolean {
return true;
}
5 changes: 5 additions & 0 deletions common/src/misc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ export class Utils {
if (httpUrl) {
try {
const url = Utils.getUrlObject(uriString);
const validHostname = tldjs?.isValid != null ? tldjs.isValid(url.hostname) : true;
if (!validHostname) {
return null;
}

if (url.hostname === 'localhost' || Utils.validIpAddress(url.hostname)) {
return url.hostname;
}
Expand Down
5 changes: 5 additions & 0 deletions spec/common/misc/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ describe('Utils Service', () => {
expect(Utils.getDomain('https://localhost')).toBe('localhost');
expect(Utils.getDomain('https://192.168.1.1')).toBe('192.168.1.1');
});

it('should reject invalid hostnames', () => {
expect(Utils.getDomain('https://mywebsite.com$.mywebsite.com')).toBeNull();
expect(Utils.getDomain('https://mywebsite.com!.mywebsite.com')).toBeNull();
});
});

describe('getHostname', () => {
Expand Down

0 comments on commit 9ee31ad

Please sign in to comment.