Skip to content

Commit

Permalink
CORS middleware should compile allowOrigin regexp at creation.
Browse files Browse the repository at this point in the history
Note: this changes MW behaviour - previously invalid regexps would cause allow origin check to fail, but now if there is a invalid AllowOrigin regexp panic will be raised during middleware creation.
  • Loading branch information
aldas committed Nov 21, 2024
1 parent 5d98929 commit bab09db
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ benchmark: ## Run benchmarks
help: ## Display this help screen
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

goversion ?= "1.19"
test_version: ## Run tests inside Docker with given version (defaults to 1.19 oldest supported). Example: make test_version goversion=1.19
goversion ?= "1.20"
test_version: ## Run tests inside Docker with given version (defaults to 1.20 oldest supported). Example: make test_version goversion=1.20
@docker run --rm -it -v $(shell pwd):/project golang:$(goversion) /bin/sh -c "cd /project && make init check"
12 changes: 9 additions & 3 deletions middleware/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package middleware

import (
"fmt"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -147,13 +148,18 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
config.AllowMethods = DefaultCORSConfig.AllowMethods
}

allowOriginPatterns := []string{}
allowOriginPatterns := make([]*regexp.Regexp, 0, len(config.AllowOrigins))
for _, origin := range config.AllowOrigins {
pattern := regexp.QuoteMeta(origin)
pattern = strings.ReplaceAll(pattern, "\\*", ".*")
pattern = strings.ReplaceAll(pattern, "\\?", ".")
pattern = "^" + pattern + "$"
allowOriginPatterns = append(allowOriginPatterns, pattern)

re, err := regexp.Compile(pattern)
if err != nil {
panic(fmt.Errorf("echo: invalid AllowOrigins regexp, err: %w", err))
}
allowOriginPatterns = append(allowOriginPatterns, re)
}

allowMethods := strings.Join(config.AllowMethods, ",")
Expand Down Expand Up @@ -239,7 +245,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
}
if checkPatterns {
for _, re := range allowOriginPatterns {
if match, _ := regexp.MatchString(re, origin); match {
if match := re.MatchString(origin); match {
allowOrigin = origin
break
}
Expand Down
8 changes: 8 additions & 0 deletions middleware/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,11 @@ func Test_allowOriginFunc(t *testing.T) {
}
}
}

func TestInvalidAllowOriginsAtCreationPanics(t *testing.T) {
assert.Panics(t, func() {
CORSWithConfig(CORSConfig{
AllowOrigins: []string{"\xff"}, // Invalid UTF-8
})
})
}

0 comments on commit bab09db

Please sign in to comment.