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

chore: dont poll requests if the client is locked #43

Merged
merged 1 commit into from
Feb 28, 2025

Conversation

shane-t
Copy link
Member

@shane-t shane-t commented Feb 28, 2025

4.11 Cronjob - Check if MetaMask Is Locked

Description
The cronjob handler calls RequestManager.poll() every 10 seconds. The poll() function access data from encrypted state via stateManager. According to the Snaps API Documentation, encrypted storage cannot be accessed while MetaMask is locked.

Note: The cronjob is running every minute. Due to the clients requirements their Snap polls for updates every 10 seconds. Therefore they install 6 promises every minute (10 seconds apart). This might lead to deferred promises triggering while MetaMask is locked.

export const onCronjob: OnCronjobHandler = async ({ request }) => {
  switch (
    request.method // @audit-info this is execute every minute * * * * *  acc. to manifest
  ) {
    case 'execute': {
      const startTime = Date.now();
      const timeoutDuration = 60000; // 1 minute in milliseconds //@audit will this work with mm snap scheduling?

      // Run pollTransactions 6 times with 10-second intervals
      for (let i = 0; i < 6; i++) {
        // Check if we've exceeded the timeout
        if (Date.now() - startTime >= timeoutDuration) {
          return;
        }

        await pollRequests(); // @audit - what if there is nothing to poll?
        // If this isn't the last iteration, wait 10 seconds
        if (i < 5) {
          await new Promise((resolve) => setTimeout(resolve, 10000)); // @audit-info sleep(10sec)
        }
      }
      return;
    }
async poll(): Promise<void> {
  const pendingRequests = (await this.listRequests()).filter(
    (request) => !request.fulfilled,
  );
async listRequests(): Promise<
  CustodialSnapRequest<SignedMessageRequest | TransactionRequest>[]
> {
  return this.#stateManager.listRequests();
}

Recommendation
Inside pollRequests return early, if snap_getClientStatus returns that MetaMask is locked.

@shane-t shane-t requested a review from PatrykLucka February 28, 2025 13:43
@shane-t shane-t merged commit 9b5e7c1 into main Feb 28, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants