Skip to content

Commit 4bc3e47

Browse files
authored
cors middleware: allow sending Access-Control-Max-Age: 0 value with config.MaxAge being negative number. (#2518)
1 parent 3950c44 commit 4bc3e47

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

middleware/cors.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ type (
9999
// MaxAge determines the value of the Access-Control-Max-Age response header.
100100
// This header indicates how long (in seconds) the results of a preflight
101101
// request can be cached.
102+
// The header is set only if MaxAge != 0, negative value sends "0" which instructs browsers not to cache that response.
102103
//
103-
// Optional. Default value 0. The header is set only if MaxAge > 0.
104+
// Optional. Default value 0 - meaning header is not sent.
104105
//
105106
// See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age
106107
MaxAge int `yaml:"max_age"`
@@ -159,7 +160,11 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
159160
allowMethods := strings.Join(config.AllowMethods, ",")
160161
allowHeaders := strings.Join(config.AllowHeaders, ",")
161162
exposeHeaders := strings.Join(config.ExposeHeaders, ",")
162-
maxAge := strconv.Itoa(config.MaxAge)
163+
164+
maxAge := "0"
165+
if config.MaxAge > 0 {
166+
maxAge = strconv.Itoa(config.MaxAge)
167+
}
163168

164169
return func(next echo.HandlerFunc) echo.HandlerFunc {
165170
return func(c echo.Context) error {
@@ -282,7 +287,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
282287
res.Header().Set(echo.HeaderAccessControlAllowHeaders, h)
283288
}
284289
}
285-
if config.MaxAge > 0 {
290+
if config.MaxAge != 0 {
286291
res.Header().Set(echo.HeaderAccessControlMaxAge, maxAge)
287292
}
288293
return c.NoContent(http.StatusNoContent)

middleware/cors_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,59 @@ func TestCORS(t *testing.T) {
6060
echo.HeaderAccessControlMaxAge: "3600",
6161
},
6262
},
63+
{
64+
name: "ok, preflight request when `Access-Control-Max-Age` is set",
65+
givenMW: CORSWithConfig(CORSConfig{
66+
AllowOrigins: []string{"localhost"},
67+
AllowCredentials: true,
68+
MaxAge: 1,
69+
}),
70+
whenMethod: http.MethodOptions,
71+
whenHeaders: map[string]string{
72+
echo.HeaderOrigin: "localhost",
73+
echo.HeaderContentType: echo.MIMEApplicationJSON,
74+
},
75+
expectHeaders: map[string]string{
76+
echo.HeaderAccessControlMaxAge: "1",
77+
},
78+
},
79+
{
80+
name: "ok, preflight request when `Access-Control-Max-Age` is set to 0 - not to cache response",
81+
givenMW: CORSWithConfig(CORSConfig{
82+
AllowOrigins: []string{"localhost"},
83+
AllowCredentials: true,
84+
MaxAge: -1, // forces `Access-Control-Max-Age: 0`
85+
}),
86+
whenMethod: http.MethodOptions,
87+
whenHeaders: map[string]string{
88+
echo.HeaderOrigin: "localhost",
89+
echo.HeaderContentType: echo.MIMEApplicationJSON,
90+
},
91+
expectHeaders: map[string]string{
92+
echo.HeaderAccessControlMaxAge: "0",
93+
},
94+
},
95+
{
96+
name: "ok, CORS check are skipped",
97+
givenMW: CORSWithConfig(CORSConfig{
98+
AllowOrigins: []string{"localhost"},
99+
AllowCredentials: true,
100+
Skipper: func(c echo.Context) bool {
101+
return true
102+
},
103+
}),
104+
whenMethod: http.MethodOptions,
105+
whenHeaders: map[string]string{
106+
echo.HeaderOrigin: "localhost",
107+
echo.HeaderContentType: echo.MIMEApplicationJSON,
108+
},
109+
notExpectHeaders: map[string]string{
110+
echo.HeaderAccessControlAllowOrigin: "localhost",
111+
echo.HeaderAccessControlAllowMethods: "GET,HEAD,PUT,PATCH,POST,DELETE",
112+
echo.HeaderAccessControlAllowCredentials: "true",
113+
echo.HeaderAccessControlMaxAge: "3600",
114+
},
115+
},
63116
{
64117
name: "ok, preflight request with wildcard `AllowOrigins` and `AllowCredentials` true",
65118
givenMW: CORSWithConfig(CORSConfig{

0 commit comments

Comments
 (0)