From dbef1f6db23649f1b41e522858b52fad7be763e4 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sat, 4 Nov 2023 12:02:47 +0100 Subject: [PATCH 1/3] Origin wildcard --- config.go | 3 ++- plugin.go | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 3bc693d..389dd5d 100644 --- a/config.go +++ b/config.go @@ -13,7 +13,8 @@ type Config struct { // CORSConfig headers configuration. type CORSConfig struct { // AllowedOrigin: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin - AllowedOrigin string `mapstructure:"allowed_origin"` + AllowedOrigin string `mapstructure:"allowed_origin"` + AllowedOriginWildcard string `mapstructure:"allowed_origin_wildcard"` // AllowedHeaders: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers AllowedHeaders string `mapstructure:"allowed_headers"` // AllowedMethods: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods diff --git a/plugin.go b/plugin.go index d5e462b..94a6a7d 100644 --- a/plugin.go +++ b/plugin.go @@ -3,6 +3,7 @@ package headers import ( "fmt" "net/http" + "regexp" "strings" "github.com/roadrunner-server/errors" @@ -70,6 +71,13 @@ func (p *Plugin) Init(cfg Configurer) error { opts.AllowedOrigins = strings.Split(p.cfg.CORS.AllowedOrigin, ",") } + // if this option is set, the content of `AllowedOrigins` is ignored + if p.cfg.CORS.AllowedOriginWildcard != "" { + opts.AllowOriginFunc = func(origin string) bool { + return regexp.MustCompile(p.cfg.CORS.AllowedOriginWildcard).MatchString(origin) + } + } + if p.cfg.CORS.AllowedMethods != "" { // trim all spaces p.cfg.CORS.AllowedMethods = strings.Trim(p.cfg.CORS.AllowedMethods, " ") From e2d71672488035a0ad64749b26620be2590db70f Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sat, 4 Nov 2023 12:32:53 +0100 Subject: [PATCH 2/3] Origin wildcard --- plugin.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/plugin.go b/plugin.go index 94a6a7d..8495fb5 100644 --- a/plugin.go +++ b/plugin.go @@ -32,9 +32,10 @@ type Configurer interface { // Plugin serves headers files. Potentially convert into middleware? type Plugin struct { // server configuration (location, forbidden files and etc) - cfg *Config - prop propagation.TextMapPropagator - cors *cors.Cors + cfg *Config + prop propagation.TextMapPropagator + cors *cors.Cors + allowedOriginRegex *regexp.Regexp } // Init must return configure service and return true if service hasStatus enabled. Must return error in case of @@ -73,8 +74,13 @@ func (p *Plugin) Init(cfg Configurer) error { // if this option is set, the content of `AllowedOrigins` is ignored if p.cfg.CORS.AllowedOriginWildcard != "" { + var err error + p.allowedOriginRegex, err = regexp.Compile(p.cfg.CORS.AllowedOriginWildcard) + if err != nil { + return errors.E(op, err) + } opts.AllowOriginFunc = func(origin string) bool { - return regexp.MustCompile(p.cfg.CORS.AllowedOriginWildcard).MatchString(origin) + return p.allowedOriginRegex.MatchString(origin) } } From 2323e5c3490f06f2a25447a0c9477aebafb4c279 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sat, 4 Nov 2023 12:40:19 +0100 Subject: [PATCH 3/3] Rename option --- config.go | 4 ++-- plugin.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 389dd5d..f4aa6ef 100644 --- a/config.go +++ b/config.go @@ -13,8 +13,8 @@ type Config struct { // CORSConfig headers configuration. type CORSConfig struct { // AllowedOrigin: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin - AllowedOrigin string `mapstructure:"allowed_origin"` - AllowedOriginWildcard string `mapstructure:"allowed_origin_wildcard"` + AllowedOrigin string `mapstructure:"allowed_origin"` + AllowedOriginRegex string `mapstructure:"allowed_origin_regex"` // AllowedHeaders: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers AllowedHeaders string `mapstructure:"allowed_headers"` // AllowedMethods: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods diff --git a/plugin.go b/plugin.go index 8495fb5..5b689e0 100644 --- a/plugin.go +++ b/plugin.go @@ -73,9 +73,9 @@ func (p *Plugin) Init(cfg Configurer) error { } // if this option is set, the content of `AllowedOrigins` is ignored - if p.cfg.CORS.AllowedOriginWildcard != "" { + if p.cfg.CORS.AllowedOriginRegex != "" { var err error - p.allowedOriginRegex, err = regexp.Compile(p.cfg.CORS.AllowedOriginWildcard) + p.allowedOriginRegex, err = regexp.Compile(p.cfg.CORS.AllowedOriginRegex) if err != nil { return errors.E(op, err) }