1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-21 18:53:29 +00:00

getDomain moved to jslib

This commit is contained in:
Kyle Spearrin
2018-10-13 22:52:49 -04:00
parent 9320d73c99
commit 48a0b84bfe
7 changed files with 18 additions and 90 deletions

View File

@@ -3,35 +3,6 @@ import BrowserPlatformUtilsService from './browserPlatformUtils.service';
import { DeviceType } from 'jslib/enums';
describe('Browser Utils Service', () => {
describe('getDomain', () => {
it('should fail for invalid urls', () => {
expect(BrowserPlatformUtilsService.getDomain(null)).toBeNull();
expect(BrowserPlatformUtilsService.getDomain(undefined)).toBeNull();
expect(BrowserPlatformUtilsService.getDomain(' ')).toBeNull();
expect(BrowserPlatformUtilsService.getDomain('https://bit!:"_&ward.com')).toBeNull();
expect(BrowserPlatformUtilsService.getDomain('bitwarden')).toBeNull();
});
it('should handle urls without protocol', () => {
expect(BrowserPlatformUtilsService.getDomain('bitwarden.com')).toBe('bitwarden.com');
expect(BrowserPlatformUtilsService.getDomain('wrong://bitwarden.com')).toBe('bitwarden.com');
});
it('should handle valid urls', () => {
expect(BrowserPlatformUtilsService.getDomain('https://bitwarden')).toBe('bitwarden');
expect(BrowserPlatformUtilsService.getDomain('https://bitwarden.com')).toBe('bitwarden.com');
expect(BrowserPlatformUtilsService.getDomain('http://bitwarden.com')).toBe('bitwarden.com');
expect(BrowserPlatformUtilsService.getDomain('http://vault.bitwarden.com')).toBe('bitwarden.com');
expect(BrowserPlatformUtilsService.getDomain('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com');
expect(BrowserPlatformUtilsService.getDomain('https://bitwarden.unknown')).toBe('bitwarden.unknown');
});
it('should support localhost and IP', () => {
expect(BrowserPlatformUtilsService.getDomain('https://localhost')).toBe('localhost');
expect(BrowserPlatformUtilsService.getDomain('https://192.168.1.1')).toBe('192.168.1.1');
});
});
describe('getBrowser', () => {
const originalUserAgent = navigator.userAgent;
const originalSafari = (window as any).safari;
@@ -55,7 +26,7 @@ describe('Browser Utils Service', () => {
configurable: true,
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.ChromeExtension);
});
@@ -65,7 +36,7 @@ describe('Browser Utils Service', () => {
configurable: true,
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.FirefoxExtension);
});
@@ -80,7 +51,7 @@ describe('Browser Utils Service', () => {
configurable: true,
value: {}
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.OperaExtension);
});
@@ -90,7 +61,7 @@ describe('Browser Utils Service', () => {
configurable: true,
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 9) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.EdgeExtension);
});

View File

@@ -1,5 +1,3 @@
import * as tldjs from 'tldjs';
import { BrowserApi } from '../browser/browserApi';
import { DeviceType } from 'jslib/enums/deviceType';
@@ -12,43 +10,6 @@ import { AnalyticsIds } from 'jslib/misc/analytics';
const DialogPromiseExpiration = 600000; // 10 minutes
export default class BrowserPlatformUtilsService implements PlatformUtilsService {
static getDomain(uriString: string): string {
if (uriString == null) {
return null;
}
uriString = uriString.trim();
if (uriString === '') {
return null;
}
if (uriString.startsWith('http://') || uriString.startsWith('https://')) {
try {
const url = new URL(uriString);
if (url.hostname === 'localhost' || BrowserPlatformUtilsService.validIpAddress(url.hostname)) {
return url.hostname;
}
const urlDomain = tldjs.getDomain(url.hostname);
return urlDomain != null ? urlDomain : url.hostname;
} catch (e) { }
}
const domain = tldjs.getDomain(uriString);
if (domain != null) {
return domain;
}
return null;
}
private static validIpAddress(ipString: string): boolean {
// tslint:disable-next-line
const ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
return ipRegex.test(ipString);
}
identityClientId: string = 'browser';
private showDialogResolves = new Map<number, { resolve: (value: boolean) => void, date: Date }>();
@@ -127,10 +88,6 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
return this.analyticsIdCache;
}
getDomain(uriString: string): string {
return BrowserPlatformUtilsService.getDomain(uriString);
}
isViewOpen(): boolean {
if (BrowserApi.isPopupOpen()) {
return true;