python config_error ai_generated true

AssertionError: allow_origins 必须是字符串的列表或元组

AssertionError: allow_origins must be a list or tuple of strings

ID: python/fastapi-cors-middleware-misconfiguration

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

版本兼容性

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

根因分析

CORS 中间件期望 allow_origins 为列表,但提供了字符串或其他类型。

English

The CORS middleware expects allow_origins as a list, but a string or other type is provided.

generic

解决方案

  1. 100% 成功率
    Use a list: allow_origins=["*"] or allow_origins=["https://example.com"]
  2. 95% 成功率
    Use allow_origin_regex for pattern matching: allow_origin_regex="https?://.*"

无效尝试

常见但无效的做法:

  1. 100% 失败

    Starlette's CORSMiddleware requires a list, even for wildcard.

  2. 90% 失败

    The assertion checks each element is a string, so non-strings cause failure.