CORS được cấu hình bằng CORSMiddleware. Không nên dùng wildcard origin cùng credentials trong production; hãy whitelist domain cụ thể theo môi trường.
Ví dụ:
python
app.add_middleware(
CORSMiddleware,
allow_origins=["https://app.example.com"],
allow_credentials=True,
allow_methods=["GET", "POST", "PATCH", "DELETE"],
allow_headers=["Authorization", "Content-Type"],
)CORS chỉ là chính sách browser, không phải security boundary cho API.
Backend vẫn phải authenticate và authorize request.
CORS is configured with CORSMiddleware. Do not use wildcard origins with credentials in production; whitelist concrete domains per environment.
Example:
python
app.add_middleware(
CORSMiddleware,
allow_origins=["https://app.example.com"],
allow_credentials=True,
allow_methods=["GET", "POST", "PATCH", "DELETE"],
allow_headers=["Authorization", "Content-Type"],
)CORS is only a browser policy, not an API security boundary.
The backend must still authenticate and authorize requests.