---
eip: 5593
label: EIP-5593
title: Restrict Ethereum Provider API Injection
status: Stagnant
type: Standards Track
category: Interface
created: 2022-09-05
requires: 1193
official: https://eips.ethereum.org/EIPS/eip-5593
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-5593.md
canonical: https://eips.huatuo.xyz/zh/eip-5593/
lang: zh
tldr: 给钱包的指导：只向安全（HTTPS 或 localhost）的第一方页面注入 window.ethereum，使其远离不安全页面以及广告等第三方 iframe。
tags: [wallet, provider, browser, privacy, security]
---

# EIP-5593: Restrict Ethereum Provider API Injection

> Wallet guidance for restricting Ethereum Provider API access to secure contexts for improved privacy and security for wallet users.

**TL;DR:** 给钱包的指导：只向安全（HTTPS 或 localhost）的第一方页面注入 window.ethereum，使其远离不安全页面以及广告等第三方 iframe。

## 它解决什么问题？

浏览器钱包传统上会向几乎所有页面和框架注入 provider 对象（`window.ethereum`，由 [EIP-1193](/eip-1193/) 定义）。该对象允许页面请求账户并请求签名或交易，因此它与摄像头或地理位置访问一样，是一种"强大"的 API。

到处注入有两个缺点：

- **攻击面。** 在合法 dApp 的 iframe 中加载的恶意广告可以与钱包通信，试图诱骗用户批准某些操作。
- **指纹识别。** 任何页面只需检查 provider，就能检测出访问者是 Web3 用户。

Web 平台已经通过 W3C 的*安全上下文*为其他强大的 API 解决了这个问题。EIP-5593 要求钱包对以太坊 provider 应用同样的边界。

## 它如何运作

该提案是一套给钱包的规则，而不是新的 API：

- 只有当页面是*潜在可信的源*时才暴露 provider，这可以通过 `window.isSecureContext` 检查。HTTPS 页面符合条件，`http://localhost` 也符合。
- 在任何 `window.isSecureContext` 为 `false` 的框架中，`window.ethereum` 必须为 `undefined`。
- **第三方 iframe**（与顶层页面的源不同，包括子域名）默认不提供 provider。钱包可以提供选择启用的途径，例如通过 iframe 的 `allow` 属性和 Permissions API，并需经用户批准。
- **带有 `sandbox` 的同源 iframe** 会被阻止，除非该 sandbox 包含 `allow-same-origin`。
- 隐私/无痕窗口仍可获得 provider。

规范中要求的部分测试用例：

| 顶层 | 框架 | 结果 |
| --- | --- | --- |
| `http://a.com` | 无 | 阻止（不安全） |
| `https://a.com` | 无 | 允许 |
| `https://a.com` | `https://a.com` | 允许 |
| `https://a.com` | `https://b.com` | 阻止（第三方） |
| `https://a.com` | `https://sub.a.com` | 阻止（第三方） |
| `https://a.com` | `https://a.com`，带无值的 `sandbox` | 阻止 |

对于本地开发，钱包应提供一个"开发者模式"开关，默认关闭，仅在尚未把 localhost 视为安全的浏览器中，对 `http://localhost` 放宽检查。

## 示例代码

钱包内容脚本可以如何决定是否注入（规则的简化示意）：

```ts
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();
}
```

在 dApp 一侧，除了应当妥善处理 provider 缺失的情况外，没有任何变化：

```ts
if (!window.isSecureContext || !(window as any).ethereum) {
  showMessage("Open this dApp over HTTPS to connect a wallet.");
}
```

## 需要注意

- **状态：Stagnant。** 这是给钱包作者的指导，并非最终确定的标准；钱包可能遵循，也可能不遵循。
- 通过普通 HTTP 提供的 dApp，或作为跨源 iframe 嵌入的 dApp，在这些规则下应预期没有 provider。
- 不鼓励把开发者开关扩展到 localhost 之外，因为攻击者可能说服用户启用它。
- [EIP-6963](/eip-6963/) 解决的是另一个 provider 问题（多个钱包争抢 `window.ethereum`）；两者互为补充。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-5593
