2022-10-10 17:19:01 +02:00
|
|
|
// eslint-disable-next-line no-restricted-imports
|
2022-02-07 16:33:10 +01:00
|
|
|
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
|
2020-12-30 22:08:02 +01:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
|
|
|
import { CryptoFunctionService } from "@bitwarden/common/abstractions/cryptoFunction.service";
|
2023-04-03 12:26:11 +02:00
|
|
|
import { KdfConfig } from "@bitwarden/common/auth/models/domain/kdf-config";
|
[AC-1266] Enums filename conventions (#5140)
* refactor: update clientType enum
* refactor: update deviceType filename
* refactor: update encryptedExportType filename
* refactor: update encryptionType filename
* refactor: update eventType filename
* refactor: update fieldType filename
* refactor: update fileUploadType filename
* refactor: update hashPurpose filename
* refactor: update htmlStorageLocation filename
* refactor: update kdfType filename
* refactor: update keySuffixOptions filename
* refactor: update linkedIdType filename
* refactor: update logLevelType filename
* refactor: update nativeMessagingVersion filename
* refactor: update notificationType filename
* refactor: update productType filename
* refactor: update secureNoteType filename
* refactor: update stateVersion filename
* refactor: update storageLocation filename
* refactor: update themeType filename
* refactor: update uriMatchType filename
* fix: update kdfType classes missed in initial pass, refs AC-1266
* fix: missing import update for device-type
* refactor: add barrel file for enums and update pathed import statements, refs AC-1266
* fix: incorrect import statements for web, refs AC-1266
* fix: missed import statement updates (browser), refs AC-1266
* fix: missed import statement changes (cli), refs AC-1266
* fix: missed import statement changes (desktop), refs AC-1266
* fix: prettier, refs AC-1266
* refactor: (libs) update relative paths to use barrel file, refs AC-1266
* fix: missed find/replace import statements for SecureNoteType, refs AC-1266
* refactor: apply .enum suffix to enums folder and modify leftover relative paths, refs AC-1266
* fix: find/replace errors for native-messaging-version, refs AC-1266
2023-04-05 05:42:21 +02:00
|
|
|
import { KdfType, DEFAULT_PBKDF2_ITERATIONS } from "@bitwarden/common/enums";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
2022-10-14 18:25:50 +02:00
|
|
|
import { EncString } from "@bitwarden/common/models/domain/enc-string";
|
|
|
|
import { CipherWithIdExport as CipherExport } from "@bitwarden/common/models/export/cipher-with-ids.export";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ExportService } from "@bitwarden/common/services/export.service";
|
2023-03-31 13:49:07 +02:00
|
|
|
import { StateService } from "@bitwarden/common/services/state.service";
|
2023-01-31 22:08:37 +01:00
|
|
|
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
|
|
|
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
|
|
|
import { CipherType } from "@bitwarden/common/vault/enums/cipher-type";
|
|
|
|
import { Cipher } from "@bitwarden/common/vault/models/domain/cipher";
|
|
|
|
import { Folder } from "@bitwarden/common/vault/models/domain/folder";
|
|
|
|
import { Login } from "@bitwarden/common/vault/models/domain/login";
|
|
|
|
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
|
|
|
|
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
|
|
|
|
import { LoginView } from "@bitwarden/common/vault/models/view/login.view";
|
2020-12-30 22:08:02 +01:00
|
|
|
|
2022-03-28 16:00:42 +02:00
|
|
|
import { BuildTestObject, GetUniqueString } from "../utils";
|
2020-12-30 22:08:02 +01:00
|
|
|
|
|
|
|
const UserCipherViews = [
|
|
|
|
generateCipherView(false),
|
|
|
|
generateCipherView(false),
|
2021-02-08 21:11:44 +01:00
|
|
|
generateCipherView(true),
|
2020-12-30 22:08:02 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
const UserCipherDomains = [
|
|
|
|
generateCipherDomain(false),
|
|
|
|
generateCipherDomain(false),
|
2021-02-08 21:11:44 +01:00
|
|
|
generateCipherDomain(true),
|
2020-12-30 22:08:02 +01:00
|
|
|
];
|
|
|
|
|
2022-12-14 15:44:10 +01:00
|
|
|
const UserFolderViews = [generateFolderView(), generateFolderView()];
|
|
|
|
|
|
|
|
const UserFolders = [generateFolder(), generateFolder()];
|
|
|
|
|
2020-12-30 22:08:02 +01:00
|
|
|
function generateCipherView(deleted: boolean) {
|
|
|
|
return BuildTestObject(
|
|
|
|
{
|
|
|
|
id: GetUniqueString("id"),
|
|
|
|
notes: GetUniqueString("notes"),
|
|
|
|
type: CipherType.Login,
|
|
|
|
login: BuildTestObject<LoginView>(
|
|
|
|
{
|
|
|
|
username: GetUniqueString("username"),
|
|
|
|
password: GetUniqueString("password"),
|
|
|
|
},
|
|
|
|
LoginView
|
|
|
|
),
|
|
|
|
collectionIds: null,
|
|
|
|
deletedDate: deleted ? new Date() : null,
|
|
|
|
},
|
|
|
|
CipherView
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateCipherDomain(deleted: boolean) {
|
|
|
|
return BuildTestObject(
|
|
|
|
{
|
|
|
|
id: GetUniqueString("id"),
|
2021-04-21 02:16:19 +02:00
|
|
|
notes: new EncString(GetUniqueString("notes")),
|
2020-12-30 22:08:02 +01:00
|
|
|
type: CipherType.Login,
|
|
|
|
login: BuildTestObject<Login>(
|
|
|
|
{
|
2021-04-21 02:16:19 +02:00
|
|
|
username: new EncString(GetUniqueString("username")),
|
|
|
|
password: new EncString(GetUniqueString("password")),
|
2020-12-30 22:08:02 +01:00
|
|
|
},
|
|
|
|
Login
|
|
|
|
),
|
|
|
|
collectionIds: null,
|
|
|
|
deletedDate: deleted ? new Date() : null,
|
|
|
|
},
|
|
|
|
Cipher
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:44:10 +01:00
|
|
|
function generateFolderView() {
|
|
|
|
return BuildTestObject(
|
|
|
|
{
|
|
|
|
id: GetUniqueString("id"),
|
|
|
|
name: GetUniqueString("name"),
|
|
|
|
revisionDate: new Date(),
|
|
|
|
},
|
|
|
|
FolderView
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateFolder() {
|
|
|
|
const actual = Folder.fromJSON({
|
|
|
|
revisionDate: new Date("2022-08-04T01:06:40.441Z").toISOString(),
|
|
|
|
name: "name",
|
|
|
|
id: "id",
|
|
|
|
});
|
|
|
|
return actual;
|
|
|
|
}
|
|
|
|
|
2020-12-30 22:08:02 +01:00
|
|
|
function expectEqualCiphers(ciphers: CipherView[] | Cipher[], jsonResult: string) {
|
|
|
|
const actual = JSON.stringify(JSON.parse(jsonResult).items);
|
|
|
|
const items: CipherExport[] = [];
|
|
|
|
ciphers.forEach((c: CipherView | Cipher) => {
|
|
|
|
const item = new CipherExport();
|
|
|
|
item.build(c);
|
|
|
|
items.push(item);
|
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2020-12-30 22:08:02 +01:00
|
|
|
expect(actual).toEqual(JSON.stringify(items));
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:44:10 +01:00
|
|
|
function expectEqualFolderViews(folderviews: FolderView[] | Folder[], jsonResult: string) {
|
|
|
|
const actual = JSON.stringify(JSON.parse(jsonResult).folders);
|
|
|
|
const folders: FolderResponse[] = [];
|
|
|
|
folderviews.forEach((c) => {
|
|
|
|
const folder = new FolderResponse();
|
|
|
|
folder.id = c.id;
|
|
|
|
folder.name = c.name.toString();
|
|
|
|
folders.push(folder);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(actual.length).toBeGreaterThan(0);
|
|
|
|
expect(actual).toEqual(JSON.stringify(folders));
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectEqualFolders(folders: Folder[], jsonResult: string) {
|
|
|
|
const actual = JSON.stringify(JSON.parse(jsonResult).folders);
|
|
|
|
const items: Folder[] = [];
|
|
|
|
folders.forEach((c) => {
|
|
|
|
const item = new Folder();
|
|
|
|
item.id = c.id;
|
|
|
|
item.name = c.name;
|
|
|
|
items.push(item);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(actual.length).toBeGreaterThan(0);
|
|
|
|
expect(actual).toEqual(JSON.stringify(items));
|
|
|
|
}
|
|
|
|
|
2020-12-30 22:08:02 +01:00
|
|
|
describe("ExportService", () => {
|
|
|
|
let exportService: ExportService;
|
|
|
|
let apiService: SubstituteOf<ApiService>;
|
2022-02-07 16:33:10 +01:00
|
|
|
let cryptoFunctionService: SubstituteOf<CryptoFunctionService>;
|
2020-12-30 22:08:02 +01:00
|
|
|
let cipherService: SubstituteOf<CipherService>;
|
|
|
|
let folderService: SubstituteOf<FolderService>;
|
2021-05-13 02:58:59 +02:00
|
|
|
let cryptoService: SubstituteOf<CryptoService>;
|
2023-03-31 13:49:07 +02:00
|
|
|
let stateService: SubstituteOf<StateService>;
|
2020-12-30 22:08:02 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
apiService = Substitute.for<ApiService>();
|
2022-02-07 16:33:10 +01:00
|
|
|
cryptoFunctionService = Substitute.for<CryptoFunctionService>();
|
2020-12-30 22:08:02 +01:00
|
|
|
cipherService = Substitute.for<CipherService>();
|
|
|
|
folderService = Substitute.for<FolderService>();
|
2021-05-13 02:58:59 +02:00
|
|
|
cryptoService = Substitute.for<CryptoService>();
|
2023-04-03 12:26:11 +02:00
|
|
|
stateService = Substitute.for<StateService>();
|
2020-12-30 22:08:02 +01:00
|
|
|
|
2022-12-14 15:44:10 +01:00
|
|
|
folderService.getAllDecryptedFromState().resolves(UserFolderViews);
|
|
|
|
folderService.getAllFromState().resolves(UserFolders);
|
2023-04-03 12:26:11 +02:00
|
|
|
stateService.getKdfType().resolves(KdfType.PBKDF2_SHA256);
|
|
|
|
stateService.getKdfConfig().resolves(new KdfConfig(DEFAULT_PBKDF2_ITERATIONS));
|
2020-12-30 22:08:02 +01:00
|
|
|
|
2022-02-07 16:33:10 +01:00
|
|
|
exportService = new ExportService(
|
|
|
|
folderService,
|
|
|
|
cipherService,
|
|
|
|
apiService,
|
|
|
|
cryptoService,
|
2023-03-31 13:49:07 +02:00
|
|
|
cryptoFunctionService,
|
|
|
|
stateService
|
2022-02-07 16:33:10 +01:00
|
|
|
);
|
2020-12-30 22:08:02 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("exports unecrypted user ciphers", async () => {
|
|
|
|
cipherService.getAllDecrypted().resolves(UserCipherViews.slice(0, 1));
|
|
|
|
|
|
|
|
const actual = await exportService.getExport("json");
|
|
|
|
|
|
|
|
expectEqualCiphers(UserCipherViews.slice(0, 1), actual);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("exports encrypted json user ciphers", async () => {
|
|
|
|
cipherService.getAll().resolves(UserCipherDomains.slice(0, 1));
|
|
|
|
|
|
|
|
const actual = await exportService.getExport("encrypted_json");
|
|
|
|
|
|
|
|
expectEqualCiphers(UserCipherDomains.slice(0, 1), actual);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not unecrypted export trashed user items", async () => {
|
|
|
|
cipherService.getAllDecrypted().resolves(UserCipherViews);
|
|
|
|
|
|
|
|
const actual = await exportService.getExport("json");
|
|
|
|
|
|
|
|
expectEqualCiphers(UserCipherViews.slice(0, 2), actual);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not encrypted export trashed user items", async () => {
|
|
|
|
cipherService.getAll().resolves(UserCipherDomains);
|
|
|
|
|
|
|
|
const actual = await exportService.getExport("encrypted_json");
|
|
|
|
|
|
|
|
expectEqualCiphers(UserCipherDomains.slice(0, 2), actual);
|
|
|
|
});
|
2022-02-07 16:33:10 +01:00
|
|
|
|
|
|
|
describe("password protected export", () => {
|
|
|
|
let exportString: string;
|
|
|
|
let exportObject: any;
|
|
|
|
let mac: SubstituteOf<EncString>;
|
|
|
|
let data: SubstituteOf<EncString>;
|
|
|
|
const password = "password";
|
|
|
|
const salt = "salt";
|
|
|
|
|
|
|
|
describe("export json object", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mac = Substitute.for<EncString>();
|
|
|
|
data = Substitute.for<EncString>();
|
|
|
|
|
2022-03-28 16:00:42 +02:00
|
|
|
mac.encryptedString.returns("mac");
|
|
|
|
data.encryptedString.returns("encData");
|
2022-02-07 16:33:10 +01:00
|
|
|
|
2022-03-28 16:00:42 +02:00
|
|
|
jest.spyOn(Utils, "fromBufferToB64").mockReturnValue(salt);
|
2022-02-07 16:33:10 +01:00
|
|
|
cipherService.getAllDecrypted().resolves(UserCipherViews.slice(0, 1));
|
|
|
|
|
|
|
|
exportString = await exportService.getPasswordProtectedExport(password);
|
|
|
|
exportObject = JSON.parse(exportString);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("specifies it is encrypted", () => {
|
|
|
|
expect(exportObject.encrypted).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("specifies it's password protected", () => {
|
|
|
|
expect(exportObject.passwordProtected).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("specifies salt", () => {
|
|
|
|
expect(exportObject.salt).toEqual("salt");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("specifies kdfIterations", () => {
|
2023-01-26 15:20:12 +01:00
|
|
|
expect(exportObject.kdfIterations).toEqual(DEFAULT_PBKDF2_ITERATIONS);
|
2022-02-07 16:33:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("has kdfType", () => {
|
|
|
|
expect(exportObject.kdfType).toEqual(KdfType.PBKDF2_SHA256);
|
|
|
|
});
|
|
|
|
|
2022-03-28 16:00:42 +02:00
|
|
|
it("has a mac property", async () => {
|
2022-02-07 16:33:10 +01:00
|
|
|
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(mac);
|
2022-03-28 16:00:42 +02:00
|
|
|
exportString = await exportService.getPasswordProtectedExport(password);
|
|
|
|
exportObject = JSON.parse(exportString);
|
|
|
|
|
2022-02-07 16:33:10 +01:00
|
|
|
expect(exportObject.encKeyValidation_DO_NOT_EDIT).toEqual(mac.encryptedString);
|
|
|
|
});
|
|
|
|
|
2022-03-28 16:00:42 +02:00
|
|
|
it("has data property", async () => {
|
2022-02-07 16:33:10 +01:00
|
|
|
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(data);
|
2022-03-28 16:00:42 +02:00
|
|
|
exportString = await exportService.getPasswordProtectedExport(password);
|
|
|
|
exportObject = JSON.parse(exportString);
|
|
|
|
|
2022-02-07 16:33:10 +01:00
|
|
|
expect(exportObject.data).toEqual(data.encryptedString);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("encrypts the data property", async () => {
|
|
|
|
const unencrypted = await exportService.getExport();
|
|
|
|
expect(exportObject.data).not.toEqual(unencrypted);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-12-14 15:44:10 +01:00
|
|
|
|
|
|
|
it("exported unencrypted object contains folders", async () => {
|
|
|
|
cipherService.getAllDecrypted().resolves(UserCipherViews.slice(0, 1));
|
|
|
|
await folderService.getAllDecryptedFromState();
|
|
|
|
const actual = await exportService.getExport("json");
|
|
|
|
|
|
|
|
expectEqualFolderViews(UserFolderViews, actual);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("exported encrypted json contains folders", async () => {
|
|
|
|
cipherService.getAll().resolves(UserCipherDomains.slice(0, 1));
|
|
|
|
await folderService.getAllFromState();
|
|
|
|
const actual = await exportService.getExport("encrypted_json");
|
|
|
|
|
|
|
|
expectEqualFolders(UserFolders, actual);
|
|
|
|
});
|
2020-12-30 22:08:02 +01:00
|
|
|
});
|
2022-12-14 15:44:10 +01:00
|
|
|
|
|
|
|
export class FolderResponse {
|
|
|
|
id: string = null;
|
|
|
|
name: string = null;
|
|
|
|
}
|