Skip to content

fix: properly decrypt legacy state blobs #2472

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

Merged
merged 4 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"branches": 91.64,
"branches": 91.68,
"functions": 96.77,
"lines": 97.89,
"statements": 97.57
Expand Down
37 changes: 37 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
getSnapControllerOptions,
getSnapControllerWithEES,
getSnapControllerWithEESOptions,
LEGACY_ENCRYPTION_KEY_DERIVATION_OPTIONS,
loopbackDetect,
LoopbackLocation,
MOCK_BLOCK_NUMBER,
Expand Down Expand Up @@ -1728,7 +1729,7 @@
});

// This isn't stable in CI unfortunately
it.skip('throws if the Snap is terminated while executing', async () => {

Check warning on line 1732 in packages/snaps-controllers/src/snaps/SnapController.test.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (@metamask/snaps-controllers)

Disabled test
const { manifest, sourceCode, svgIcon } =
await getMockSnapFilesWithUpdatedChecksum({
sourceCode: `
Expand Down Expand Up @@ -8178,6 +8179,42 @@
snapController.destroy();
});

it('uses legacy decryption where needed', async () => {
const messenger = getSnapControllerMessenger();

const state = { foo: 'bar' };

const { data, iv, salt } = JSON.parse(
await encrypt(
ENCRYPTION_KEY,
state,
undefined,
undefined,
LEGACY_ENCRYPTION_KEY_DERIVATION_OPTIONS,
),
);

const snapController = getSnapController(
getSnapControllerOptions({
messenger,
state: {
snaps: {
[MOCK_SNAP_ID]: getPersistedSnapObject(),
},
snapStates: {
[MOCK_SNAP_ID]: JSON.stringify({ data, iv, salt }),
},
},
}),
);

expect(
await messenger.call('SnapController:getSnapState', MOCK_SNAP_ID, true),
).toStrictEqual(state);

snapController.destroy();
});

it('throws an error if the state is corrupt', async () => {
const messenger = getSnapControllerMessenger();

Expand Down
9 changes: 8 additions & 1 deletion packages/snaps-controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,14 @@ export class SnapController extends BaseController<
snapId,
salt,
useCache,
keyMetadata,
// When decrypting state we expect key metadata to be present.
// If it isn't present, we assume that the Snap state we are decrypting is old enough to use the legacy encryption params.
keyMetadata: keyMetadata ?? {
algorithm: 'PBKDF2',
params: {
iterations: 10_000,
},
},
});
const decryptedState = await this.#encryptor.decryptWithKey(key, parsed);

Expand Down
7 changes: 7 additions & 0 deletions packages/snaps-controllers/src/test-utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,13 @@ export const DEFAULT_ENCRYPTION_KEY_DERIVATION_OPTIONS = {
},
};

export const LEGACY_ENCRYPTION_KEY_DERIVATION_OPTIONS = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this to the Snap controller file (or a constants file) and use it as the fallback, to remove some duplication?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

algorithm: 'PBKDF2' as const,
params: {
iterations: 10_000,
},
};

const getSnapControllerEncryptor = () => {
return {
encryptWithKey,
Expand Down
Loading