CORS (Cross-Origin Resource Sharing) என்பது ஒரு browser security mechanism ஆகும், இது ஒரு origin-இலிருந்து வரும் web page வேறு origin-இல் உள்ள உங்கள் API-ஐ அழைக்க முடியுமா என்பதைக் கட்டுப்படுத்துகிறது. FastAPI இதை built-in CORSMiddleware மூலம் configure செய்கிறது. வேறு domain/port-இல் உள்ள ஒரு frontend உங்கள் API-ஐ அழைக்கும்போதெல்லாம் நீங்கள் CORS-ஐ சந்திப்பீர்கள்.
CORS தீர்க்கும் பிரச்சினை
A React app at http://localhost:3000 calling an API at http://localhost:8000 is
CROSS-ORIGIN (different port). The BROWSER blocks the response unless the API
sends CORS headers permitting that origin.
(origin = scheme + host + port)
CORSMiddleware-ஐ configure செய்தல்
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["https://app.example.com", "http://localhost:3000"], # ALLOWLIST of origins
allow_credentials=True, # allow cookies/auth (requires specific origins, not "*")
allow_methods=["*"], # or ["GET", "POST", ...]
allow_headers=["*"],
)
Middleware தேவையான CORS response headers-ஐ சேர்க்கிறது, எந்த origins, methods, மற்றும் headers அனுமதிக்கப்படுகின்றன என்பதை browser-க்குச் சொல்கிறது. Browser இந்த விதிகளைச் செயல்படுத்துகிறது.
Security: origins-ஐ கட்டுப்படுத்துங்கள் ("*"-ஐப் பயன்படுத்த வேண்டாம்)
# ❌ allowing all origins — convenient but insecure for credentialed/private APIs
allow_origins=["*"]
# ✅ restrict to your known frontend origins (an allowlist)
allow_origins=["https://app.example.com"]
# NOTE: allow_credentials=True is INCOMPATIBLE with allow_origins=["*"]
Production-க்கு, * பயன்படுத்துவதற்குப் பதிலாக allow_origins-ஐ ஒரு allowlist-க்கு கட்டுப்படுத்துங்கள். மேலும், credentials (cookies/auth) அனுமதிப்பதற்கு குறிப்பிட்ட origins தேவை — wildcard credentials உடன் அனுமதிக்கப்படாது.
ஒரு முக்கியமான தவறான புரிதல்
CORS is enforced by the BROWSER, not the server. It does NOT protect your API from
non-browser clients (curl, Postman, other servers) — those ignore CORS entirely.
CORS only governs what browser JavaScript from other origins may do.
இது ஏன் முக்கியம்
Web development-இல் CORS மிகவும் பொதுவான தடைகளில் ஒன்றாகும் — கிட்டத்தட்ட ஒவ்வொரு frontend-to-API setup-உம் இதைச் சந்திக்கிறது (குறிப்பாக வெவ்வேறு ports உள்ள local development-இல், மற்றும் தனி frontend domain உள்ள production-இல்), எனவே FastAPI-இன் CORSMiddleware மூலம் இதை எவ்வாறு configure செய்வது என்பதை அறிவது நடைமுறை, அடிக்கடி தேவைப்படும் அறிவாகும்.
இது ஏன் இருக்கிறது (browser same-origin security), server response headers வழியாக எவ்வாறு opt in செய்கிறது, மற்றும் இதை எவ்வாறு configure செய்வது (அனுமதிக்கப்பட்ட origins, methods, headers, credentials) என்பதைப் புரிந்துகொள்வது requests தடுக்கப்படாமல் frontends-ஐ FastAPI APIs உடன் இணைக்க அவசியம்.
இதை பாதுகாப்பாக configure செய்வதும் சமமாக முக்கியம் — அனுமதிக்கும் * பதிலாக allow_origins-ஐ ஒரு குறிப்பிட்ட allowlist-க்கு கட்டுப்படுத்துதல் (மற்றும் credentials wildcard உடன் இணக்கமற்றது என்பதைப் புரிந்துகொள்ளுதல்) — மேலும் CORS என்பது API authorization அல்ல, ஒரு browser mechanism என்ற முக்கியமான தவறான புரிதலைப் புரிந்துகொள்வது (இது non-browser clients-க்கு எதிராக பாதுகாப்பு அளிக்காது).
CORS-ஐ சரியாகவும் பாதுகாப்பாகவும் எவ்வாறு அமைப்பது என்பதை அறிவது, ஒரு web frontend-ஆல் பயன்படுத்தப்படும் எந்த FastAPI API-க்கும் முக்கியமான அன்றாட அறிவாகும்.
