/
insert-autofill-content.service.ts
390 lines (344 loc) · 13.5 KB
/
insert-autofill-content.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import { EVENTS, TYPE_CHECK } from "@bitwarden/common/autofill/constants";
import AutofillScript, { AutofillInsertActions, FillScript } from "../models/autofill-script";
import { FormFieldElement } from "../types";
import {
elementIsFillableFormField,
elementIsInputElement,
elementIsSelectElement,
elementIsTextAreaElement,
nodeIsInputElement,
} from "../utils";
import { InsertAutofillContentService as InsertAutofillContentServiceInterface } from "./abstractions/insert-autofill-content.service";
import CollectAutofillContentService from "./collect-autofill-content.service";
import DomElementVisibilityService from "./dom-element-visibility.service";
class InsertAutofillContentService implements InsertAutofillContentServiceInterface {
private readonly domElementVisibilityService: DomElementVisibilityService;
private readonly collectAutofillContentService: CollectAutofillContentService;
private readonly autofillInsertActions: AutofillInsertActions = {
fill_by_opid: ({ opid, value }) => this.handleFillFieldByOpidAction(opid, value),
click_on_opid: ({ opid }) => this.handleClickOnFieldByOpidAction(opid),
focus_by_opid: ({ opid }) => this.handleFocusOnFieldByOpidAction(opid),
};
/**
* InsertAutofillContentService constructor. Instantiates the
* DomElementVisibilityService and CollectAutofillContentService classes.
*/
constructor(
domElementVisibilityService: DomElementVisibilityService,
collectAutofillContentService: CollectAutofillContentService,
) {
this.domElementVisibilityService = domElementVisibilityService;
this.collectAutofillContentService = collectAutofillContentService;
}
/**
* Handles autofill of the forms on the current page based on the
* data within the passed fill script object.
* @param {AutofillScript} fillScript
* @returns {Promise<void>}
* @public
*/
async fillForm(fillScript: AutofillScript) {
if (
!fillScript.script?.length ||
this.fillingWithinSandboxedIframe() ||
this.userCancelledInsecureUrlAutofill(fillScript.savedUrls) ||
this.userCancelledUntrustedIframeAutofill(fillScript)
) {
return;
}
const fillActionPromises = fillScript.script.map(this.runFillScriptAction);
await Promise.all(fillActionPromises);
}
/**
* Identifies if the execution of this script is happening
* within a sandboxed iframe.
* @returns {boolean}
* @private
*/
private fillingWithinSandboxedIframe() {
return (
String(self.origin).toLowerCase() === "null" ||
globalThis.frameElement?.hasAttribute("sandbox") ||
globalThis.location.hostname === ""
);
}
/**
* Checks if the autofill is occurring on a page that can be considered secure. If the page is not secure,
* the user is prompted to confirm that they want to autofill on the page.
* @param {string[] | null} savedUrls
* @returns {boolean}
* @private
*/
private userCancelledInsecureUrlAutofill(savedUrls?: string[] | null): boolean {
if (
!savedUrls?.some((url) => url.startsWith(`https://${globalThis.location.hostname}`)) ||
globalThis.location.protocol !== "http:" ||
!this.isPasswordFieldWithinDocument()
) {
return false;
}
const confirmationWarning = [
chrome.i18n.getMessage("insecurePageWarning"),
chrome.i18n.getMessage("insecurePageWarningFillPrompt", [globalThis.location.hostname]),
].join("\n\n");
return !globalThis.confirm(confirmationWarning);
}
/**
* Checks if there is a password field within the current document. Includes
* password fields that are present within the shadow DOM.
* @returns {boolean}
* @private
*/
private isPasswordFieldWithinDocument(): boolean {
return Boolean(
this.collectAutofillContentService.queryAllTreeWalkerNodes(
document.documentElement,
(node: Node) => nodeIsInputElement(node) && node.type === "password",
false,
)?.length,
);
}
/**
* Checking if the autofill is occurring within an untrusted iframe. If the page is within an untrusted iframe,
* the user is prompted to confirm that they want to autofill on the page. If the user cancels the autofill,
* the script will not continue.
*
* Note: confirm() is blocked by sandboxed iframes, but we don't want to fill sandboxed iframes anyway.
* If this occurs, confirm() returns false without displaying the dialog box, and autofill will be aborted.
* The browser may print a message to the console, but this is not a standard error that we can handle.
* @param {AutofillScript} fillScript
* @returns {boolean}
* @private
*/
private userCancelledUntrustedIframeAutofill(fillScript: AutofillScript): boolean {
if (!fillScript.untrustedIframe) {
return false;
}
const confirmationWarning = [
chrome.i18n.getMessage("autofillIframeWarning"),
chrome.i18n.getMessage("autofillIframeWarningTip", [globalThis.location.hostname]),
].join("\n\n");
return !globalThis.confirm(confirmationWarning);
}
/**
* Runs the autofill action based on the action type and the opid.
* Each action is subsequently delayed by 20 milliseconds.
* @param {"click_on_opid" | "focus_by_opid" | "fill_by_opid"} action
* @param {string} opid
* @param {string} value
* @param {number} actionIndex
* @returns {Promise<void>}
* @private
*/
private runFillScriptAction = (
[action, opid, value]: FillScript,
actionIndex: number,
): Promise<void> => {
if (!opid || !this.autofillInsertActions[action]) {
return;
}
const delayActionsInMilliseconds = 20;
return new Promise((resolve) =>
setTimeout(() => {
this.autofillInsertActions[action]({ opid, value });
resolve();
}, delayActionsInMilliseconds * actionIndex),
);
};
/**
* Queries the DOM for an element by opid and inserts the passed value into the element.
* @param {string} opid
* @param {string} value
* @private
*/
private handleFillFieldByOpidAction(opid: string, value: string) {
const element = this.collectAutofillContentService.getAutofillFieldElementByOpid(opid);
this.insertValueIntoField(element, value);
}
/**
* Handles finding an element by opid and triggering a click event on the element.
* @param {string} opid
* @private
*/
private handleClickOnFieldByOpidAction(opid: string) {
const element = this.collectAutofillContentService.getAutofillFieldElementByOpid(opid);
this.triggerClickOnElement(element);
}
/**
* Handles finding an element by opid and triggering click and focus events on the element.
* To ensure that we trigger a blur event correctly on a filled field, we first check if the
* element is already focused. If it is, we blur the element before focusing on it again.
*
* @param {string} opid - The opid of the element to focus on.
*/
private handleFocusOnFieldByOpidAction(opid: string) {
const element = this.collectAutofillContentService.getAutofillFieldElementByOpid(opid);
if (document.activeElement === element) {
element.blur();
}
this.simulateUserMouseClickAndFocusEventInteractions(element, true);
}
/**
* Identifies the type of element passed and inserts the value into the element.
* Will trigger simulated events on the element to ensure that the element is
* properly updated.
* @param {FormFieldElement | null} element
* @param {string} value
* @private
*/
private insertValueIntoField(element: FormFieldElement | null, value: string) {
const elementCanBeReadonly =
elementIsInputElement(element) || elementIsTextAreaElement(element);
const elementCanBeFilled = elementCanBeReadonly || elementIsSelectElement(element);
if (
!element ||
!value ||
(elementCanBeReadonly && element.readOnly) ||
(elementCanBeFilled && element.disabled)
) {
return;
}
if (!elementIsFillableFormField(element)) {
this.handleInsertValueAndTriggerSimulatedEvents(element, () => (element.innerText = value));
return;
}
const isFillableCheckboxOrRadioElement =
elementIsInputElement(element) &&
new Set(["checkbox", "radio"]).has(element.type) &&
new Set(["true", "y", "1", "yes", "✓"]).has(String(value).toLowerCase());
if (isFillableCheckboxOrRadioElement) {
this.handleInsertValueAndTriggerSimulatedEvents(element, () => (element.checked = true));
return;
}
this.handleInsertValueAndTriggerSimulatedEvents(element, () => (element.value = value));
}
/**
* Simulates pre- and post-insert events on the element meant to mimic user interactions
* while inserting the autofill value into the element.
* @param {FormFieldElement} element
* @param {Function} valueChangeCallback
* @private
*/
private handleInsertValueAndTriggerSimulatedEvents(
element: FormFieldElement,
valueChangeCallback: CallableFunction,
): void {
this.triggerPreInsertEventsOnElement(element);
valueChangeCallback();
this.triggerPostInsertEventsOnElement(element);
this.triggerFillAnimationOnElement(element);
}
/**
* Simulates a mouse click event on the element, including focusing the event, and
* the triggers a simulated keyboard event on the element. Will attempt to ensure
* that the initial element value is not arbitrarily changed by the simulated events.
* @param {FormFieldElement} element
* @private
*/
private triggerPreInsertEventsOnElement(element: FormFieldElement): void {
const initialElementValue = "value" in element ? element.value : "";
this.simulateUserMouseClickAndFocusEventInteractions(element);
this.simulateUserKeyboardEventInteractions(element);
if ("value" in element && initialElementValue !== element.value) {
element.value = initialElementValue;
}
}
/**
* Simulates a keyboard event on the element before assigning the autofilled value to the element, and then
* simulates an input change event on the element to trigger expected events after autofill occurs.
* @param {FormFieldElement} element
* @private
*/
private triggerPostInsertEventsOnElement(element: FormFieldElement): void {
const autofilledValue = "value" in element ? element.value : "";
this.simulateUserKeyboardEventInteractions(element);
if ("value" in element && autofilledValue !== element.value) {
element.value = autofilledValue;
}
this.simulateInputElementChangedEvent(element);
}
/**
* Identifies if a passed element can be animated and sets a class on the element
* to trigger a CSS animation. The animation is removed after a short delay.
* @param {FormFieldElement} element
* @private
*/
private triggerFillAnimationOnElement(element: FormFieldElement): void {
const skipAnimatingElement =
elementIsFillableFormField(element) &&
!new Set(["email", "text", "password", "number", "tel", "url"]).has(element?.type);
if (this.domElementVisibilityService.isElementHiddenByCss(element) || skipAnimatingElement) {
return;
}
element.classList.add("com-bitwarden-browser-animated-fill");
setTimeout(() => element.classList.remove("com-bitwarden-browser-animated-fill"), 200);
}
/**
* Simulates a click event on the element.
* @param {HTMLElement} element
* @private
*/
private triggerClickOnElement(element?: HTMLElement): void {
if (typeof element?.click !== TYPE_CHECK.FUNCTION) {
return;
}
element.click();
}
/**
* Simulates a focus event on the element. Will optionally reset the value of the element
* if the element has a value property.
* @param {HTMLElement | undefined} element
* @param {boolean} shouldResetValue
* @private
*/
private triggerFocusOnElement(element: HTMLElement | undefined, shouldResetValue = false): void {
if (typeof element?.focus !== TYPE_CHECK.FUNCTION) {
return;
}
let initialValue = "";
if (shouldResetValue && "value" in element) {
initialValue = String(element.value);
}
element.focus();
if (initialValue && "value" in element) {
element.value = initialValue;
}
}
/**
* Simulates a mouse click and focus event on the element.
* @param {FormFieldElement} element
* @param {boolean} shouldResetValue
* @private
*/
private simulateUserMouseClickAndFocusEventInteractions(
element: FormFieldElement,
shouldResetValue = false,
): void {
this.triggerClickOnElement(element);
this.triggerFocusOnElement(element, shouldResetValue);
}
/**
* Simulates several keyboard events on the element, mocking a user interaction with the element.
* @param {FormFieldElement} element
* @private
*/
private simulateUserKeyboardEventInteractions(element: FormFieldElement): void {
const simulatedKeyboardEvents = [EVENTS.KEYDOWN, EVENTS.KEYPRESS, EVENTS.KEYUP];
for (let index = 0; index < simulatedKeyboardEvents.length; index++) {
element.dispatchEvent(new KeyboardEvent(simulatedKeyboardEvents[index], { bubbles: true }));
}
}
/**
* Simulates an input change event on the element, mocking behavior that would occur if a user
* manually changed a value for the element.
* @param {FormFieldElement} element
* @private
*/
private simulateInputElementChangedEvent(element: FormFieldElement): void {
const simulatedInputEvents = [EVENTS.INPUT, EVENTS.CHANGE];
for (let index = 0; index < simulatedInputEvents.length; index++) {
element.dispatchEvent(new Event(simulatedInputEvents[index], { bubbles: true }));
}
}
}
export default InsertAutofillContentService;