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 1 commit
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
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
Loading