grpc config_error ai_generated true

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

INTERNAL: grpc-web: CORS preflight failed for origin http://example.com: allowed origins: [http://localhost:3000]

ID: grpc/grpc-web-cors-origin-mismatch

其他格式: JSON · Markdown 中文 · English
92%修复率
84%置信度
1证据数
2024-07-22首次发现

版本兼容性

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

根因分析

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

English

The gRPC-Web proxy's CORS configuration does not include the requesting origin, causing the browser to block the preflight request and preventing the gRPC call from proceeding.

generic

官方文档

https://github.com/grpc/grpc-web/blob/master/doc/cors.md

解决方案

  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: ["*"]`,但生产环境需恢复。

无效尝试

常见但无效的做法:

  1. 90% 失败

    Disabling CORS entirely in the browser (e.g., using a flag) is insecure and not a production solution.

  2. 80% 失败

    Changing the client origin in the browser's developer tools does not persist and is not a real fix.

  3. 60% 失败

    Adding a wildcard '*' to allowed origins may work but exposes the service to any origin, creating security risks.