Debian Codegraph @

browserpass-extension/src/inject.js

raw
1 (function () {
2 const FORM_MARKERS = ["login", "log-in", "log_in", "signin", "sign-in", "sign_in"];
3 const OPENID_FIELDS = {
4 selectors: ["input[name*=openid i]", "input[id*=openid i]", "input[class*=openid i]"],
5 types: ["text"],
6 };
7 const USERNAME_FIELDS = {
8 selectors: [
9 "input[autocomplete=username i]",
10
11 "input[name=login i]",
12 "input[name=user i]",
13 "input[name=username i]",
14 "input[name=email i]",
15 "input[name=alias i]",
16 "input[id=login i]",
17 "input[id=user i]",
18 "input[id=username i]",
19 "input[id=email i]",
20 "input[id=alias i]",
21 "input[class=login i]",
22 "input[class=user i]",
23 "input[class=username i]",
24 "input[class=email i]",
25 "input[class=alias i]",
26
27 "input[name*=login i]",
28 "input[name*=user i]",
29 "input[name*=email i]",
30 "input[name*=alias i]",
31 "input[id*=login i]",
32 "input[id*=user i]",
33 "input[id*=email i]",
34 "input[id*=alias i]",
35 "input[class*=login i]",
36 "input[class*=user i]",
37 "input[class*=email i]",
38 "input[class*=alias i]",
39
40 "input[type=email i]",
41 "input[autocomplete=email i]",
42 "input[type=text i]",
43 "input[type=tel i]",
44 ],
45 types: ["email", "text", "tel"],
46 };
47 const PASSWORD_FIELDS = {
48 selectors: [
49 "input[type=password i][autocomplete=current-password i]",
50 "input[type=password i]",
51 ],
52 };
53 const INPUT_FIELDS = {
54 selectors: PASSWORD_FIELDS.selectors
55 .concat(USERNAME_FIELDS.selectors)
56 .concat(OPENID_FIELDS.selectors),
57 };
58 const SUBMIT_FIELDS = {
59 selectors: [
60 "[type=submit i]",
61
62 "button[name=login i]",
63 "button[name=log-in i]",
64 "button[name=log_in i]",
65 "button[name=signin i]",
66 "button[name=sign-in i]",
67 "button[name=sign_in i]",
68 "button[id=login i]",
69 "button[id=log-in i]",
70 "button[id=log_in i]",
71 "button[id=signin i]",
72 "button[id=sign-in i]",
73 "button[id=sign_in i]",
74 "button[class=login i]",
75 "button[class=log-in i]",
76 "button[class=log_in i]",
77 "button[class=signin i]",
78 "button[class=sign-in i]",
79 "button[class=sign_in i]",
80 "input[type=button i][name=login i]",
81 "input[type=button i][name=log-in i]",
82 "input[type=button i][name=log_in i]",
83 "input[type=button i][name=signin i]",
84 "input[type=button i][name=sign-in i]",
85 "input[type=button i][name=sign_in i]",
86 "input[type=button i][id=login i]",
87 "input[type=button i][id=log-in i]",
88 "input[type=button i][id=log_in i]",
89 "input[type=button i][id=signin i]",
90 "input[type=button i][id=sign-in i]",
91 "input[type=button i][id=sign_in i]",
92 "input[type=button i][class=login i]",
93 "input[type=button i][class=log-in i]",
94 "input[type=button i][class=log_in i]",
95 "input[type=button i][class=signin i]",
96 "input[type=button i][class=sign-in i]",
97 "input[type=button i][class=sign_in i]",
98
99 "button[name*=login i]",
100 "button[name*=log-in i]",
101 "button[name*=log_in i]",
102 "button[name*=signin i]",
103 "button[name*=sign-in i]",
104 "button[name*=sign_in i]",
105 "button[id*=login i]",
106 "button[id*=log-in i]",
107 "button[id*=log_in i]",
108 "button[id*=signin i]",
109 "button[id*=sign-in i]",
110 "button[id*=sign_in i]",
111 "button[class*=login i]",
112 "button[class*=log-in i]",
113 "button[class*=log_in i]",
114 "button[class*=signin i]",
115 "button[class*=sign-in i]",
116 "button[class*=sign_in i]",
117 "input[type=button i][name*=login i]",
118 "input[type=button i][name*=log-in i]",
119 "input[type=button i][name*=log_in i]",
120 "input[type=button i][name*=signin i]",
121 "input[type=button i][name*=sign-in i]",
122 "input[type=button i][name*=sign_in i]",
123 "input[type=button i][id*=login i]",
124 "input[type=button i][id*=log-in i]",
125 "input[type=button i][id*=log_in i]",
126 "input[type=button i][id*=signin i]",
127 "input[type=button i][id*=sign-in i]",
128 "input[type=button i][id*=sign_in i]",
129 "input[type=button i][class*=login i]",
130 "input[type=button i][class*=log-in i]",
131 "input[type=button i][class*=log_in i]",
132 "input[type=button i][class*=signin i]",
133 "input[type=button i][class*=sign-in i]",
134 "input[type=button i][class*=sign_in i]",
135 ],
136 };
137
138 /**
139 * Fill password
140 *
141 * @since 3.0.0
142 *
143 * @param object request Form fill request
144 * @return object result of filling a form
145 */
146 function fillLogin(request) {
147 var result = {
148 filledFields: [],
149 foreignFill: undefined,
150 };
151
152 // get the login form
153 let loginForm = undefined;
154 if (request.fields.includes("openid")) {
155 // this is an attempt to fill a form containing only openid field
156 loginForm = form(OPENID_FIELDS);
157 } else {
158 // this is an attempt to fill a regular login form
159 loginForm = form(INPUT_FIELDS);
160 }
161
162 // don't attempt to fill non-secret forms unless non-secret filling is allowed
163 if (!request.allowNoSecret && !find(PASSWORD_FIELDS, loginForm)) {
164 return result;
165 }
166
167 // ensure the origin is the same, or ask the user for permissions to continue
168 if (window.location.origin !== request.origin) {
169 if (!request.allowForeign || request.foreignFills[window.location.origin] === false) {
170 return result;
171 }
172 var message =
173 "You have requested to fill login credentials into an embedded document from a " +
174 "different origin than the main document in this tab. Do you wish to proceed?\n\n" +
175 `Tab origin: ${request.origin}\n` +
176 `Embedded origin: ${window.location.origin}`;
177 if (request.foreignFills[window.location.origin] !== true) {
178 result.foreignOrigin = window.location.origin;
179 result.foreignFill = confirm(message);
180 if (!result.foreignFill) {
181 return result;
182 }
183 }
184 }
185
186 // fill login field
187 if (
188 request.fields.includes("login") &&
189 update(USERNAME_FIELDS, request.login.fields.login, loginForm)
190 ) {
191 result.filledFields.push("login");
192 }
193
194 // fill secret field
195 if (
196 request.fields.includes("secret") &&
197 update(PASSWORD_FIELDS, request.login.fields.secret, loginForm)
198 ) {
199 result.filledFields.push("secret");
200 }
201
202 // fill openid field
203 if (
204 request.fields.includes("openid") &&
205 update(OPENID_FIELDS, request.login.fields.openid, loginForm)
206 ) {
207 result.filledFields.push("openid");
208 }
209
210 // finished filling things successfully
211 return result;
212 }
213
214 /**
215 * Focus submit button, and maybe click on it (based on user settings)
216 *
217 * @since 3.0.0
218 *
219 * @param object request Form fill request
220 * @return object result of focusing or submitting a form
221 */
222 function focusOrSubmit(request) {
223 var result = {};
224
225 // get the login form
226 let loginForm = undefined;
227 if (request.filledFields.includes("openid")) {
228 // this is an attempt to focus or submit a form containing only openid field
229 loginForm = form(OPENID_FIELDS);
230 } else {
231 // this is an attempt to focus or submit a regular login form
232 loginForm = form(INPUT_FIELDS);
233 }
234
235 // ensure the origin is the same or allowed
236 if (window.location.origin !== request.origin) {
237 if (!request.allowForeign || request.foreignFills[window.location.origin] === false) {
238 return;
239 }
240 }
241
242 // check for multiple password fields in the login form
243 var password_inputs = queryAllVisible(document, PASSWORD_FIELDS, loginForm);
244 if (password_inputs.length > 1) {
245 // There is likely a field asking for OTP code, so do not submit form just yet
246 password_inputs[1].select();
247 } else {
248 // try to locate the submit button
249 var submit = find(SUBMIT_FIELDS, loginForm);
250
251 // Try to submit the form, or focus on the submit button (based on user settings)
252 if (submit) {
253 if (request.autoSubmit) {
254 submit.click();
255 } else {
256 submit.focus();
257 }
258 } else {
259 // We need to keep focus somewhere within the form, so that Enter hopefully submits the form.
260 for (let selectors of [OPENID_FIELDS, PASSWORD_FIELDS, USERNAME_FIELDS]) {
261 let field = find(selectors, loginForm);
262 if (field) {
263 field.focus();
264 break;
265 }
266 }
267 }
268 }
269
270 return result;
271 }
272
273 /**
274 * Query all visible elements
275 *
276 * @since 3.0.0
277 *
278 * @param DOMElement parent Parent element to query
279 * @param object field Field to search for
280 * @param DOMElement form Search only within this form
281 * @return array List of search results
282 */
283 function queryAllVisible(parent, field, form) {
284 const result = [];
285 for (let i = 0; i < field.selectors.length; i++) {
286 let elems = parent.querySelectorAll(field.selectors[i]);
287 for (let j = 0; j < elems.length; j++) {
288 let elem = elems[j];
289 // Select only elements from specified form
290 if (form && form != elem.form) {
291 continue;
292 }
293 // Ignore disabled fields
294 if (elem.disabled) {
295 continue;
296 }
297 // Elem or its parent has a style 'display: none',
298 // or it is just too narrow to be a real field (a trap for spammers?).
299 if (elem.offsetWidth < 30 || elem.offsetHeight < 10) {
300 continue;
301 }
302 // We may have a whitelist of acceptable field types. If so, skip elements of a different type.
303 if (field.types && field.types.indexOf(elem.type.toLowerCase()) < 0) {
304 continue;
305 }
306 // Elem takes space on the screen, but it or its parent is hidden with a visibility style.
307 let style = window.getComputedStyle(elem);
308 if (style.visibility == "hidden") {
309 continue;
310 }
311 // Elem is outside of the boundaries of the visible viewport.
312 let rect = elem.getBoundingClientRect();
313 if (
314 rect.x + rect.width < 0 ||
315 rect.y + rect.height < 0 ||
316 rect.x > window.innerWidth ||
317 rect.y > window.innerHeight
318 ) {
319 continue;
320 }
321 // Elem is hidden by its or or its parent's opacity rules
322 const OPACITY_LIMIT = 0.1;
323 let opacity = 1;
324 for (
325 let testElem = elem;
326 opacity >= OPACITY_LIMIT && testElem && testElem.nodeType === Node.ELEMENT_NODE;
327 testElem = testElem.parentNode
328 ) {
329 let style = window.getComputedStyle(testElem);
330 if (style.opacity) {
331 opacity *= parseFloat(style.opacity);
332 }
333 }
334 if (opacity < OPACITY_LIMIT) {
335 continue;
336 }
337 // This element is visible, will use it.
338 result.push(elem);
339 }
340 }
341 return result;
342 }
343
344 /**
345 * Query first visible element
346 *
347 * @since 3.0.0
348 *
349 * @param DOMElement parent Parent element to query
350 * @param object field Field to search for
351 * @param DOMElement form Search only within this form
352 * @return array First search result
353 */
354 function queryFirstVisible(parent, field, form) {
355 var elems = queryAllVisible(parent, field, form);
356 return elems.length > 0 ? elems[0] : undefined;
357 }
358
359 /**
360 * Detect the login form
361 *
362 * @since 3.0.0
363 *
364 * @param array selectors Selectors to use to find the right form
365 * @return The login form
366 */
367 function form(selectors) {
368 const elems = queryAllVisible(document, selectors, undefined);
369 const forms = [];
370 for (let elem of elems) {
371 const form = elem.form;
372 if (form && forms.indexOf(form) < 0) {
373 forms.push(form);
374 }
375 }
376
377 // Try to filter only forms that have some identifying marker
378 const markedForms = [];
379 for (let form of forms) {
380 const props = ["id", "name", "class", "action"];
381 for (let marker of FORM_MARKERS) {
382 for (let prop of props) {
383 let propValue = form.getAttribute(prop) || "";
384 if (propValue.toLowerCase().indexOf(marker) > -1) {
385 markedForms.push(form);
386 }
387 }
388 }
389 }
390
391 // Try to filter only forms that have a password field
392 const formsWithPassword = [];
393 for (let form of markedForms) {
394 if (find(PASSWORD_FIELDS, form)) {
395 formsWithPassword.push(form);
396 }
397 }
398
399 // Give up and return the first available form, if any
400 if (formsWithPassword.length > 0) {
401 return formsWithPassword[0];
402 }
403 if (markedForms.length > 0) {
404 return markedForms[0];
405 }
406 if (forms.length > 0) {
407 return forms[0];
408 }
409 return undefined;
410 }
411
412 /**
413 * Find a form field
414 *
415 * @since 3.0.0
416 *
417 * @param object field Field to search for
418 * @param DOMElement form Form to search in
419 * @return DOMElement First matching form field
420 */
421 function find(field, form) {
422 return queryFirstVisible(document, field, form);
423 }
424
425 /**
426 * Update a form field value
427 *
428 * @since 3.0.0
429 *
430 * @param object field Field to update
431 * @param string value Value to set
432 * @param DOMElement form Form for which to set the given field
433 * @return bool Whether the update succeeded
434 */
435 function update(field, value, form) {
436 if (value === undefined) {
437 // undefined values should not be filled, but are always considered successful
438 return true;
439 }
440
441 if (!value.length) {
442 return false;
443 }
444
445 // Focus the input element first
446 let el = find(field, form);
447 if (!el) {
448 return false;
449 }
450 for (let eventName of ["click", "focus"]) {
451 el.dispatchEvent(new Event(eventName, { bubbles: true }));
452 }
453
454 // Focus may have triggered unvealing a true input, find it again
455 el = find(field, form);
456 if (!el) {
457 return false;
458 }
459
460 // Focus the potentially new element again
461 for (let eventName of ["click", "focus"]) {
462 el.dispatchEvent(new Event(eventName, { bubbles: true }));
463 }
464
465 // Send some keyboard events indicating that value modification has started (no associated keycode)
466 for (let eventName of ["keydown", "keypress", "keyup", "input", "change"]) {
467 el.dispatchEvent(new Event(eventName, { bubbles: true }));
468 }
469
470 // truncate the value if required by the field
471 if (el.maxLength > 0) {
472 value = value.substr(0, el.maxLength);
473 }
474
475 // Set the field value
476 let initialValue = el.value || el.getAttribute("value");
477 el.setAttribute("value", value);
478 el.value = value;
479
480 // Send the keyboard events again indicating that value modification has finished (no associated keycode)
481 for (let eventName of ["keydown", "keypress", "keyup", "input", "change"]) {
482 el.dispatchEvent(new Event(eventName, { bubbles: true }));
483 }
484
485 // re-set value if unchanged after firing post-fill events
486 // (in case of sabotage by the site's own event handlers)
487 if ((el.value || el.getAttribute("value")) === initialValue) {
488 el.setAttribute("value", value);
489 el.value = value;
490 }
491
492 // Finally unfocus the element
493 el.dispatchEvent(new Event("blur", { bubbles: true }));
494 return true;
495 }
496
497 // set window object
498 window.browserpass = {
499 fillLogin: fillLogin,
500 focusOrSubmit: focusOrSubmit,
501 };
502 })();