# 内部错误：gRPC-Web：来源 http://example.com 的 CORS 预检失败：允许的来源：[http://localhost:3000]

- **ID:** `grpc/grpc-web-cors-origin-mismatch`
- **领域:** grpc
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

gRPC-Web 代理的 CORS 配置未包含请求来源，导致浏览器阻止预检请求并阻止 gRPC 调用继续进行。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| gRPC-Web v1.4.0 | active | — | — |
| Envoy 1.27.0 | active | — | — |
| gRPC v1.60.0 | active | — | — |

## 解决方案

1. ```
   更新 Envoy 或 gRPC-Web 代理配置以包含请求来源：在 Envoy 中，将 `allowed_origins: ["http://example.com"]` 添加到 CORS 过滤器。
   ```
2. ```
   使用反向代理（如 Nginx）处理 CORS 标头：`add_header Access-Control-Allow-Origin "http://example.com" always;`
   ```
3. ```
   临时启用通配符来源用于开发：在 Envoy 配置中设置 `allowed_origins: ["*"]`，但生产环境需恢复。
   ```

## 无效尝试

- **** — Disabling CORS entirely in the browser (e.g., using a flag) is insecure and not a production solution. (90% 失败率)
- **** — Changing the client origin in the browser's developer tools does not persist and is not a real fix. (80% 失败率)
- **** — Adding a wildcard '*' to allowed origins may work but exposes the service to any origin, creating security risks. (60% 失败率)
