diff --git a/common/spec/domain/attachment.spec.ts b/common/spec/domain/attachment.spec.ts new file mode 100644 index 0000000000..f625adbfff --- /dev/null +++ b/common/spec/domain/attachment.spec.ts @@ -0,0 +1,83 @@ +import Substitute, { Arg } from "@fluffy-spoon/substitute"; + +import { CryptoService } from "jslib-common/abstractions/crypto.service"; +import { AttachmentData } from "jslib-common/models/data/attachmentData"; +import { Attachment } from "jslib-common/models/domain/attachment"; +import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey"; +import { ContainerService } from "jslib-common/services/container.service"; + +import { makeStaticByteArray, mockEnc } from "../utils"; + +describe("Attachment", () => { + let data: AttachmentData; + + beforeEach(() => { + data = { + id: "id", + url: "url", + fileName: "fileName", + key: "key", + size: "1100", + sizeName: "1.1 KB", + }; + }); + + it("Convert from empty", () => { + const data = new AttachmentData(); + const attachment = new Attachment(data); + + expect(attachment).toEqual({ + id: null, + url: null, + size: undefined, + sizeName: null, + key: null, + fileName: null, + }); + }); + + it("Convert", () => { + const attachment = new Attachment(data); + + expect(attachment).toEqual({ + size: "1100", + id: "id", + url: "url", + sizeName: "1.1 KB", + fileName: { encryptedString: "fileName", encryptionType: 0 }, + key: { encryptedString: "key", encryptionType: 0 }, + }); + }); + + it("toAttachmentData", () => { + const attachment = new Attachment(data); + expect(attachment.toAttachmentData()).toEqual(data); + }); + + it("Decrypt", async () => { + const attachment = new Attachment(); + attachment.id = "id"; + attachment.url = "url"; + attachment.size = "1100"; + attachment.sizeName = "1.1 KB"; + attachment.key = mockEnc("key"); + attachment.fileName = mockEnc("fileName"); + + const cryptoService = Substitute.for(); + cryptoService.getOrgKey(null).resolves(null); + cryptoService.decryptToBytes(Arg.any(), Arg.any()).resolves(makeStaticByteArray(32)); + + (window as any).bitwardenContainerService = new ContainerService(cryptoService); + + const view = await attachment.decrypt(null); + + expect(view).toEqual({ + id: "id", + url: "url", + size: "1100", + sizeName: "1.1 KB", + fileName: "fileName", + key: expect.any(SymmetricCryptoKey), + }); + }); +}); diff --git a/common/spec/domain/card.spec.ts b/common/spec/domain/card.spec.ts new file mode 100644 index 0000000000..f4709bccf9 --- /dev/null +++ b/common/spec/domain/card.spec.ts @@ -0,0 +1,73 @@ +import { CardData } from "jslib-common/models/data/cardData"; +import { Card } from "jslib-common/models/domain/card"; + +import { mockEnc } from "../utils"; + +describe("Card", () => { + let data: CardData; + + beforeEach(() => { + data = { + cardholderName: "encHolder", + brand: "encBrand", + number: "encNumber", + expMonth: "encMonth", + expYear: "encYear", + code: "encCode", + }; + }); + + it("Convert from empty", () => { + const data = new CardData(); + const card = new Card(data); + + expect(card).toEqual({ + cardholderName: null, + brand: null, + number: null, + expMonth: null, + expYear: null, + code: null, + }); + }); + + it("Convert", () => { + const card = new Card(data); + + expect(card).toEqual({ + cardholderName: { encryptedString: "encHolder", encryptionType: 0 }, + brand: { encryptedString: "encBrand", encryptionType: 0 }, + number: { encryptedString: "encNumber", encryptionType: 0 }, + expMonth: { encryptedString: "encMonth", encryptionType: 0 }, + expYear: { encryptedString: "encYear", encryptionType: 0 }, + code: { encryptedString: "encCode", encryptionType: 0 }, + }); + }); + + it("toCardData", () => { + const card = new Card(data); + expect(card.toCardData()).toEqual(data); + }); + + it("Decrypt", async () => { + const card = new Card(); + card.cardholderName = mockEnc("cardHolder"); + card.brand = mockEnc("brand"); + card.number = mockEnc("number"); + card.expMonth = mockEnc("expMonth"); + card.expYear = mockEnc("expYear"); + card.code = mockEnc("code"); + + const view = await card.decrypt(null); + + expect(view).toEqual({ + _brand: "brand", + _number: "number", + _subTitle: null, + cardholderName: "cardHolder", + code: "code", + expMonth: "expMonth", + expYear: "expYear", + }); + }); +}); diff --git a/common/spec/domain/cipher.spec.ts b/common/spec/domain/cipher.spec.ts new file mode 100644 index 0000000000..0e6ed45237 --- /dev/null +++ b/common/spec/domain/cipher.spec.ts @@ -0,0 +1,599 @@ +import Substitute, { Arg } from "@fluffy-spoon/substitute"; + +import { CipherRepromptType } from "jslib-common/enums/cipherRepromptType"; +import { CipherType } from "jslib-common/enums/cipherType"; +import { FieldType } from "jslib-common/enums/fieldType"; +import { SecureNoteType } from "jslib-common/enums/secureNoteType"; +import { UriMatchType } from "jslib-common/enums/uriMatchType"; +import { CipherData } from "jslib-common/models/data/cipherData"; +import { Card } from "jslib-common/models/domain/card"; +import { Cipher } from "jslib-common/models/domain/cipher"; +import { Identity } from "jslib-common/models/domain/identity"; +import { Login } from "jslib-common/models/domain/login"; +import { SecureNote } from "jslib-common/models/domain/secureNote"; +import { CardView } from "jslib-common/models/view/cardView"; +import { IdentityView } from "jslib-common/models/view/identityView"; +import { LoginView } from "jslib-common/models/view/loginView"; + +import { mockEnc } from "../utils"; + +describe("Cipher DTO", () => { + it("Convert from empty CipherData", () => { + const data = new CipherData(); + const cipher = new Cipher(data); + + expect(cipher).toEqual({ + id: null, + userId: null, + organizationId: null, + folderId: null, + name: null, + notes: null, + type: undefined, + favorite: undefined, + organizationUseTotp: undefined, + edit: undefined, + viewPassword: true, + revisionDate: null, + collectionIds: undefined, + localData: null, + deletedDate: null, + reprompt: undefined, + attachments: null, + fields: null, + passwordHistory: null, + }); + }); + + describe("LoginCipher", () => { + let cipherData: CipherData; + + beforeEach(() => { + cipherData = { + id: "id", + organizationId: "orgId", + folderId: "folderId", + userId: "userId", + edit: true, + viewPassword: true, + organizationUseTotp: true, + favorite: false, + revisionDate: "2022-01-31T12:00:00.000Z", + type: CipherType.Login, + name: "EncryptedString", + notes: "EncryptedString", + deletedDate: null, + reprompt: CipherRepromptType.None, + login: { + uris: [{ uri: "EncryptedString", match: UriMatchType.Domain }], + username: "EncryptedString", + password: "EncryptedString", + passwordRevisionDate: "2022-01-31T12:00:00.000Z", + totp: "EncryptedString", + autofillOnPageLoad: false, + }, + passwordHistory: [ + { password: "EncryptedString", lastUsedDate: "2022-01-31T12:00:00.000Z" }, + ], + attachments: [ + { + id: "a1", + url: "url", + size: "1100", + sizeName: "1.1 KB", + fileName: "file", + key: "EncKey", + }, + { + id: "a2", + url: "url", + size: "1100", + sizeName: "1.1 KB", + fileName: "file", + key: "EncKey", + }, + ], + fields: [ + { + name: "EncryptedString", + value: "EncryptedString", + type: FieldType.Text, + linkedId: null, + }, + { + name: "EncryptedString", + value: "EncryptedString", + type: FieldType.Hidden, + linkedId: null, + }, + ], + }; + }); + + it("Convert", () => { + const cipher = new Cipher(cipherData); + + expect(cipher).toEqual({ + id: "id", + userId: "userId", + organizationId: "orgId", + folderId: "folderId", + name: { encryptedString: "EncryptedString", encryptionType: 0 }, + notes: { encryptedString: "EncryptedString", encryptionType: 0 }, + type: 1, + favorite: false, + organizationUseTotp: true, + edit: true, + viewPassword: true, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + collectionIds: undefined, + localData: null, + deletedDate: null, + reprompt: 0, + login: { + passwordRevisionDate: new Date("2022-01-31T12:00:00.000Z"), + autofillOnPageLoad: false, + username: { encryptedString: "EncryptedString", encryptionType: 0 }, + password: { encryptedString: "EncryptedString", encryptionType: 0 }, + totp: { encryptedString: "EncryptedString", encryptionType: 0 }, + uris: [{ match: 0, uri: { encryptedString: "EncryptedString", encryptionType: 0 } }], + }, + attachments: [ + { + fileName: { encryptedString: "file", encryptionType: 0 }, + id: "a1", + key: { encryptedString: "EncKey", encryptionType: 0 }, + size: "1100", + sizeName: "1.1 KB", + url: "url", + }, + { + fileName: { encryptedString: "file", encryptionType: 0 }, + id: "a2", + key: { encryptedString: "EncKey", encryptionType: 0 }, + size: "1100", + sizeName: "1.1 KB", + url: "url", + }, + ], + fields: [ + { + linkedId: null, + name: { encryptedString: "EncryptedString", encryptionType: 0 }, + type: 0, + value: { encryptedString: "EncryptedString", encryptionType: 0 }, + }, + { + linkedId: null, + name: { encryptedString: "EncryptedString", encryptionType: 0 }, + type: 1, + value: { encryptedString: "EncryptedString", encryptionType: 0 }, + }, + ], + passwordHistory: [ + { + lastUsedDate: new Date("2022-01-31T12:00:00.000Z"), + password: { encryptedString: "EncryptedString", encryptionType: 0 }, + }, + ], + }); + }); + + it("toCipherData", () => { + const cipher = new Cipher(cipherData); + expect(cipher.toCipherData("userId")).toEqual(cipherData); + }); + + it("Decrypt", async () => { + const cipher = new Cipher(); + cipher.id = "id"; + cipher.organizationId = "orgId"; + cipher.folderId = "folderId"; + cipher.edit = true; + cipher.viewPassword = true; + cipher.organizationUseTotp = true; + cipher.favorite = false; + cipher.revisionDate = new Date("2022-01-31T12:00:00.000Z"); + cipher.type = CipherType.Login; + cipher.name = mockEnc("EncryptedString"); + cipher.notes = mockEnc("EncryptedString"); + cipher.deletedDate = null; + cipher.reprompt = CipherRepromptType.None; + + const loginView = new LoginView(); + loginView.username = "username"; + loginView.password = "password"; + + const login = Substitute.for(); + login.decrypt(Arg.any(), Arg.any()).resolves(loginView); + cipher.login = login; + + const cipherView = await cipher.decrypt(); + + expect(cipherView).toMatchObject({ + id: "id", + organizationId: "orgId", + folderId: "folderId", + name: "EncryptedString", + notes: "EncryptedString", + type: 1, + favorite: false, + organizationUseTotp: true, + edit: true, + viewPassword: true, + login: loginView, + attachments: null, + fields: null, + passwordHistory: null, + collectionIds: undefined, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + deletedDate: null, + reprompt: 0, + localData: undefined, + }); + }); + }); + + describe("SecureNoteCipher", () => { + let cipherData: CipherData; + + beforeEach(() => { + cipherData = { + id: "id", + organizationId: "orgId", + folderId: "folderId", + userId: "userId", + edit: true, + viewPassword: true, + organizationUseTotp: true, + favorite: false, + revisionDate: "2022-01-31T12:00:00.000Z", + type: CipherType.SecureNote, + name: "EncryptedString", + notes: "EncryptedString", + deletedDate: null, + reprompt: CipherRepromptType.None, + secureNote: { + type: SecureNoteType.Generic, + }, + }; + }); + + it("Convert", () => { + const cipher = new Cipher(cipherData); + + expect(cipher).toEqual({ + id: "id", + userId: "userId", + organizationId: "orgId", + folderId: "folderId", + name: { encryptedString: "EncryptedString", encryptionType: 0 }, + notes: { encryptedString: "EncryptedString", encryptionType: 0 }, + type: 2, + favorite: false, + organizationUseTotp: true, + edit: true, + viewPassword: true, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + collectionIds: undefined, + localData: null, + deletedDate: null, + reprompt: 0, + secureNote: { type: SecureNoteType.Generic }, + attachments: null, + fields: null, + passwordHistory: null, + }); + }); + + it("toCipherData", () => { + const cipher = new Cipher(cipherData); + expect(cipher.toCipherData("userId")).toEqual(cipherData); + }); + + it("Decrypt", async () => { + const cipher = new Cipher(); + cipher.id = "id"; + cipher.organizationId = "orgId"; + cipher.folderId = "folderId"; + cipher.edit = true; + cipher.viewPassword = true; + cipher.organizationUseTotp = true; + cipher.favorite = false; + cipher.revisionDate = new Date("2022-01-31T12:00:00.000Z"); + cipher.type = CipherType.SecureNote; + cipher.name = mockEnc("EncryptedString"); + cipher.notes = mockEnc("EncryptedString"); + cipher.deletedDate = null; + cipher.reprompt = CipherRepromptType.None; + cipher.secureNote = new SecureNote(); + cipher.secureNote.type = SecureNoteType.Generic; + + const cipherView = await cipher.decrypt(); + + expect(cipherView).toMatchObject({ + id: "id", + organizationId: "orgId", + folderId: "folderId", + name: "EncryptedString", + notes: "EncryptedString", + type: 2, + favorite: false, + organizationUseTotp: true, + edit: true, + viewPassword: true, + secureNote: { type: 0 }, + attachments: null, + fields: null, + passwordHistory: null, + collectionIds: undefined, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + deletedDate: null, + reprompt: 0, + localData: undefined, + }); + }); + }); + + describe("CardCipher", () => { + let cipherData: CipherData; + + beforeEach(() => { + cipherData = { + id: "id", + organizationId: "orgId", + folderId: "folderId", + userId: "userId", + edit: true, + viewPassword: true, + organizationUseTotp: true, + favorite: false, + revisionDate: "2022-01-31T12:00:00.000Z", + type: CipherType.Card, + name: "EncryptedString", + notes: "EncryptedString", + deletedDate: null, + reprompt: CipherRepromptType.None, + card: { + cardholderName: "EncryptedString", + brand: "EncryptedString", + number: "EncryptedString", + expMonth: "EncryptedString", + expYear: "EncryptedString", + code: "EncryptedString", + }, + }; + }); + + it("Convert", () => { + const cipher = new Cipher(cipherData); + + expect(cipher).toEqual({ + id: "id", + userId: "userId", + organizationId: "orgId", + folderId: "folderId", + name: { encryptedString: "EncryptedString", encryptionType: 0 }, + notes: { encryptedString: "EncryptedString", encryptionType: 0 }, + type: 3, + favorite: false, + organizationUseTotp: true, + edit: true, + viewPassword: true, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + collectionIds: undefined, + localData: null, + deletedDate: null, + reprompt: 0, + card: { + cardholderName: { encryptedString: "EncryptedString", encryptionType: 0 }, + brand: { encryptedString: "EncryptedString", encryptionType: 0 }, + number: { encryptedString: "EncryptedString", encryptionType: 0 }, + expMonth: { encryptedString: "EncryptedString", encryptionType: 0 }, + expYear: { encryptedString: "EncryptedString", encryptionType: 0 }, + code: { encryptedString: "EncryptedString", encryptionType: 0 }, + }, + attachments: null, + fields: null, + passwordHistory: null, + }); + }); + + it("toCipherData", () => { + const cipher = new Cipher(cipherData); + expect(cipher.toCipherData("userId")).toEqual(cipherData); + }); + + it("Decrypt", async () => { + const cipher = new Cipher(); + cipher.id = "id"; + cipher.organizationId = "orgId"; + cipher.folderId = "folderId"; + cipher.edit = true; + cipher.viewPassword = true; + cipher.organizationUseTotp = true; + cipher.favorite = false; + cipher.revisionDate = new Date("2022-01-31T12:00:00.000Z"); + cipher.type = CipherType.Card; + cipher.name = mockEnc("EncryptedString"); + cipher.notes = mockEnc("EncryptedString"); + cipher.deletedDate = null; + cipher.reprompt = CipherRepromptType.None; + + const cardView = new CardView(); + cardView.cardholderName = "cardholderName"; + cardView.number = "4111111111111111"; + + const card = Substitute.for(); + card.decrypt(Arg.any(), Arg.any()).resolves(cardView); + cipher.card = card; + + const cipherView = await cipher.decrypt(); + + expect(cipherView).toMatchObject({ + id: "id", + organizationId: "orgId", + folderId: "folderId", + name: "EncryptedString", + notes: "EncryptedString", + type: 3, + favorite: false, + organizationUseTotp: true, + edit: true, + viewPassword: true, + card: cardView, + attachments: null, + fields: null, + passwordHistory: null, + collectionIds: undefined, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + deletedDate: null, + reprompt: 0, + localData: undefined, + }); + }); + }); + + describe("IdentityCipher", () => { + let cipherData: CipherData; + + beforeEach(() => { + cipherData = { + id: "id", + organizationId: "orgId", + folderId: "folderId", + userId: "userId", + edit: true, + viewPassword: true, + organizationUseTotp: true, + favorite: false, + revisionDate: "2022-01-31T12:00:00.000Z", + type: CipherType.Identity, + name: "EncryptedString", + notes: "EncryptedString", + deletedDate: null, + reprompt: CipherRepromptType.None, + identity: { + title: "EncryptedString", + firstName: "EncryptedString", + middleName: "EncryptedString", + lastName: "EncryptedString", + address1: "EncryptedString", + address2: "EncryptedString", + address3: "EncryptedString", + city: "EncryptedString", + state: "EncryptedString", + postalCode: "EncryptedString", + country: "EncryptedString", + company: "EncryptedString", + email: "EncryptedString", + phone: "EncryptedString", + ssn: "EncryptedString", + username: "EncryptedString", + passportNumber: "EncryptedString", + licenseNumber: "EncryptedString", + }, + }; + }); + + it("Convert", () => { + const cipher = new Cipher(cipherData); + + expect(cipher).toEqual({ + id: "id", + userId: "userId", + organizationId: "orgId", + folderId: "folderId", + name: { encryptedString: "EncryptedString", encryptionType: 0 }, + notes: { encryptedString: "EncryptedString", encryptionType: 0 }, + type: 4, + favorite: false, + organizationUseTotp: true, + edit: true, + viewPassword: true, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + collectionIds: undefined, + localData: null, + deletedDate: null, + reprompt: 0, + identity: { + title: { encryptedString: "EncryptedString", encryptionType: 0 }, + firstName: { encryptedString: "EncryptedString", encryptionType: 0 }, + middleName: { encryptedString: "EncryptedString", encryptionType: 0 }, + lastName: { encryptedString: "EncryptedString", encryptionType: 0 }, + address1: { encryptedString: "EncryptedString", encryptionType: 0 }, + address2: { encryptedString: "EncryptedString", encryptionType: 0 }, + address3: { encryptedString: "EncryptedString", encryptionType: 0 }, + city: { encryptedString: "EncryptedString", encryptionType: 0 }, + state: { encryptedString: "EncryptedString", encryptionType: 0 }, + postalCode: { encryptedString: "EncryptedString", encryptionType: 0 }, + country: { encryptedString: "EncryptedString", encryptionType: 0 }, + company: { encryptedString: "EncryptedString", encryptionType: 0 }, + email: { encryptedString: "EncryptedString", encryptionType: 0 }, + phone: { encryptedString: "EncryptedString", encryptionType: 0 }, + ssn: { encryptedString: "EncryptedString", encryptionType: 0 }, + username: { encryptedString: "EncryptedString", encryptionType: 0 }, + passportNumber: { encryptedString: "EncryptedString", encryptionType: 0 }, + licenseNumber: { encryptedString: "EncryptedString", encryptionType: 0 }, + }, + attachments: null, + fields: null, + passwordHistory: null, + }); + }); + + it("toCipherData", () => { + const cipher = new Cipher(cipherData); + expect(cipher.toCipherData("userId")).toEqual(cipherData); + }); + + it("Decrypt", async () => { + const cipher = new Cipher(); + cipher.id = "id"; + cipher.organizationId = "orgId"; + cipher.folderId = "folderId"; + cipher.edit = true; + cipher.viewPassword = true; + cipher.organizationUseTotp = true; + cipher.favorite = false; + cipher.revisionDate = new Date("2022-01-31T12:00:00.000Z"); + cipher.type = CipherType.Identity; + cipher.name = mockEnc("EncryptedString"); + cipher.notes = mockEnc("EncryptedString"); + cipher.deletedDate = null; + cipher.reprompt = CipherRepromptType.None; + + const identityView = new IdentityView(); + identityView.firstName = "firstName"; + identityView.lastName = "lastName"; + + const identity = Substitute.for(); + identity.decrypt(Arg.any(), Arg.any()).resolves(identityView); + cipher.identity = identity; + + const cipherView = await cipher.decrypt(); + + expect(cipherView).toMatchObject({ + id: "id", + organizationId: "orgId", + folderId: "folderId", + name: "EncryptedString", + notes: "EncryptedString", + type: 4, + favorite: false, + organizationUseTotp: true, + edit: true, + viewPassword: true, + identity: identityView, + attachments: null, + fields: null, + passwordHistory: null, + collectionIds: undefined, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + deletedDate: null, + reprompt: 0, + localData: undefined, + }); + }); + }); +}); diff --git a/common/spec/domain/collection.spec.ts b/common/spec/domain/collection.spec.ts new file mode 100644 index 0000000000..713bf2eba2 --- /dev/null +++ b/common/spec/domain/collection.spec.ts @@ -0,0 +1,66 @@ +import { CollectionData } from "jslib-common/models/data/collectionData"; +import { Collection } from "jslib-common/models/domain/collection"; + +import { mockEnc } from "../utils"; + +describe("Collection", () => { + let data: CollectionData; + + beforeEach(() => { + data = { + id: "id", + organizationId: "orgId", + name: "encName", + externalId: "extId", + readOnly: true, + }; + }); + + it("Convert from empty", () => { + const data = new CollectionData({} as any); + const card = new Collection(data); + + expect(card).toEqual({ + externalId: null, + hidePasswords: null, + id: null, + name: null, + organizationId: null, + readOnly: null, + }); + }); + + it("Convert", () => { + const collection = new Collection(data); + + expect(collection).toEqual({ + id: "id", + organizationId: "orgId", + name: { encryptedString: "encName", encryptionType: 0 }, + externalId: "extId", + readOnly: true, + hidePasswords: null, + }); + }); + + it("Decrypt", async () => { + const collection = new Collection(); + collection.id = "id"; + collection.organizationId = "orgId"; + collection.name = mockEnc("encName"); + collection.externalId = "extId"; + collection.readOnly = false; + collection.hidePasswords = false; + + const view = await collection.decrypt(); + + expect(view).toEqual({ + externalId: "extId", + hidePasswords: false, + id: "id", + name: "encName", + organizationId: "orgId", + readOnly: false, + }); + }); +}); diff --git a/common/spec/domain/encString.spec.ts b/common/spec/domain/encString.spec.ts new file mode 100644 index 0000000000..e2477447d6 --- /dev/null +++ b/common/spec/domain/encString.spec.ts @@ -0,0 +1,195 @@ +import Substitute, { Arg } from "@fluffy-spoon/substitute"; + +import { CryptoService } from "jslib-common/abstractions/crypto.service"; +import { EncryptionType } from "jslib-common/enums/encryptionType"; +import { EncString } from "jslib-common/models/domain/encString"; +import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey"; +import { ContainerService } from "jslib-common/services/container.service"; + +describe("EncString", () => { + afterEach(() => { + (window as any).bitwardenContainerService = undefined; + }); + + describe("Rsa2048_OaepSha256_B64", () => { + it("constructor", () => { + const encString = new EncString(EncryptionType.Rsa2048_OaepSha256_B64, "data"); + + expect(encString).toEqual({ + data: "data", + encryptedString: "3.data", + encryptionType: 3, + }); + }); + + describe("parse existing", () => { + it("valid", () => { + const encString = new EncString("3.data"); + + expect(encString).toEqual({ + data: "data", + encryptedString: "3.data", + encryptionType: 3, + }); + }); + + it("invalid", () => { + const encString = new EncString("3.data|test"); + + expect(encString).toEqual({ + encryptedString: "3.data|test", + encryptionType: 3, + }); + }); + }); + + describe("decrypt", () => { + const encString = new EncString(EncryptionType.Rsa2048_OaepSha256_B64, "data"); + + const cryptoService = Substitute.for(); + cryptoService.getOrgKey(null).resolves(null); + cryptoService.decryptToUtf8(encString, Arg.any()).resolves("decrypted"); + + beforeEach(() => { + (window as any).bitwardenContainerService = new ContainerService(cryptoService); + }); + + it("decrypts correctly", async () => { + const decrypted = await encString.decrypt(null); + + expect(decrypted).toBe("decrypted"); + }); + + it("result should be cached", async () => { + const decrypted = await encString.decrypt(null); + cryptoService.received(1).decryptToUtf8(Arg.any(), Arg.any()); + + expect(decrypted).toBe("decrypted"); + }); + }); + }); + + describe("AesCbc256_B64", () => { + it("constructor", () => { + const encString = new EncString(EncryptionType.AesCbc256_B64, "data", "iv"); + + expect(encString).toEqual({ + data: "data", + encryptedString: "0.iv|data", + encryptionType: 0, + iv: "iv", + }); + }); + + describe("parse existing", () => { + it("valid", () => { + const encString = new EncString("0.iv|data"); + + expect(encString).toEqual({ + data: "data", + encryptedString: "0.iv|data", + encryptionType: 0, + iv: "iv", + }); + }); + + it("invalid", () => { + const encString = new EncString("0.iv|data|mac"); + + expect(encString).toEqual({ + encryptedString: "0.iv|data|mac", + encryptionType: 0, + }); + }); + }); + }); + + describe("AesCbc256_HmacSha256_B64", () => { + it("constructor", () => { + const encString = new EncString(EncryptionType.AesCbc256_HmacSha256_B64, "data", "iv", "mac"); + + expect(encString).toEqual({ + data: "data", + encryptedString: "2.iv|data|mac", + encryptionType: 2, + iv: "iv", + mac: "mac", + }); + }); + + it("valid", () => { + const encString = new EncString("2.iv|data|mac"); + + expect(encString).toEqual({ + data: "data", + encryptedString: "2.iv|data|mac", + encryptionType: 2, + iv: "iv", + mac: "mac", + }); + }); + + it("invalid", () => { + const encString = new EncString("2.iv|data"); + + expect(encString).toEqual({ + encryptedString: "2.iv|data", + encryptionType: 2, + }); + }); + }); + + it("Exit early if null", () => { + const encString = new EncString(null); + + expect(encString).toEqual({ + encryptedString: null, + }); + }); + + describe("decrypt", () => { + it("throws exception when bitwarden container not initialized", async () => { + const encString = new EncString(null); + + expect.assertions(1); + try { + await encString.decrypt(null); + } catch (e) { + expect(e.message).toEqual("global bitwardenContainerService not initialized."); + } + }); + + it("handles value it can't decrypt", async () => { + const encString = new EncString(null); + + const cryptoService = Substitute.for(); + cryptoService.getOrgKey(null).resolves(null); + cryptoService.decryptToUtf8(encString, Arg.any()).throws("error"); + + (window as any).bitwardenContainerService = new ContainerService(cryptoService); + + const decrypted = await encString.decrypt(null); + + expect(decrypted).toBe("[error: cannot decrypt]"); + + expect(encString).toEqual({ + decryptedValue: "[error: cannot decrypt]", + encryptedString: null, + }); + }); + + it("passes along key", async () => { + const encString = new EncString(null); + const key = Substitute.for(); + + const cryptoService = Substitute.for(); + cryptoService.getOrgKey(null).resolves(null); + + (window as any).bitwardenContainerService = new ContainerService(cryptoService); + + await encString.decrypt(null, key); + + cryptoService.received().decryptToUtf8(encString, key); + }); + }); +}); diff --git a/common/spec/domain/field.spec.ts b/common/spec/domain/field.spec.ts new file mode 100644 index 0000000000..40ceb710a4 --- /dev/null +++ b/common/spec/domain/field.spec.ts @@ -0,0 +1,64 @@ +import { FieldType } from "jslib-common/enums/fieldType"; +import { FieldData } from "jslib-common/models/data/fieldData"; +import { Field } from "jslib-common/models/domain/field"; + +import { mockEnc } from "../utils"; + +describe("Field", () => { + let data: FieldData; + + beforeEach(() => { + data = { + type: FieldType.Text, + name: "encName", + value: "encValue", + linkedId: null, + }; + }); + + it("Convert from empty", () => { + const data = new FieldData(); + const field = new Field(data); + + expect(field).toEqual({ + type: undefined, + name: null, + value: null, + linkedId: undefined, + }); + }); + + it("Convert", () => { + const field = new Field(data); + + expect(field).toEqual({ + type: FieldType.Text, + name: { encryptedString: "encName", encryptionType: 0 }, + value: { encryptedString: "encValue", encryptionType: 0 }, + linkedId: null, + }); + }); + + it("toFieldData", () => { + const field = new Field(data); + expect(field.toFieldData()).toEqual(data); + }); + + it("Decrypt", async () => { + const field = new Field(); + field.type = FieldType.Text; + field.name = mockEnc("encName"); + field.value = mockEnc("encValue"); + + const view = await field.decrypt(null); + + expect(view).toEqual({ + type: 0, + name: "encName", + value: "encValue", + newField: false, + showCount: false, + showValue: false, + }); + }); +}); diff --git a/common/spec/domain/folder.spec.ts b/common/spec/domain/folder.spec.ts new file mode 100644 index 0000000000..64ee40b994 --- /dev/null +++ b/common/spec/domain/folder.spec.ts @@ -0,0 +1,42 @@ +import { FolderData } from "jslib-common/models/data/folderData"; +import { Folder } from "jslib-common/models/domain/folder"; + +import { mockEnc } from "../utils"; + +describe("Folder", () => { + let data: FolderData; + + beforeEach(() => { + data = { + id: "id", + userId: "userId", + name: "encName", + revisionDate: "2022-01-31T12:00:00.000Z", + }; + }); + + it("Convert", () => { + const field = new Folder(data); + + expect(field).toEqual({ + id: "id", + name: { encryptedString: "encName", encryptionType: 0 }, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + }); + }); + + it("Decrypt", async () => { + const folder = new Folder(); + folder.id = "id"; + folder.name = mockEnc("encName"); + folder.revisionDate = new Date("2022-01-31T12:00:00.000Z"); + + const view = await folder.decrypt(); + + expect(view).toEqual({ + id: "id", + name: "encName", + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + }); + }); +}); diff --git a/common/spec/domain/identity.spec.ts b/common/spec/domain/identity.spec.ts new file mode 100644 index 0000000000..ffaf504925 --- /dev/null +++ b/common/spec/domain/identity.spec.ts @@ -0,0 +1,134 @@ +import { IdentityData } from "jslib-common/models/data/identityData"; +import { Identity } from "jslib-common/models/domain/identity"; + +import { mockEnc } from "../utils"; + +describe("Identity", () => { + let data: IdentityData; + + beforeEach(() => { + data = { + title: "enctitle", + firstName: "encfirstName", + middleName: "encmiddleName", + lastName: "enclastName", + address1: "encaddress1", + address2: "encaddress2", + address3: "encaddress3", + city: "enccity", + state: "encstate", + postalCode: "encpostalCode", + country: "enccountry", + company: "enccompany", + email: "encemail", + phone: "encphone", + ssn: "encssn", + username: "encusername", + passportNumber: "encpassportNumber", + licenseNumber: "enclicenseNumber", + }; + }); + + it("Convert from empty", () => { + const data = new IdentityData(); + const identity = new Identity(data); + + expect(identity).toEqual({ + address1: null, + address2: null, + address3: null, + city: null, + company: null, + country: null, + email: null, + firstName: null, + lastName: null, + licenseNumber: null, + middleName: null, + passportNumber: null, + phone: null, + postalCode: null, + ssn: null, + state: null, + title: null, + username: null, + }); + }); + + it("Convert", () => { + const identity = new Identity(data); + + expect(identity).toEqual({ + title: { encryptedString: "enctitle", encryptionType: 0 }, + firstName: { encryptedString: "encfirstName", encryptionType: 0 }, + middleName: { encryptedString: "encmiddleName", encryptionType: 0 }, + lastName: { encryptedString: "enclastName", encryptionType: 0 }, + address1: { encryptedString: "encaddress1", encryptionType: 0 }, + address2: { encryptedString: "encaddress2", encryptionType: 0 }, + address3: { encryptedString: "encaddress3", encryptionType: 0 }, + city: { encryptedString: "enccity", encryptionType: 0 }, + state: { encryptedString: "encstate", encryptionType: 0 }, + postalCode: { encryptedString: "encpostalCode", encryptionType: 0 }, + country: { encryptedString: "enccountry", encryptionType: 0 }, + company: { encryptedString: "enccompany", encryptionType: 0 }, + email: { encryptedString: "encemail", encryptionType: 0 }, + phone: { encryptedString: "encphone", encryptionType: 0 }, + ssn: { encryptedString: "encssn", encryptionType: 0 }, + username: { encryptedString: "encusername", encryptionType: 0 }, + passportNumber: { encryptedString: "encpassportNumber", encryptionType: 0 }, + licenseNumber: { encryptedString: "enclicenseNumber", encryptionType: 0 }, + }); + }); + + it("toIdentityData", () => { + const identity = new Identity(data); + expect(identity.toIdentityData()).toEqual(data); + }); + + it("Decrypt", async () => { + const identity = new Identity(); + + identity.title = mockEnc("mockTitle"); + identity.firstName = mockEnc("mockFirstName"); + identity.middleName = mockEnc("mockMiddleName"); + identity.lastName = mockEnc("mockLastName"); + identity.address1 = mockEnc("mockAddress1"); + identity.address2 = mockEnc("mockAddress2"); + identity.address3 = mockEnc("mockAddress3"); + identity.city = mockEnc("mockCity"); + identity.state = mockEnc("mockState"); + identity.postalCode = mockEnc("mockPostalCode"); + identity.country = mockEnc("mockCountry"); + identity.company = mockEnc("mockCompany"); + identity.email = mockEnc("mockEmail"); + identity.phone = mockEnc("mockPhone"); + identity.ssn = mockEnc("mockSsn"); + identity.username = mockEnc("mockUsername"); + identity.passportNumber = mockEnc("mockPassportNumber"); + identity.licenseNumber = mockEnc("mockLicenseNumber"); + + const view = await identity.decrypt(null); + + expect(view).toEqual({ + _firstName: "mockFirstName", + _lastName: "mockLastName", + _subTitle: null, + address1: "mockAddress1", + address2: "mockAddress2", + address3: "mockAddress3", + city: "mockCity", + company: "mockCompany", + country: "mockCountry", + email: "mockEmail", + licenseNumber: "mockLicenseNumber", + middleName: "mockMiddleName", + passportNumber: "mockPassportNumber", + phone: "mockPhone", + postalCode: "mockPostalCode", + ssn: "mockSsn", + state: "mockState", + title: "mockTitle", + username: "mockUsername", + }); + }); +}); diff --git a/common/spec/domain/login.spec.ts b/common/spec/domain/login.spec.ts new file mode 100644 index 0000000000..88c31a5f7a --- /dev/null +++ b/common/spec/domain/login.spec.ts @@ -0,0 +1,101 @@ +import Substitute, { Arg } from "@fluffy-spoon/substitute"; + +import { UriMatchType } from "jslib-common/enums/uriMatchType"; +import { LoginData } from "jslib-common/models/data/loginData"; +import { Login } from "jslib-common/models/domain/login"; +import { LoginUri } from "jslib-common/models/domain/loginUri"; +import { LoginUriView } from "jslib-common/models/view/loginUriView"; + +import { mockEnc } from "../utils"; + +describe("Login DTO", () => { + it("Convert from empty LoginData", () => { + const data = new LoginData(); + const login = new Login(data); + + expect(login).toEqual({ + passwordRevisionDate: null, + autofillOnPageLoad: undefined, + username: null, + password: null, + totp: null, + }); + }); + + it("Convert from full LoginData", () => { + const data: LoginData = { + uris: [{ uri: "uri", match: UriMatchType.Domain }], + username: "username", + password: "password", + passwordRevisionDate: "2022-01-31T12:00:00.000Z", + totp: "123", + autofillOnPageLoad: false, + }; + const login = new Login(data); + + expect(login).toEqual({ + passwordRevisionDate: new Date("2022-01-31T12:00:00.000Z"), + autofillOnPageLoad: false, + username: { encryptedString: "username", encryptionType: 0 }, + password: { encryptedString: "password", encryptionType: 0 }, + totp: { encryptedString: "123", encryptionType: 0 }, + uris: [{ match: 0, uri: { encryptedString: "uri", encryptionType: 0 } }], + }); + }); + + it("Initialize without LoginData", () => { + const login = new Login(); + + expect(login).toEqual({}); + }); + + it("Decrypts correctly", async () => { + const loginUri = Substitute.for(); + const loginUriView = new LoginUriView(); + loginUriView.uri = "decrypted uri"; + loginUri.decrypt(Arg.any()).resolves(loginUriView); + + const login = new Login(); + login.uris = [loginUri]; + login.username = mockEnc("encrypted username"); + login.password = mockEnc("encrypted password"); + login.passwordRevisionDate = new Date("2022-01-31T12:00:00.000Z"); + login.totp = mockEnc("encrypted totp"); + login.autofillOnPageLoad = true; + + const loginView = await login.decrypt(null); + expect(loginView).toEqual({ + username: "encrypted username", + password: "encrypted password", + passwordRevisionDate: new Date("2022-01-31T12:00:00.000Z"), + totp: "encrypted totp", + uris: [ + { + match: null, + _uri: "decrypted uri", + _domain: null, + _hostname: null, + _host: null, + _canLaunch: null, + }, + ], + autofillOnPageLoad: true, + }); + }); + + it("Converts from LoginData and back", () => { + const data: LoginData = { + uris: [{ uri: "uri", match: UriMatchType.Domain }], + username: "username", + password: "password", + passwordRevisionDate: "2022-01-31T12:00:00.000Z", + totp: "123", + autofillOnPageLoad: false, + }; + const login = new Login(data); + + const loginData = login.toLoginData(); + + expect(loginData).toEqual(data); + }); +}); diff --git a/common/spec/domain/loginUri.spec.ts b/common/spec/domain/loginUri.spec.ts new file mode 100644 index 0000000000..2ea5ab59ad --- /dev/null +++ b/common/spec/domain/loginUri.spec.ts @@ -0,0 +1,57 @@ +import { UriMatchType } from "jslib-common/enums/uriMatchType"; +import { LoginUriData } from "jslib-common/models/data/loginUriData"; +import { LoginUri } from "jslib-common/models/domain/loginUri"; + +import { mockEnc } from "../utils"; + +describe("LoginUri", () => { + let data: LoginUriData; + + beforeEach(() => { + data = { + uri: "encUri", + match: UriMatchType.Domain, + }; + }); + + it("Convert from empty", () => { + const data = new LoginUriData(); + const loginUri = new LoginUri(data); + + expect(loginUri).toEqual({ + match: null, + uri: null, + }); + }); + + it("Convert", () => { + const loginUri = new LoginUri(data); + + expect(loginUri).toEqual({ + match: 0, + uri: { encryptedString: "encUri", encryptionType: 0 }, + }); + }); + + it("toLoginUriData", () => { + const loginUri = new LoginUri(data); + expect(loginUri.toLoginUriData()).toEqual(data); + }); + + it("Decrypt", async () => { + const loginUri = new LoginUri(); + loginUri.match = UriMatchType.Exact; + loginUri.uri = mockEnc("uri"); + + const view = await loginUri.decrypt(null); + + expect(view).toEqual({ + _canLaunch: null, + _domain: null, + _host: null, + _hostname: null, + _uri: "uri", + match: 3, + }); + }); +}); diff --git a/common/spec/domain/password.spec.ts b/common/spec/domain/password.spec.ts new file mode 100644 index 0000000000..4054c9510a --- /dev/null +++ b/common/spec/domain/password.spec.ts @@ -0,0 +1,51 @@ +import { PasswordHistoryData } from "jslib-common/models/data/passwordHistoryData"; +import { Password } from "jslib-common/models/domain/password"; + +import { mockEnc } from "../utils"; + +describe("Password", () => { + let data: PasswordHistoryData; + + beforeEach(() => { + data = { + password: "encPassword", + lastUsedDate: "2022-01-31T12:00:00.000Z", + }; + }); + + it("Convert from empty", () => { + const data = new PasswordHistoryData(); + const password = new Password(data); + + expect(password).toMatchObject({ + password: null, + }); + }); + + it("Convert", () => { + const password = new Password(data); + + expect(password).toEqual({ + password: { encryptedString: "encPassword", encryptionType: 0 }, + lastUsedDate: new Date("2022-01-31T12:00:00.000Z"), + }); + }); + + it("toPasswordHistoryData", () => { + const password = new Password(data); + expect(password.toPasswordHistoryData()).toEqual(data); + }); + + it("Decrypt", async () => { + const password = new Password(); + password.password = mockEnc("password"); + password.lastUsedDate = new Date("2022-01-31T12:00:00.000Z"); + + const view = await password.decrypt(null); + + expect(view).toEqual({ + password: "password", + lastUsedDate: new Date("2022-01-31T12:00:00.000Z"), + }); + }); +}); diff --git a/common/spec/domain/secureNote.spec.ts b/common/spec/domain/secureNote.spec.ts new file mode 100644 index 0000000000..50ca67ed26 --- /dev/null +++ b/common/spec/domain/secureNote.spec.ts @@ -0,0 +1,46 @@ +import { SecureNoteType } from "jslib-common/enums/secureNoteType"; +import { SecureNoteData } from "jslib-common/models/data/secureNoteData"; +import { SecureNote } from "jslib-common/models/domain/secureNote"; + +describe("SecureNote", () => { + let data: SecureNoteData; + + beforeEach(() => { + data = { + type: SecureNoteType.Generic, + }; + }); + + it("Convert from empty", () => { + const data = new SecureNoteData(); + const secureNote = new SecureNote(data); + + expect(secureNote).toEqual({ + type: undefined, + }); + }); + + it("Convert", () => { + const secureNote = new SecureNote(data); + + expect(secureNote).toEqual({ + type: 0, + }); + }); + + it("toSecureNoteData", () => { + const secureNote = new SecureNote(data); + expect(secureNote.toSecureNoteData()).toEqual(data); + }); + + it("Decrypt", async () => { + const secureNote = new SecureNote(); + secureNote.type = SecureNoteType.Generic; + + const view = await secureNote.decrypt(null); + + expect(view).toEqual({ + type: 0, + }); + }); +}); diff --git a/common/spec/domain/send.spec.ts b/common/spec/domain/send.spec.ts new file mode 100644 index 0000000000..d63c622ad9 --- /dev/null +++ b/common/spec/domain/send.spec.ts @@ -0,0 +1,144 @@ +import Substitute, { Arg, SubstituteOf } from "@fluffy-spoon/substitute"; + +import { CryptoService } from "jslib-common/abstractions/crypto.service"; +import { SendType } from "jslib-common/enums/sendType"; +import { SendData } from "jslib-common/models/data/sendData"; +import { EncString } from "jslib-common/models/domain/encString"; +import { Send } from "jslib-common/models/domain/send"; +import { SendText } from "jslib-common/models/domain/sendText"; +import { ContainerService } from "jslib-common/services/container.service"; + +import { makeStaticByteArray, mockEnc } from "../utils"; + +describe("Send", () => { + let data: SendData; + + beforeEach(() => { + data = { + id: "id", + accessId: "accessId", + userId: "userId", + type: SendType.Text, + name: "encName", + notes: "encNotes", + text: { + text: "encText", + hidden: true, + }, + file: null, + key: "encKey", + maxAccessCount: null, + accessCount: 10, + revisionDate: "2022-01-31T12:00:00.000Z", + expirationDate: "2022-01-31T12:00:00.000Z", + deletionDate: "2022-01-31T12:00:00.000Z", + password: "password", + disabled: false, + hideEmail: true, + }; + }); + + it("Convert from empty", () => { + const data = new SendData(); + const send = new Send(data); + + expect(send).toEqual({ + id: null, + accessId: null, + userId: null, + type: undefined, + name: null, + notes: null, + text: undefined, + file: undefined, + key: null, + maxAccessCount: undefined, + accessCount: undefined, + revisionDate: null, + expirationDate: null, + deletionDate: null, + password: undefined, + disabled: undefined, + hideEmail: undefined, + }); + }); + + it("Convert", () => { + const send = new Send(data); + + expect(send).toEqual({ + id: "id", + accessId: "accessId", + userId: "userId", + type: SendType.Text, + name: { encryptedString: "encName", encryptionType: 0 }, + notes: { encryptedString: "encNotes", encryptionType: 0 }, + text: { + text: { encryptedString: "encText", encryptionType: 0 }, + hidden: true, + }, + key: { encryptedString: "encKey", encryptionType: 0 }, + maxAccessCount: null, + accessCount: 10, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + expirationDate: new Date("2022-01-31T12:00:00.000Z"), + deletionDate: new Date("2022-01-31T12:00:00.000Z"), + password: "password", + disabled: false, + hideEmail: true, + }); + }); + + it("Decrypt", async () => { + const text = Substitute.for(); + text.decrypt(Arg.any()).resolves("textView" as any); + + const send = new Send(); + send.id = "id"; + send.accessId = "accessId"; + send.userId = "userId"; + send.type = SendType.Text; + send.name = mockEnc("name"); + send.notes = mockEnc("notes"); + send.text = text; + send.key = mockEnc("key"); + send.accessCount = 10; + send.revisionDate = new Date("2022-01-31T12:00:00.000Z"); + send.expirationDate = new Date("2022-01-31T12:00:00.000Z"); + send.deletionDate = new Date("2022-01-31T12:00:00.000Z"); + send.password = "password"; + send.disabled = false; + send.hideEmail = true; + + const cryptoService = Substitute.for(); + cryptoService.decryptToBytes(send.key, null).resolves(makeStaticByteArray(32)); + cryptoService.makeSendKey(Arg.any()).resolves("cryptoKey" as any); + + (window as any).bitwardenContainerService = new ContainerService(cryptoService); + + const view = await send.decrypt(); + + text.received(1).decrypt("cryptoKey" as any); + (send.name as SubstituteOf).received(1).decrypt(null, "cryptoKey" as any); + + expect(view).toMatchObject({ + id: "id", + accessId: "accessId", + name: "name", + notes: "notes", + type: 0, + key: expect.anything(), + cryptoKey: "cryptoKey", + file: expect.anything(), + text: "textView", + maxAccessCount: undefined, + accessCount: 10, + revisionDate: new Date("2022-01-31T12:00:00.000Z"), + expirationDate: new Date("2022-01-31T12:00:00.000Z"), + deletionDate: new Date("2022-01-31T12:00:00.000Z"), + password: "password", + disabled: false, + hideEmail: true, + }); + }); +}); diff --git a/common/spec/domain/sendAccess.spec.ts b/common/spec/domain/sendAccess.spec.ts new file mode 100644 index 0000000000..fc3001a5b9 --- /dev/null +++ b/common/spec/domain/sendAccess.spec.ts @@ -0,0 +1,84 @@ +import Substitute, { Arg } from "@fluffy-spoon/substitute"; + +import { SendType } from "jslib-common/enums/sendType"; +import { SendAccess } from "jslib-common/models/domain/sendAccess"; +import { SendText } from "jslib-common/models/domain/sendText"; +import { SendAccessResponse } from "jslib-common/models/response/sendAccessResponse"; + +import { mockEnc } from "../utils"; + +describe("SendAccess", () => { + let request: SendAccessResponse; + + beforeEach(() => { + request = { + id: "id", + type: SendType.Text, + name: "encName", + file: null, + text: { + text: "encText", + hidden: true, + }, + expirationDate: new Date("2022-01-31T12:00:00.000Z"), + creatorIdentifier: "creatorIdentifier", + } as SendAccessResponse; + }); + + it("Convert from empty", () => { + const request = new SendAccessResponse({}); + const sendAccess = new SendAccess(request); + + expect(sendAccess).toEqual({ + id: null, + type: undefined, + name: null, + creatorIdentifier: null, + expirationDate: null, + }); + }); + + it("Convert", () => { + const sendAccess = new SendAccess(request); + + expect(sendAccess).toEqual({ + id: "id", + type: 0, + name: { encryptedString: "encName", encryptionType: 0 }, + text: { + hidden: true, + text: { encryptedString: "encText", encryptionType: 0 }, + }, + expirationDate: new Date("2022-01-31T12:00:00.000Z"), + creatorIdentifier: "creatorIdentifier", + }); + }); + + it("Decrypt", async () => { + const sendAccess = new SendAccess(); + sendAccess.id = "id"; + sendAccess.type = SendType.Text; + sendAccess.name = mockEnc("name"); + + const text = Substitute.for(); + text.decrypt(Arg.any()).resolves({} as any); + sendAccess.text = text; + + sendAccess.expirationDate = new Date("2022-01-31T12:00:00.000Z"); + sendAccess.creatorIdentifier = "creatorIdentifier"; + + const view = await sendAccess.decrypt(null); + + text.received(1).decrypt(Arg.any()); + + expect(view).toEqual({ + id: "id", + type: 0, + name: "name", + text: {}, + file: expect.anything(), + expirationDate: new Date("2022-01-31T12:00:00.000Z"), + creatorIdentifier: "creatorIdentifier", + }); + }); +}); diff --git a/common/spec/domain/sendFile.spec.ts b/common/spec/domain/sendFile.spec.ts new file mode 100644 index 0000000000..9fde0b76df --- /dev/null +++ b/common/spec/domain/sendFile.spec.ts @@ -0,0 +1,57 @@ +import { SendFileData } from "jslib-common/models/data/sendFileData"; +import { SendFile } from "jslib-common/models/domain/sendFile"; + +import { mockEnc } from "../utils"; + +describe("SendFile", () => { + let data: SendFileData; + + beforeEach(() => { + data = { + id: "id", + size: "1100", + sizeName: "1.1 KB", + fileName: "encFileName", + }; + }); + + it("Convert from empty", () => { + const data = new SendFileData(); + const sendFile = new SendFile(data); + + expect(sendFile).toEqual({ + fileName: null, + id: null, + size: undefined, + sizeName: null, + }); + }); + + it("Convert", () => { + const sendFile = new SendFile(data); + + expect(sendFile).toEqual({ + id: "id", + size: "1100", + sizeName: "1.1 KB", + fileName: { encryptedString: "encFileName", encryptionType: 0 }, + }); + }); + + it("Decrypt", async () => { + const sendFile = new SendFile(); + sendFile.id = "id"; + sendFile.size = "1100"; + sendFile.sizeName = "1.1 KB"; + sendFile.fileName = mockEnc("fileName"); + + const view = await sendFile.decrypt(null); + + expect(view).toEqual({ + fileName: "fileName", + id: "id", + size: "1100", + sizeName: "1.1 KB", + }); + }); +}); diff --git a/common/spec/domain/sendText.spec.ts b/common/spec/domain/sendText.spec.ts new file mode 100644 index 0000000000..776a4c5705 --- /dev/null +++ b/common/spec/domain/sendText.spec.ts @@ -0,0 +1,47 @@ +import { SendTextData } from "jslib-common/models/data/sendTextData"; +import { SendText } from "jslib-common/models/domain/sendText"; + +import { mockEnc } from "../utils"; + +describe("SendText", () => { + let data: SendTextData; + + beforeEach(() => { + data = { + text: "encText", + hidden: false, + }; + }); + + it("Convert from empty", () => { + const data = new SendTextData(); + const secureNote = new SendText(data); + + expect(secureNote).toEqual({ + hidden: undefined, + text: null, + }); + }); + + it("Convert", () => { + const secureNote = new SendText(data); + + expect(secureNote).toEqual({ + hidden: false, + text: { encryptedString: "encText", encryptionType: 0 }, + }); + }); + + it("Decrypt", async () => { + const secureNote = new SendText(); + secureNote.text = mockEnc("text"); + secureNote.hidden = true; + + const view = await secureNote.decrypt(null); + + expect(view).toEqual({ + text: "text", + hidden: true, + }); + }); +}); diff --git a/common/spec/domain/symmetricCryptoKey.spec.ts b/common/spec/domain/symmetricCryptoKey.spec.ts new file mode 100644 index 0000000000..62f096c1ec --- /dev/null +++ b/common/spec/domain/symmetricCryptoKey.spec.ts @@ -0,0 +1,69 @@ +import { EncryptionType } from "jslib-common/enums/encryptionType"; +import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey"; + +import { makeStaticByteArray } from "../utils"; + +describe("SymmetricCryptoKey", () => { + it("errors if no key", () => { + const t = () => { + new SymmetricCryptoKey(null); + }; + + expect(t).toThrowError("Must provide key"); + }); + + describe("guesses encKey from key length", () => { + it("AesCbc256_B64", () => { + const key = makeStaticByteArray(32); + const cryptoKey = new SymmetricCryptoKey(key); + + expect(cryptoKey).toEqual({ + encKey: key, + encKeyB64: "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=", + encType: 0, + key: key, + keyB64: "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=", + macKey: null, + }); + }); + + it("AesCbc128_HmacSha256_B64", () => { + const key = makeStaticByteArray(32); + const cryptoKey = new SymmetricCryptoKey(key, EncryptionType.AesCbc128_HmacSha256_B64); + + expect(cryptoKey).toEqual({ + encKey: key.slice(0, 16), + encKeyB64: "AAECAwQFBgcICQoLDA0ODw==", + encType: 1, + key: key, + keyB64: "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=", + macKey: key.slice(16, 32), + macKeyB64: "EBESExQVFhcYGRobHB0eHw==", + }); + }); + + it("AesCbc256_HmacSha256_B64", () => { + const key = makeStaticByteArray(64); + const cryptoKey = new SymmetricCryptoKey(key); + + expect(cryptoKey).toEqual({ + encKey: key.slice(0, 32), + encKeyB64: "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=", + encType: 2, + key: key, + keyB64: + "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==", + macKey: key.slice(32, 64), + macKeyB64: "ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj8=", + }); + }); + + it("unknown length", () => { + const t = () => { + new SymmetricCryptoKey(makeStaticByteArray(30)); + }; + + expect(t).toThrowError("Unable to determine encType."); + }); + }); +}); diff --git a/common/spec/utils.ts b/common/spec/utils.ts index bd47af7aa1..58963d0f83 100644 --- a/common/spec/utils.ts +++ b/common/spec/utils.ts @@ -1,3 +1,7 @@ +import Substitute, { Arg } from "@fluffy-spoon/substitute"; + +import { EncString } from "jslib-common/models/domain/encString"; + function newGuid() { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0; @@ -16,3 +20,18 @@ export function BuildTestObject( ): T { return Object.assign(constructor === null ? {} : new constructor(), def) as T; } + +export function mockEnc(s: string): EncString { + const mock = Substitute.for(); + mock.decrypt(Arg.any(), Arg.any()).resolves(s); + + return mock; +} + +export function makeStaticByteArray(length: number) { + const arr = new Uint8Array(length); + for (let i = 0; i < length; i++) { + arr[i] = i; + } + return arr; +} diff --git a/common/src/models/api/sendFileApi.ts b/common/src/models/api/sendFileApi.ts index f4d0926ced..99fa0ff5d1 100644 --- a/common/src/models/api/sendFileApi.ts +++ b/common/src/models/api/sendFileApi.ts @@ -3,7 +3,6 @@ import { BaseResponse } from "../response/baseResponse"; export class SendFileApi extends BaseResponse { id: string; fileName: string; - key: string; size: string; sizeName: string; @@ -14,7 +13,6 @@ export class SendFileApi extends BaseResponse { } this.id = this.getResponseProperty("Id"); this.fileName = this.getResponseProperty("FileName"); - this.key = this.getResponseProperty("Key"); this.size = this.getResponseProperty("Size"); this.sizeName = this.getResponseProperty("SizeName"); } diff --git a/common/src/models/data/cipherData.ts b/common/src/models/data/cipherData.ts index e541fee307..7ebc7070a9 100644 --- a/common/src/models/data/cipherData.ts +++ b/common/src/models/data/cipherData.ts @@ -21,7 +21,6 @@ export class CipherData { favorite: boolean; revisionDate: string; type: CipherType; - sizeName: string; name: string; notes: string; login?: LoginData; diff --git a/common/src/models/data/sendFileData.ts b/common/src/models/data/sendFileData.ts index d7bf8ea40e..aaf868c19d 100644 --- a/common/src/models/data/sendFileData.ts +++ b/common/src/models/data/sendFileData.ts @@ -3,7 +3,6 @@ import { SendFileApi } from "../api/sendFileApi"; export class SendFileData { id: string; fileName: string; - key: string; size: string; sizeName: string; @@ -14,7 +13,6 @@ export class SendFileData { this.id = data.id; this.fileName = data.fileName; - this.key = data.key; this.size = data.size; this.sizeName = data.sizeName; } diff --git a/common/src/models/domain/attachment.ts b/common/src/models/domain/attachment.ts index 712382ea98..cbfffdcf68 100644 --- a/common/src/models/domain/attachment.ts +++ b/common/src/models/domain/attachment.ts @@ -11,7 +11,7 @@ export class Attachment extends Domain { id: string; url: string; size: string; - sizeName: string; + sizeName: string; // Readable size, ex: "4.2 KB" or "1.43 GB" key: EncString; fileName: EncString; diff --git a/common/src/models/domain/loginUri.ts b/common/src/models/domain/loginUri.ts index 685ce4a938..9bd78c655f 100644 --- a/common/src/models/domain/loginUri.ts +++ b/common/src/models/domain/loginUri.ts @@ -45,6 +45,7 @@ export class LoginUri extends Domain { u, { uri: null, + match: null, }, ["match"] );