Skip to content

Commit

Permalink
Merge branch 'hotfix-rc-cli' of https://github.com/bitwarden/clients
Browse files Browse the repository at this point in the history
…into hotfix-rc-cli
  • Loading branch information
trmartin4 committed Jan 24, 2025
2 parents a48ad0b + 02d79f0 commit 2cadd5f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
6 changes: 4 additions & 2 deletions libs/common/src/state-migrations/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
},
},
});
});
});
Original file line number Diff line number Diff line change
@@ -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)));
}
}
2 changes: 1 addition & 1 deletion libs/common/src/vault/services/key-state/folder.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 2cadd5f

Please sign in to comment.