From 3577b7c1009f8373b55acbb00f28e9f91a9ddb02 Mon Sep 17 00:00:00 2001 From: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Date: Thu, 18 May 2023 13:35:13 -0400 Subject: [PATCH] [PM-1072] Convert autofill.js to Typescript (#5376) * Rename autofill.js to ts and update webpack * Remove wrapping function * Remove unreachable data-onepassword-title code * Remove unused post-submit logic * Run prettier * Remove unused fake tested code * Add typing * Disable certain eslint rules or fix eslint violations * Update modifications list * Remove unnecessary/confusing types * Checkout autofill.js from master * Add ENV switch for autofill versions * Rename autofill.ts to avoid confusion * Use string union type for FillScriptOp --- apps/browser/package.json | 1 + .../src/autofill/content/autofillv2.ts | 1391 +++++++++++++++++ .../src/autofill/models/autofill-script.ts | 27 +- apps/browser/webpack.config.js | 11 +- 4 files changed, 1423 insertions(+), 7 deletions(-) create mode 100644 apps/browser/src/autofill/content/autofillv2.ts diff --git a/apps/browser/package.json b/apps/browser/package.json index 0057704287..b29ab9c27b 100644 --- a/apps/browser/package.json +++ b/apps/browser/package.json @@ -6,6 +6,7 @@ "build:mv3": "cross-env MANIFEST_VERSION=3 webpack", "build:watch": "webpack --watch", "build:watch:mv3": "cross-env MANIFEST_VERSION=3 webpack --watch", + "build:watch:autofill": "cross-env AUTOFILL_VERSION=2 webpack --watch", "build:prod": "cross-env NODE_ENV=production webpack", "build:prod:watch": "cross-env NODE_ENV=production webpack --watch", "dist": "npm run build:prod && gulp dist", diff --git a/apps/browser/src/autofill/content/autofillv2.ts b/apps/browser/src/autofill/content/autofillv2.ts new file mode 100644 index 0000000000..8bf16ff879 --- /dev/null +++ b/apps/browser/src/autofill/content/autofillv2.ts @@ -0,0 +1,1391 @@ +/* eslint-disable no-var, no-console, no-prototype-builtins */ +// These eslint rules are disabled because the original JS was not written with them in mind and we don't want to fix +// them all now + +/* + 1Password Extension + + Lovingly handcrafted by Dave Teare, Michael Fey, Rad Azzouz, and Roustem Karimov. + Copyright (c) 2014 AgileBits. All rights reserved. + + ================================================================================ + + Copyright (c) 2014 AgileBits Inc. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + */ + +/* + MODIFICATIONS FROM ORIGINAL + + 1. Populate isFirefox + 2. Remove isChrome and isSafari since they are not used. + 3. Unminify and format to meet Mozilla review requirements. + 4. Remove unnecessary input types from getFormElements query selector and limit number of elements returned. + 5. Remove fakeTested prop. + 6. Rename com.agilebits.* stuff to com.bitwarden.* + 7. Remove "some useful globals" on window + 8. Add ability to autofill span[data-bwautofill] elements + 9. Add new handler, for new command that responds with page details in response callback + 10. Handle sandbox iframe and sandbox rule in CSP + 11. Work on array of saved urls instead of just one to determine if we should autofill non-https sites + 12. Remove setting of attribute com.browser.browser.userEdited on user-inputs + 13. Handle null value URLs in urlNotSecure + 14. Convert to Typescript, add typings and remove dead code (not marked with START/END MODIFICATION) + */ +import AutofillForm from "../models/autofill-form"; +import AutofillPageDetails from "../models/autofill-page-details"; +import AutofillScript, { + AutofillScriptOptions, + FillScript, + FillScriptOp, +} from "../models/autofill-script"; + +/** + * The Document with additional custom properties added by this script + */ +type AutofillDocument = Document & { + elementsByOPID: Record; + elementForOPID: (opId: string) => Element; +}; + +/** + * A HTMLElement (usually a form element) with additional custom properties added by this script + */ +type ElementWithOpId = T & { + opid: string; +}; + +/** + * This script's definition of a Form Element (only a subset of HTML form elements) + * This is defined by getFormElements + */ +type FormElement = HTMLInputElement | HTMLSelectElement | HTMLSpanElement; + +/** + * A Form Element that we can set a value on (fill) + */ +type FillableControl = HTMLInputElement | HTMLSelectElement; + +function collect(document: Document) { + // START MODIFICATION + var isFirefox = + navigator.userAgent.indexOf("Firefox") !== -1 || navigator.userAgent.indexOf("Gecko/") !== -1; + // END MODIFICATION + + (document as AutofillDocument).elementsByOPID = {}; + + function getPageDetails(theDoc: Document, oneShotId: string) { + // start helpers + + /** + * For a given element `el`, returns the value of the attribute `attrName`. + * @param {HTMLElement} el + * @param {string} attrName + * @returns {string} The value of the attribute + */ + function getElementAttrValue(el: any, attrName: string) { + var attrVal = el[attrName]; + if ("string" == typeof attrVal) { + return attrVal; + } + attrVal = el.getAttribute(attrName); + return "string" == typeof attrVal ? attrVal : null; + } + + /** + * Returns the value of the given element. + * @param {HTMLElement} el + * @returns {any} Value of the element + */ + function getElementValue(el: any) { + switch (toLowerString(el.type)) { + case "checkbox": + return el.checked ? "✓" : ""; + + case "hidden": + el = el.value; + if (!el || "number" != typeof el.length) { + return ""; + } + 254 < el.length && (el = el.substr(0, 254) + "...SNIPPED"); + return el; + + default: + // START MODIFICATION + if (!el.type && el.tagName.toLowerCase() === "span") { + return el.innerText; + } + // END MODIFICATION + return el.value; + } + } + + /** + * If `el` is a `