MetaMask / MetaMask/snaps

Mock endowments for `fetch` and other common functions

Open
#1,998 0 comments 0 reactions 0 assignees View on GitHub
area-capabilities snaps-unrefined-backlog type-enhancement
Dominant language
TypeScript
Stars
853
Forks
662
Avg merge
2d 8h
Merged PRs (30d)
8

Description

Currently when you try to use `fetch` without the `endowment:network-access` permission, you receive the following error:

> fetch is not a function

This may be confusing to Snap developers, since `fetch` is a global function in normal browser contexts. Instead, I suggest we inject a mock endowment for `fetch` and other commonly used global functions which are only usable with a permission. For example, if the Snap does not have the `endowment:network-access` permission, it could inject the following function instead:

```ts
function fetch(url: string) {
throw new Error(`Failed to fetch "${url}": Using this function requires the "endowment:network-access" permission. Refer to the documentation for more information: https://docs.metamask.io/...`);
}
```

This gives the developer more information about how to resolve this problem.

Contributor guide

Open the contributing guide

Research direction

Start by tracing how Snap globals and permission-gated endowments are assembled, then inspect how the `endowment:network-access` permission currently affects `fetch`. Define which commonly used gated functions belong in scope and verify that missing permissions produce actionable errors; done means the behavior is covered for the selected functions.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience, security
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.