Improve URL parsing (#411)

* Check hostname is valid in getDomain

* fix linting

* Update noop implementation

* Fix tests

* Fix tests
This commit is contained in:
Thomas Rittson 2021-06-23 06:00:14 +10:00 committed by GitHub
parent 18bf616e2e
commit 9ee31ad2fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 0 deletions

View File

@ -1,3 +1,7 @@
export function getDomain(host: string): string | null { export function getDomain(host: string): string | null {
return null; return null;
} }
export function isValid(host: string): boolean {
return true;
}

View File

@ -221,6 +221,11 @@ export class Utils {
if (httpUrl) { if (httpUrl) {
try { try {
const url = Utils.getUrlObject(uriString); 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)) { if (url.hostname === 'localhost' || Utils.validIpAddress(url.hostname)) {
return url.hostname; return url.hostname;
} }

View File

@ -33,6 +33,11 @@ describe('Utils Service', () => {
expect(Utils.getDomain('https://localhost')).toBe('localhost'); expect(Utils.getDomain('https://localhost')).toBe('localhost');
expect(Utils.getDomain('https://192.168.1.1')).toBe('192.168.1.1'); 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', () => { describe('getHostname', () => {