Skip to content

Commit

Permalink
Don't modify the config in Validate (#71)
Browse files Browse the repository at this point in the history
Instead of processing '*' and changing the configuration passed into
Validate, which can break the configuration and cause unexpected panics,
process it in newCors so the modification only exists in the internal
copy of the configuration.

Fixes #70
  • Loading branch information
lantw44 committed Nov 4, 2020
1 parent 88812e7 commit 8c02baa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 6 additions & 0 deletions config.go
Expand Up @@ -43,6 +43,12 @@ func newCors(config Config) *cors {
panic(err.Error())
}

for _, origin := range config.AllowOrigins {
if origin == "*" {
config.AllowAllOrigins = true
}
}

return &cors{
allowOriginFunc: config.AllowOriginFunc,
allowAllOrigins: config.AllowAllOrigins,
Expand Down
7 changes: 2 additions & 5 deletions cors.go
Expand Up @@ -95,18 +95,15 @@ func (c Config) validateAllowedSchemas(origin string) bool {
}

// Validate is check configuration of user defined.
func (c *Config) Validate() error {
func (c Config) Validate() error {
if c.AllowAllOrigins && (c.AllowOriginFunc != nil || len(c.AllowOrigins) > 0) {
return errors.New("conflict settings: all origins are allowed. AllowOriginFunc or AllowOrigins is not needed")
}
if !c.AllowAllOrigins && c.AllowOriginFunc == nil && len(c.AllowOrigins) == 0 {
return errors.New("conflict settings: all origins disabled")
}
for _, origin := range c.AllowOrigins {
if origin == "*" {
c.AllowAllOrigins = true
return nil
} else if !strings.Contains(origin, "*") && !c.validateAllowedSchemas(origin) {
if !strings.Contains(origin, "*") && !c.validateAllowedSchemas(origin) {
return errors.New("bad origin: origins must contain '*' or include " + strings.Join(c.getAllowedSchemas(), ","))
}
}
Expand Down

0 comments on commit 8c02baa

Please sign in to comment.