python config_error ai_generated true

AssertionError: 'allow_origins' must be a list or tuple

ID: python/fastapi-cors-middleware-config

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-11-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

CORS 中间件的 allow_origins 参数类型错误,例如传入了字符串。

generic

中文

CORS 中间件的 allow_origins 参数类型错误,例如传入了字符串。

Workarounds

  1. 100% success
    app.add_middleware(CORSMiddleware, allow_origins=['*'], allow_methods=['*'])
    # 或 allow_origins=["http://localhost:3000"]

Dead Ends

Common approaches that don't work:

  1. 80% fail

    仍然是一个字符串,不是列表

  2. 50% fail

    可能过于宽松,导致安全问题