Skip to content

Commit

Permalink
PM-16984: Improving type safety of decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
mzieniukbw committed Jan 15, 2025
1 parent 3f4809c commit a175bce
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ export class Collection extends Domain {
}

decrypt(orgKey: OrgKey): Promise<CollectionView> {
return this.decryptObj(
new CollectionView(this),
{
name: null,
},
this.organizationId,
orgKey,
);
return this.decryptObj(new CollectionView(this), ["name"], this.organizationId, orgKey);
}
}
27 changes: 10 additions & 17 deletions libs/common/src/platform/models/domain/domain-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default class Domain {
}
}
}

protected buildDataModel<D extends Domain>(
domain: D,
dataObj: any,
Expand All @@ -60,28 +61,20 @@ export default class Domain {

protected async decryptObj<T extends View>(
viewModel: T,
map: any,
orgId: string,
props: (keyof T & keyof this)[] & (string | EncString)[],
orgId: string | null,
key: SymmetricCryptoKey = null,
objectContext: string = "No Domain Context",
): Promise<T> {
const self: any = this;

for (const prop in map) {
// eslint-disable-next-line
if (!map.hasOwnProperty(prop)) {
continue;
}

const mapProp = map[prop] || prop;
if (self[mapProp]) {
(viewModel as any)[prop] = await self[mapProp].decrypt(
for (const prop of props) {
(viewModel[prop] as string) =
(await (this[prop] as EncString)?.decrypt(
orgId,
key,
`Property: ${prop}; ObjectContext: ${objectContext}`,
);
}
`Property: ${prop as string}; ObjectContext: ${objectContext}`,
)) ?? null;
}

return viewModel;
}

Expand Down Expand Up @@ -111,7 +104,7 @@ export default class Domain {
const decryptedObjects = [];

for (const prop of encryptedProperties) {
const value = (this as any)[prop] as EncString;
const value = this[prop] as EncString;
const decrypted = await this.decryptProperty(
prop,
value,
Expand Down
9 changes: 1 addition & 8 deletions libs/common/src/tools/send/models/domain/send-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,7 @@ export class SendAccess extends Domain {
async decrypt(key: SymmetricCryptoKey): Promise<SendAccessView> {
const model = new SendAccessView(this);

await this.decryptObj(
model,
{
name: null,
},
null,
key,
);
await this.decryptObj(model, ["name"], null, key);

switch (this.type) {
case SendType.File:
Expand Down
9 changes: 1 addition & 8 deletions libs/common/src/tools/send/models/domain/send-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ export class SendFile extends Domain {
}

async decrypt(key: SymmetricCryptoKey): Promise<SendFileView> {
const view = await this.decryptObj(
new SendFileView(this),
{
fileName: null,
},
null,
key,
);
const view = await this.decryptObj(new SendFileView(this), ["fileName"], null, key);
return view;
}

Expand Down
9 changes: 1 addition & 8 deletions libs/common/src/tools/send/models/domain/send-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ export class SendText extends Domain {
}

decrypt(key: SymmetricCryptoKey): Promise<SendTextView> {
return this.decryptObj(
new SendTextView(this),
{
text: null,
},
null,
key,
);
return this.decryptObj(new SendTextView(this), ["text"], null, key);
}

static fromJSON(obj: Jsonify<SendText>) {
Expand Down
10 changes: 1 addition & 9 deletions libs/common/src/tools/send/models/domain/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,7 @@ export class Send extends Domain {
// TODO: error?
}

await this.decryptObj(
model,
{
name: null,
notes: null,
},
null,
model.cryptoKey,
);
await this.decryptObj(model, ["name", "notes"], null, model.cryptoKey);

switch (this.type) {
case SendType.File:
Expand Down
4 changes: 1 addition & 3 deletions libs/common/src/vault/models/domain/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export class Attachment extends Domain {
): Promise<AttachmentView> {
const view = await this.decryptObj(
new AttachmentView(this),
{
fileName: null,
},
["fileName"],
orgId,
encKey,
"DomainType: Attachment; " + context,
Expand Down
9 changes: 1 addition & 8 deletions libs/common/src/vault/models/domain/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,7 @@ export class Card extends Domain {
): Promise<CardView> {
return this.decryptObj(
new CardView(),
{
cardholderName: null,
brand: null,
number: null,
expMonth: null,
expYear: null,
code: null,
},
["cardholderName", "brand", "number", "expMonth", "expYear", "code"],
orgId,
encKey,
"DomainType: Card; " + context,
Expand Down
10 changes: 1 addition & 9 deletions libs/common/src/vault/models/domain/cipher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,7 @@ export class Cipher extends Domain implements Decryptable<CipherView> {
bypassValidation = false;
}

await this.decryptObj(
model,
{
name: null,
notes: null,
},
this.organizationId,
encKey,
);
await this.decryptObj(model, ["name", "notes"], this.organizationId, encKey);

switch (this.type) {
case CipherType.Login:
Expand Down
39 changes: 15 additions & 24 deletions libs/common/src/vault/models/domain/fido2-credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,30 @@ export class Fido2Credential extends Domain {
async decrypt(orgId: string, encKey?: SymmetricCryptoKey): Promise<Fido2CredentialView> {
const view = await this.decryptObj(
new Fido2CredentialView(),
{
credentialId: null,
keyType: null,
keyAlgorithm: null,
keyCurve: null,
keyValue: null,
rpId: null,
userHandle: null,
userName: null,
rpName: null,
userDisplayName: null,
discoverable: null,
},
[
"credentialId",
"keyType",
"keyAlgorithm",
"keyCurve",
"keyValue",
"rpId",
"userHandle",
"userName",
"rpName",
"userDisplayName",
"discoverable",
],
orgId,
encKey,
);

const { counter } = await this.decryptObj(
{ counter: "" },
{
counter: null,
},
orgId,
encKey,
);
const { counter } = await this.decryptObj({ counter: "" }, ["counter"], orgId, encKey);
// Counter will end up as NaN if this fails
view.counter = parseInt(counter);

const { discoverable } = await this.decryptObj(
{ discoverable: "" },
{
discoverable: null,
},
["discoverable"],
orgId,
encKey,
);
Expand Down
10 changes: 1 addition & 9 deletions libs/common/src/vault/models/domain/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ export class Field extends Domain {
}

decrypt(orgId: string, encKey?: SymmetricCryptoKey): Promise<FieldView> {
return this.decryptObj(
new FieldView(this),
{
name: null,
value: null,
},
orgId,
encKey,
);
return this.decryptObj(new FieldView(this), ["name", "value"], orgId, encKey);
}

toFieldData(): FieldData {
Expand Down
8 changes: 1 addition & 7 deletions libs/common/src/vault/models/domain/folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ export class Folder extends Domain {
}

decrypt(): Promise<FolderView> {
return this.decryptObj(
new FolderView(this),
{
name: null,
},
null,
);
return this.decryptObj(new FolderView(this), ["name"], null);
}

async decryptWithKey(
Expand Down
40 changes: 20 additions & 20 deletions libs/common/src/vault/models/domain/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,26 @@ export class Identity extends Domain {
): Promise<IdentityView> {
return this.decryptObj(
new IdentityView(),
{
title: null,
firstName: null,
middleName: null,
lastName: null,
address1: null,
address2: null,
address3: null,
city: null,
state: null,
postalCode: null,
country: null,
company: null,
email: null,
phone: null,
ssn: null,
username: null,
passportNumber: null,
licenseNumber: null,
},
[
"title",
"firstName",
"middleName",
"lastName",
"address1",
"address2",
"address3",
"city",
"state",
"postalCode",
"country",
"company",
"email",
"phone",
"ssn",
"username",
"passportNumber",
"licenseNumber",
],
orgId,
encKey,
"DomainType: Identity; " + context,
Expand Down
10 changes: 1 addition & 9 deletions libs/common/src/vault/models/domain/login-uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ export class LoginUri extends Domain {
context: string = "No Cipher Context",
encKey?: SymmetricCryptoKey,
): Promise<LoginUriView> {
return this.decryptObj(
new LoginUriView(this),
{
uri: null,
},
orgId,
encKey,
context,
);
return this.decryptObj(new LoginUriView(this), ["uri"], orgId, encKey, context);
}

async validateChecksum(clearTextUri: string, orgId: string, encKey: SymmetricCryptoKey) {
Expand Down
6 changes: 1 addition & 5 deletions libs/common/src/vault/models/domain/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ export class Login extends Domain {
): Promise<LoginView> {
const view = await this.decryptObj(
new LoginView(this),
{
username: null,
password: null,
totp: null,
},
["username", "password", "totp"],
orgId,
encKey,
`DomainType: Login; ${context}`,
Expand Down
4 changes: 1 addition & 3 deletions libs/common/src/vault/models/domain/password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export class Password extends Domain {
decrypt(orgId: string, encKey?: SymmetricCryptoKey): Promise<PasswordHistoryView> {
return this.decryptObj(
new PasswordHistoryView(this),
{
password: null,
},
["password"],
orgId,
encKey,
"DomainType: PasswordHistory",
Expand Down
6 changes: 1 addition & 5 deletions libs/common/src/vault/models/domain/ssh-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ export class SshKey extends Domain {
): Promise<SshKeyView> {
return this.decryptObj(
new SshKeyView(),
{
privateKey: null,
publicKey: null,
keyFingerprint: null,
},
["privateKey", "publicKey", "keyFingerprint"],
orgId,
encKey,
"DomainType: SshKey; " + context,
Expand Down

0 comments on commit a175bce

Please sign in to comment.