-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PS-11868] Require key for enc string decryption #10981
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
95525f8
Specify enc string decryption key and service.
MGibson1 6ae9e26
Fix issue with identifying `this` type within extended classes
MGibson1 58a143c
Folder decryption example
MGibson1 e279d58
Test enc string changes
MGibson1 62767d6
Fix test name
MGibson1 0aa67b9
test decrypt with key
MGibson1 13ab55f
Merge branch 'main' into ps-11868/require-key-for-enc-strings
MGibson1 3cb5e3e
Merge branch 'main' into ps-11868/require-key-for-enc-strings
MGibson1 d47b2ae
Merge branch 'main' into ps-11868/require-key-for-enc-strings
MGibson1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
libs/common/src/platform/models/domain/domain-base.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { mock, MockProxy } from "jest-mock-extended"; | ||
|
||
import { makeEncString, makeSymmetricCryptoKey } from "../../../../spec"; | ||
import { EncryptService } from "../../abstractions/encrypt.service"; | ||
import { Utils } from "../../misc/utils"; | ||
|
||
import Domain from "./domain-base"; | ||
import { EncString } from "./enc-string"; | ||
|
||
class TestDomain extends Domain { | ||
plainText: string; | ||
encToString: EncString; | ||
encString2: EncString; | ||
} | ||
|
||
describe("DomainBase", () => { | ||
let encryptService: MockProxy<EncryptService>; | ||
const key = makeSymmetricCryptoKey(64); | ||
|
||
beforeEach(() => { | ||
encryptService = mock<EncryptService>(); | ||
}); | ||
|
||
function setUpCryptography() { | ||
encryptService.encrypt.mockImplementation((value) => { | ||
let data: string; | ||
if (typeof value === "string") { | ||
data = value; | ||
} else { | ||
data = Utils.fromBufferToUtf8(value); | ||
} | ||
|
||
return Promise.resolve(makeEncString(data)); | ||
}); | ||
|
||
encryptService.decryptToUtf8.mockImplementation((value) => { | ||
return Promise.resolve(value.data); | ||
}); | ||
|
||
encryptService.decryptToBytes.mockImplementation((value) => { | ||
return Promise.resolve(value.dataBytes); | ||
}); | ||
} | ||
|
||
describe("decryptWithKey", () => { | ||
it("validates the view matches the domain", async () => { | ||
const domain = new TestDomain(); | ||
|
||
await domain["decryptObjWithKey"]( | ||
// @ts-expect-error -- clear is not of type EncString | ||
["plainText"], | ||
makeSymmetricCryptoKey(64), | ||
mock<EncryptService>(), | ||
); | ||
|
||
await domain["decryptObjWithKey"]( | ||
// @ts-expect-error -- Clear is not of type EncString | ||
["encToString", "encString2", "plainText"], | ||
makeSymmetricCryptoKey(64), | ||
mock<EncryptService>(), | ||
); | ||
|
||
const decrypted = await domain["decryptObjWithKey"]( | ||
["encToString"], | ||
makeSymmetricCryptoKey(64), | ||
mock<EncryptService>(), | ||
); | ||
|
||
// @ts-expect-error -- encString2 was not decrypted | ||
decrypted as { encToString: string; encString2: string; plainText: string }; | ||
|
||
// encString2 was not decrypted, so it's still an EncString | ||
decrypted as { encToString: string; encString2: EncString; plainText: string }; | ||
}); | ||
|
||
it("decrypts the encrypted properties", async () => { | ||
setUpCryptography(); | ||
|
||
const domain = new TestDomain(); | ||
|
||
domain.encToString = await encryptService.encrypt("string", key); | ||
|
||
const decrypted = await domain["decryptObjWithKey"](["encToString"], key, encryptService); | ||
|
||
expect(decrypted).toEqual({ | ||
encToString: "string", | ||
}); | ||
}); | ||
|
||
it("decrypts multiple encrypted properties", async () => { | ||
setUpCryptography(); | ||
|
||
const domain = new TestDomain(); | ||
|
||
domain.encToString = await encryptService.encrypt("string", key); | ||
domain.encString2 = await encryptService.encrypt("string2", key); | ||
|
||
const decrypted = await domain["decryptObjWithKey"]( | ||
["encToString", "encString2"], | ||
key, | ||
encryptService, | ||
); | ||
|
||
expect(decrypted).toEqual({ | ||
encToString: "string", | ||
encString2: "string2", | ||
}); | ||
}); | ||
|
||
it("does not decrypt properties that are not encrypted", async () => { | ||
const domain = new TestDomain(); | ||
domain.plainText = "clear"; | ||
|
||
const decrypted = await domain["decryptObjWithKey"]([], key, encryptService); | ||
|
||
expect(decrypted).toEqual({ | ||
plainText: "clear", | ||
}); | ||
}); | ||
|
||
it("does not decrypt properties that were not requested to be decrypted", async () => { | ||
setUpCryptography(); | ||
|
||
const domain = new TestDomain(); | ||
|
||
domain.plainText = "clear"; | ||
domain.encToString = makeEncString("string"); | ||
domain.encString2 = makeEncString("string2"); | ||
|
||
const decrypted = await domain["decryptObjWithKey"]([], key, encryptService); | ||
|
||
expect(decrypted).toEqual({ | ||
plainText: "clear", | ||
encToString: makeEncString("string"), | ||
encString2: makeEncString("string2"), | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These folder updates are example usages. We can remove from this PR to limit scope. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type inferrer has a lot of trouble with
this
type on derived classes. It seems to always resolve to the parent class when using in the derived class. This pattern allows us to specify the specificDomain
implementation by providing a constructor as an optional 5th argument. See theFolder
changes for an example of its value.