1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/services/autofillService.js

389 lines
13 KiB
JavaScript
Raw Normal View History

function AutofillService(utilsService, totpService, tokenService, loginService) {
this.utilsService = utilsService;
this.totpService = totpService;
this.tokenService = tokenService;
this.loginService = loginService;
initAutofill();
2017-07-14 21:34:05 +02:00
}
function initAutofill() {
// Add other languages to this array
var usernameFieldNames = ['username', 'user name', 'email', 'email address', 'e-mail', 'e-mail address',
'userid', 'user id'];
AutofillService.prototype.generateFillScript = function (pageDetails, fill) {
if (!pageDetails) {
return null;
}
var fillScript = {
documentUUID: pageDetails.documentUUID,
script: [],
autosubmit: null,
properties: {},
options: {},
metadata: {}
};
var passwordFields = [],
passwords = [],
usernames = [],
filledOpIds = [],
pf = null,
username = null,
i = 0;
if (fill.fields && fill.fields.length) {
var fieldNames = [];
for (i = 0; i < fill.fields.length; i++) {
if (fill.fields[i].name && fill.fields[i].name !== '') {
fieldNames.push(fill.fields[i].name.toLowerCase());
}
else {
fieldNames.push(null);
}
}
for (i = 0; i < pageDetails.fields.length; i++) {
var field = pageDetails.fields[i];
if (filledOpIds.indexOf(field.opid) > -1 || !field.viewable) {
continue;
}
var matchingIndex = findMatchingFieldIndex(field, fieldNames);
if (matchingIndex > -1) {
filledOpIds.push(field.opid);
fillScript.script.push(['click_on_opid', field.opid]);
fillScript.script.push(['fill_by_opid', field.opid, fill.fields[matchingIndex].value]);
}
}
}
if (!fill.password || fill.password === '') {
// No password for this login. Maybe they just wanted to auto-fill some custom fields?
if (!filledOpIds.length) {
return null;
}
fillScript.script.push(['focus_by_opid', filledOpIds[filledOpIds.length - 1]]);
return fillScript;
}
passwordFields = loadPasswordFields(pageDetails, false);
if (!passwordFields.length) {
2016-11-26 20:41:08 +01:00
// not able to find any viewable password fields. maybe there are some "hidden" ones?
passwordFields = loadPasswordFields(pageDetails, true);
}
for (var formKey in pageDetails.forms) {
var passwordFieldsForForm = [];
2017-07-14 21:34:05 +02:00
for (i = 0; i < passwordFields.length; i++) {
if (formKey === passwordFields[i].form) {
passwordFieldsForForm.push(passwordFields[i]);
}
}
for (i = 0; i < passwordFieldsForForm.length; i++) {
pf = passwordFieldsForForm[i];
passwords.push(pf);
if (fill.username) {
2017-01-21 18:38:52 +01:00
username = findUsernameField(pageDetails, pf, false, false);
if (!username) {
2016-11-26 20:41:08 +01:00
// not able to find any viewable username fields. maybe there are some "hidden" ones?
2017-01-21 18:38:52 +01:00
username = findUsernameField(pageDetails, pf, true, false);
}
if (username) {
usernames.push(username);
}
}
}
}
if (passwordFields.length && !passwords.length) {
// The page does not have any forms with password fields. Use the first password field on the page and the
// input field just before it as the username.
pf = passwordFields[0];
passwords.push(pf);
if (fill.username && pf.elementNumber > 0) {
2017-01-21 18:38:52 +01:00
username = findUsernameField(pageDetails, pf, false, true);
if (!username) {
2016-11-26 20:41:08 +01:00
// not able to find any viewable username fields. maybe there are some "hidden" ones?
2017-01-21 18:38:52 +01:00
username = findUsernameField(pageDetails, pf, true, true);
}
if (username) {
usernames.push(username);
}
}
}
if (!passwordFields.length) {
// No password fields on this page. Let's try to just fuzzy fill the username.
for (i = 0; i < pageDetails.fields.length; i++) {
var f = pageDetails.fields[i];
if (f.type === 'text' || f.type === 'email' || f.type === 'tel' && fieldIsFuzzyMatch(f, usernameFieldNames)) {
usernames.push(f);
}
}
}
for (i = 0; i < usernames.length; i++) {
if (filledOpIds.indexOf(usernames[i].opid) > -1) {
continue;
}
filledOpIds.push(usernames[i].opid);
fillScript.script.push(['click_on_opid', usernames[i].opid]);
fillScript.script.push(['fill_by_opid', usernames[i].opid, fill.username]);
}
for (i = 0; i < passwords.length; i++) {
if (filledOpIds.indexOf(passwords[i].opid) > -1) {
continue;
}
filledOpIds.push(passwords[i].opid);
fillScript.script.push(['click_on_opid', passwords[i].opid]);
fillScript.script.push(['fill_by_opid', passwords[i].opid, fill.password]);
}
if (filledOpIds.length) {
fillScript.script.push(['focus_by_opid', filledOpIds[filledOpIds.length - 1]]);
}
return fillScript;
};
AutofillService.prototype.getFormsWithPasswordFields = function (pageDetails) {
var passwordFields = [],
formData = [];
passwordFields = loadPasswordFields(pageDetails, false);
if (!passwordFields.length) {
// not able to find any viewable password fields. maybe there are some "hidden" ones?
passwordFields = loadPasswordFields(pageDetails, true);
}
if (passwordFields.length) {
for (var formKey in pageDetails.forms) {
for (var i = 0; i < passwordFields.length; i++) {
var pf = passwordFields[i];
if (formKey === pf.form) {
2017-01-21 18:38:52 +01:00
var uf = findUsernameField(pageDetails, pf, false, false);
if (!uf) {
// not able to find any viewable username fields. maybe there are some "hidden" ones?
2017-01-21 18:38:52 +01:00
uf = findUsernameField(pageDetails, pf, true, false);
}
formData.push({
form: pageDetails.forms[formKey],
password: pf,
username: uf
});
break;
}
}
}
}
return formData;
};
AutofillService.prototype.doAutoFill = function (login, pageDetails, fromBackground, skipTotp, skipLastUsed) {
var deferred = Q.defer();
var self = this;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var tab = null;
if (tabs.length > 0) {
tab = tabs[0];
}
else {
deferred.reject();
return;
}
if (!tab || !login || !pageDetails || !pageDetails.length) {
deferred.reject();
return;
}
var didAutofill = false;
for (var i = 0; i < pageDetails.length; i++) {
// make sure we're still on correct tab
if (pageDetails[i].tab.id !== tab.id || pageDetails[i].tab.url !== tab.url) {
continue;
}
var fillScript = self.generateFillScript(pageDetails[i].details, {
username: login.username,
password: login.password,
fields: login.fields
});
if (!fillScript || !fillScript.script || !fillScript.script.length) {
continue;
}
didAutofill = true;
if (!skipLastUsed) {
self.loginService.updateLastUsedDate(login.id, function () { });
}
chrome.tabs.sendMessage(tab.id, {
command: 'fillForm',
fillScript: fillScript
}, { frameId: pageDetails[i].frameId });
if ((fromBackground && self.utilsService.isFirefox()) ||
skipTotp || !login.totp || !self.tokenService.getPremium()) {
deferred.resolve();
return;
}
self.totpService.isAutoCopyEnabled().then(function (enabled) {
if (enabled) {
return self.totpService.getCode(login.totp);
}
return null;
}).then(function (code) {
if (code) {
self.utilsService.copyToClipboard(code);
}
deferred.resolve();
return;
});
break;
}
if (!didAutofill) {
deferred.reject();
return;
}
});
return deferred.promise;
};
AutofillService.prototype.doAutoFillForLastUsedLogin = function (pageDetails) {
var self = this;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var tab = null;
if (tabs.length > 0) {
tab = tabs[0];
}
if (!tab || !tab.url) {
return;
}
var tabDomain = self.utilsService.getDomain(tab.url);
if (!tabDomain) {
return;
}
2017-09-08 05:26:56 +02:00
self.loginService.getLastUsedForDomain(tabDomain).then(function (login) {
if (!login) {
return;
}
2017-09-08 05:26:56 +02:00
self.doAutoFill(login, pageDetails, true, true, true);
});
});
};
function loadPasswordFields(pageDetails, canBeHidden) {
var arr = [];
for (var i = 0; i < pageDetails.fields.length; i++) {
if (pageDetails.fields[i].type === 'password' && (canBeHidden || pageDetails.fields[i].viewable)) {
arr.push(pageDetails.fields[i]);
}
}
return arr;
}
2017-01-21 18:38:52 +01:00
function findUsernameField(pageDetails, passwordField, canBeHidden, withoutForm) {
var usernameField = null;
for (var i = 0; i < pageDetails.fields.length; i++) {
var f = pageDetails.fields[i];
2017-01-21 18:38:52 +01:00
if (f.elementNumber >= passwordField.elementNumber) {
break;
}
2017-01-21 18:38:52 +01:00
if ((withoutForm || f.form === passwordField.form) && (canBeHidden || f.viewable) &&
(f.type === 'text' || f.type === 'email' || f.type === 'tel')) {
usernameField = f;
if (findMatchingFieldIndex(f, usernameFieldNames) > -1) {
// We found an exact match. No need to keep looking.
break;
}
}
}
return usernameField;
}
function findMatchingFieldIndex(field, names) {
var matchingIndex = -1;
if (field.htmlID && field.htmlID !== '') {
matchingIndex = names.indexOf(field.htmlID.toLowerCase());
}
if (matchingIndex < 0 && field.htmlName && field.htmlName !== '') {
matchingIndex = names.indexOf(field.htmlName.toLowerCase());
}
if (matchingIndex < 0 && field['label-tag'] && field['label-tag'] !== '') {
matchingIndex = names.indexOf(field['label-tag'].replace(/(?:\r\n|\r|\n)/g, '').trim().toLowerCase());
}
if (matchingIndex < 0 && field.placeholder && field.placeholder !== '') {
matchingIndex = names.indexOf(field.placeholder.toLowerCase());
}
return matchingIndex;
}
function fieldIsFuzzyMatch(field, names) {
if (field.htmlID && field.htmlID !== '' && fuzzyMatch(names, field.htmlID.toLowerCase())) {
return true;
}
if (field.htmlName && field.htmlName !== '' && fuzzyMatch(names, field.htmlName.toLowerCase())) {
return true;
}
if (field['label-tag'] && field['label-tag'] !== '' &&
fuzzyMatch(names, field['label-tag'].replace(/(?:\r\n|\r|\n)/g, '').trim().toLowerCase())) {
return true;
}
if (field.placeholder && field.placeholder !== '' && fuzzyMatch(names, field.placeholder.toLowerCase())) {
return true;
}
return false;
}
function fuzzyMatch(options, value) {
if (!options || !options.length || !value || value === '') {
return false;
}
for (var i = 0; i < options.length; i++) {
if (value.indexOf(options[i]) > -1) {
return true;
}
}
return false;
}
2017-07-14 21:34:05 +02:00
}