|
4 | 4 | package middleware
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "fmt" |
7 | 8 | "net/http"
|
8 | 9 | "regexp"
|
9 | 10 | "strconv"
|
@@ -147,13 +148,18 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
|
147 | 148 | config.AllowMethods = DefaultCORSConfig.AllowMethods
|
148 | 149 | }
|
149 | 150 |
|
150 |
| - allowOriginPatterns := []string{} |
| 151 | + allowOriginPatterns := make([]*regexp.Regexp, 0, len(config.AllowOrigins)) |
151 | 152 | for _, origin := range config.AllowOrigins {
|
152 | 153 | pattern := regexp.QuoteMeta(origin)
|
153 | 154 | pattern = strings.ReplaceAll(pattern, "\\*", ".*")
|
154 | 155 | pattern = strings.ReplaceAll(pattern, "\\?", ".")
|
155 | 156 | pattern = "^" + pattern + "$"
|
156 |
| - allowOriginPatterns = append(allowOriginPatterns, pattern) |
| 157 | + |
| 158 | + re, err := regexp.Compile(pattern) |
| 159 | + if err != nil { |
| 160 | + panic(fmt.Errorf("echo: invalid AllowOrigins regexp, err: %w", err)) |
| 161 | + } |
| 162 | + allowOriginPatterns = append(allowOriginPatterns, re) |
157 | 163 | }
|
158 | 164 |
|
159 | 165 | allowMethods := strings.Join(config.AllowMethods, ",")
|
@@ -239,7 +245,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
|
239 | 245 | }
|
240 | 246 | if checkPatterns {
|
241 | 247 | for _, re := range allowOriginPatterns {
|
242 |
| - if match, _ := regexp.MatchString(re, origin); match { |
| 248 | + if match := re.MatchString(origin); match { |
243 | 249 | allowOrigin = origin
|
244 | 250 | break
|
245 | 251 | }
|
|
0 commit comments