EIP-5593 · Stagnant · Interface · 2022-09-05
EIP-5593: Restrict Ethereum Provider API Injection
Wallet guidance to inject window.ethereum only into secure (HTTPS or localhost) first-party pages, keeping it away from insecure pages and third-party iframes such as ads.
What problem does it solve?
Browser wallets traditionally inject the provider object (window.ethereum, defined by EIP-1193) into almost every page and frame. That object lets a page ask for accounts and request signatures or transactions, so it is a "powerful" API in the same sense as camera or geolocation access.
Injecting it everywhere has two downsides:
- Attack surface. A malicious advertisement loaded in an iframe on a legitimate dApp can talk to the wallet and try to trick the user into approving something.
- Fingerprinting. Any page can detect that the visitor is a Web3 user just by checking for the provider.
The web platform already solves this for other powerful APIs with W3C secure contexts. EIP-5593 asks wallets to apply the same boundary to the Ethereum provider.
How it works
The proposal is a set of rules for wallets, not a new API:
- The provider is only exposed when the page is a potentially trustworthy origin, which you can check with
window.isSecureContext. HTTPS pages qualify, and so doeshttp://localhost. - In any frame where
window.isSecureContextisfalse,window.ethereummust beundefined. - Third-party iframes (a different origin from the top-level page, including subdomains) get no provider by default. Wallets may offer an opt-in path, for example via the iframe
allowattribute and the Permissions API, subject to user approval. - Same-origin iframes with
sandboxare blocked, unless the sandbox includesallow-same-origin. - Private/incognito windows may still get the provider.
Some of the required test cases from the spec:
| Top level | Frame | Result |
|---|---|---|
http://a.com |
none | blocked (insecure) |
https://a.com |
none | allowed |
https://a.com |
https://a.com |
allowed |
https://a.com |
https://b.com |
blocked (third party) |
https://a.com |
https://sub.a.com |
blocked (third party) |
https://a.com |
https://a.com with bare sandbox |
blocked |
For local development, wallets should offer a "developer mode" toggle, off by default, that relaxes the check only for http://localhost in browsers that do not already treat localhost as secure.
Sample code
How a wallet content script could decide whether to inject (simplified illustration of the rules):
function shouldInjectProvider(win: Window): boolean {
// 1. Must be a secure context (HTTPS, or localhost)
if (!win.isSecureContext) return false;
// 2. Top-level window: OK
if (win === win.top) return true;
// 3. Iframe: must be same-origin with the top-level page
try {
if (win.location.origin !== win.top!.location.origin) return false;
} catch {
return false; // cross-origin access throws -> third party
}
// 4. Same-origin but sandboxed without allow-same-origin -> blocked.
// A sandbox without allow-same-origin gives the frame an opaque origin,
// so its origin reads as "null".
if (win.origin === "null") return false;
return true;
}
if (shouldInjectProvider(window)) {
(window as any).ethereum = createProvider();
}
On the dApp side, nothing changes except that you should handle a missing provider gracefully:
if (!window.isSecureContext || !(window as any).ethereum) {
showMessage("Open this dApp over HTTPS to connect a wallet.");
}
Things to know
- Status: Stagnant. It is guidance for wallet authors and is not a finalised standard; wallets may or may not follow it.
- A dApp served over plain HTTP, or embedded as a cross-origin iframe, should expect no provider under these rules.
- Extending the developer toggle beyond localhost is discouraged, since attackers could talk users into enabling it.
- EIP-6963 addresses a different provider problem (several wallets competing for
window.ethereum); the two are complementary.