Skip to content

Commit d832efd

Browse files
committed
cors: not checking for origin header
Signed-off-by: Vishal Rana <[email protected]>
1 parent 6ead4be commit d832efd

File tree

3 files changed

+4
-31
lines changed

3 files changed

+4
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ website/public
55
vendor
66

77
.DS_Store
8+
_test

middleware/cors.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
7575
if len(config.AllowMethods) == 0 {
7676
config.AllowMethods = DefaultCORSConfig.AllowMethods
7777
}
78+
allowedOrigins := strings.Join(config.AllowOrigins, ",")
7879
allowMethods := strings.Join(config.AllowMethods, ",")
7980
allowHeaders := strings.Join(config.AllowHeaders, ",")
8081
exposeHeaders := strings.Join(config.ExposeHeaders, ",")
@@ -88,25 +89,11 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
8889

8990
req := c.Request()
9091
res := c.Response()
91-
origin := req.Header.Get(echo.HeaderOrigin)
92-
_, originSet := req.Header[echo.HeaderOrigin]
93-
94-
// Check allowed origins
95-
allowedOrigin := ""
96-
for _, o := range config.AllowOrigins {
97-
if o == "*" || o == origin {
98-
allowedOrigin = o
99-
break
100-
}
101-
}
10292

10393
// Simple request
10494
if req.Method != echo.OPTIONS {
10595
res.Header().Add(echo.HeaderVary, echo.HeaderOrigin)
106-
if !originSet || allowedOrigin == "" {
107-
return next(c)
108-
}
109-
res.Header().Set(echo.HeaderAccessControlAllowOrigin, allowedOrigin)
96+
res.Header().Set(echo.HeaderAccessControlAllowOrigin, allowedOrigins)
11097
if config.AllowCredentials {
11198
res.Header().Set(echo.HeaderAccessControlAllowCredentials, "true")
11299
}
@@ -120,10 +107,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
120107
res.Header().Add(echo.HeaderVary, echo.HeaderOrigin)
121108
res.Header().Add(echo.HeaderVary, echo.HeaderAccessControlRequestMethod)
122109
res.Header().Add(echo.HeaderVary, echo.HeaderAccessControlRequestHeaders)
123-
if !originSet || allowedOrigin == "" {
124-
return next(c)
125-
}
126-
res.Header().Set(echo.HeaderAccessControlAllowOrigin, allowedOrigin)
110+
res.Header().Set(echo.HeaderAccessControlAllowOrigin, allowedOrigins)
127111
res.Header().Set(echo.HeaderAccessControlAllowMethods, allowMethods)
128112
if config.AllowCredentials {
129113
res.Header().Set(echo.HeaderAccessControlAllowCredentials, "true")

middleware/cors_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ func TestCORS(t *testing.T) {
2121
return c.String(http.StatusOK, "test")
2222
})
2323

24-
// No origin header
25-
h(c)
26-
assert.Equal(t, "", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
27-
28-
// Empty origin header
29-
req, _ = http.NewRequest(echo.GET, "/", nil)
30-
rec = httptest.NewRecorder()
31-
c = e.NewContext(req, rec)
32-
req.Header.Set(echo.HeaderOrigin, "")
33-
h(c)
34-
assert.Equal(t, "*", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
35-
3624
// Wildcard origin
3725
req, _ = http.NewRequest(echo.GET, "/", nil)
3826
rec = httptest.NewRecorder()

0 commit comments

Comments
 (0)