bitwarden-estensione-browser/src/services/autofill.service.ts

1190 lines
53 KiB
TypeScript
Raw Normal View History

2021-09-17 15:44:27 +02:00
import { CipherService } from 'jslib-common/abstractions/cipher.service';
import { EventService } from 'jslib-common/abstractions/event.service';
import { LogService } from 'jslib-common/abstractions/log.service';
2021-09-17 15:44:27 +02:00
import { TotpService } from 'jslib-common/abstractions/totp.service';
import { UserService } from 'jslib-common/abstractions/user.service';
2017-11-06 20:16:06 +01:00
2021-09-17 15:44:27 +02:00
import { AutofillService as AutofillServiceInterface } from './abstractions/autofill.service';
import { CipherRepromptType } from 'jslib-common/enums/cipherRepromptType';
import { CipherType } from 'jslib-common/enums/cipherType';
import { EventType } from 'jslib-common/enums/eventType';
import { FieldType } from 'jslib-common/enums/fieldType';
import { CipherView } from 'jslib-common/models/view/cipherView';
2018-01-11 21:59:07 +01:00
import AutofillField from '../models/autofillField';
import AutofillPageDetails from '../models/autofillPageDetails';
import AutofillScript from '../models/autofillScript';
2017-11-06 20:16:06 +01:00
import { BrowserApi } from '../browser/browserApi';
const CardAttributes: string[] = ['autoCompleteType', 'data-stripe', 'htmlName', 'htmlID', 'label-tag',
2019-10-08 18:49:43 +02:00
'placeholder', 'label-left', 'label-top', 'data-recurly'];
2017-11-06 20:16:06 +01:00
2018-07-16 17:00:49 +02:00
const CardAttributesExtended: string[] = [...CardAttributes, 'label-right'];
const IdentityAttributes: string[] = ['autoCompleteType', 'data-stripe', 'htmlName', 'htmlID', 'label-tag',
2019-10-08 18:49:43 +02:00
'placeholder', 'label-left', 'label-top', 'data-recurly'];
2017-11-06 20:16:06 +01:00
2017-12-29 22:12:57 +01:00
const UsernameFieldNames: string[] = [
// English
'username', 'user name', 'email', 'email address', 'e-mail', 'e-mail address', 'userid', 'user id',
'customer id', 'login id',
2017-12-29 22:12:57 +01:00
// German
'benutzername', 'benutzer name', 'email adresse', 'e-mail adresse', 'benutzerid', 'benutzer id'];
2017-11-06 20:16:06 +01:00
2020-10-24 13:01:03 +02:00
const FirstnameFieldNames: string[] = [
// English
'f-name', 'first-name', 'given-name', 'first-n',
// German
2021-02-10 16:40:15 +01:00
'vorname',
];
2020-10-24 13:01:03 +02:00
const LastnameFieldNames: string[] = [
// English
'l-name', 'last-name', 's-name', 'surname', 'family-name', 'family-n', 'last-n',
// German
2021-02-10 16:40:15 +01:00
'nachname', 'familienname',
];
2020-10-24 13:01:03 +02:00
const ExcludedAutofillTypes: string[] = ['radio', 'checkbox', 'hidden', 'file', 'button', 'image', 'reset', 'search'];
// Each index represents a language. These three arrays should all be the same length.
// 0: English, 1: Danish, 2: German/Dutch, 3: French/Spanish/Italian, 4: Russian, 5: Portuguese
const MonthAbbr = ['mm', 'mm', 'mm', 'mm', 'mm', 'mm'];
const YearAbbrShort = ['yy', 'åå', 'jj', 'aa', 'гг', 'rr'];
const YearAbbrLong = ['yyyy', 'åååå', 'jjjj', 'aa', 'гггг', 'rrrr'];
2020-05-05 22:59:20 +02:00
const OperationDelays = new Map<string, number>([
['buzzsprout.com', 100],
]);
2017-11-06 20:16:06 +01:00
/* tslint:disable */
const IsoCountries: { [id: string]: string; } = {
afghanistan: "AF", "aland islands": "AX", albania: "AL", algeria: "DZ", "american samoa": "AS", andorra: "AD",
angola: "AO", anguilla: "AI", antarctica: "AQ", "antigua and barbuda": "AG", argentina: "AR", armenia: "AM",
aruba: "AW", australia: "AU", austria: "AT", azerbaijan: "AZ", bahamas: "BS", bahrain: "BH", bangladesh: "BD",
2018-04-17 19:27:56 +02:00
barbados: "BB", belarus: "BY", belgium: "BE", belize: "BZ", benin: "BJ", bermuda: "BM", bhutan: "BT",
bolivia: "BO", "bosnia and herzegovina": "BA", botswana: "BW", "bouvet island": "BV", brazil: "BR",
"british indian ocean territory": "IO", "brunei darussalam": "BN", bulgaria: "BG", "burkina faso": "BF",
burundi: "BI", cambodia: "KH", cameroon: "CM", canada: "CA", "cape verde": "CV", "cayman islands": "KY",
2017-11-06 20:16:06 +01:00
"central african republic": "CF", chad: "TD", chile: "CL", china: "CN", "christmas island": "CX",
"cocos (keeling) islands": "CC", colombia: "CO", comoros: "KM", congo: "CG", "congo, democratic republic": "CD",
"cook islands": "CK", "costa rica": "CR", "cote d'ivoire": "CI", croatia: "HR", cuba: "CU", cyprus: "CY",
"czech republic": "CZ", denmark: "DK", djibouti: "DJ", dominica: "DM", "dominican republic": "DO", ecuador: "EC",
egypt: "EG", "el salvador": "SV", "equatorial guinea": "GQ", eritrea: "ER", estonia: "EE", ethiopia: "ET",
"falkland islands": "FK", "faroe islands": "FO", fiji: "FJ", finland: "FI", france: "FR", "french guiana": "GF",
2018-04-17 19:27:56 +02:00
"french polynesia": "PF", "french southern territories": "TF", gabon: "GA", gambia: "GM", georgia: "GE",
germany: "DE", ghana: "GH", gibraltar: "GI", greece: "GR", greenland: "GL", grenada: "GD", guadeloupe: "GP",
guam: "GU", guatemala: "GT", guernsey: "GG", guinea: "GN", "guinea-bissau": "GW", guyana: "GY", haiti: "HT",
2017-11-06 20:16:06 +01:00
"heard island & mcdonald islands": "HM", "holy see (vatican city state)": "VA", honduras: "HN", "hong kong": "HK",
hungary: "HU", iceland: "IS", india: "IN", indonesia: "ID", "iran, islamic republic of": "IR", iraq: "IQ",
ireland: "IE", "isle of man": "IM", israel: "IL", italy: "IT", jamaica: "JM", japan: "JP", jersey: "JE",
jordan: "JO", kazakhstan: "KZ", kenya: "KE", kiribati: "KI", "republic of korea": "KR", "south korea": "KR",
"democratic people's republic of korea": "KP", "north korea": "KP", kuwait: "KW", kyrgyzstan: "KG",
"lao people's democratic republic": "LA", latvia: "LV", lebanon: "LB", lesotho: "LS", liberia: "LR",
2018-04-17 19:27:56 +02:00
"libyan arab jamahiriya": "LY", liechtenstein: "LI", lithuania: "LT", luxembourg: "LU", macao: "MO",
macedonia: "MK", madagascar: "MG", malawi: "MW", malaysia: "MY", maldives: "MV", mali: "ML", malta: "MT",
"marshall islands": "MH", martinique: "MQ", mauritania: "MR", mauritius: "MU", mayotte: "YT", mexico: "MX",
"micronesia, federated states of": "FM", moldova: "MD", monaco: "MC", mongolia: "MN", montenegro: "ME",
montserrat: "MS", morocco: "MA", mozambique: "MZ", myanmar: "MM", namibia: "NA", nauru: "NR", nepal: "NP",
netherlands: "NL", "netherlands antilles": "AN", "new caledonia": "NC", "new zealand": "NZ", nicaragua: "NI",
niger: "NE", nigeria: "NG", niue: "NU", "norfolk island": "NF", "northern mariana islands": "MP", norway: "NO",
oman: "OM", pakistan: "PK", palau: "PW", "palestinian territory, occupied": "PS", panama: "PA",
"papua new guinea": "PG", paraguay: "PY", peru: "PE", philippines: "PH", pitcairn: "PN", poland: "PL",
portugal: "PT", "puerto rico": "PR", qatar: "QA", reunion: "RE", romania: "RO", "russian federation": "RU",
rwanda: "RW", "saint barthelemy": "BL", "saint helena": "SH", "saint kitts and nevis": "KN", "saint lucia": "LC",
"saint martin": "MF", "saint pierre and miquelon": "PM", "saint vincent and grenadines": "VC", samoa: "WS",
"san marino": "SM", "sao tome and principe": "ST", "saudi arabia": "SA", senegal: "SN", serbia: "RS",
seychelles: "SC", "sierra leone": "SL", singapore: "SG", slovakia: "SK", slovenia: "SI", "solomon islands": "SB",
somalia: "SO", "south africa": "ZA", "south georgia and sandwich isl.": "GS", spain: "ES", "sri lanka": "LK",
sudan: "SD", suriname: "SR", "svalbard and jan mayen": "SJ", swaziland: "SZ", sweden: "SE", switzerland: "CH",
"syrian arab republic": "SY", taiwan: "TW", tajikistan: "TJ", tanzania: "TZ", thailand: "TH", "timor-leste": "TL",
togo: "TG", tokelau: "TK", tonga: "TO", "trinidad and tobago": "TT", tunisia: "TN", turkey: "TR",
turkmenistan: "TM", "turks and caicos islands": "TC", tuvalu: "TV", uganda: "UG", ukraine: "UA",
"united arab emirates": "AE", "united kingdom": "GB", "united states": "US",
"united states outlying islands": "UM", uruguay: "UY", uzbekistan: "UZ", vanuatu: "VU", venezuela: "VE",
vietnam: "VN", "virgin islands, british": "VG", "virgin islands, u.s.": "VI", "wallis and futuna": "WF",
"western sahara": "EH", yemen: "YE", zambia: "ZM", zimbabwe: "ZW",
2017-11-06 20:16:06 +01:00
};
const IsoStates: { [id: string]: string; } = {
2018-04-17 19:27:56 +02:00
alabama: 'AL', alaska: 'AK', 'american samoa': 'AS', arizona: 'AZ', arkansas: 'AR', california: 'CA',
colorado: 'CO', connecticut: 'CT', delaware: 'DE', 'district of columbia': 'DC',
'federated states of micronesia': 'FM', florida: 'FL', georgia: 'GA', guam: 'GU', hawaii: 'HI', idaho: 'ID',
illinois: 'IL', indiana: 'IN', iowa: 'IA', kansas: 'KS', kentucky: 'KY', louisiana: 'LA', maine: 'ME',
'marshall islands': 'MH', maryland: 'MD', massachusetts: 'MA', michigan: 'MI', minnesota: 'MN', mississippi: 'MS',
missouri: 'MO', montana: 'MT', nebraska: 'NE', nevada: 'NV', 'new hampshire': 'NH', 'new jersey': 'NJ',
'new mexico': 'NM', 'new york': 'NY', 'north carolina': 'NC', 'north dakota': 'ND',
'northern mariana islands': 'MP', ohio: 'OH', oklahoma: 'OK', oregon: 'OR', palau: 'PW', pennsylvania: 'PA',
'puerto rico': 'PR', 'rhode island': 'RI', 'south carolina': 'SC', 'south dakota': 'SD', tennessee: 'TN',
texas: 'TX', utah: 'UT', vermont: 'VT', 'virgin islands': 'VI', virginia: 'VA', washington: 'WA',
2017-11-06 20:16:06 +01:00
'west virginia': 'WV', wisconsin: 'WI', wyoming: 'WY',
};
var IsoProvinces: { [id: string]: string; } = {
alberta: 'AB', 'british columbia': 'BC', manitoba: 'MB', 'new brunswick': 'NB', 'newfoundland and labrador': 'NL',
'nova scotia': 'NS', ontario: 'ON', 'prince edward island': 'PE', quebec: 'QC', saskatchewan: 'SK',
};
/* tslint:enable */
2018-01-11 20:45:27 +01:00
export default class AutofillService implements AutofillServiceInterface {
2018-08-29 05:17:39 +02:00
constructor(private cipherService: CipherService, private userService: UserService,
private totpService: TotpService, private eventService: EventService,
private logService: LogService) { }
2017-11-06 20:16:06 +01:00
getFormsWithPasswordFields(pageDetails: AutofillPageDetails): any[] {
const formData: any[] = [];
const passwordFields = this.loadPasswordFields(pageDetails, true, true, false, false);
2017-11-06 20:16:06 +01:00
if (passwordFields.length === 0) {
return formData;
}
for (const formKey in pageDetails.forms) {
if (!pageDetails.forms.hasOwnProperty(formKey)) {
continue;
}
2021-02-10 16:40:15 +01:00
const formPasswordFields = passwordFields.filter(pf => formKey === pf.form);
2018-08-01 05:24:11 +02:00
if (formPasswordFields.length > 0) {
let uf = this.findUsernameField(pageDetails, formPasswordFields[0], false, false, false);
2017-11-06 20:16:06 +01:00
if (uf == null) {
// not able to find any viewable username fields. maybe there are some "hidden" ones?
uf = this.findUsernameField(pageDetails, formPasswordFields[0], true, true, false);
2017-11-06 20:16:06 +01:00
}
formData.push({
form: pageDetails.forms[formKey],
2018-08-01 05:24:11 +02:00
password: formPasswordFields[0],
2017-11-06 20:16:06 +01:00
username: uf,
2018-08-01 05:24:11 +02:00
passwords: formPasswordFields,
2017-11-06 20:16:06 +01:00
});
}
}
return formData;
}
async doAutoFill(options: any) {
let totpPromise: Promise<string> = null;
const tab = await this.getActiveTab();
if (!tab || !options.cipher || !options.pageDetails || !options.pageDetails.length) {
throw new Error('Nothing to auto-fill.');
}
2018-08-29 05:17:39 +02:00
const canAccessPremium = await this.userService.canAccessPremium();
2017-11-06 20:16:06 +01:00
let didAutofill = false;
2017-11-16 18:49:23 +01:00
options.pageDetails.forEach((pd: any) => {
2017-11-06 20:16:06 +01:00
// make sure we're still on correct tab
if (pd.tab.id !== tab.id || pd.tab.url !== tab.url) {
2017-11-16 18:49:23 +01:00
return;
2017-11-06 20:16:06 +01:00
}
const fillScript = this.generateFillScript(pd.details, {
skipUsernameOnlyFill: options.skipUsernameOnlyFill || false,
onlyEmptyFields: options.onlyEmptyFields || false,
onlyVisibleFields: options.onlyVisibleFields || false,
fillNewPassword: options.fillNewPassword || false,
2017-11-06 20:16:06 +01:00
cipher: options.cipher,
});
if (!fillScript || !fillScript.script || !fillScript.script.length) {
2017-11-16 18:49:23 +01:00
return;
2017-11-06 20:16:06 +01:00
}
2020-05-05 22:59:20 +02:00
// Add a small delay between operations
fillScript.properties.delay_between_operations = 20;
2017-11-06 20:16:06 +01:00
didAutofill = true;
if (!options.skipLastUsed) {
this.cipherService.updateLastUsedDate(options.cipher.id);
}
BrowserApi.tabSendMessage(tab, {
2017-11-06 20:16:06 +01:00
command: 'fillForm',
fillScript: fillScript,
2018-05-18 23:05:46 +02:00
url: tab.url,
2017-11-06 20:16:06 +01:00
}, { frameId: pd.frameId });
if (options.cipher.type !== CipherType.Login || totpPromise || !options.cipher.login.totp ||
(!canAccessPremium && !options.cipher.organizationUseTotp)) {
2017-11-16 18:49:23 +01:00
return;
2017-11-06 20:16:06 +01:00
}
2021-02-10 16:40:15 +01:00
totpPromise = this.totpService.isAutoCopyEnabled().then(enabled => {
2017-11-06 20:16:06 +01:00
if (enabled) {
return this.totpService.getCode(options.cipher.login.totp);
}
return null;
});
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
if (didAutofill) {
2019-07-12 20:54:17 +02:00
this.eventService.collect(EventType.Cipher_ClientAutofilled, options.cipher.id);
2017-11-06 20:16:06 +01:00
if (totpPromise != null) {
return await totpPromise;
2017-11-06 20:16:06 +01:00
} else {
return null;
}
} else {
throw new Error('Did not auto-fill.');
}
}
async doAutoFillActiveTab(pageDetails: any, fromCommand: boolean) {
2017-11-06 20:16:06 +01:00
const tab = await this.getActiveTab();
if (!tab || !tab.url) {
return;
}
let cipher: CipherView;
if (fromCommand) {
cipher = await this.cipherService.getNextCipherForUrl(tab.url);
} else {
2020-10-26 23:13:09 +01:00
const lastLaunchedCipher = await this.cipherService.getLastLaunchedForUrl(tab.url, true);
if (lastLaunchedCipher && Date.now().valueOf() - lastLaunchedCipher.localData?.lastLaunched?.valueOf() < 30000) {
cipher = lastLaunchedCipher;
}
else {
2020-10-26 23:13:09 +01:00
cipher = await this.cipherService.getLastUsedForUrl(tab.url, true);
}
2021-03-15 03:35:23 +01:00
if (cipher == null) {
2020-10-26 23:13:09 +01:00
return null;
}
2017-11-06 20:16:06 +01:00
}
if (cipher.reprompt !== CipherRepromptType.None) {
return;
}
2021-05-05 05:57:50 +02:00
2021-01-02 17:32:22 +01:00
const totpCode = await this.doAutoFill({
cipher: cipher,
2017-11-06 20:16:06 +01:00
pageDetails: pageDetails,
skipLastUsed: !fromCommand,
2017-11-06 20:16:06 +01:00
skipUsernameOnlyFill: !fromCommand,
onlyEmptyFields: !fromCommand,
onlyVisibleFields: !fromCommand,
fillNewPassword: fromCommand,
2017-11-06 20:16:06 +01:00
});
2021-01-02 17:32:22 +01:00
// Update last used index as autofill has succeed
if (fromCommand) {
this.cipherService.updateLastUsedIndexForUrl(tab.url);
}
2021-01-02 17:32:22 +01:00
return totpCode;
2017-11-06 20:16:06 +01:00
}
// Helpers
private async getActiveTab(): Promise<any> {
const tab = await BrowserApi.getTabFromCurrentWindow();
if (!tab) {
throw new Error('No tab found.');
}
return tab;
2017-11-06 20:16:06 +01:00
}
private generateFillScript(pageDetails: AutofillPageDetails, options: any): AutofillScript {
if (!pageDetails || !options.cipher) {
return null;
}
let fillScript = new AutofillScript(pageDetails.documentUUID);
const filledFields: { [id: string]: AutofillField; } = {};
const fields = options.cipher.fields;
if (fields && fields.length) {
const fieldNames: string[] = [];
2017-11-16 18:49:23 +01:00
fields.forEach((f: any) => {
if (this.hasValue(f.name)) {
2017-11-06 20:16:06 +01:00
fieldNames.push(f.name.toLowerCase());
}
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
2017-11-16 18:49:23 +01:00
pageDetails.fields.forEach((field: any) => {
if (filledFields.hasOwnProperty(field.opid)) {
return;
}
if (!field.viewable && field.tagName !== 'span') {
2017-11-16 18:49:23 +01:00
return;
2017-11-06 20:16:06 +01:00
}
const matchingIndex = this.findMatchingFieldIndex(field, fieldNames);
if (matchingIndex > -1) {
let val = fields[matchingIndex].value;
2018-01-09 20:26:20 +01:00
if (val == null && fields[matchingIndex].type === FieldType.Boolean) {
val = 'false';
}
2017-11-06 20:16:06 +01:00
filledFields[field.opid] = field;
2019-04-21 03:29:13 +02:00
this.fillByOpid(fillScript, field, val);
2017-11-06 20:16:06 +01:00
}
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
}
switch (options.cipher.type) {
2018-01-09 20:26:20 +01:00
case CipherType.Login:
2017-11-06 20:16:06 +01:00
fillScript = this.generateLoginFillScript(fillScript, pageDetails, filledFields, options);
break;
2018-01-09 20:26:20 +01:00
case CipherType.Card:
2017-11-06 20:16:06 +01:00
fillScript = this.generateCardFillScript(fillScript, pageDetails, filledFields, options);
break;
2018-01-09 20:26:20 +01:00
case CipherType.Identity:
2017-11-06 20:16:06 +01:00
fillScript = this.generateIdentityFillScript(fillScript, pageDetails, filledFields, options);
break;
default:
return null;
}
return fillScript;
}
private generateLoginFillScript(fillScript: AutofillScript, pageDetails: any,
2017-11-22 14:31:22 +01:00
filledFields: { [id: string]: AutofillField; }, options: any): AutofillScript {
2017-11-06 20:16:06 +01:00
if (!options.cipher.login) {
return null;
}
const passwords: AutofillField[] = [];
const usernames: AutofillField[] = [];
let pf: AutofillField = null;
let username: AutofillField = null;
const login = options.cipher.login;
if (!login.password || login.password === '') {
// No password for this login. Maybe they just wanted to auto-fill some custom fields?
fillScript = this.setFillScriptForFocus(filledFields, fillScript);
return fillScript;
}
let passwordFields = this.loadPasswordFields(pageDetails, false, false, options.onlyEmptyFields,
options.fillNewPassword);
if (!passwordFields.length && !options.onlyVisibleFields) {
2017-11-06 20:16:06 +01:00
// not able to find any viewable password fields. maybe there are some "hidden" ones?
passwordFields = this.loadPasswordFields(pageDetails, true, true, options.onlyEmptyFields,
options.fillNewPassword);
2017-11-06 20:16:06 +01:00
}
for (const formKey in pageDetails.forms) {
if (!pageDetails.forms.hasOwnProperty(formKey)) {
continue;
}
const passwordFieldsForForm: AutofillField[] = [];
2021-02-10 16:40:15 +01:00
passwordFields.forEach(passField => {
2017-11-06 20:16:06 +01:00
if (formKey === passField.form) {
passwordFieldsForForm.push(passField);
}
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
2021-02-10 16:40:15 +01:00
passwordFields.forEach(passField => {
2017-11-06 20:16:06 +01:00
pf = passField;
passwords.push(pf);
if (login.username) {
username = this.findUsernameField(pageDetails, pf, false, false, false);
2017-11-06 20:16:06 +01:00
if (!username && !options.onlyVisibleFields) {
2017-11-06 20:16:06 +01:00
// not able to find any viewable username fields. maybe there are some "hidden" ones?
username = this.findUsernameField(pageDetails, pf, true, true, false);
2017-11-06 20:16:06 +01:00
}
if (username) {
usernames.push(username);
}
}
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
}
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 (login.username && pf.elementNumber > 0) {
username = this.findUsernameField(pageDetails, pf, false, false, true);
2017-11-06 20:16:06 +01:00
if (!username && !options.onlyVisibleFields) {
2017-11-06 20:16:06 +01:00
// not able to find any viewable username fields. maybe there are some "hidden" ones?
username = this.findUsernameField(pageDetails, pf, true, true, true);
2017-11-06 20:16:06 +01:00
}
if (username) {
usernames.push(username);
}
}
}
if (!passwordFields.length && !options.skipUsernameOnlyFill) {
// No password fields on this page. Let's try to just fuzzy fill the username.
2017-11-16 18:49:23 +01:00
pageDetails.fields.forEach((f: any) => {
2017-11-06 20:16:06 +01:00
if (f.viewable && (f.type === 'text' || f.type === 'email' || f.type === 'tel') &&
this.fieldIsFuzzyMatch(f, UsernameFieldNames)) {
usernames.push(f);
}
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
}
2021-02-10 16:40:15 +01:00
usernames.forEach(u => {
2017-11-06 20:16:06 +01:00
if (filledFields.hasOwnProperty(u.opid)) {
2017-11-16 18:49:23 +01:00
return;
2017-11-06 20:16:06 +01:00
}
filledFields[u.opid] = u;
2019-04-21 03:29:13 +02:00
this.fillByOpid(fillScript, u, login.username);
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
2021-02-10 16:40:15 +01:00
passwords.forEach(p => {
2017-11-06 20:16:06 +01:00
if (filledFields.hasOwnProperty(p.opid)) {
2017-11-16 18:49:23 +01:00
return;
2017-11-06 20:16:06 +01:00
}
filledFields[p.opid] = p;
2019-04-21 03:29:13 +02:00
this.fillByOpid(fillScript, p, login.password);
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
fillScript = this.setFillScriptForFocus(filledFields, fillScript);
return fillScript;
}
private generateCardFillScript(fillScript: AutofillScript, pageDetails: any,
2017-11-22 14:31:22 +01:00
filledFields: { [id: string]: AutofillField; }, options: any): AutofillScript {
2017-11-06 20:16:06 +01:00
if (!options.cipher.card) {
return null;
}
const fillFields: { [id: string]: AutofillField; } = {};
2017-11-16 18:49:23 +01:00
pageDetails.fields.forEach((f: any) => {
if (this.forCustomFieldsOnly(f)) {
return;
}
if (this.isExcludedType(f.type, ExcludedAutofillTypes)) {
return;
}
2018-05-13 04:21:39 +02:00
for (let i = 0; i < CardAttributes.length; i++) {
const attr = CardAttributes[i];
if (!f.hasOwnProperty(attr) || !f[attr] || !f.viewable) {
2018-05-13 04:21:39 +02:00
continue;
2017-11-11 14:10:47 +01:00
}
// ref https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill
// ref https://developers.google.com/web/fundamentals/design-and-ux/input/forms/
if (!fillFields.cardholderName && this.isFieldMatch(f[attr],
['cc-name', 'card-name', 'cardholder-name', 'cardholder', 'name', 'nom'],
['cc-name', 'card-name', 'cardholder-name', 'cardholder', 'tbName'])) {
2017-11-11 14:10:47 +01:00
fillFields.cardholderName = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.number && this.isFieldMatch(f[attr],
2018-03-08 16:39:49 +01:00
['cc-number', 'cc-num', 'card-number', 'card-num', 'number', 'cc', 'cc-no', 'card-no',
'credit-card', 'numero-carte', 'carte', 'carte-credit', 'num-carte', 'cb-num'],
2018-03-16 01:27:02 +01:00
['cc-number', 'cc-num', 'card-number', 'card-num', 'cc-no', 'card-no', 'numero-carte',
'num-carte', 'cb-num'])) {
2017-11-11 14:10:47 +01:00
fillFields.number = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.exp && this.isFieldMatch(f[attr],
['cc-exp', 'card-exp', 'cc-expiration', 'card-expiration', 'cc-ex', 'card-ex',
'card-expire', 'card-expiry', 'validite', 'expiration', 'expiry', 'mm-yy',
'mm-yyyy', 'yy-mm', 'yyyy-mm', 'expiration-date', 'payment-card-expiration',
'payment-cc-date'],
['mm-yy', 'mm-yyyy', 'yy-mm', 'yyyy-mm', 'expiration-date',
'payment-card-expiration'])) {
2017-11-11 14:10:47 +01:00
fillFields.exp = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.expMonth && this.isFieldMatch(f[attr],
['exp-month', 'cc-exp-month', 'cc-month', 'card-month', 'cc-mo', 'card-mo', 'exp-mo',
'card-exp-mo', 'cc-exp-mo', 'card-expiration-month', 'expiration-month',
2018-02-27 20:41:52 +01:00
'cc-mm', 'cc-m', 'card-mm', 'card-m', 'card-exp-mm', 'cc-exp-mm', 'exp-mm', 'exp-m',
2018-08-29 19:49:41 +02:00
'expire-month', 'expire-mo', 'expiry-month', 'expiry-mo', 'card-expire-month',
'card-expire-mo', 'card-expiry-month', 'card-expiry-mo', 'mois-validite',
2018-06-01 14:36:46 +02:00
'mois-expiration', 'm-validite', 'm-expiration', 'expiry-date-field-month',
'expiration-date-month', 'expiration-date-mm', 'exp-mon', 'validity-mo',
'exp-date-mo', 'cb-date-mois', 'date-m'])) {
2017-11-11 14:10:47 +01:00
fillFields.expMonth = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.expYear && this.isFieldMatch(f[attr],
['exp-year', 'cc-exp-year', 'cc-year', 'card-year', 'cc-yr', 'card-yr', 'exp-yr',
'card-exp-yr', 'cc-exp-yr', 'card-expiration-year', 'expiration-year',
'cc-yy', 'cc-y', 'card-yy', 'card-y', 'card-exp-yy', 'cc-exp-yy', 'exp-yy', 'exp-y',
2018-03-08 16:39:49 +01:00
'cc-yyyy', 'card-yyyy', 'card-exp-yyyy', 'cc-exp-yyyy', 'expire-year', 'expire-yr',
2018-08-29 19:49:41 +02:00
'expiry-year', 'expiry-yr', 'card-expire-year', 'card-expire-yr', 'card-expiry-year',
'card-expiry-yr', 'an-validite', 'an-expiration', 'annee-validite',
'annee-expiration', 'expiry-date-field-year', 'expiration-date-year', 'cb-date-ann',
'expiration-date-yy', 'expiration-date-yyyy', 'validity-year', 'exp-date-year', 'date-y'])) {
2017-11-11 14:10:47 +01:00
fillFields.expYear = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.code && this.isFieldMatch(f[attr],
['cvv', 'cvc', 'cvv2', 'cc-csc', 'cc-cvv', 'card-csc', 'card-cvv', 'cvd', 'cid', 'cvc2',
'cnv', 'cvn2', 'cc-code', 'card-code', 'code-securite', 'security-code', 'crypto',
2019-02-07 20:26:50 +01:00
'card-verif', 'verification-code', 'csc', 'ccv'])) {
2017-11-11 14:10:47 +01:00
fillFields.code = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.brand && this.isFieldMatch(f[attr],
['cc-type', 'card-type', 'card-brand', 'cc-brand', 'cb-type'])) {
2017-11-11 14:10:47 +01:00
fillFields.brand = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-06 20:16:06 +01:00
}
2018-05-13 04:21:39 +02:00
}
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
const card = options.cipher.card;
this.makeScriptAction(fillScript, card, fillFields, filledFields, 'cardholderName');
this.makeScriptAction(fillScript, card, fillFields, filledFields, 'number');
this.makeScriptAction(fillScript, card, fillFields, filledFields, 'code');
this.makeScriptAction(fillScript, card, fillFields, filledFields, 'brand');
if (fillFields.expMonth && this.hasValue(card.expMonth)) {
let expMonth: string = card.expMonth;
2017-11-24 21:49:13 +01:00
if (fillFields.expMonth.selectInfo && fillFields.expMonth.selectInfo.options) {
let index: number = null;
const siOptions = fillFields.expMonth.selectInfo.options;
if (siOptions.length === 12) {
2017-11-24 21:49:13 +01:00
index = parseInt(card.expMonth, null) - 1;
} else if (siOptions.length === 13) {
if (siOptions[0][0] != null && siOptions[0][0] !== '' &&
(siOptions[12][0] == null || siOptions[12][0] === '')) {
index = parseInt(card.expMonth, null) - 1;
} else {
index = parseInt(card.expMonth, null);
}
2017-11-24 21:49:13 +01:00
}
if (index != null) {
const option = siOptions[index];
2017-11-24 21:49:13 +01:00
if (option.length > 1) {
expMonth = option[1];
}
}
2019-01-15 04:45:12 +01:00
} else if ((this.fieldAttrsContain(fillFields.expMonth, 'mm') || fillFields.expMonth.maxLength === 2)
&& expMonth.length === 1) {
expMonth = '0' + expMonth;
2017-11-24 21:49:13 +01:00
}
filledFields[fillFields.expMonth.opid] = fillFields.expMonth;
2019-04-21 03:29:13 +02:00
this.fillByOpid(fillScript, fillFields.expMonth, expMonth);
2017-11-24 21:49:13 +01:00
}
if (fillFields.expYear && this.hasValue(card.expYear)) {
let expYear: string = card.expYear;
if (fillFields.expYear.selectInfo && fillFields.expYear.selectInfo.options) {
for (let i = 0; i < fillFields.expYear.selectInfo.options.length; i++) {
const o: [string, string] = fillFields.expYear.selectInfo.options[i];
2019-02-07 20:26:50 +01:00
if (o[0] === card.expYear || o[1] === card.expYear) {
expYear = o[1];
break;
}
if (o[1].length === 2 && card.expYear.length === 4 && o[1] === card.expYear.substring(2)) {
expYear = o[1];
break;
}
const colonIndex = o[1].indexOf(':');
if (colonIndex > -1 && o[1].length > colonIndex + 1) {
const val = o[1].substring(colonIndex + 2);
if (val != null && val.trim() !== '' && val === card.expYear) {
expYear = o[1];
break;
}
}
}
} else if (this.fieldAttrsContain(fillFields.expYear, 'yyyy') || fillFields.expYear.maxLength === 4) {
if (expYear.length === 2) {
expYear = '20' + expYear;
}
2019-01-15 04:45:12 +01:00
} else if (this.fieldAttrsContain(fillFields.expYear, 'yy') || fillFields.expYear.maxLength === 2) {
if (expYear.length === 4) {
expYear = expYear.substr(2);
}
}
filledFields[fillFields.expYear.opid] = fillFields.expYear;
2019-04-21 03:29:13 +02:00
this.fillByOpid(fillScript, fillFields.expYear, expYear);
}
if (fillFields.exp && this.hasValue(card.expMonth) && this.hasValue(card.expYear)) {
2018-03-16 02:16:40 +01:00
const fullMonth = ('0' + card.expMonth).slice(-2);
let fullYear: string = card.expYear;
let partYear: string = null;
if (fullYear.length === 2) {
partYear = fullYear;
fullYear = '20' + fullYear;
} else if (fullYear.length === 4) {
partYear = fullYear.substr(2, 2);
}
let exp: string = null;
for (let i = 0; i < MonthAbbr.length; i++) {
if (this.fieldAttrsContain(fillFields.exp, MonthAbbr[i] + '/' + YearAbbrShort[i]) &&
partYear != null) {
exp = fullMonth + '/' + partYear;
} else if (this.fieldAttrsContain(fillFields.exp, MonthAbbr[i] + '/' + YearAbbrLong[i])) {
exp = fullMonth + '/' + fullYear;
} else if (this.fieldAttrsContain(fillFields.exp, YearAbbrShort[i] + '/' + MonthAbbr[i]) &&
partYear != null) {
exp = partYear + '/' + fullMonth;
} else if (this.fieldAttrsContain(fillFields.exp, YearAbbrLong[i] + '/' + MonthAbbr[i])) {
exp = fullYear + '/' + fullMonth;
} else if (this.fieldAttrsContain(fillFields.exp, MonthAbbr[i] + '-' + YearAbbrShort[i]) &&
partYear != null) {
exp = fullMonth + '-' + partYear;
} else if (this.fieldAttrsContain(fillFields.exp, MonthAbbr[i] + '-' + YearAbbrLong[i])) {
exp = fullMonth + '-' + fullYear;
} else if (this.fieldAttrsContain(fillFields.exp, YearAbbrShort[i] + '-' + MonthAbbr[i]) &&
partYear != null) {
exp = partYear + '-' + fullMonth;
} else if (this.fieldAttrsContain(fillFields.exp, YearAbbrLong[i] + '-' + MonthAbbr[i])) {
exp = fullYear + '-' + fullMonth;
} else if (this.fieldAttrsContain(fillFields.exp, YearAbbrShort[i] + MonthAbbr[i]) &&
partYear != null) {
exp = partYear + fullMonth;
} else if (this.fieldAttrsContain(fillFields.exp, YearAbbrLong[i] + MonthAbbr[i])) {
exp = fullYear + fullMonth;
} else if (this.fieldAttrsContain(fillFields.exp, MonthAbbr[i] + YearAbbrShort[i]) &&
partYear != null) {
exp = fullMonth + partYear;
} else if (this.fieldAttrsContain(fillFields.exp, MonthAbbr[i] + YearAbbrLong[i])) {
exp = fullMonth + fullYear;
}
if (exp != null) {
break;
}
}
if (exp == null) {
2018-03-16 02:16:40 +01:00
exp = fullYear + '-' + fullMonth;
2017-11-06 20:16:06 +01:00
}
this.makeScriptActionWithValue(fillScript, exp, fillFields.exp, filledFields);
2017-11-06 20:16:06 +01:00
}
return fillScript;
}
2018-03-16 02:16:40 +01:00
private fieldAttrsContain(field: any, containsVal: string) {
if (!field) {
return false;
}
let doesContain = false;
2021-02-10 16:40:15 +01:00
CardAttributesExtended.forEach(attr => {
2018-03-16 02:16:40 +01:00
if (doesContain || !field.hasOwnProperty(attr) || !field[attr]) {
return;
}
let val = field[attr];
val = val.replace(/ /g, '').toLowerCase();
doesContain = val.indexOf(containsVal) > -1;
});
return doesContain;
}
2017-11-06 20:16:06 +01:00
private generateIdentityFillScript(fillScript: AutofillScript, pageDetails: any,
2017-11-22 14:31:22 +01:00
filledFields: { [id: string]: AutofillField; }, options: any): AutofillScript {
2017-11-06 20:16:06 +01:00
if (!options.cipher.identity) {
return null;
}
const fillFields: { [id: string]: AutofillField; } = {};
2017-11-16 18:49:23 +01:00
pageDetails.fields.forEach((f: any) => {
if (this.forCustomFieldsOnly(f)) {
return;
}
if (this.isExcludedType(f.type, ExcludedAutofillTypes)) {
return;
}
2018-05-13 04:21:39 +02:00
for (let i = 0; i < IdentityAttributes.length; i++) {
const attr = IdentityAttributes[i];
if (!f.hasOwnProperty(attr) || !f[attr] || !f.viewable) {
2018-05-13 04:21:39 +02:00
continue;
2017-11-11 14:10:47 +01:00
}
// ref https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill
// ref https://developers.google.com/web/fundamentals/design-and-ux/input/forms/
if (!fillFields.name && this.isFieldMatch(f[attr],
['name', 'full-name', 'your-name'], ['full-name', 'your-name'])) {
fillFields.name = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.firstName && this.isFieldMatch(f[attr],
2020-10-24 13:01:03 +02:00
FirstnameFieldNames)) {
2017-11-11 14:10:47 +01:00
fillFields.firstName = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.middleName && this.isFieldMatch(f[attr],
['m-name', 'middle-name', 'additional-name', 'middle-initial', 'middle-n', 'middle-i'])) {
fillFields.middleName = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.lastName && this.isFieldMatch(f[attr],
2020-10-24 13:01:03 +02:00
LastnameFieldNames)) {
2017-11-11 14:10:47 +01:00
fillFields.lastName = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.title && this.isFieldMatch(f[attr],
['honorific-prefix', 'prefix', 'title'])) {
fillFields.title = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.email && this.isFieldMatch(f[attr],
['e-mail', 'email-address'])) {
fillFields.email = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.address && this.isFieldMatch(f[attr],
['address', 'street-address', 'addr', 'street', 'mailing-addr', 'billing-addr',
'mail-addr', 'bill-addr'], ['mailing-addr', 'billing-addr', 'mail-addr', 'bill-addr'])) {
2017-11-11 14:10:47 +01:00
fillFields.address = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.address1 && this.isFieldMatch(f[attr],
2018-05-24 14:57:14 +02:00
['address-1', 'address-line-1', 'addr-1', 'street-1'])) {
2017-11-11 14:10:47 +01:00
fillFields.address1 = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.address2 && this.isFieldMatch(f[attr],
2018-05-24 14:57:14 +02:00
['address-2', 'address-line-2', 'addr-2', 'street-2'])) {
2017-11-11 14:10:47 +01:00
fillFields.address2 = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.address3 && this.isFieldMatch(f[attr],
2018-05-24 14:57:14 +02:00
['address-3', 'address-line-3', 'addr-3', 'street-3'])) {
2017-11-11 14:10:47 +01:00
fillFields.address3 = f;
2018-05-13 04:21:39 +02:00
break;
} else if (!fillFields.postalCode && this.isFieldMatch(f[attr],
['postal', 'zip', 'zip2', 'zip-code', 'postal-code', 'post-code', 'address-zip',
'address-postal', 'address-code', 'address-postal-code', 'address-zip-code'])) {
fillFields.postalCode = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.city && this.isFieldMatch(f[attr],
['city', 'town', 'address-level-2', 'address-city', 'address-town'])) {
fillFields.city = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.state && this.isFieldMatch(f[attr],
['state', 'province', 'provence', 'address-level-1', 'address-state',
'address-province'])) {
fillFields.state = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.country && this.isFieldMatch(f[attr],
['country', 'country-code', 'country-name', 'address-country', 'address-country-name',
'address-country-code'])) {
fillFields.country = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.phone && this.isFieldMatch(f[attr],
['phone', 'mobile', 'mobile-phone', 'tel', 'telephone', 'phone-number'])) {
fillFields.phone = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.username && this.isFieldMatch(f[attr],
['user-name', 'user-id', 'screen-name'])) {
fillFields.username = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-11 14:10:47 +01:00
} else if (!fillFields.company && this.isFieldMatch(f[attr],
['company', 'company-name', 'organization', 'organization-name'])) {
fillFields.company = f;
2018-05-13 04:21:39 +02:00
break;
2017-11-06 20:16:06 +01:00
}
2018-05-13 04:21:39 +02:00
}
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
const identity = options.cipher.identity;
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'title');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'firstName');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'middleName');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'lastName');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'address1');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'address2');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'address3');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'city');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'postalCode');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'company');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'email');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'phone');
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'username');
let filledState = false;
if (fillFields.state && identity.state && identity.state.length > 2) {
const stateLower = identity.state.toLowerCase();
const isoState = IsoStates[stateLower] || IsoProvinces[stateLower];
if (isoState) {
filledState = true;
this.makeScriptActionWithValue(fillScript, isoState, fillFields.state, filledFields);
2017-11-06 20:16:06 +01:00
}
}
if (!filledState) {
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'state');
}
let filledCountry = false;
if (fillFields.country && identity.country && identity.country.length > 2) {
const countryLower = identity.country.toLowerCase();
const isoCountry = IsoCountries[countryLower];
if (isoCountry) {
filledCountry = true;
this.makeScriptActionWithValue(fillScript, isoCountry, fillFields.country, filledFields);
2017-11-06 20:16:06 +01:00
}
}
if (!filledCountry) {
this.makeScriptAction(fillScript, identity, fillFields, filledFields, 'country');
}
if (fillFields.name && (identity.firstName || identity.lastName)) {
let fullName = '';
if (this.hasValue(identity.firstName)) {
2017-11-06 20:16:06 +01:00
fullName = identity.firstName;
}
if (this.hasValue(identity.middleName)) {
2017-11-06 20:16:06 +01:00
if (fullName !== '') {
fullName += ' ';
}
fullName += identity.middleName;
}
if (this.hasValue(identity.lastName)) {
2017-11-06 20:16:06 +01:00
if (fullName !== '') {
fullName += ' ';
}
fullName += identity.lastName;
}
this.makeScriptActionWithValue(fillScript, fullName, fillFields.name, filledFields);
2017-11-06 20:16:06 +01:00
}
if (fillFields.address && this.hasValue(identity.address1)) {
2017-11-06 20:16:06 +01:00
let address = '';
if (this.hasValue(identity.address1)) {
2017-11-06 20:16:06 +01:00
address = identity.address1;
}
if (this.hasValue(identity.address2)) {
2017-11-06 20:16:06 +01:00
if (address !== '') {
address += ', ';
}
address += identity.address2;
}
if (this.hasValue(identity.address3)) {
2017-11-06 20:16:06 +01:00
if (address !== '') {
address += ', ';
}
address += identity.address3;
}
this.makeScriptActionWithValue(fillScript, address, fillFields.address, filledFields);
2017-11-06 20:16:06 +01:00
}
return fillScript;
}
private isExcludedType(type: string, excludedTypes: string[]) {
return excludedTypes.indexOf(type) > -1;
}
private isFieldMatch(value: string, options: string[], containsOptions?: string[]): boolean {
2018-03-16 01:27:02 +01:00
value = value.trim().toLowerCase().replace(/[^a-zA-Z0-9]+/g, '');
2017-11-16 18:49:23 +01:00
for (let i = 0; i < options.length; i++) {
let option = options[i];
const checkValueContains = containsOptions == null || containsOptions.indexOf(option) > -1;
2018-07-04 04:24:21 +02:00
option = option.toLowerCase().replace(/-/g, '');
if (value === option || (checkValueContains && value.indexOf(option) > -1)) {
return true;
}
}
return false;
}
private makeScriptAction(fillScript: AutofillScript, cipherData: any, fillFields: { [id: string]: AutofillField; },
2017-11-24 21:49:13 +01:00
filledFields: { [id: string]: AutofillField; }, dataProp: string, fieldProp?: string) {
2017-11-06 20:16:06 +01:00
fieldProp = fieldProp || dataProp;
this.makeScriptActionWithValue(fillScript, cipherData[dataProp], fillFields[fieldProp], filledFields);
}
private makeScriptActionWithValue(fillScript: AutofillScript, dataValue: any, field: AutofillField,
filledFields: { [id: string]: AutofillField; }) {
let doFill = false;
if (this.hasValue(dataValue) && field) {
if (field.type === 'select-one' && field.selectInfo && field.selectInfo.options) {
for (let i = 0; i < field.selectInfo.options.length; i++) {
const option = field.selectInfo.options[i];
for (let j = 0; j < option.length; j++) {
2018-07-16 17:00:49 +02:00
if (this.hasValue(option[j]) && option[j].toLowerCase() === dataValue.toLowerCase()) {
doFill = true;
if (option.length > 1) {
dataValue = option[1];
}
break;
}
}
if (doFill) {
break;
}
}
} else {
doFill = true;
}
}
if (doFill) {
filledFields[field.opid] = field;
2019-04-21 03:29:13 +02:00
this.fillByOpid(fillScript, field, dataValue);
2017-11-06 20:16:06 +01:00
}
}
private loadPasswordFields(pageDetails: AutofillPageDetails, canBeHidden: boolean, canBeReadOnly: boolean,
mustBeEmpty: boolean, fillNewPassword: boolean) {
2017-11-06 20:16:06 +01:00
const arr: AutofillField[] = [];
2021-02-10 16:40:15 +01:00
pageDetails.fields.forEach(f => {
if (this.forCustomFieldsOnly(f)) {
return;
}
2018-08-03 17:07:40 +02:00
const isPassword = f.type === 'password';
const valueIsLikePassword = (value: string) => {
if (value == null) {
return false;
}
// Removes all whitespace, _ and - characters
const cleanedValue = value.toLowerCase().replace(/[\s_\-]/g, '');
if (cleanedValue.indexOf('password') < 0) {
return false;
}
const ignoreList = ['onetimepassword', 'captcha', 'findanything'];
2021-02-10 16:40:15 +01:00
if (ignoreList.some(i => cleanedValue.indexOf(i) > -1)) {
return false;
}
return true;
};
const isLikePassword = () => {
if (f.type !== 'text') {
return false;
}
if (valueIsLikePassword(f.htmlID)) {
return true;
}
if (valueIsLikePassword(f.htmlName)) {
return true;
}
if (valueIsLikePassword(f.placeholder)) {
return true;
}
return false;
};
if (!f.disabled && (canBeReadOnly || !f.readonly) && (isPassword || isLikePassword())
&& (canBeHidden || f.viewable) && (!mustBeEmpty || f.value == null || f.value.trim() === '')
&& (fillNewPassword || f.autoCompleteType !== 'new-password')) {
2017-11-06 20:16:06 +01:00
arr.push(f);
}
2017-11-16 18:49:23 +01:00
});
2017-11-06 20:16:06 +01:00
return arr;
}
private findUsernameField(pageDetails: AutofillPageDetails, passwordField: AutofillField, canBeHidden: boolean,
canBeReadOnly: boolean, withoutForm: boolean) {
2017-11-06 20:16:06 +01:00
let usernameField: AutofillField = null;
2017-11-16 18:49:23 +01:00
for (let i = 0; i < pageDetails.fields.length; i++) {
const f = pageDetails.fields[i];
if (this.forCustomFieldsOnly(f)) {
continue;
}
2017-11-06 20:16:06 +01:00
if (f.elementNumber >= passwordField.elementNumber) {
break;
}
if (!f.disabled && (canBeReadOnly || !f.readonly) &&
2017-11-22 14:31:22 +01:00
(withoutForm || f.form === passwordField.form) && (canBeHidden || f.viewable) &&
2017-11-06 20:16:06 +01:00
(f.type === 'text' || f.type === 'email' || f.type === 'tel')) {
usernameField = f;
if (this.findMatchingFieldIndex(f, UsernameFieldNames) > -1) {
// We found an exact match. No need to keep looking.
break;
}
}
}
return usernameField;
}
private findMatchingFieldIndex(field: AutofillField, names: string[]): number {
2017-11-13 19:07:59 +01:00
for (let i = 0; i < names.length; i++) {
if (names[i].indexOf('=') > -1) {
if (this.fieldPropertyIsPrefixMatch(field, 'htmlID', names[i], 'id')) {
return i;
}
if (this.fieldPropertyIsPrefixMatch(field, 'htmlName', names[i], 'name')) {
return i;
}
if (this.fieldPropertyIsPrefixMatch(field, 'label-tag', names[i], 'label')) {
return i;
}
if (this.fieldPropertyIsPrefixMatch(field, 'label-aria', names[i], 'label')) {
return i;
}
if (this.fieldPropertyIsPrefixMatch(field, 'placeholder', names[i], 'placeholder')) {
return i;
}
}
2017-11-13 19:07:59 +01:00
if (this.fieldPropertyIsMatch(field, 'htmlID', names[i])) {
return i;
}
if (this.fieldPropertyIsMatch(field, 'htmlName', names[i])) {
return i;
}
if (this.fieldPropertyIsMatch(field, 'label-tag', names[i])) {
return i;
}
2018-11-25 04:54:30 +01:00
if (this.fieldPropertyIsMatch(field, 'label-aria', names[i])) {
return i;
}
2017-11-13 19:07:59 +01:00
if (this.fieldPropertyIsMatch(field, 'placeholder', names[i])) {
return i;
}
2017-11-06 20:16:06 +01:00
}
2017-11-13 19:07:59 +01:00
return -1;
}
private fieldPropertyIsPrefixMatch(field: any, property: string, name: string, prefix: string,
separator = '='): boolean {
if (name.indexOf(prefix + separator) === 0) {
const sepIndex = name.indexOf(separator);
const val = name.substring(sepIndex + 1);
return val != null && this.fieldPropertyIsMatch(field, property, val);
}
return false;
}
2017-11-13 19:07:59 +01:00
private fieldPropertyIsMatch(field: any, property: string, name: string): boolean {
let fieldVal = field[property] as string;
if (!this.hasValue(fieldVal)) {
2017-11-13 19:07:59 +01:00
return false;
2017-11-06 20:16:06 +01:00
}
2017-11-13 19:07:59 +01:00
2017-11-13 20:09:45 +01:00
fieldVal = fieldVal.trim().replace(/(?:\r\n|\r|\n)/g, '');
if (name.startsWith('regex=')) {
2017-11-13 19:07:59 +01:00
try {
2017-11-13 20:09:45 +01:00
const regexParts = name.split('=', 2);
2017-11-13 19:07:59 +01:00
if (regexParts.length === 2) {
const regex = new RegExp(regexParts[1], 'i');
return regex.test(fieldVal);
}
} catch (e) {
this.logService.error(e);
}
2017-11-13 20:09:45 +01:00
} else if (name.startsWith('csv=')) {
const csvParts = name.split('=', 2);
if (csvParts.length === 2) {
const csvVals = csvParts[1].split(',');
2017-11-16 18:49:23 +01:00
for (let i = 0; i < csvVals.length; i++) {
const val = csvVals[i];
2017-11-13 20:09:45 +01:00
if (val != null && val.trim().toLowerCase() === fieldVal.toLowerCase()) {
return true;
}
}
return false;
}
2017-11-06 20:16:06 +01:00
}
2017-11-13 19:07:59 +01:00
return fieldVal.toLowerCase() === name;
2017-11-06 20:16:06 +01:00
}
private fieldIsFuzzyMatch(field: AutofillField, names: string[]): boolean {
if (this.hasValue(field.htmlID) && this.fuzzyMatch(names, field.htmlID)) {
2017-11-06 20:16:06 +01:00
return true;
}
if (this.hasValue(field.htmlName) && this.fuzzyMatch(names, field.htmlName)) {
2017-11-06 20:16:06 +01:00
return true;
}
if (this.hasValue(field['label-tag']) && this.fuzzyMatch(names, field['label-tag'])) {
2017-11-06 20:16:06 +01:00
return true;
}
if (this.hasValue(field.placeholder) && this.fuzzyMatch(names, field.placeholder)) {
return true;
}
if (this.hasValue(field['label-left']) && this.fuzzyMatch(names, field['label-left'])) {
return true;
}
if (this.hasValue(field['label-top']) && this.fuzzyMatch(names, field['label-top'])) {
2017-11-06 20:16:06 +01:00
return true;
}
2018-11-25 04:54:30 +01:00
if (this.hasValue(field['label-aria']) && this.fuzzyMatch(names, field['label-aria'])) {
return true;
}
2017-11-06 20:16:06 +01:00
return false;
}
private fuzzyMatch(options: string[], value: string): boolean {
if (options == null || options.length === 0 || value == null || value === '') {
return false;
}
value = value.replace(/(?:\r\n|\r|\n)/g, '').trim().toLowerCase();
2017-11-16 18:49:23 +01:00
for (let i = 0; i < options.length; i++) {
if (value.indexOf(options[i]) > -1) {
2017-11-06 20:16:06 +01:00
return true;
}
}
return false;
}
private hasValue(str: string): boolean {
return str && str !== '';
}
2017-11-06 20:16:06 +01:00
private setFillScriptForFocus(filledFields: { [id: string]: AutofillField; },
2017-11-13 20:09:45 +01:00
fillScript: AutofillScript): AutofillScript {
2017-11-06 20:16:06 +01:00
let lastField: AutofillField = null;
let lastPasswordField: AutofillField = null;
for (const opid in filledFields) {
if (filledFields.hasOwnProperty(opid) && filledFields[opid].viewable) {
lastField = filledFields[opid];
if (filledFields[opid].type === 'password') {
lastPasswordField = filledFields[opid];
}
}
}
// Prioritize password field over others.
if (lastPasswordField) {
fillScript.script.push(['focus_by_opid', lastPasswordField.opid]);
} else if (lastField) {
fillScript.script.push(['focus_by_opid', lastField.opid]);
}
return fillScript;
}
2019-04-21 03:29:13 +02:00
private fillByOpid(fillScript: AutofillScript, field: AutofillField, value: string): void {
if (field.maxLength && value && value.length > field.maxLength) {
2019-04-21 03:29:13 +02:00
value = value.substr(0, value.length);
}
if (field.tagName !== 'span') {
fillScript.script.push(['click_on_opid', field.opid]);
fillScript.script.push(['focus_by_opid', field.opid]);
}
2019-04-21 03:29:13 +02:00
fillScript.script.push(['fill_by_opid', field.opid, value]);
}
private forCustomFieldsOnly(field: AutofillField): boolean {
return field.tagName === 'span';
}
2017-11-06 20:16:06 +01:00
}