1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-06 23:51:28 +01:00

[PM-17541] Fix folder service key definition (#13060)

* [PM-17541] Switch folder key definition back to "folders" and add migration script for users that have switched to the incorrect key

* [PM-17541] Fix import path

* [PM-17541] Fix implicit any in spec file
This commit is contained in:
Shane Melton 2025-01-24 10:12:52 -08:00 committed by GitHub
parent b396dda406
commit b1744c4e0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 148 additions and 3 deletions

View File

@ -66,13 +66,14 @@ import { ForwarderOptionsMigrator } from "./migrations/65-migrate-forwarder-sett
import { MoveFinalDesktopSettingsMigrator } from "./migrations/66-move-final-desktop-settings";
import { RemoveUnassignedItemsBannerDismissed } from "./migrations/67-remove-unassigned-items-banner-dismissed";
import { MoveLastSyncDate } from "./migrations/68-move-last-sync-date";
import { MigrateIncorrectFolderKey } from "./migrations/69-migrate-incorrect-folder-key";
import { MoveBiometricAutoPromptToAccount } from "./migrations/7-move-biometric-auto-prompt-to-account";
import { MoveStateVersionMigrator } from "./migrations/8-move-state-version";
import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-settings-to-global";
import { MinVersionMigrator } from "./migrations/min-version";
export const MIN_VERSION = 3;
export const CURRENT_VERSION = 68;
export const CURRENT_VERSION = 69;
export type MinVersion = typeof MIN_VERSION;
export function createMigrationBuilder() {
@ -142,7 +143,8 @@ export function createMigrationBuilder() {
.with(ForwarderOptionsMigrator, 64, 65)
.with(MoveFinalDesktopSettingsMigrator, 65, 66)
.with(RemoveUnassignedItemsBannerDismissed, 66, 67)
.with(MoveLastSyncDate, 67, CURRENT_VERSION);
.with(MoveLastSyncDate, 67, 68)
.with(MigrateIncorrectFolderKey, 68, CURRENT_VERSION);
}
export async function currentVersion(

View File

@ -0,0 +1,98 @@
import { runMigrator } from "../migration-helper.spec";
import { MigrateIncorrectFolderKey } from "./69-migrate-incorrect-folder-key";
function exampleJSON() {
return {
global_account_accounts: {
user1: null as any,
user2: null as any,
},
user_user1_folder_folder: {
// Incorrect "folder" key
folderId1: {
id: "folderId1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
folderId2: {
id: "folderId2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
user_user2_folder_folder: null as any,
};
}
describe("MigrateIncorrectFolderKey", () => {
const sut = new MigrateIncorrectFolderKey(68, 69);
it("migrates data", async () => {
const output = await runMigrator(sut, exampleJSON());
expect(output).toEqual({
global_account_accounts: {
user1: null,
user2: null,
},
user_user1_folder_folders: {
// Correct "folders" key
folderId1: {
id: "folderId1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
folderId2: {
id: "folderId2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
});
});
it("rolls back data", async () => {
const output = await runMigrator(
sut,
{
global_account_accounts: {
user1: null,
user2: null,
},
user_user1_folder_folders: {
folderId1: {
id: "folderId1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
folderId2: {
id: "folderId2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
},
"rollback",
);
expect(output).toEqual({
global_account_accounts: {
user1: null,
user2: null,
},
user_user1_folder_folder: {
// Incorrect "folder" key
folderId1: {
id: "folderId1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
folderId2: {
id: "folderId2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
});
});
});

View File

@ -0,0 +1,45 @@
import {
KeyDefinitionLike,
MigrationHelper,
} from "@bitwarden/common/state-migrations/migration-helper";
import { Migrator } from "@bitwarden/common/state-migrations/migrator";
const BAD_FOLDER_KEY: KeyDefinitionLike = {
key: "folder", // We inadvertently changed the key from "folders" to "folder"
stateDefinition: {
name: "folder",
},
};
const GOOD_FOLDER_KEY: KeyDefinitionLike = {
key: "folders", // We should keep the key as "folders"
stateDefinition: {
name: "folder",
},
};
export class MigrateIncorrectFolderKey extends Migrator<68, 69> {
async migrate(helper: MigrationHelper): Promise<void> {
async function migrateUser(userId: string) {
const value = await helper.getFromUser(userId, BAD_FOLDER_KEY);
if (value != null) {
await helper.setToUser(userId, GOOD_FOLDER_KEY, value);
}
await helper.removeFromUser(userId, BAD_FOLDER_KEY);
}
const users = await helper.getKnownUserIds();
await Promise.all(users.map((userId) => migrateUser(userId)));
}
async rollback(helper: MigrationHelper): Promise<void> {
async function rollbackUser(userId: string) {
const value = await helper.getFromUser(userId, GOOD_FOLDER_KEY);
if (value != null) {
await helper.setToUser(userId, BAD_FOLDER_KEY, value);
}
await helper.removeFromUser(userId, GOOD_FOLDER_KEY);
}
const users = await helper.getKnownUserIds();
await Promise.all(users.map((userId) => rollbackUser(userId)));
}
}

View File

@ -6,7 +6,7 @@ import { FolderView } from "../../models/view/folder.view";
export const FOLDER_ENCRYPTED_FOLDERS = UserKeyDefinition.record<FolderData>(
FOLDER_DISK,
"folder",
"folders",
{
deserializer: (obj: Jsonify<FolderData>) => FolderData.fromJSON(obj),
clearOn: ["logout"],