Skip to content

Commit

Permalink
fix: properly decrypt legacy state blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Jun 10, 2024
1 parent 1bf7e4f commit 4e3d119
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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 @@ import {
getSnapControllerOptions,
getSnapControllerWithEES,
getSnapControllerWithEESOptions,
LEGACY_ENCRYPTION_KEY_DERIVATION_OPTIONS,
loopbackDetect,
LoopbackLocation,
MOCK_BLOCK_NUMBER,
Expand Down Expand Up @@ -8178,6 +8179,42 @@ describe('SnapController', () => {
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

0 comments on commit 4e3d119

Please sign in to comment.