mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 19:23:52 +00:00
separated BrowserUtils from generic Utils
This commit is contained in:
17
src/services/abstractions/browserUtils.service.ts
Normal file
17
src/services/abstractions/browserUtils.service.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { BrowserType } from '../../enums/browserType.enum';
|
||||
|
||||
export interface BrowserUtilsService {
|
||||
getBrowser(): BrowserType;
|
||||
getBrowserString(): string;
|
||||
isFirefox(): boolean;
|
||||
isChrome(): boolean;
|
||||
isEdge(): boolean;
|
||||
isOpera(): boolean;
|
||||
analyticsId(): string;
|
||||
initListSectionItemListeners(doc: Document, angular: any): void;
|
||||
getDomain(uriString: string): string;
|
||||
inSidebar(theWindow: Window): boolean;
|
||||
inTab(theWindow: Window): boolean;
|
||||
inPopout(theWindow: Window): boolean;
|
||||
inPopup(theWindow: Window): boolean;
|
||||
}
|
||||
@@ -1,19 +1,4 @@
|
||||
import { BrowserType } from '../../enums/browserType.enum';
|
||||
|
||||
export interface UtilsService {
|
||||
getBrowser(): BrowserType;
|
||||
getBrowserString(): string;
|
||||
isFirefox(): boolean;
|
||||
isChrome(): boolean;
|
||||
isEdge(): boolean;
|
||||
isOpera(): boolean;
|
||||
analyticsId(): string;
|
||||
initListSectionItemListeners(doc: Document, angular: any): void;
|
||||
copyToClipboard(text: string, doc?: Document): void;
|
||||
getDomain(uriString: string): string;
|
||||
getHostname(uriString: string): string;
|
||||
inSidebar(theWindow: Window): boolean;
|
||||
inTab(theWindow: Window): boolean;
|
||||
inPopout(theWindow: Window): boolean;
|
||||
inPopup(theWindow: Window): boolean;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import AppIdService from './appId.service';
|
||||
import BrowserUtilsService from './browserUtils.service';
|
||||
import ConstantsService from './constants.service';
|
||||
import TokenService from './token.service';
|
||||
import UtilsService from './utils.service';
|
||||
|
||||
import EnvironmentUrls from '../models/domain/environmentUrls';
|
||||
|
||||
@@ -32,11 +32,13 @@ export default class ApiService {
|
||||
urlsSet: boolean = false;
|
||||
baseUrl: string;
|
||||
identityBaseUrl: string;
|
||||
deviceType: string;
|
||||
logoutCallback: Function;
|
||||
|
||||
constructor(private tokenService: TokenService, private utilsService: UtilsService,
|
||||
constructor(private tokenService: TokenService, browserUtilsService: BrowserUtilsService,
|
||||
logoutCallback: Function) {
|
||||
this.logoutCallback = logoutCallback;
|
||||
this.deviceType = browserUtilsService.getBrowser().toString();
|
||||
}
|
||||
|
||||
setUrls(urls: EnvironmentUrls) {
|
||||
@@ -86,7 +88,7 @@ export default class ApiService {
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
|
||||
'Accept': 'application/json',
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'POST',
|
||||
}));
|
||||
@@ -126,7 +128,7 @@ export default class ApiService {
|
||||
cache: 'no-cache',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'POST',
|
||||
}));
|
||||
@@ -146,7 +148,7 @@ export default class ApiService {
|
||||
headers: new Headers({
|
||||
'Accept': 'application/json',
|
||||
'Authorization': authHeader,
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
}));
|
||||
|
||||
@@ -164,7 +166,7 @@ export default class ApiService {
|
||||
cache: 'no-cache',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'POST',
|
||||
}));
|
||||
@@ -181,7 +183,7 @@ export default class ApiService {
|
||||
cache: 'no-cache',
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'POST',
|
||||
}));
|
||||
@@ -203,7 +205,7 @@ export default class ApiService {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': authHeader,
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'POST',
|
||||
}));
|
||||
@@ -226,7 +228,7 @@ export default class ApiService {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': authHeader,
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'PUT',
|
||||
}));
|
||||
@@ -246,7 +248,7 @@ export default class ApiService {
|
||||
cache: 'no-cache',
|
||||
headers: new Headers({
|
||||
'Authorization': authHeader,
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'DELETE',
|
||||
}));
|
||||
@@ -268,7 +270,7 @@ export default class ApiService {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': authHeader,
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'POST',
|
||||
}));
|
||||
@@ -291,7 +293,7 @@ export default class ApiService {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': authHeader,
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'PUT',
|
||||
}));
|
||||
@@ -311,7 +313,7 @@ export default class ApiService {
|
||||
cache: 'no-cache',
|
||||
headers: new Headers({
|
||||
'Authorization': authHeader,
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'DELETE',
|
||||
}));
|
||||
@@ -332,7 +334,7 @@ export default class ApiService {
|
||||
headers: new Headers({
|
||||
'Accept': 'application/json',
|
||||
'Authorization': authHeader,
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'POST',
|
||||
}));
|
||||
@@ -352,7 +354,7 @@ export default class ApiService {
|
||||
cache: 'no-cache',
|
||||
headers: new Headers({
|
||||
'Authorization': authHeader,
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'DELETE',
|
||||
}));
|
||||
@@ -372,7 +374,7 @@ export default class ApiService {
|
||||
headers: new Headers({
|
||||
'Accept': 'application/json',
|
||||
'Authorization': authHeader,
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
}));
|
||||
|
||||
@@ -430,7 +432,7 @@ export default class ApiService {
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
|
||||
'Accept': 'application/json',
|
||||
'Device-Type': this.utilsService.getBrowser().toString(),
|
||||
'Device-Type': this.deviceType,
|
||||
}),
|
||||
method: 'POST',
|
||||
}));
|
||||
|
||||
@@ -5,6 +5,7 @@ import AutofillField from '../models/domain/autofillField';
|
||||
import AutofillPageDetails from '../models/domain/autofillPageDetails';
|
||||
import AutofillScript from '../models/domain/autofillScript';
|
||||
|
||||
import BrowserUtilsService from './browserUtils.service';
|
||||
import CipherService from './cipher.service';
|
||||
import TokenService from './token.service';
|
||||
import TotpService from './totp.service';
|
||||
@@ -92,7 +93,8 @@ var IsoProvinces: { [id: string]: string; } = {
|
||||
|
||||
export default class AutofillService {
|
||||
constructor(public cipherService: CipherService, public tokenService: TokenService,
|
||||
public totpService: TotpService, public utilsService: UtilsService) {
|
||||
public totpService: TotpService, public utilsService: UtilsService,
|
||||
public browserUtilsService: BrowserUtilsService) {
|
||||
}
|
||||
|
||||
getFormsWithPasswordFields(pageDetails: AutofillPageDetails): any[] {
|
||||
@@ -167,7 +169,7 @@ export default class AutofillService {
|
||||
}, { frameId: pd.frameId });
|
||||
|
||||
if (options.cipher.type !== CipherType.Login || totpPromise ||
|
||||
(options.fromBackground && this.utilsService.isFirefox()) || options.skipTotp ||
|
||||
(options.fromBackground && this.browserUtilsService.isFirefox()) || options.skipTotp ||
|
||||
!options.cipher.login.totp || !this.tokenService.getPremium()) {
|
||||
return;
|
||||
}
|
||||
@@ -205,7 +207,7 @@ export default class AutofillService {
|
||||
return;
|
||||
}
|
||||
|
||||
const tabDomain = UtilsService.getDomain(tab.url);
|
||||
const tabDomain = BrowserUtilsService.getDomain(tab.url);
|
||||
if (tabDomain == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
84
src/services/browserUtils.service.spec.ts
Normal file
84
src/services/browserUtils.service.spec.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import BrowserUtilsService from './browserUtils.service';
|
||||
import { BrowserType } from '../enums/browserType.enum';
|
||||
|
||||
describe('Browser Utils Service', () => {
|
||||
describe('getDomain', () => {
|
||||
it('should fail for invalid urls', () => {
|
||||
expect(BrowserUtilsService.getDomain(null)).toBeNull();
|
||||
expect(BrowserUtilsService.getDomain(undefined)).toBeNull();
|
||||
expect(BrowserUtilsService.getDomain(' ')).toBeNull();
|
||||
expect(BrowserUtilsService.getDomain('https://bit!:"_&ward.com')).toBeNull();
|
||||
expect(BrowserUtilsService.getDomain('bitwarden')).toBeNull();
|
||||
});
|
||||
|
||||
it('should handle urls without protocol', () => {
|
||||
expect(BrowserUtilsService.getDomain('bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(BrowserUtilsService.getDomain('wrong://bitwarden.com')).toBe('bitwarden.com');
|
||||
});
|
||||
|
||||
it('should handle valid urls', () => {
|
||||
expect(BrowserUtilsService.getDomain('https://bitwarden')).toBe('bitwarden');
|
||||
expect(BrowserUtilsService.getDomain('https://bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(BrowserUtilsService.getDomain('http://bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(BrowserUtilsService.getDomain('http://vault.bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(BrowserUtilsService.getDomain('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com');
|
||||
expect(BrowserUtilsService.getDomain('https://bitwarden.unknown')).toBe('bitwarden.unknown');
|
||||
});
|
||||
|
||||
it('should support localhost and IP', () => {
|
||||
expect(BrowserUtilsService.getDomain('https://localhost')).toBe('localhost');
|
||||
expect(BrowserUtilsService.getDomain('https://192.168.1.1')).toBe('192.168.1.1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getBrowser', () => {
|
||||
const original = navigator.userAgent;
|
||||
|
||||
// Reset the userAgent.
|
||||
afterAll(() => {
|
||||
Object.defineProperty(navigator, 'userAgent', {
|
||||
value: original
|
||||
});
|
||||
});
|
||||
|
||||
it('should detect chrome', () => {
|
||||
Object.defineProperty(navigator, 'userAgent', {
|
||||
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 browserUtilsService = new BrowserUtilsService();
|
||||
expect(browserUtilsService.getBrowser()).toBe(BrowserType.Chrome);
|
||||
});
|
||||
|
||||
it('should detect firefox', () => {
|
||||
Object.defineProperty(navigator, 'userAgent', {
|
||||
configurable: true,
|
||||
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'
|
||||
});
|
||||
|
||||
const browserUtilsService = new BrowserUtilsService();
|
||||
expect(browserUtilsService.getBrowser()).toBe(BrowserType.Firefox);
|
||||
});
|
||||
|
||||
it('should detect opera', () => {
|
||||
Object.defineProperty(navigator, 'userAgent', {
|
||||
configurable: true,
|
||||
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3175.3 Safari/537.36 OPR/49.0.2695.0 (Edition developer)'
|
||||
});
|
||||
|
||||
const browserUtilsService = new BrowserUtilsService();
|
||||
expect(browserUtilsService.getBrowser()).toBe(BrowserType.Opera);
|
||||
});
|
||||
|
||||
it('should detect edge', () => {
|
||||
Object.defineProperty(navigator, 'userAgent', {
|
||||
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 browserUtilsService = new BrowserUtilsService();
|
||||
expect(browserUtilsService.getBrowser()).toBe(BrowserType.Edge);
|
||||
});
|
||||
});
|
||||
});
|
||||
215
src/services/browserUtils.service.ts
Normal file
215
src/services/browserUtils.service.ts
Normal file
@@ -0,0 +1,215 @@
|
||||
import * as tldjs from 'tldjs';
|
||||
import { BrowserType } from '../enums/browserType.enum';
|
||||
import { BrowserUtilsService as BrowserUtilsServiceInterface } from './abstractions/browserUtils.service';
|
||||
|
||||
const AnalyticsIds = {
|
||||
[BrowserType.Chrome]: 'UA-81915606-6',
|
||||
[BrowserType.Firefox]: 'UA-81915606-7',
|
||||
[BrowserType.Opera]: 'UA-81915606-8',
|
||||
[BrowserType.Edge]: 'UA-81915606-9',
|
||||
[BrowserType.Vivaldi]: 'UA-81915606-15',
|
||||
[BrowserType.Safari]: 'UA-81915606-16',
|
||||
};
|
||||
|
||||
export default class BrowserUtilsService implements BrowserUtilsServiceInterface {
|
||||
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' || BrowserUtilsService.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);
|
||||
}
|
||||
|
||||
private browserCache: BrowserType = null;
|
||||
private analyticsIdCache: string = null;
|
||||
|
||||
getBrowser(): BrowserType {
|
||||
if (this.browserCache) {
|
||||
return this.browserCache;
|
||||
}
|
||||
|
||||
if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1) {
|
||||
this.browserCache = BrowserType.Firefox;
|
||||
} else if ((!!(window as any).opr && !!opr.addons) || !!(window as any).opera ||
|
||||
navigator.userAgent.indexOf(' OPR/') >= 0) {
|
||||
this.browserCache = BrowserType.Opera;
|
||||
} else if (navigator.userAgent.indexOf(' Edge/') !== -1) {
|
||||
this.browserCache = BrowserType.Edge;
|
||||
} else if (navigator.userAgent.indexOf(' Vivaldi/') !== -1) {
|
||||
this.browserCache = BrowserType.Vivaldi;
|
||||
} else if ((window as any).chrome) {
|
||||
this.browserCache = BrowserType.Chrome;
|
||||
}
|
||||
|
||||
return this.browserCache;
|
||||
}
|
||||
|
||||
getBrowserString(): string {
|
||||
return BrowserType[this.getBrowser()].toLowerCase();
|
||||
}
|
||||
|
||||
isFirefox(): boolean {
|
||||
return this.getBrowser() === BrowserType.Firefox;
|
||||
}
|
||||
|
||||
isChrome(): boolean {
|
||||
return this.getBrowser() === BrowserType.Chrome;
|
||||
}
|
||||
|
||||
isEdge(): boolean {
|
||||
return this.getBrowser() === BrowserType.Edge;
|
||||
}
|
||||
|
||||
isOpera(): boolean {
|
||||
return this.getBrowser() === BrowserType.Opera;
|
||||
}
|
||||
|
||||
isVivaldi(): boolean {
|
||||
return this.getBrowser() === BrowserType.Vivaldi;
|
||||
}
|
||||
|
||||
isSafari(): boolean {
|
||||
return this.getBrowser() === BrowserType.Safari;
|
||||
}
|
||||
|
||||
analyticsId(): string {
|
||||
if (this.analyticsIdCache) {
|
||||
return this.analyticsIdCache;
|
||||
}
|
||||
|
||||
this.analyticsIdCache = AnalyticsIds[this.getBrowser()];
|
||||
return this.analyticsIdCache;
|
||||
}
|
||||
|
||||
initListSectionItemListeners(doc: Document, angular: any): void {
|
||||
if (!doc) {
|
||||
throw new Error('doc parameter required');
|
||||
}
|
||||
|
||||
const sectionItems = doc.querySelectorAll(
|
||||
'.list-section-item:not([data-bw-events="1"])');
|
||||
const sectionFormItems = doc.querySelectorAll(
|
||||
'.list-section-item:not([data-bw-events="1"]) input, ' +
|
||||
'.list-section-item:not([data-bw-events="1"]) select, ' +
|
||||
'.list-section-item:not([data-bw-events="1"]) textarea');
|
||||
|
||||
sectionItems.forEach((item) => {
|
||||
(item as HTMLElement).dataset.bwEvents = '1';
|
||||
|
||||
item.addEventListener('click', (e) => {
|
||||
if (e.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
|
||||
const el = e.target as HTMLElement;
|
||||
|
||||
// Some elements will already focus properly
|
||||
if (el.tagName != null) {
|
||||
switch (el.tagName.toLowerCase()) {
|
||||
case 'label': case 'input': case 'textarea': case 'select':
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const cell = el.closest('.list-section-item');
|
||||
if (!cell) {
|
||||
return;
|
||||
}
|
||||
|
||||
const textFilter = 'input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"])';
|
||||
const text = cell.querySelectorAll(textFilter + ', textarea');
|
||||
const checkbox = cell.querySelectorAll('input[type="checkbox"]');
|
||||
const select = cell.querySelectorAll('select');
|
||||
|
||||
if (text.length > 0) {
|
||||
(text[0] as HTMLElement).focus();
|
||||
} else if (select.length > 0) {
|
||||
(select[0] as HTMLElement).focus();
|
||||
} else if (checkbox.length > 0) {
|
||||
const cb = checkbox[0] as HTMLInputElement;
|
||||
cb.checked = !cb.checked;
|
||||
if (angular) {
|
||||
angular.element(checkbox[0]).triggerHandler('click');
|
||||
}
|
||||
}
|
||||
}, false);
|
||||
});
|
||||
|
||||
sectionFormItems.forEach((item) => {
|
||||
const itemCell = item.closest('.list-section-item');
|
||||
(itemCell as HTMLElement).dataset.bwEvents = '1';
|
||||
|
||||
item.addEventListener('focus', (e: Event) => {
|
||||
const el = e.target as HTMLElement;
|
||||
const cell = el.closest('.list-section-item');
|
||||
if (!cell) {
|
||||
return;
|
||||
}
|
||||
|
||||
cell.classList.add('active');
|
||||
}, false);
|
||||
|
||||
item.addEventListener('blur', (e: Event) => {
|
||||
const el = e.target as HTMLElement;
|
||||
const cell = el.closest('.list-section-item');
|
||||
if (!cell) {
|
||||
return;
|
||||
}
|
||||
|
||||
cell.classList.remove('active');
|
||||
}, false);
|
||||
});
|
||||
}
|
||||
|
||||
getDomain(uriString: string): string {
|
||||
return BrowserUtilsService.getDomain(uriString);
|
||||
}
|
||||
|
||||
inSidebar(theWindow: Window): boolean {
|
||||
return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=sidebar') > -1;
|
||||
}
|
||||
|
||||
inTab(theWindow: Window): boolean {
|
||||
return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=tab') > -1;
|
||||
}
|
||||
|
||||
inPopout(theWindow: Window): boolean {
|
||||
return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=popout') > -1;
|
||||
}
|
||||
|
||||
inPopup(theWindow: Window): boolean {
|
||||
return theWindow.location.search === '' || theWindow.location.search.indexOf('uilocation=') === -1 ||
|
||||
theWindow.location.search.indexOf('uilocation=popup') > -1;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import UtilsService from './utils.service';
|
||||
import BrowserUtilsService from './browserUtils.service';
|
||||
|
||||
export default class ConstantsService {
|
||||
static readonly environmentUrlsKey: string = 'environmentUrls';
|
||||
@@ -57,8 +57,8 @@ export default class ConstantsService {
|
||||
|
||||
twoFactorProviderInfo: any[];
|
||||
|
||||
constructor(i18nService: any, utilsService: UtilsService) {
|
||||
if (utilsService.isEdge()) {
|
||||
constructor(i18nService: any, browserUtilsService: BrowserUtilsService) {
|
||||
if (browserUtilsService.isEdge()) {
|
||||
// delay for i18n fetch
|
||||
setTimeout(() => {
|
||||
this.bootstrap(i18nService);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import UtilsService from '../services/utils.service';
|
||||
import BrowserUtilsService from '../services/browserUtils.service';
|
||||
|
||||
export default function i18nService(utilsService: UtilsService) {
|
||||
export default function i18nService(browserUtilsService: BrowserUtilsService) {
|
||||
const edgeMessages: any = {};
|
||||
|
||||
if (utilsService.isEdge()) {
|
||||
if (browserUtilsService.isEdge()) {
|
||||
fetch('../_locales/en/messages.json').then((file) => {
|
||||
return file.json();
|
||||
}).then((locales) => {
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import BrowserUtilsService from './browserUtils.service';
|
||||
import CipherService from './cipher.service';
|
||||
import CollectionService from './collection.service';
|
||||
import ConstantsService from './constants.service';
|
||||
import CryptoService from './crypto.service';
|
||||
import FolderService from './folder.service';
|
||||
import UtilsService from './utils.service';
|
||||
|
||||
import { StorageService } from './abstractions/storage.service';
|
||||
|
||||
export default class LockService {
|
||||
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||
private collectionService: CollectionService, private cryptoService: CryptoService,
|
||||
private utilsService: UtilsService, private storageService: StorageService,
|
||||
private browserUtilsService: BrowserUtilsService, private storageService: StorageService,
|
||||
private setIcon: Function, private refreshBadgeAndMenu: Function) {
|
||||
this.checkLock();
|
||||
setInterval(() => this.checkLock(), 10 * 1000); // check every 10 seconds
|
||||
@@ -81,9 +81,9 @@ export default class LockService {
|
||||
// Helpers
|
||||
|
||||
private sidebarViewName(): string {
|
||||
if ((window as any).chrome.sidebarAction && this.utilsService.isFirefox()) {
|
||||
if ((window as any).chrome.sidebarAction && this.browserUtilsService.isFirefox()) {
|
||||
return 'sidebar';
|
||||
} else if (this.utilsService.isOpera() && (typeof opr !== 'undefined') && opr.sidebarAction) {
|
||||
} else if (this.browserUtilsService.isOpera() && (typeof opr !== 'undefined') && opr.sidebarAction) {
|
||||
return 'sidebar_panel';
|
||||
}
|
||||
|
||||
|
||||
@@ -1,36 +1,6 @@
|
||||
import UtilsService from './utils.service';
|
||||
import { BrowserType } from '../enums/browserType.enum';
|
||||
|
||||
describe('Utils Service', () => {
|
||||
describe('getDomain', () => {
|
||||
it('should fail for invalid urls', () => {
|
||||
expect(UtilsService.getDomain(null)).toBeNull();
|
||||
expect(UtilsService.getDomain(undefined)).toBeNull();
|
||||
expect(UtilsService.getDomain(' ')).toBeNull();
|
||||
expect(UtilsService.getDomain('https://bit!:"_&ward.com')).toBeNull();
|
||||
expect(UtilsService.getDomain('bitwarden')).toBeNull();
|
||||
});
|
||||
|
||||
it('should handle urls without protocol', () => {
|
||||
expect(UtilsService.getDomain('bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getDomain('wrong://bitwarden.com')).toBe('bitwarden.com');
|
||||
});
|
||||
|
||||
it('should handle valid urls', () => {
|
||||
expect(UtilsService.getDomain('https://bitwarden')).toBe('bitwarden');
|
||||
expect(UtilsService.getDomain('https://bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getDomain('http://bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getDomain('http://vault.bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getDomain('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getDomain('https://bitwarden.unknown')).toBe('bitwarden.unknown');
|
||||
});
|
||||
|
||||
it('should support localhost and IP', () => {
|
||||
expect(UtilsService.getDomain('https://localhost')).toBe('localhost');
|
||||
expect(UtilsService.getDomain('https://192.168.1.1')).toBe('192.168.1.1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getHostname', () => {
|
||||
it('should fail for invalid urls', () => {
|
||||
expect(UtilsService.getHostname(null)).toBeNull();
|
||||
@@ -59,55 +29,4 @@ describe('Utils Service', () => {
|
||||
expect(UtilsService.newGuid()).toMatch(validGuid);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getBrowser', () => {
|
||||
const original = navigator.userAgent;
|
||||
|
||||
// Reset the userAgent.
|
||||
afterAll(() => {
|
||||
Object.defineProperty(navigator, 'userAgent', {
|
||||
value: original
|
||||
});
|
||||
});
|
||||
|
||||
it('should detect chrome', () => {
|
||||
Object.defineProperty(navigator, 'userAgent', {
|
||||
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 utilsService = new UtilsService();
|
||||
expect(utilsService.getBrowser()).toBe(BrowserType.Chrome);
|
||||
});
|
||||
|
||||
it('should detect firefox', () => {
|
||||
Object.defineProperty(navigator, 'userAgent', {
|
||||
configurable: true,
|
||||
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'
|
||||
});
|
||||
|
||||
const utilsService = new UtilsService();
|
||||
expect(utilsService.getBrowser()).toBe(BrowserType.Firefox);
|
||||
});
|
||||
|
||||
it('should detect opera', () => {
|
||||
Object.defineProperty(navigator, 'userAgent', {
|
||||
configurable: true,
|
||||
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3175.3 Safari/537.36 OPR/49.0.2695.0 (Edition developer)'
|
||||
});
|
||||
|
||||
const utilsService = new UtilsService();
|
||||
expect(utilsService.getBrowser()).toBe(BrowserType.Opera);
|
||||
});
|
||||
|
||||
it('should detect edge', () => {
|
||||
Object.defineProperty(navigator, 'userAgent', {
|
||||
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 utilsService = new UtilsService();
|
||||
expect(utilsService.getBrowser()).toBe(BrowserType.Edge);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,16 +1,5 @@
|
||||
import * as tldjs from 'tldjs';
|
||||
import { BrowserType } from '../enums/browserType.enum';
|
||||
import { UtilsService as UtilsServiceInterface } from './abstractions/utils.service';
|
||||
|
||||
const AnalyticsIds = {
|
||||
[BrowserType.Chrome]: 'UA-81915606-6',
|
||||
[BrowserType.Firefox]: 'UA-81915606-7',
|
||||
[BrowserType.Opera]: 'UA-81915606-8',
|
||||
[BrowserType.Edge]: 'UA-81915606-9',
|
||||
[BrowserType.Vivaldi]: 'UA-81915606-15',
|
||||
[BrowserType.Safari]: 'UA-81915606-16',
|
||||
};
|
||||
|
||||
export default class UtilsService implements UtilsServiceInterface {
|
||||
static copyToClipboard(text: string, doc?: Document): void {
|
||||
doc = doc || document;
|
||||
@@ -136,37 +125,6 @@ export default class UtilsService implements UtilsServiceInterface {
|
||||
return decodeURIComponent(escape(encodedString));
|
||||
}
|
||||
|
||||
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' || UtilsService.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;
|
||||
}
|
||||
|
||||
static getHostname(uriString: string): string {
|
||||
if (uriString == null) {
|
||||
return null;
|
||||
@@ -187,159 +145,6 @@ export default class UtilsService implements UtilsServiceInterface {
|
||||
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);
|
||||
}
|
||||
|
||||
private browserCache: BrowserType = null;
|
||||
private analyticsIdCache: string = null;
|
||||
|
||||
getBrowser(): BrowserType {
|
||||
if (this.browserCache) {
|
||||
return this.browserCache;
|
||||
}
|
||||
|
||||
if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1) {
|
||||
this.browserCache = BrowserType.Firefox;
|
||||
} else if ((!!(window as any).opr && !!opr.addons) || !!(window as any).opera ||
|
||||
navigator.userAgent.indexOf(' OPR/') >= 0) {
|
||||
this.browserCache = BrowserType.Opera;
|
||||
} else if (navigator.userAgent.indexOf(' Edge/') !== -1) {
|
||||
this.browserCache = BrowserType.Edge;
|
||||
} else if (navigator.userAgent.indexOf(' Vivaldi/') !== -1) {
|
||||
this.browserCache = BrowserType.Vivaldi;
|
||||
} else if ((window as any).chrome) {
|
||||
this.browserCache = BrowserType.Chrome;
|
||||
}
|
||||
|
||||
return this.browserCache;
|
||||
}
|
||||
|
||||
getBrowserString(): string {
|
||||
return BrowserType[this.getBrowser()].toLowerCase();
|
||||
}
|
||||
|
||||
isFirefox(): boolean {
|
||||
return this.getBrowser() === BrowserType.Firefox;
|
||||
}
|
||||
|
||||
isChrome(): boolean {
|
||||
return this.getBrowser() === BrowserType.Chrome;
|
||||
}
|
||||
|
||||
isEdge(): boolean {
|
||||
return this.getBrowser() === BrowserType.Edge;
|
||||
}
|
||||
|
||||
isOpera(): boolean {
|
||||
return this.getBrowser() === BrowserType.Opera;
|
||||
}
|
||||
|
||||
isVivaldi(): boolean {
|
||||
return this.getBrowser() === BrowserType.Vivaldi;
|
||||
}
|
||||
|
||||
isSafari(): boolean {
|
||||
return this.getBrowser() === BrowserType.Safari;
|
||||
}
|
||||
|
||||
analyticsId(): string {
|
||||
if (this.analyticsIdCache) {
|
||||
return this.analyticsIdCache;
|
||||
}
|
||||
|
||||
this.analyticsIdCache = AnalyticsIds[this.getBrowser()];
|
||||
return this.analyticsIdCache;
|
||||
}
|
||||
|
||||
initListSectionItemListeners(doc: Document, angular: any): void {
|
||||
if (!doc) {
|
||||
throw new Error('doc parameter required');
|
||||
}
|
||||
|
||||
const sectionItems = doc.querySelectorAll(
|
||||
'.list-section-item:not([data-bw-events="1"])');
|
||||
const sectionFormItems = doc.querySelectorAll(
|
||||
'.list-section-item:not([data-bw-events="1"]) input, ' +
|
||||
'.list-section-item:not([data-bw-events="1"]) select, ' +
|
||||
'.list-section-item:not([data-bw-events="1"]) textarea');
|
||||
|
||||
sectionItems.forEach((item) => {
|
||||
(item as HTMLElement).dataset.bwEvents = '1';
|
||||
|
||||
item.addEventListener('click', (e) => {
|
||||
if (e.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
|
||||
const el = e.target as HTMLElement;
|
||||
|
||||
// Some elements will already focus properly
|
||||
if (el.tagName != null) {
|
||||
switch (el.tagName.toLowerCase()) {
|
||||
case 'label': case 'input': case 'textarea': case 'select':
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const cell = el.closest('.list-section-item');
|
||||
if (!cell) {
|
||||
return;
|
||||
}
|
||||
|
||||
const textFilter = 'input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"])';
|
||||
const text = cell.querySelectorAll(textFilter + ', textarea');
|
||||
const checkbox = cell.querySelectorAll('input[type="checkbox"]');
|
||||
const select = cell.querySelectorAll('select');
|
||||
|
||||
if (text.length > 0) {
|
||||
(text[0] as HTMLElement).focus();
|
||||
} else if (select.length > 0) {
|
||||
(select[0] as HTMLElement).focus();
|
||||
} else if (checkbox.length > 0) {
|
||||
const cb = checkbox[0] as HTMLInputElement;
|
||||
cb.checked = !cb.checked;
|
||||
if (angular) {
|
||||
angular.element(checkbox[0]).triggerHandler('click');
|
||||
}
|
||||
}
|
||||
}, false);
|
||||
});
|
||||
|
||||
sectionFormItems.forEach((item) => {
|
||||
const itemCell = item.closest('.list-section-item');
|
||||
(itemCell as HTMLElement).dataset.bwEvents = '1';
|
||||
|
||||
item.addEventListener('focus', (e: Event) => {
|
||||
const el = e.target as HTMLElement;
|
||||
const cell = el.closest('.list-section-item');
|
||||
if (!cell) {
|
||||
return;
|
||||
}
|
||||
|
||||
cell.classList.add('active');
|
||||
}, false);
|
||||
|
||||
item.addEventListener('blur', (e: Event) => {
|
||||
const el = e.target as HTMLElement;
|
||||
const cell = el.closest('.list-section-item');
|
||||
if (!cell) {
|
||||
return;
|
||||
}
|
||||
|
||||
cell.classList.remove('active');
|
||||
}, false);
|
||||
});
|
||||
}
|
||||
|
||||
getDomain(uriString: string): string {
|
||||
return UtilsService.getDomain(uriString);
|
||||
}
|
||||
|
||||
getHostname(uriString: string): string {
|
||||
return UtilsService.getHostname(uriString);
|
||||
}
|
||||
@@ -347,21 +152,4 @@ export default class UtilsService implements UtilsServiceInterface {
|
||||
copyToClipboard(text: string, doc?: Document) {
|
||||
UtilsService.copyToClipboard(text, doc);
|
||||
}
|
||||
|
||||
inSidebar(theWindow: Window): boolean {
|
||||
return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=sidebar') > -1;
|
||||
}
|
||||
|
||||
inTab(theWindow: Window): boolean {
|
||||
return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=tab') > -1;
|
||||
}
|
||||
|
||||
inPopout(theWindow: Window): boolean {
|
||||
return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=popout') > -1;
|
||||
}
|
||||
|
||||
inPopup(theWindow: Window): boolean {
|
||||
return theWindow.location.search === '' || theWindow.location.search.indexOf('uilocation=') === -1 ||
|
||||
theWindow.location.search.indexOf('uilocation=popup') > -1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user