1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 00:03:56 +00:00

[PM-5560] Implement Autofill Settings state provider (#7767)

* Begin migration of autofill settings

Co-authored-by: Cesar Gonzalez <cagonzalezcs@users.noreply.github.com>
Co-authored-by: Thomas Avery <Thomas-Avery@users.noreply.github.com>
Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com>
Co-authored-by: Colton Hurst <coltonhurst@users.noreply.github.com>

* add browser dependency for AutofillSettingsService

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>

* update autofill settings service

* replace usages of stateService get/set autofillOnPageLoad with autofillSettingsService

* replace usages of stateService get/set autofillOnPageLoadDefault with autofillSettingsService

* replace usages of stateService get/set autoCopyTotp with autofillSettingsService

* replace usages of stateService get/set autoFillOnPageLoadCalloutIsDismissed with autofillSettingsService

* replace usages of stateService get/set activateAutoFillOnPageLoadFromPolicy with autofillSettingsService

* replace usages of get/set autoFillOverlayVisibility with autofillSettingsService

* inlineMenuVisibility should use global state

* add the AutofillSettingsService to background scripts

* fix typing

* replace additional usages of get/set autoFillOverlayVisibility and disableAutoTotpCopy with autofillSettingsService equivalents

* replace additional usages of get/set autofillOnPageLoadDefault with autofillSettingsService equivalent

* replace additional usages of get/set activateAutoFillOnPageLoadFromPolicy with autofillSettingsService equivalent

* remove additional deprecated and unused state service calls

* improve naming conventions and consistency

* fix missing mock for policy service test

* replace missing overlay background tests

* cleanup

* fix double inversion

* fix reference to wrong setter

* move handleActivateAutofillPolicy out of BrowserPolicyService

* create state migration script

* resolve linting issues

* remove migrated setting properties

* add AutofillSettingsSErvice to jslib-services

* handle conditional content script loading via autofillOnPageLoad check

* add deprecated note to getFromLocalStorage

* add jsdoc decorators to new autofill service methods

* handle undefined globalState

* move autofill settings out of BrowserPolicyService

* Move autofill settings code out of policyService

* fix tests

* fix typo in state definition

---------

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
Co-authored-by: Cesar Gonzalez <cagonzalezcs@users.noreply.github.com>
Co-authored-by: Thomas Avery <Thomas-Avery@users.noreply.github.com>
Co-authored-by: Colton Hurst <coltonhurst@users.noreply.github.com>
Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
This commit is contained in:
Jonathan Prusik
2024-02-12 17:11:04 -05:00
committed by GitHub
parent bf16682f42
commit c65e92f769
34 changed files with 975 additions and 306 deletions

View File

@@ -13,6 +13,7 @@ import { MoveBiometricClientKeyHalfToStateProviders } from "./migrations/14-move
import { FolderMigrator } from "./migrations/15-move-folder-state-to-state-provider";
import { LastSyncMigrator } from "./migrations/16-move-last-sync-to-state-provider";
import { EnablePasskeysMigrator } from "./migrations/17-move-enable-passkeys-to-state-providers";
import { AutofillSettingsKeyMigrator } from "./migrations/18-move-autofill-settings-to-state-providers";
import { FixPremiumMigrator } from "./migrations/3-fix-premium";
import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked";
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
@@ -23,7 +24,7 @@ import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-setting
import { MinVersionMigrator } from "./migrations/min-version";
export const MIN_VERSION = 2;
export const CURRENT_VERSION = 17;
export const CURRENT_VERSION = 18;
export type MinVersion = typeof MIN_VERSION;
export async function migrate(
@@ -56,7 +57,8 @@ export async function migrate(
.with(MoveBiometricClientKeyHalfToStateProviders, 13, 14)
.with(FolderMigrator, 14, 15)
.with(LastSyncMigrator, 15, 16)
.with(EnablePasskeysMigrator, 16, CURRENT_VERSION)
.with(EnablePasskeysMigrator, 16, 17)
.with(AutofillSettingsKeyMigrator, 17, CURRENT_VERSION)
.migrate(migrationHelper);
}

View File

@@ -0,0 +1,223 @@
import { any, MockProxy } from "jest-mock-extended";
import { AutofillOverlayVisibility } from "../../../../../apps/browser/src/autofill/utils/autofill-overlay.enum";
import { StateDefinitionLike, MigrationHelper } from "../migration-helper";
import { mockMigrationHelper } from "../migration-helper.spec";
import { AutofillSettingsKeyMigrator } from "./18-move-autofill-settings-to-state-providers";
function exampleJSON() {
return {
global: {
autoFillOverlayVisibility: AutofillOverlayVisibility.OnButtonClick,
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["user-1", "user-2", "user-3"],
"user-1": {
settings: {
autoFillOnPageLoadDefault: true,
enableAutoFillOnPageLoad: true,
dismissedAutoFillOnPageLoadCallout: true,
disableAutoTotpCopy: false,
activateAutoFillOnPageLoadFromPolicy: true,
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
settings: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
function rollbackJSON() {
return {
global_autofillSettingsLocal_inlineMenuVisibility: AutofillOverlayVisibility.OnButtonClick,
"user_user-1_autofillSettings_autoCopyTotp": true,
"user_user-1_autofillSettings_autofillOnPageLoad": true,
"user_user-1_autofillSettings_autofillOnPageLoadCalloutIsDismissed": true,
"user_user-1_autofillSettings_autofillOnPageLoadDefault": true,
"user_user-1_autofillSettingsLocal_activateAutofillOnPageLoadFromPolicy": true,
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["user-1", "user-2", "user-3"],
"user-1": {
settings: {
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
settings: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
const autofillSettingsStateDefinition: {
stateDefinition: StateDefinitionLike;
} = {
stateDefinition: {
name: "autofillSettings",
},
};
describe("ProviderKeysMigrator", () => {
let helper: MockProxy<MigrationHelper>;
let sut: AutofillSettingsKeyMigrator;
describe("migrate", () => {
beforeEach(() => {
helper = mockMigrationHelper(exampleJSON(), 17);
sut = new AutofillSettingsKeyMigrator(17, 18);
});
it("should remove autofill settings from all accounts", async () => {
await sut.migrate(helper);
expect(helper.set).toHaveBeenCalledTimes(2);
expect(helper.set).toHaveBeenCalledWith("global", {
otherStuff: "otherStuff1",
});
expect(helper.set).toHaveBeenCalledWith("user-1", {
settings: {
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
});
});
it("should set autofill setting values for each account", async () => {
await sut.migrate(helper);
expect(helper.setToGlobal).toHaveBeenCalledTimes(1);
expect(helper.setToGlobal).toHaveBeenCalledWith(
{
stateDefinition: {
name: "autofillSettingsLocal",
},
key: "inlineMenuVisibility",
},
1,
);
expect(helper.setToUser).toHaveBeenCalledTimes(5);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoadDefault" },
true,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoad" },
true,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoadCalloutIsDismissed" },
true,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{ ...autofillSettingsStateDefinition, key: "autoCopyTotp" },
true,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{
stateDefinition: {
name: "autofillSettingsLocal",
},
key: "activateAutofillOnPageLoadFromPolicy",
},
true,
);
});
});
describe("rollback", () => {
beforeEach(() => {
helper = mockMigrationHelper(rollbackJSON(), 16);
sut = new AutofillSettingsKeyMigrator(17, 18);
});
it("should null out new values for each account", async () => {
await sut.rollback(helper);
expect(helper.setToGlobal).toHaveBeenCalledTimes(1);
expect(helper.setToGlobal).toHaveBeenCalledWith(
{
stateDefinition: {
name: "autofillSettingsLocal",
},
key: "inlineMenuVisibility",
},
null,
);
expect(helper.setToUser).toHaveBeenCalledTimes(5);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoadDefault" },
null,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoad" },
null,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoadCalloutIsDismissed" },
null,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{ ...autofillSettingsStateDefinition, key: "autoCopyTotp" },
null,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{
stateDefinition: {
name: "autofillSettingsLocal",
},
key: "activateAutofillOnPageLoadFromPolicy",
},
null,
);
});
it("should add explicit value back to accounts", async () => {
await sut.rollback(helper);
expect(helper.set).toHaveBeenCalledTimes(2);
expect(helper.set).toHaveBeenCalledWith("global", {
autoFillOverlayVisibility: 1,
otherStuff: "otherStuff1",
});
expect(helper.set).toHaveBeenCalledWith("user-1", {
settings: {
otherStuff: "otherStuff2",
autoFillOnPageLoadDefault: true,
enableAutoFillOnPageLoad: true,
dismissedAutoFillOnPageLoadCallout: true,
disableAutoTotpCopy: false,
activateAutoFillOnPageLoadFromPolicy: true,
},
otherStuff: "otherStuff3",
});
});
it("should not try to restore values to missing accounts", async () => {
await sut.rollback(helper);
expect(helper.set).not.toHaveBeenCalledWith("user-3", any());
});
});
});

View File

@@ -0,0 +1,262 @@
import { InlineMenuVisibilitySetting } from "../../../../../apps/browser/src/autofill/utils/autofill-overlay.enum";
import { StateDefinitionLike, MigrationHelper } from "../migration-helper";
import { Migrator } from "../migrator";
type ExpectedAccountState = {
settings?: {
autoFillOnPageLoadDefault?: boolean;
enableAutoFillOnPageLoad?: boolean;
dismissedAutoFillOnPageLoadCallout?: boolean;
disableAutoTotpCopy?: boolean;
activateAutoFillOnPageLoadFromPolicy?: InlineMenuVisibilitySetting;
};
};
type ExpectedGlobalState = { autoFillOverlayVisibility?: InlineMenuVisibilitySetting };
const autofillSettingsStateDefinition: {
stateDefinition: StateDefinitionLike;
} = {
stateDefinition: {
name: "autofillSettings",
},
};
export class AutofillSettingsKeyMigrator extends Migrator<17, 18> {
async migrate(helper: MigrationHelper): Promise<void> {
// global state (e.g. "autoFillOverlayVisibility -> inlineMenuVisibility")
const globalState = await helper.get<ExpectedGlobalState>("global");
if (globalState?.autoFillOverlayVisibility != null) {
await helper.setToGlobal(
{
stateDefinition: {
name: "autofillSettingsLocal",
},
key: "inlineMenuVisibility",
},
globalState.autoFillOverlayVisibility,
);
// delete `autoFillOverlayVisibility` from state global
delete globalState.autoFillOverlayVisibility;
await helper.set<ExpectedGlobalState>("global", globalState);
}
// account state (e.g. account settings -> state provider framework keys)
const accounts = await helper.getAccounts<ExpectedAccountState>();
await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]);
// migrate account state
async function migrateAccount(userId: string, account: ExpectedAccountState): Promise<void> {
let updateAccount = false;
const accountSettings = account?.settings;
if (accountSettings?.autoFillOnPageLoadDefault != null) {
await helper.setToUser(
userId,
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoadDefault" },
accountSettings.autoFillOnPageLoadDefault,
);
delete account.settings.autoFillOnPageLoadDefault;
updateAccount = true;
}
if (accountSettings?.enableAutoFillOnPageLoad != null) {
await helper.setToUser(
userId,
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoad" },
accountSettings?.enableAutoFillOnPageLoad,
);
delete account.settings.enableAutoFillOnPageLoad;
updateAccount = true;
}
if (accountSettings?.dismissedAutoFillOnPageLoadCallout != null) {
await helper.setToUser(
userId,
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoadCalloutIsDismissed" },
accountSettings?.dismissedAutoFillOnPageLoadCallout,
);
delete account.settings.dismissedAutoFillOnPageLoadCallout;
updateAccount = true;
}
if (accountSettings?.disableAutoTotpCopy != null) {
await helper.setToUser(
userId,
{ ...autofillSettingsStateDefinition, key: "autoCopyTotp" },
// invert the value to match the new naming convention
!accountSettings?.disableAutoTotpCopy,
);
delete account.settings.disableAutoTotpCopy;
updateAccount = true;
}
if (accountSettings?.activateAutoFillOnPageLoadFromPolicy != null) {
await helper.setToUser(
userId,
{
stateDefinition: {
name: "autofillSettingsLocal",
},
key: "activateAutofillOnPageLoadFromPolicy",
},
accountSettings?.activateAutoFillOnPageLoadFromPolicy,
);
delete account.settings.activateAutoFillOnPageLoadFromPolicy;
updateAccount = true;
}
if (updateAccount) {
// update the state account settings with the migrated values deleted
await helper.set(userId, account);
}
}
}
async rollback(helper: MigrationHelper): Promise<void> {
// global state (e.g. "inlineMenuVisibility -> autoFillOverlayVisibility")
const globalState = (await helper.get<ExpectedGlobalState>("global")) || {};
const inlineMenuVisibility: InlineMenuVisibilitySetting = await helper.getFromGlobal({
stateDefinition: {
name: "autofillSettingsLocal",
},
key: "inlineMenuVisibility",
});
if (inlineMenuVisibility) {
await helper.set<ExpectedGlobalState>("global", {
...globalState,
autoFillOverlayVisibility: inlineMenuVisibility,
});
// remove the global state provider framework key for `inlineMenuVisibility`
await helper.setToGlobal(
{
stateDefinition: {
name: "autofillSettingsLocal",
},
key: "inlineMenuVisibility",
},
null,
);
}
// account state (e.g. state provider framework keys -> account settings)
const accounts = await helper.getAccounts<ExpectedAccountState>();
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
// rollback account state
async function rollbackAccount(userId: string, account: ExpectedAccountState): Promise<void> {
let updateAccount = false;
let settings = account?.settings || {};
const autoFillOnPageLoadDefault: boolean = await helper.getFromUser(userId, {
...autofillSettingsStateDefinition,
key: "autofillOnPageLoadDefault",
});
const enableAutoFillOnPageLoad: boolean = await helper.getFromUser(userId, {
...autofillSettingsStateDefinition,
key: "autofillOnPageLoad",
});
const dismissedAutoFillOnPageLoadCallout: boolean = await helper.getFromUser(userId, {
...autofillSettingsStateDefinition,
key: "autofillOnPageLoadCalloutIsDismissed",
});
const autoCopyTotp: boolean = await helper.getFromUser(userId, {
...autofillSettingsStateDefinition,
key: "autoCopyTotp",
});
const activateAutoFillOnPageLoadFromPolicy: InlineMenuVisibilitySetting =
await helper.getFromUser(userId, {
stateDefinition: {
name: "autofillSettingsLocal",
},
key: "activateAutofillOnPageLoadFromPolicy",
});
// update new settings and remove the account state provider framework keys for the rolled back values
if (autoFillOnPageLoadDefault != null) {
settings = { ...settings, autoFillOnPageLoadDefault };
await helper.setToUser(
userId,
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoadDefault" },
null,
);
updateAccount = true;
}
if (enableAutoFillOnPageLoad != null) {
settings = { ...settings, enableAutoFillOnPageLoad };
await helper.setToUser(
userId,
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoad" },
null,
);
updateAccount = true;
}
if (dismissedAutoFillOnPageLoadCallout != null) {
settings = { ...settings, dismissedAutoFillOnPageLoadCallout };
await helper.setToUser(
userId,
{ ...autofillSettingsStateDefinition, key: "autofillOnPageLoadCalloutIsDismissed" },
null,
);
updateAccount = true;
}
if (autoCopyTotp != null) {
// invert the value to match the new naming convention
settings = { ...settings, disableAutoTotpCopy: !autoCopyTotp };
await helper.setToUser(
userId,
{ ...autofillSettingsStateDefinition, key: "autoCopyTotp" },
null,
);
updateAccount = true;
}
if (activateAutoFillOnPageLoadFromPolicy != null) {
settings = { ...settings, activateAutoFillOnPageLoadFromPolicy };
await helper.setToUser(
userId,
{
stateDefinition: {
name: "autofillSettingsLocal",
},
key: "activateAutofillOnPageLoadFromPolicy",
},
null,
);
updateAccount = true;
}
if (updateAccount) {
// commit updated settings to state
await helper.set(userId, {
...account,
settings,
});
}
}
}
}