You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/middleware/cors.md
+171Lines changed: 171 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -169,6 +169,12 @@ The CORS middleware works by adding the necessary CORS headers to responses from
169
169
170
170
When a request comes in, the middleware first checks if it's a preflight request, which is a CORS mechanism to determine whether the actual request is safe to send. Preflight requests are HTTP OPTIONS requests with specific CORS headers. If it's a preflight request, the middleware responds with the appropriate CORS headers and ends the request.
171
171
172
+
:::note
173
+
Preflight requests are typically sent by browsers before making actual cross-origin requests, especially for methods other than GET or POST, or when custom headers are used.
174
+
175
+
A preflight request is an HTTP OPTIONS request that includes the `Origin`, `Access-Control-Request-Method`, and optionally `Access-Control-Request-Headers` headers. The browser sends this request to check if the server allows the actual request method and headers.
176
+
:::
177
+
172
178
If it's not a preflight request, the middleware adds the CORS headers to the response and passes the request to the next handler. The actual CORS headers added depend on the configuration of the middleware.
173
179
174
180
The `AllowOrigins` option controls which origins can make cross-origin requests. The middleware handles different `AllowOrigins` configurations as follows:
@@ -197,6 +203,171 @@ The `MaxAge` option indicates how long the results of a preflight request can be
197
203
198
204
The `Vary` header is used in this middleware to inform the client that the server's response to a request. For or both preflight and actual requests, the Vary header is set to `Access-Control-Request-Method` and `Access-Control-Request-Headers`. For preflight requests, the Vary header is also set to `Origin`. The `Vary` header is important for caching. It helps caches (like a web browser's cache or a CDN) determine when a cached response can be used in response to a future request, and when the server needs to be queried for a new response.
199
205
206
+
## Infrastructure Considerations
207
+
208
+
When deploying Fiber applications behind infrastructure components like CDNs, API gateways, load balancers, or reverse proxies, you have two main options for handling CORS:
209
+
210
+
### Option 1: Use Infrastructure-Level CORS (Recommended)
211
+
212
+
**For most production deployments, it's often preferable to handle CORS at the infrastructure level** rather than in your Fiber application. This approach offers several advantages:
213
+
214
+
-**Better Performance**: CORS headers are added at the edge, closer to the client
215
+
-**Reduced Server Load**: Preflight requests are handled without reaching your application
216
+
-**Centralized Configuration**: Manage CORS policies alongside other infrastructure settings
217
+
-**Built-in Caching**: Infrastructure providers optimize CORS response caching
218
+
219
+
**Common infrastructure CORS solutions:**
220
+
221
+
-**CDNs**: CloudFront, CloudFlare, Azure CDN - handle CORS at edge locations
222
+
-**API Gateways**: AWS API Gateway, Google Cloud API Gateway - centralized CORS management
223
+
-**Load Balancers**: Application Load Balancers with CORS rules
224
+
-**Reverse Proxies**: Nginx, Apache with CORS modules
225
+
226
+
If using infrastructure-level CORS, **disable Fiber's CORS middleware** to avoid conflicts:
227
+
228
+
```go
229
+
// Don't use both - choose one approach
230
+
// app.Use(cors.New()) // Remove this line when using infrastructure CORS
231
+
```
232
+
233
+
### Option 2: Application-Level CORS (Fiber Middleware)
234
+
235
+
Use Fiber's CORS middleware when you need:
236
+
237
+
-**Dynamic origin validation** based on application logic
238
+
-**Fine-grained control** over CORS policies per route
239
+
-**Integration with application state** (database-driven origins, etc.)
240
+
-**Development environments** where infrastructure CORS isn't available
241
+
242
+
If choosing this approach, ensure that **all CORS headers reach your Fiber application unchanged**.
243
+
244
+
### Required Headers for CORS Preflight Requests
245
+
246
+
For CORS preflight requests to work correctly, these headers **must not be stripped or modified by caching layers**:
247
+
248
+
-`Origin` - Required to identify the requesting origin
249
+
-`Access-Control-Request-Method` - Required to identify the HTTP method for the actual request
250
+
-`Access-Control-Request-Headers` - Optional, contains custom headers the actual request will use
251
+
-`Access-Control-Request-Private-Network` - Optional, for private network access requests
252
+
253
+
:::warning Critical Preflight Requirement
254
+
If the `Access-Control-Request-Method` header is missing from an OPTIONS request, Fiber will not recognize them as CORS preflight requests. Instead, they'll be treated as regular OPTIONS requests, which typically return `405 Method Not Allowed` since most applications don't define explicit OPTIONS handlers.
255
+
:::
256
+
257
+
### CORS Response Headers (Set by Fiber)
258
+
259
+
The middleware sets these response headers based on your configuration:
260
+
261
+
**For all CORS requests:**
262
+
263
+
-`Access-Control-Allow-Origin` - Set to the allowed origin or "*"
264
+
-`Access-Control-Allow-Credentials` - Set to "true" when `AllowCredentials: true`
265
+
-`Access-Control-Expose-Headers` - Lists headers the client can access
266
+
-`Vary` - Set to "Origin" (unless wildcard origins are used)
Configure CORS at the infrastructure level when possible for optimal performance and reduced complexity.
369
+
:::
370
+
200
371
## Security Considerations
201
372
202
373
When configuring CORS, misconfiguration can potentially expose your application to various security risks. Here are some secure configurations and common pitfalls to avoid:
0 commit comments