1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 17:53:39 +00:00

Feature/password protected export (#689)

* Simplify password protected file format

* no items to import is not an error

* Await inner importer

* Add export format type

* Error if import file is password protected

* Update tests

* Test password protected with normat json importer

* Simplify imports

* Ignore code coverage directory

* Expand importer options  without changing display options

* Import password require import error handling

* Use interface

* Fix curlies

* linter fixes

* Add null of empty util

* Lint fixes

* run prettier

* Move import options to separate enum file

* Fix imports
This commit is contained in:
Matt Gibson
2022-02-22 21:02:07 -06:00
committed by GitHub
parent 842d6cd001
commit 1fb3d54014
16 changed files with 194 additions and 233 deletions

View File

@@ -0,0 +1,31 @@
import { Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { BitwardenJsonImporter } from "jslib-common/importers/bitwardenJsonImporter";
import { data as passwordProtectedData } from "./testData/bitwardenJson/passwordProtected.json";
describe("bitwarden json importer", () => {
let sut: BitwardenJsonImporter;
let cryptoService: SubstituteOf<CryptoService>;
let i18nService: SubstituteOf<I18nService>;
beforeEach(() => {
cryptoService = Substitute.for<CryptoService>();
i18nService = Substitute.for<I18nService>();
sut = new BitwardenJsonImporter(cryptoService, i18nService);
});
it("should fail if password is needed", async () => {
expect((await sut.parse(passwordProtectedData)).success).toBe(false);
});
it("should return password needed error message", async () => {
const expected = "Password required error message";
i18nService.t("importPasswordRequired").returns(expected);
expect((await sut.parse(passwordProtectedData)).errorMessage).toEqual(expected);
});
});

View File

@@ -2,17 +2,15 @@ import Substitute, { Arg, SubstituteOf } from "@fluffy-spoon/substitute";
import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { ImportService } from "jslib-common/abstractions/import.service";
import { KdfType } from "jslib-common/enums/kdfType";
import { BitwardenPasswordProtectedImporter } from "jslib-common/importers/bitwardenPasswordProtectedImporter";
import { Importer } from "jslib-common/importers/importer";
import { Utils } from "jslib-common/misc/utils";
import { ImportResult } from "jslib-common/models/domain/importResult";
import { data as emptyDecryptedData } from "./testData/bitwardenJson/empty.json";
describe("BitwardenPasswordProtectedImporter", () => {
let importer: BitwardenPasswordProtectedImporter;
let innerImporter: SubstituteOf<Importer>;
let importService: SubstituteOf<ImportService>;
let cryptoService: SubstituteOf<CryptoService>;
let i18nService: SubstituteOf<I18nService>;
const password = Utils.newGuid();
@@ -20,7 +18,6 @@ describe("BitwardenPasswordProtectedImporter", () => {
let jDoc: {
encrypted?: boolean;
passwordProtected?: boolean;
format?: string;
salt?: string;
kdfIterations?: any;
kdfType?: any;
@@ -31,13 +28,10 @@ describe("BitwardenPasswordProtectedImporter", () => {
beforeEach(() => {
cryptoService = Substitute.for<CryptoService>();
i18nService = Substitute.for<I18nService>();
importService = Substitute.for<ImportService>();
innerImporter = Substitute.for<Importer>();
jDoc = {
encrypted: true,
passwordProtected: true,
format: "csv",
salt: "c2FsdA==",
kdfIterations: 100000,
kdfType: KdfType.PBKDF2_SHA256,
@@ -46,32 +40,12 @@ describe("BitwardenPasswordProtectedImporter", () => {
};
result.success = true;
innerImporter.parse(Arg.any()).resolves(result);
importer = new BitwardenPasswordProtectedImporter(
importService,
cryptoService,
i18nService,
password
);
importer = new BitwardenPasswordProtectedImporter(cryptoService, i18nService, password);
});
describe("Required Json Data", () => {
it("succeeds with default jdoc", async () => {
cryptoService.decryptToUtf8(Arg.any(), Arg.any()).resolves("successful decryption");
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(true);
});
it("accepts json format", async () => {
jDoc.format = "json";
cryptoService.decryptToUtf8(Arg.any(), Arg.any()).resolves("successful decryption");
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(true);
});
it("accepts encrypted_json format", async () => {
jDoc.format = "encrypted_json";
cryptoService.decryptToUtf8(Arg.any(), Arg.any()).resolves("successful decryption");
cryptoService.decryptToUtf8(Arg.any(), Arg.any()).resolves(emptyDecryptedData);
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(true);
});
@@ -96,16 +70,6 @@ describe("BitwardenPasswordProtectedImporter", () => {
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
});
it("fails if format === null", async () => {
jDoc.format = null;
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
});
it("fails if format not known", async () => {
jDoc.format = "Not a real format";
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
});
it("fails if salt === null", async () => {
jDoc.salt = null;
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
@@ -146,55 +110,4 @@ describe("BitwardenPasswordProtectedImporter", () => {
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
});
});
describe("inner importer", () => {
beforeEach(() => {
cryptoService.decryptToUtf8(Arg.any(), Arg.any()).resolves("successful decryption");
});
it("delegates success", async () => {
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(true);
result.success = false;
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
});
it("passes on organization Id", async () => {
jDoc.format = "csv";
importer.organizationId = Utils.newGuid();
await importer.parse(JSON.stringify(jDoc));
importService.received(1).getImporter("bitwardencsv", importer.organizationId);
});
it("passes null organizationId if none set", async () => {
jDoc.format = "csv";
importer.organizationId = null;
await importer.parse(JSON.stringify(jDoc));
importService.received(1).getImporter("bitwardencsv", null);
});
it("gets csv importer for csv format", async () => {
jDoc.format = "csv";
await importer.parse(JSON.stringify(jDoc));
importService.received(1).getImporter("bitwardencsv", Arg.any());
});
it("gets json importer for json format", async () => {
jDoc.format = "json";
await importer.parse(JSON.stringify(jDoc));
importService.received(1).getImporter("bitwardenjson", Arg.any());
});
it("gets json importer for encrypted_json format", async () => {
jDoc.format = "encrypted_json";
await importer.parse(JSON.stringify(jDoc));
importService.received(1).getImporter("bitwardenjson", Arg.any());
});
});
});

View File

@@ -0,0 +1 @@
export const data = '{"encrypted":false,"folders":[],"items":[]}';

View File

@@ -0,0 +1,9 @@
export const data = `{
"encrypted": true,
"passwordPro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fG5BO51+/vFqQj6AkaMEcwg5xNrObHYfQ/DMhIn7YDM2zdzbNTdhnobGkz6YRKFPCgFe3EmIEPEpeh9S3eKE9C7MQsrR8jVSiseR/FipJLsN+W7iOwzeXdwxUFlC/0a98bTKvdrbMgNi6ZVXykHY/t2UyEGpxZGTHoZwhX01kiQrwzC4/+v/676ldxPluO9GY7MtrLveCDsiyBz15u43IGHayDEBNT0rqrOKLYmfzwCWoahRLZQrSmepe/FXqgPqRfyWc/Ro+w3sT9dXUkx3B5xxWgSyABowPV48yBUSJuefhKTpqgzkU+LzhNnWHjnxJzzQ2/|IhlRjnyhIoDM85qHX/bY2zaIU5YaRO/iFVTQDd3uFDo="
}`;

View File

@@ -172,10 +172,6 @@ describe("ExportService", () => {
expect(exportObject.passwordProtected).toBe(true);
});
it("specifies format", () => {
expect(exportObject).toEqual(jasmine.objectContaining({ format: jasmine.any(String) }));
});
it("specifies salt", () => {
expect(exportObject.salt).toEqual("salt");
});