1515 Skipper Skipper
1616
1717 // AllowOrigin defines a list of origins that may access the resource.
18- // Optional. If request header `Origin` is set, value is []string{"<Origin>"}
19- // else []string{"*"}.
18+ // Optional. Default value []string{"*"}.
2019 AllowOrigins []string `json:"allow_origins"`
2120
2221 // AllowMethods defines a list methods allowed when accessing the resource.
5251 // DefaultCORSConfig is the default CORS middleware config.
5352 DefaultCORSConfig = CORSConfig {
5453 Skipper : defaultSkipper ,
54+ AllowOrigins : []string {"*" },
5555 AllowMethods : []string {echo .GET , echo .HEAD , echo .PUT , echo .PATCH , echo .POST , echo .DELETE },
5656 }
5757)
@@ -69,11 +69,13 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
6969 if config .Skipper == nil {
7070 config .Skipper = DefaultCORSConfig .Skipper
7171 }
72+ if len (config .AllowOrigins ) == 0 {
73+ config .AllowOrigins = DefaultCORSConfig .AllowOrigins
74+ }
7275 if len (config .AllowMethods ) == 0 {
7376 config .AllowMethods = DefaultCORSConfig .AllowMethods
7477 }
7578
76- allowedOrigins := strings .Join (config .AllowOrigins , "," )
7779 allowMethods := strings .Join (config .AllowMethods , "," )
7880 allowHeaders := strings .Join (config .AllowHeaders , "," )
7981 exposeHeaders := strings .Join (config .ExposeHeaders , "," )
@@ -88,21 +90,20 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
8890 req := c .Request ()
8991 res := c .Response ()
9092 origin := req .Header .Get (echo .HeaderOrigin )
93+ allowOrigin := ""
9194
92- if allowedOrigins == "" {
93- if origin != "" {
94- allowedOrigins = origin
95- } else {
96- if ! config .AllowCredentials {
97- allowedOrigins = "*"
98- }
95+ // Check allowed origins
96+ for _ , o := range config .AllowOrigins {
97+ if o == "*" || o == origin {
98+ allowOrigin = o
99+ break
99100 }
100101 }
101102
102103 // Simple request
103104 if req .Method != echo .OPTIONS {
104105 res .Header ().Add (echo .HeaderVary , echo .HeaderOrigin )
105- res .Header ().Set (echo .HeaderAccessControlAllowOrigin , allowedOrigins )
106+ res .Header ().Set (echo .HeaderAccessControlAllowOrigin , allowOrigin )
106107 if config .AllowCredentials {
107108 res .Header ().Set (echo .HeaderAccessControlAllowCredentials , "true" )
108109 }
@@ -116,7 +117,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
116117 res .Header ().Add (echo .HeaderVary , echo .HeaderOrigin )
117118 res .Header ().Add (echo .HeaderVary , echo .HeaderAccessControlRequestMethod )
118119 res .Header ().Add (echo .HeaderVary , echo .HeaderAccessControlRequestHeaders )
119- res .Header ().Set (echo .HeaderAccessControlAllowOrigin , allowedOrigins )
120+ res .Header ().Set (echo .HeaderAccessControlAllowOrigin , allowOrigin )
120121 res .Header ().Set (echo .HeaderAccessControlAllowMethods , allowMethods )
121122 if config .AllowCredentials {
122123 res .Header ().Set (echo .HeaderAccessControlAllowCredentials , "true" )
0 commit comments