1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-27 12:36:14 +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; return;
} }
var tabDomain = tldjs.getDomain(url); var tabDomain = utilsService.getDomain(url);
if (!tabDomain) { if (!tabDomain) {
return; return;
} }
@ -393,7 +393,7 @@ function collectPageDetailsForContentScript(tab) {
} }
function addLogin(login, tab) { function addLogin(login, tab) {
var loginDomain = tldjs.getDomain(login.url); var loginDomain = utilsService.getDomain(login.url);
if (!loginDomain) { if (!loginDomain) {
return; return;
} }
@ -449,7 +449,7 @@ function saveAddLogin(tab) {
if (loginsToAdd[i].tabId === tab.id) { if (loginsToAdd[i].tabId === tab.id) {
var loginToAdd = loginsToAdd[i]; var loginToAdd = loginsToAdd[i];
var tabDomain = tldjs.getDomain(tab.url); var tabDomain = utilsService.getDomain(tab.url);
if (tabDomain && tabDomain === loginToAdd.domain) { if (tabDomain && tabDomain === loginToAdd.domain) {
loginsToAdd.splice(i, 1); loginsToAdd.splice(i, 1);
loginService.encrypt({ loginService.encrypt({
@ -504,7 +504,7 @@ function checkLoginsToAdd(tab, callback) {
return; return;
} }
var tabDomain = tldjs.getDomain(tab.url); var tabDomain = utilsService.getDomain(tab.url);
if (!tabDomain) { if (!tabDomain) {
if (callback) { if (callback) {
callback(); callback();

View File

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

View File

@ -1,7 +1,7 @@
angular angular
.module('bit.current') .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) { autofillService, $analytics, i18nService) {
$scope.i18n = i18nService; $scope.i18n = i18nService;
@ -30,7 +30,7 @@ angular
return; return;
} }
domain = tldjs.getDomain(url); domain = utilsService.getDomain(url);
if (!domain) { if (!domain) {
$scope.loaded = true; $scope.loaded = true;
$scope.$apply(); $scope.$apply();

View File

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

View File

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

View File

@ -128,4 +128,53 @@ function initUtilsService() {
$(this).parent().removeClass('active'); $(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);
}
}; };