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

Allow regexp on AllowOrigins #36

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cors

import (
"net/http"
"regexp"
"strings"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -105,8 +106,14 @@ func (cors *cors) validateOrigin(origin string) bool {
if cors.allowAllOrigins {
return true
}
r, _ := regexp.Compile("^\\/(.+)\\/[gimuy]?$")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move regexp.Compile to the top block as a global variable for better performance.

for _, value := range cors.allowOrigins {
if value == origin {
if r.MatchString(value) {
match, _ := regexp.MatchString(r.FindStringSubmatch(value)[1], origin)
if match {
return true
}
} else if value == origin {
return true
}
}
Expand Down
9 changes: 9 additions & 0 deletions cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cors

import (
"errors"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -86,6 +87,14 @@ func (c Config) getAllowedSchemas() []string {

func (c Config) validateAllowedSchemas(origin string) bool {
allowedSchemas := c.getAllowedSchemas()

r, _ := regexp.Compile("^\\/(.+)\\/[gimuy]?$")
if r.MatchString(origin) {
// Normalize regexp-based origins
origin = r.FindStringSubmatch(origin)[1]
origin = strings.Replace(origin, "?", "", 1)
}

for _, schema := range allowedSchemas {
if strings.HasPrefix(origin, schema) {
return true
Expand Down
24 changes: 24 additions & 0 deletions cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ func TestBadConfig(t *testing.T) {
AllowOrigins: []string{"google.com"},
})
})
assert.Panics(t, func() {
New(Config{
AllowOrigins: []string{"/http://google.com"},
})
})
assert.Panics(t, func() {
New(Config{
AllowOrigins: []string{"http?://google.com"},
})
})
assert.Panics(t, func() {
New(Config{
AllowOrigins: []string{"http?://google.com/g"},
})
})
}

func TestNormalize(t *testing.T) {
Expand Down Expand Up @@ -263,6 +278,15 @@ func TestValidateOrigin(t *testing.T) {
assert.True(t, cors.validateOrigin("https://google.com"))
assert.True(t, cors.validateOrigin("example.com"))
assert.True(t, cors.validateOrigin("chrome-extension://random-extension-id"))

cors = newCors(Config{
AllowOrigins: []string{"/https?://(?:.+\\.)?google\\.com/g"},
})
assert.True(t, cors.validateOrigin("http://google.com"))
assert.True(t, cors.validateOrigin("https://google.com"))
assert.True(t, cors.validateOrigin("https://maps.google.com"))
assert.True(t, cors.validateOrigin("https://maps.test.google.com"))
assert.False(t, cors.validateOrigin("https://maps.google.it"))
}

func TestPassesAllowOrigins(t *testing.T) {
Expand Down