Skip to content
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

Add mutex to getSnapState to prevent concurrent decryption #3234

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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,6 +1,6 @@
{
"branches": 93.31,
"functions": 97.05,
"functions": 97.06,
"lines": 98.25,
"statements": 97.98
}
49 changes: 29 additions & 20 deletions packages/snaps-controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ export type SnapRuntimeData = {
* A mutex to prevent concurrent state updates.
*/
stateMutex: Mutex;

/**
* A mutex to prevent concurrent state decryption.
*/
getStateMutex: Mutex;
};

export type SnapError = {
Expand Down Expand Up @@ -2046,33 +2051,36 @@ export class SnapController extends BaseController<
*/
async getSnapState(snapId: SnapId, encrypted: boolean): Promise<Json> {
const runtime = this.#getRuntimeExpect(snapId);
const cachedState = encrypted ? runtime.state : runtime.unencryptedState;
return await runtime.getStateMutex.runExclusive(async () => {
const cachedState = encrypted ? runtime.state : runtime.unencryptedState;

if (cachedState !== undefined) {
return cachedState;
}
if (cachedState !== undefined) {
return cachedState;
}

const state = encrypted
? this.state.snapStates[snapId]
: this.state.unencryptedSnapStates[snapId];
const state = encrypted
? this.state.snapStates[snapId]
: this.state.unencryptedSnapStates[snapId];

if (state === null || state === undefined) {
return null;
}
if (state === null || state === undefined) {
return null;
}

if (!encrypted) {
// For performance reasons, we do not validate that the state is JSON,
// since we control serialization.
const json = JSON.parse(state);
runtime.unencryptedState = json;
if (!encrypted) {
// For performance reasons, we do not validate that the state is JSON,
// since we control serialization.
const json = JSON.parse(state);
runtime.unencryptedState = json;

return json;
}
return json;
}

const decrypted = await this.#decryptSnapState(snapId, state);
runtime.state = decrypted;
const decrypted = await this.#decryptSnapState(snapId, state);
// eslint-disable-next-line require-atomic-updates
runtime.state = decrypted;

return decrypted;
return decrypted;
});
}

/**
Expand Down Expand Up @@ -3968,6 +3976,7 @@ export class SnapController extends BaseController<
interpreter,
stopping: false,
stateMutex: new Mutex(),
getStateMutex: new Mutex(),
});
}

Expand Down
Loading