From 3b1ccb409e653e78cfd994eec99fcdfe1a57dd88 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Thu, 13 Jan 2022 12:03:19 -0500 Subject: [PATCH] Fix/lowdb no cache (#443) * Add jslib prettier commit to client ignore hashes * Remove lowdb caching * Fix state service remove being set to null * Await in-memory key retrieval * Fix key loading and unlock requests. * Linter fixes * linter fixes * linter fixes --- .git-blame-ignore-revs | 3 + src/bw.ts | 2 +- src/program.ts | 58 ++++++++++---------- src/services/nodeEnvSecureStorage.service.ts | 6 +- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 973c5531a1..3b52d52fee 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -1,2 +1,5 @@ # Apply Prettier https://github.com/bitwarden/cli/pull/426 910b4a24e649f21acbf4da5b2d422b121d514bd5 + +# jslib Apply Prettier https://github.com/bitwarden/jslib/pull/581 +193434461dbd9c48fe5dcbad95693470aec422ac diff --git a/src/bw.ts b/src/bw.ts index 13ae9e9b12..6592984c2b 100644 --- a/src/bw.ts +++ b/src/bw.ts @@ -120,7 +120,7 @@ export class Main { (level) => process.env.BITWARDENCLI_DEBUG !== "true" && level <= LogLevelType.Info ); this.cryptoFunctionService = new NodeCryptoFunctionService(); - this.storageService = new LowdbStorageService(this.logService, null, p, true); + this.storageService = new LowdbStorageService(this.logService, null, p, false); this.secureStorageService = new NodeEnvSecureStorageService( this.storageService, this.logService, diff --git a/src/program.ts b/src/program.ts index 478525c660..d6eec6e2e9 100644 --- a/src/program.ts +++ b/src/program.ts @@ -25,6 +25,8 @@ import { CliUtils } from "./utils"; import { BaseProgram } from "jslib-node/cli/baseProgram"; +import { KeySuffixOptions } from "jslib-common/enums/keySuffixOptions"; + const writeLn = CliUtils.writeLn; export class Program extends BaseProgram { @@ -473,36 +475,34 @@ export class Program extends BaseProgram { protected async exitIfLocked() { await this.exitIfNotAuthed(); - const hasKey = await this.main.cryptoService.hasKey(); - if (!hasKey) { - const canInteract = process.env.BW_NOINTERACTION !== "true"; - if (canInteract) { - const usesKeyConnector = await this.main.keyConnectorService.getUsesKeyConnector(); - - if (usesKeyConnector) { - const response = Response.error( - "Your vault is locked. You must unlock your vault using your session key.\n" + - "If you do not have your session key, you can get a new one by logging out and logging in again." - ); - this.processResponse(response, true); - } else { - const command = new UnlockCommand( - this.main.cryptoService, - this.main.stateService, - this.main.cryptoFunctionService, - this.main.apiService, - this.main.logService - ); - const response = await command.run(null, null); - if (!response.success) { - this.processResponse(response, true); - } - } - } else { - this.processResponse(Response.error("Vault is locked."), true); - } - } else if (!this.main.cryptoService.hasKeyInMemory()) { + if (await this.main.cryptoService.hasKeyInMemory()) { + return; + } else if (await this.main.cryptoService.hasKeyStored(KeySuffixOptions.Auto)) { + // load key into memory await this.main.cryptoService.getKey(); + } else if (process.env.BW_NOINTERACTION !== "true") { + // must unlock + if (await this.main.keyConnectorService.getUsesKeyConnector()) { + const response = Response.error( + "Your vault is locked. You must unlock your vault using your session key.\n" + + "If you do not have your session key, you can get a new one by logging out and logging in again." + ); + this.processResponse(response, true); + } else { + const command = new UnlockCommand( + this.main.cryptoService, + this.main.stateService, + this.main.cryptoFunctionService, + this.main.apiService, + this.main.logService + ); + const response = await command.run(null, null); + if (!response.success) { + this.processResponse(response, true); + } + } + } else { + this.processResponse(Response.error("Vault is locked."), true); } } } diff --git a/src/services/nodeEnvSecureStorage.service.ts b/src/services/nodeEnvSecureStorage.service.ts index 3a93616a13..badfbd5040 100644 --- a/src/services/nodeEnvSecureStorage.service.ts +++ b/src/services/nodeEnvSecureStorage.service.ts @@ -26,7 +26,11 @@ export class NodeEnvSecureStorageService implements StorageService { } async save(key: string, obj: any): Promise { - if (typeof obj !== "string") { + if (obj == null) { + return this.remove(key); + } + + if (obj !== null && typeof obj !== "string") { throw new Error("Only string storage is allowed."); } const protectedObj = await this.encrypt(obj);