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
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.
解决方案
-
100% 成功率
Use a list: allow_origins=["*"] or allow_origins=["https://example.com"]
-
95% 成功率
Use allow_origin_regex for pattern matching: allow_origin_regex="https?://.*"
无效尝试
常见但无效的做法:
-
100% 失败
Starlette's CORSMiddleware requires a list, even for wildcard.
-
90% 失败
The assertion checks each element is a string, so non-strings cause failure.