Skip to content

Commit 28eabcb

Browse files
gabyCopilotgemini-code-assist[bot]
authored
🧹 chore: Fix CORS docs and comments (#3637)
Co-authored-by: Copilot <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent ceacde5 commit 28eabcb

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

docs/middleware/cors.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ panic: [CORS] Configuration error: When 'AllowCredentials' is set to true, 'Allo
118118
| AllowOrigins | `[]string` | AllowOrigins defines a list of origins that may access the resource. This supports subdomain matching, so you can use a value like "https://*.example.com" to allow any subdomain of example.com to submit requests. If the special wildcard `"*"` is present in the list, all origins will be allowed. | `["*"]` |
119119
| AllowOriginsFunc | `func(origin string) bool` | `AllowOriginsFunc` is a function that dynamically determines whether to allow a request based on its origin. If this function returns `true`, the 'Access-Control-Allow-Origin' response header will be set to the request's 'origin' header. This function is only used if the request's origin doesn't match any origin in `AllowOrigins`. | `nil` |
120120
| AllowPrivateNetwork | `bool` | Indicates whether the `Access-Control-Allow-Private-Network` response header should be set to `true`, allowing requests from private networks. This aligns with modern security practices for web applications interacting with private networks. | `false` |
121-
| ExposeHeaders | `string` | ExposeHeaders defines an allowlist of headers that clients are allowed to access. | `[]` |
121+
| ExposeHeaders | `[]string` | ExposeHeaders defines an allowlist of headers that clients are allowed to access. | `[]` |
122122
| MaxAge | `int` | MaxAge indicates how long (in seconds) the results of a preflight request can be cached. If you pass MaxAge 0, the Access-Control-Max-Age header will not be added and the browser will use 5 seconds by default. To disable caching completely, pass MaxAge value negative. It will set the Access-Control-Max-Age header to 0. | `0` |
123123
| Next | `func(fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` |
124124

@@ -159,7 +159,7 @@ If you want to allow CORS requests from any subdomain of `example.com`, includin
159159

160160
```go
161161
app.Use(cors.New(cors.Config{
162-
AllowOrigins: "https://*.example.com",
162+
AllowOrigins: []string{"https://*.example.com"},
163163
}))
164164
```
165165

@@ -201,7 +201,7 @@ The `ExposeHeaders` option defines an allowlist of headers that clients are allo
201201

202202
The `MaxAge` option indicates how long the results of a preflight request can be cached. If `MaxAge` is set to `3600`, the middleware adds the header `Access-Control-Max-Age: 3600` to the response.
203203

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.
204+
The `Vary` header helps caches store the correct response. For simple requests the middleware sets `Vary: Origin` unless all origins are allowed. Preflight responses add `Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers` (and `Access-Control-Request-Private-Network` when enabled and requested). This ensures caches know when to reuse a response and when to revalidate with the server.
205205

206206
## Infrastructure Considerations
207207

middleware/cors/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ type Config struct {
5959
// AllowCredentials indicates whether or not the response to the request
6060
// can be exposed when the credentials flag is true. When used as part of
6161
// a response to a preflight request, this indicates whether or not the
62-
// actual request can be made using credentials. Note: If true, AllowOrigins
63-
// cannot be set to true to prevent security vulnerabilities.
62+
// actual request can be made using credentials. Note: if true, the
63+
// AllowOrigins setting cannot contain the wildcard "*" to prevent
64+
// security vulnerabilities.
6465
//
6566
// Optional. Default value false.
6667
AllowCredentials bool

0 commit comments

Comments
 (0)