-
Couldn't load subscription status.
- Fork 85
feat(core): improve secret encryption #1063
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,7 +183,7 @@ export async function deserialize( | |
| "See: https://alchemy.run/concepts/secret/#encryption-password", | ||
| ); | ||
| } | ||
| if (typeof value["@secret"] === "object") { | ||
| if (typeof value["@secret"] === "object" && "data" in value["@secret"]) { | ||
|
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. What happens if data is not present? Do we just skip it? 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. Quick answer: no, we don't skip it. The - if (typeof value["@secret"] === "object") {
+ if (typeof value["@secret"] === "object" && "data" in value["@secret"]) {
return new Secret(
JSON.parse(
await decryptWithKey(value["@secret"].data, scope.password),
),
);
}
return new Secret(await decryptWithKey(value["@secret"], scope.password));Why the change? The encrypted value is now an object with properties, so The serialization logic is completely unchanged. 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. I was looking for the code that is handling backwards compatibility (decrypting old secrets). Can you point that out to me. 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. That's handled by the |
||
| return new Secret( | ||
| JSON.parse( | ||
| await decryptWithKey(value["@secret"].data, scope.password), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { decryptWithKey, encrypt, libsodiumEncrypt } from "../src/encrypt.ts"; | ||
|
|
||
| describe("encrypt", () => { | ||
| it("encrypts and decrypts a string", async () => { | ||
| const passphrase = crypto.randomUUID(); | ||
| const value = "test-value"; | ||
| const encrypted = await encrypt(value, passphrase); | ||
| expect(encrypted).toMatchObject({ | ||
| version: "v1", | ||
| ciphertext: expect.any(String), | ||
| iv: expect.any(String), | ||
| salt: expect.any(String), | ||
| tag: expect.any(String), | ||
| }); | ||
| const decrypted = await decryptWithKey(encrypted, passphrase); | ||
| expect(decrypted).toBe(value); | ||
| }); | ||
|
|
||
| it("decrypts a string encrypted with libsodium", async () => { | ||
| const passphrase = crypto.randomUUID(); | ||
| const value = "test-value"; | ||
| const encrypted = await libsodiumEncrypt(value, passphrase); | ||
| const decrypted = await decryptWithKey(encrypted, passphrase); | ||
| expect(decrypted).toBe(value); | ||
| }); | ||
|
|
||
| it("fails to decrypt from libsodium with incorrect passphrase", async () => { | ||
| const passphrase = crypto.randomUUID(); | ||
| const value = "test-value"; | ||
| const encrypted = await libsodiumEncrypt(value, passphrase); | ||
| await expect( | ||
| decryptWithKey(encrypted, crypto.randomUUID()), | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it("fails to decrypt from scrypt with incorrect passphrase", async () => { | ||
| const passphrase = crypto.randomUUID(); | ||
| const value = "test-value"; | ||
| const encrypted = await encrypt(value, passphrase); | ||
| expect(encrypted).toMatchObject({ | ||
| version: "v1", | ||
| ciphertext: expect.any(String), | ||
| iv: expect.any(String), | ||
| salt: expect.any(String), | ||
| tag: expect.any(String), | ||
| }); | ||
| await expect( | ||
| decryptWithKey(encrypted, crypto.randomUUID()), | ||
| ).rejects.toThrow(); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.