Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't modify the config in Validate #71

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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