mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 00:03:56 +00:00
[PM-5562] Implement Domain Settings state provider (#8226)
* create domain settings state provider * replace callsites for defaultUriMatch and neverDomains with DomainSettingsService equivalents * replace callsites for equivalentDomains with DomainSettingsService equivalents and clean up unused AccountSettingsSettings * add migrations for domain settings state * do not use enum for URI match strategy constants and types * add getUrlEquivalentDomains test * PR suggestions/cleanup * refactor getUrlEquivalentDomains to return an observable Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: ✨ Audrey ✨ <ajensen@bitwarden.com> * update tests * add UriMatchStrategy docs notes * service class renames * use service abstraction at callsites previously using service class directly --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: ✨ Audrey ✨ <ajensen@bitwarden.com>
This commit is contained in:
@@ -29,6 +29,7 @@ import { PolicyMigrator } from "./migrations/30-move-policy-state-to-state-provi
|
||||
import { EnableContextMenuMigrator } from "./migrations/31-move-enable-context-menu-to-autofill-settings-state-provider";
|
||||
import { PreferredLanguageMigrator } from "./migrations/32-move-preferred-language";
|
||||
import { AppIdMigrator } from "./migrations/33-move-app-id-to-state-providers";
|
||||
import { DomainSettingsMigrator } from "./migrations/34-move-domain-settings-to-state-providers";
|
||||
import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked";
|
||||
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
|
||||
import { RemoveLegacyEtmKeyMigrator } from "./migrations/6-remove-legacy-etm-key";
|
||||
@@ -38,7 +39,7 @@ import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-setting
|
||||
import { MinVersionMigrator } from "./migrations/min-version";
|
||||
|
||||
export const MIN_VERSION = 2;
|
||||
export const CURRENT_VERSION = 33;
|
||||
export const CURRENT_VERSION = 34;
|
||||
export type MinVersion = typeof MIN_VERSION;
|
||||
|
||||
export function createMigrationBuilder() {
|
||||
@@ -74,7 +75,8 @@ export function createMigrationBuilder() {
|
||||
.with(PolicyMigrator, 29, 30)
|
||||
.with(EnableContextMenuMigrator, 30, 31)
|
||||
.with(PreferredLanguageMigrator, 31, 32)
|
||||
.with(AppIdMigrator, 32, CURRENT_VERSION);
|
||||
.with(AppIdMigrator, 32, 33)
|
||||
.with(DomainSettingsMigrator, 33, CURRENT_VERSION);
|
||||
}
|
||||
|
||||
export async function currentVersion(
|
||||
|
||||
@@ -0,0 +1,255 @@
|
||||
import { any, MockProxy } from "jest-mock-extended";
|
||||
|
||||
import { StateDefinitionLike, MigrationHelper } from "../migration-helper";
|
||||
import { mockMigrationHelper } from "../migration-helper.spec";
|
||||
|
||||
import { DomainSettingsMigrator } from "./34-move-domain-settings-to-state-providers";
|
||||
|
||||
const mockNeverDomains = { "bitwarden.test": null, locahost: null, "www.example.com": null } as {
|
||||
[key: string]: null;
|
||||
};
|
||||
|
||||
function exampleJSON() {
|
||||
return {
|
||||
global: {
|
||||
otherStuff: "otherStuff1",
|
||||
neverDomains: mockNeverDomains,
|
||||
},
|
||||
authenticatedAccounts: ["user-1", "user-2", "user-3"],
|
||||
"user-1": {
|
||||
settings: {
|
||||
defaultUriMatch: 3,
|
||||
settings: {
|
||||
equivalentDomains: [] as string[][],
|
||||
},
|
||||
otherStuff: "otherStuff2",
|
||||
},
|
||||
otherStuff: "otherStuff3",
|
||||
},
|
||||
"user-2": {
|
||||
settings: {
|
||||
settings: {
|
||||
equivalentDomains: [["apple.com", "icloud.com"]],
|
||||
},
|
||||
otherStuff: "otherStuff4",
|
||||
},
|
||||
otherStuff: "otherStuff5",
|
||||
},
|
||||
"user-3": {
|
||||
settings: {
|
||||
defaultUriMatch: 1,
|
||||
otherStuff: "otherStuff6",
|
||||
},
|
||||
otherStuff: "otherStuff7",
|
||||
},
|
||||
"user-4": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff8",
|
||||
},
|
||||
otherStuff: "otherStuff9",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function rollbackJSON() {
|
||||
return {
|
||||
global_domainSettings_neverDomains: mockNeverDomains,
|
||||
"user_user-1_domainSettings_defaultUriMatchStrategy": 3,
|
||||
"user_user-1_domainSettings_equivalentDomains": [] as string[][],
|
||||
"user_user-2_domainSettings_equivalentDomains": [["apple.com", "icloud.com"]],
|
||||
"user_user-3_domainSettings_defaultUriMatchStrategy": 1,
|
||||
global: {
|
||||
otherStuff: "otherStuff1",
|
||||
},
|
||||
authenticatedAccounts: ["user-1", "user-2", "user-3"],
|
||||
"user-1": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff2",
|
||||
},
|
||||
otherStuff: "otherStuff3",
|
||||
},
|
||||
"user-2": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff4",
|
||||
},
|
||||
otherStuff: "otherStuff5",
|
||||
},
|
||||
"user-3": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff6",
|
||||
},
|
||||
otherStuff: "otherStuff7",
|
||||
},
|
||||
"user-4": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff8",
|
||||
},
|
||||
otherStuff: "otherStuff9",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const domainSettingsStateDefinition: {
|
||||
stateDefinition: StateDefinitionLike;
|
||||
} = {
|
||||
stateDefinition: {
|
||||
name: "domainSettings",
|
||||
},
|
||||
};
|
||||
|
||||
describe("DomainSettingsMigrator", () => {
|
||||
let helper: MockProxy<MigrationHelper>;
|
||||
let sut: DomainSettingsMigrator;
|
||||
|
||||
describe("migrate", () => {
|
||||
beforeEach(() => {
|
||||
helper = mockMigrationHelper(exampleJSON(), 33);
|
||||
sut = new DomainSettingsMigrator(33, 34);
|
||||
});
|
||||
|
||||
it("should remove global neverDomains and defaultUriMatch and equivalentDomains settings from all accounts", async () => {
|
||||
await sut.migrate(helper);
|
||||
expect(helper.set).toHaveBeenCalledTimes(4);
|
||||
expect(helper.set).toHaveBeenCalledWith("global", {
|
||||
otherStuff: "otherStuff1",
|
||||
});
|
||||
expect(helper.set).toHaveBeenCalledWith("user-1", {
|
||||
settings: {
|
||||
otherStuff: "otherStuff2",
|
||||
},
|
||||
otherStuff: "otherStuff3",
|
||||
});
|
||||
expect(helper.set).toHaveBeenCalledWith("user-1", {
|
||||
settings: {
|
||||
otherStuff: "otherStuff2",
|
||||
},
|
||||
otherStuff: "otherStuff3",
|
||||
});
|
||||
expect(helper.set).toHaveBeenCalledWith("user-2", {
|
||||
settings: {
|
||||
otherStuff: "otherStuff4",
|
||||
},
|
||||
otherStuff: "otherStuff5",
|
||||
});
|
||||
expect(helper.set).toHaveBeenCalledWith("user-3", {
|
||||
settings: {
|
||||
otherStuff: "otherStuff6",
|
||||
},
|
||||
otherStuff: "otherStuff7",
|
||||
});
|
||||
});
|
||||
|
||||
it("should set global neverDomains and defaultUriMatchStrategy and equivalentDomains setting values for each account", async () => {
|
||||
await sut.migrate(helper);
|
||||
|
||||
expect(helper.setToGlobal).toHaveBeenCalledTimes(1);
|
||||
expect(helper.setToGlobal).toHaveBeenCalledWith(
|
||||
{ ...domainSettingsStateDefinition, key: "neverDomains" },
|
||||
mockNeverDomains,
|
||||
);
|
||||
|
||||
expect(helper.setToUser).toHaveBeenCalledTimes(4);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-1",
|
||||
{ ...domainSettingsStateDefinition, key: "defaultUriMatchStrategy" },
|
||||
3,
|
||||
);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-1",
|
||||
{ ...domainSettingsStateDefinition, key: "equivalentDomains" },
|
||||
[],
|
||||
);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-2",
|
||||
{ ...domainSettingsStateDefinition, key: "equivalentDomains" },
|
||||
[["apple.com", "icloud.com"]],
|
||||
);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-3",
|
||||
{ ...domainSettingsStateDefinition, key: "defaultUriMatchStrategy" },
|
||||
1,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("rollback", () => {
|
||||
beforeEach(() => {
|
||||
helper = mockMigrationHelper(rollbackJSON(), 34);
|
||||
sut = new DomainSettingsMigrator(33, 34);
|
||||
});
|
||||
|
||||
it("should null out new values globally and for each account", async () => {
|
||||
await sut.rollback(helper);
|
||||
|
||||
expect(helper.setToGlobal).toHaveBeenCalledTimes(1);
|
||||
expect(helper.setToGlobal).toHaveBeenCalledWith(
|
||||
{ ...domainSettingsStateDefinition, key: "neverDomains" },
|
||||
null,
|
||||
);
|
||||
|
||||
expect(helper.setToUser).toHaveBeenCalledTimes(4);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-1",
|
||||
{ ...domainSettingsStateDefinition, key: "defaultUriMatchStrategy" },
|
||||
null,
|
||||
);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-1",
|
||||
{ ...domainSettingsStateDefinition, key: "equivalentDomains" },
|
||||
null,
|
||||
);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-2",
|
||||
{ ...domainSettingsStateDefinition, key: "equivalentDomains" },
|
||||
null,
|
||||
);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-3",
|
||||
{ ...domainSettingsStateDefinition, key: "defaultUriMatchStrategy" },
|
||||
null,
|
||||
);
|
||||
});
|
||||
|
||||
it("should add explicit value back to accounts", async () => {
|
||||
await sut.rollback(helper);
|
||||
|
||||
expect(helper.set).toHaveBeenCalledTimes(4);
|
||||
expect(helper.set).toHaveBeenCalledWith("global", {
|
||||
neverDomains: mockNeverDomains,
|
||||
otherStuff: "otherStuff1",
|
||||
});
|
||||
expect(helper.set).toHaveBeenCalledWith("user-1", {
|
||||
settings: {
|
||||
defaultUriMatch: 3,
|
||||
settings: {
|
||||
equivalentDomains: [] as string[][],
|
||||
},
|
||||
otherStuff: "otherStuff2",
|
||||
},
|
||||
otherStuff: "otherStuff3",
|
||||
});
|
||||
expect(helper.set).toHaveBeenCalledWith("user-2", {
|
||||
settings: {
|
||||
settings: {
|
||||
equivalentDomains: [["apple.com", "icloud.com"]],
|
||||
},
|
||||
otherStuff: "otherStuff4",
|
||||
},
|
||||
otherStuff: "otherStuff5",
|
||||
});
|
||||
expect(helper.set).toHaveBeenCalledWith("user-3", {
|
||||
settings: {
|
||||
defaultUriMatch: 1,
|
||||
otherStuff: "otherStuff6",
|
||||
},
|
||||
otherStuff: "otherStuff7",
|
||||
});
|
||||
});
|
||||
|
||||
it("should not try to restore values to missing accounts", async () => {
|
||||
await sut.rollback(helper);
|
||||
|
||||
expect(helper.set).not.toHaveBeenCalledWith("user-4", any());
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,167 @@
|
||||
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
|
||||
import { Migrator } from "../migrator";
|
||||
|
||||
const UriMatchStrategy = {
|
||||
Domain: 0,
|
||||
Host: 1,
|
||||
StartsWith: 2,
|
||||
Exact: 3,
|
||||
RegularExpression: 4,
|
||||
Never: 5,
|
||||
} as const;
|
||||
|
||||
type UriMatchStrategySetting = (typeof UriMatchStrategy)[keyof typeof UriMatchStrategy];
|
||||
|
||||
type ExpectedAccountState = {
|
||||
settings?: {
|
||||
defaultUriMatch?: UriMatchStrategySetting;
|
||||
settings?: {
|
||||
equivalentDomains?: string[][];
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
type ExpectedGlobalState = {
|
||||
neverDomains?: { [key: string]: null };
|
||||
};
|
||||
|
||||
const defaultUriMatchStrategyDefinition: KeyDefinitionLike = {
|
||||
stateDefinition: {
|
||||
name: "domainSettings",
|
||||
},
|
||||
key: "defaultUriMatchStrategy",
|
||||
};
|
||||
|
||||
const equivalentDomainsDefinition: KeyDefinitionLike = {
|
||||
stateDefinition: {
|
||||
name: "domainSettings",
|
||||
},
|
||||
key: "equivalentDomains",
|
||||
};
|
||||
|
||||
const neverDomainsDefinition: KeyDefinitionLike = {
|
||||
stateDefinition: {
|
||||
name: "domainSettings",
|
||||
},
|
||||
key: "neverDomains",
|
||||
};
|
||||
|
||||
export class DomainSettingsMigrator extends Migrator<33, 34> {
|
||||
async migrate(helper: MigrationHelper): Promise<void> {
|
||||
let updateAccount = false;
|
||||
|
||||
// global state ("neverDomains")
|
||||
const globalState = await helper.get<ExpectedGlobalState>("global");
|
||||
|
||||
if (globalState?.neverDomains != null) {
|
||||
await helper.setToGlobal(neverDomainsDefinition, globalState.neverDomains);
|
||||
|
||||
// delete `neverDomains` from state global
|
||||
delete globalState.neverDomains;
|
||||
|
||||
await helper.set<ExpectedGlobalState>("global", globalState);
|
||||
}
|
||||
|
||||
// account state ("defaultUriMatch" and "settings.equivalentDomains")
|
||||
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> {
|
||||
const accountSettings = account?.settings;
|
||||
|
||||
if (accountSettings?.defaultUriMatch != undefined) {
|
||||
await helper.setToUser(
|
||||
userId,
|
||||
defaultUriMatchStrategyDefinition,
|
||||
accountSettings.defaultUriMatch,
|
||||
);
|
||||
delete account.settings.defaultUriMatch;
|
||||
|
||||
updateAccount = true;
|
||||
}
|
||||
|
||||
if (accountSettings?.settings?.equivalentDomains != undefined) {
|
||||
await helper.setToUser(
|
||||
userId,
|
||||
equivalentDomainsDefinition,
|
||||
accountSettings.settings.equivalentDomains,
|
||||
);
|
||||
delete account.settings.settings.equivalentDomains;
|
||||
delete account.settings.settings;
|
||||
|
||||
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> {
|
||||
let updateAccount = false;
|
||||
|
||||
// global state ("neverDomains")
|
||||
const globalState = (await helper.get<ExpectedGlobalState>("global")) || {};
|
||||
const neverDomains: { [key: string]: null } =
|
||||
await helper.getFromGlobal(neverDomainsDefinition);
|
||||
|
||||
if (neverDomains != null) {
|
||||
await helper.set<ExpectedGlobalState>("global", {
|
||||
...globalState,
|
||||
neverDomains: neverDomains,
|
||||
});
|
||||
|
||||
// remove the global state provider framework key for `neverDomains`
|
||||
await helper.setToGlobal(neverDomainsDefinition, null);
|
||||
}
|
||||
|
||||
// account state ("defaultUriMatchStrategy" and "equivalentDomains")
|
||||
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 settings = account?.settings || {};
|
||||
|
||||
const defaultUriMatchStrategy: UriMatchStrategySetting = await helper.getFromUser(
|
||||
userId,
|
||||
defaultUriMatchStrategyDefinition,
|
||||
);
|
||||
|
||||
const equivalentDomains: string[][] = await helper.getFromUser(
|
||||
userId,
|
||||
equivalentDomainsDefinition,
|
||||
);
|
||||
|
||||
// update new settings and remove the account state provider framework keys for the rolled back values
|
||||
if (defaultUriMatchStrategy != null) {
|
||||
settings = { ...settings, defaultUriMatch: defaultUriMatchStrategy };
|
||||
|
||||
await helper.setToUser(userId, defaultUriMatchStrategyDefinition, null);
|
||||
|
||||
updateAccount = true;
|
||||
}
|
||||
|
||||
if (equivalentDomains != null) {
|
||||
settings = { ...settings, settings: { equivalentDomains } };
|
||||
|
||||
await helper.setToUser(userId, equivalentDomainsDefinition, null);
|
||||
|
||||
updateAccount = true;
|
||||
}
|
||||
|
||||
// commit updated settings to state
|
||||
if (updateAccount) {
|
||||
await helper.set(userId, {
|
||||
...account,
|
||||
settings,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user