Improve URL parsing (#411)
* Check hostname is valid in getDomain * fix linting * Update noop implementation * Fix tests * Fix tests
This commit is contained in:
parent
18bf616e2e
commit
9ee31ad2fb
|
@ -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;
|
||||||
|
}
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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', () => {
|
||||||
|
|
Loading…
Reference in New Issue