python auth_error ai_generated true

Starlette CORS中间件错误:无效的CORS来源

starlette.middleware.cors.CORSMiddleware: Invalid CORS origin: 'https://example.com'

ID: python/starlette-multiple-cors-origins

其他格式: JSON · Markdown 中文 · English
80%修复率
82%置信度
0证据数
2024-05-30首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

CORS中间件配置了允许的来源列表,但请求的Origin头不在列表中,导致被拒绝。

English

The CORS middleware is configured with a list of allowed origins, but the incoming request's Origin header is not in the list, causing rejection.

generic

解决方案

  1. 95% 成功率
    Add the exact origin to `allow_origins` list, or use a regex pattern with `allow_origin_regex`.
  2. 90% 成功率
    If credentials are needed, cannot use wildcard; use specific origins.

无效尝试

常见但无效的做法:

  1. 90% 失败

    Adding the origin to the list after the request doesn't help because the middleware checks at request time.

  2. 85% 失败

    Using wildcard `*` with credentials is not allowed in browsers, so it fails for credentialed requests.