1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-23 11:56:00 +01:00

better domain parsing with tldjs backup

This commit is contained in:
Kyle Spearrin 2017-03-28 22:11:47 -04:00
parent 02dc3609b4
commit 45d6c6f341
6 changed files with 62 additions and 13 deletions

View File

@ -253,7 +253,7 @@ function loadMenuAndUpdateBadge(url, tabId, contextMenuEnabled) {
return;
}
var tabDomain = tldjs.getDomain(url);
var tabDomain = utilsService.getDomain(url);
if (!tabDomain) {
return;
}
@ -393,7 +393,7 @@ function collectPageDetailsForContentScript(tab) {
}
function addLogin(login, tab) {
var loginDomain = tldjs.getDomain(login.url);
var loginDomain = utilsService.getDomain(login.url);
if (!loginDomain) {
return;
}
@ -449,7 +449,7 @@ function saveAddLogin(tab) {
if (loginsToAdd[i].tabId === tab.id) {
var loginToAdd = loginsToAdd[i];
var tabDomain = tldjs.getDomain(tab.url);
var tabDomain = utilsService.getDomain(tab.url);
if (tabDomain && tabDomain === loginToAdd.domain) {
loginsToAdd.splice(i, 1);
loginService.encrypt({
@ -504,7 +504,7 @@ function checkLoginsToAdd(tab, callback) {
return;
}
var tabDomain = tldjs.getDomain(tab.url);
var tabDomain = utilsService.getDomain(tab.url);
if (!tabDomain) {
if (callback) {
callback();

View File

@ -80,7 +80,10 @@ var Folder = function (obj, alreadyEncrypted) {
return null;
}).then(function (val) {
model.uri = val;
model.domain = tldjs.getDomain(val);
var utilsService = chrome.extension.getBackgroundPage().utilsService;
model.domain = utilsService.getDomain(val);
if (self.username) {
return self.username.decrypt();
}

View File

@ -1,7 +1,7 @@
angular
.module('bit.current')
.controller('currentController', function ($scope, loginService, tldjs, toastr, $q, $window, $state, $timeout,
.controller('currentController', function ($scope, loginService, utilsService, toastr, $q, $window, $state, $timeout,
autofillService, $analytics, i18nService) {
$scope.i18n = i18nService;
@ -30,7 +30,7 @@ angular
return;
}
domain = tldjs.getDomain(url);
domain = utilsService.getDomain(url);
if (!domain) {
$scope.loaded = true;
$scope.$apply();

View File

@ -22,9 +22,6 @@
.factory('syncService', function () {
return chrome.extension.getBackgroundPage().syncService;
})
.factory('tldjs', function () {
return chrome.extension.getBackgroundPage().tldjs;
})
.factory('autofillService', function () {
return chrome.extension.getBackgroundPage().autofillService;
})

View File

@ -1,8 +1,8 @@
angular
.module('bit.vault')
.controller('vaultViewLoginController', function ($scope, $state, $stateParams, loginService, tldjs, toastr, $q,
$analytics, i18nService) {
.controller('vaultViewLoginController', function ($scope, $state, $stateParams, loginService, toastr, $q,
$analytics, i18nService, utilsService) {
$scope.i18n = i18nService;
var from = $stateParams.from;
@ -26,7 +26,7 @@ angular
if (model.uri) {
$scope.login.showLaunch = model.uri.startsWith('http://') || model.uri.startsWith('https://');
var domain = tldjs.getDomain(model.uri);
var domain = utilsService.getDomain(model.uri);
if (domain) {
$scope.login.website = domain;
}

View File

@ -128,4 +128,53 @@ function initUtilsService() {
$(this).parent().removeClass('active');
});
}
UtilsService.prototype.getDomain = function (uriString) {
if (!uriString) {
return null;
}
uriString = uriString.trim();
if (uriString === '') {
return null;
}
if (uriString.startsWith('http://') || uriString.startsWith('https://')) {
try {
var url = new URL(uriString);
if (!url || !url.hostname) {
return null;
}
if (url.hostname === 'localhost' || validIpAddress(url.hostname)) {
return url.hostname;
}
if (tldjs) {
var domain = tldjs.getDomain(uriString);
if (domain) {
return domain;
}
}
return url.hostname;
}
catch (e) {
return null;
}
}
else if (tldjs) {
var domain2 = tldjs.getDomain(uriString);
if (domain2) {
return domain2;
}
}
return null;
}
function validIpAddress(ipString) {
var 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);
}
};