Skip to content

Commit

Permalink
Merge pull request #33 from Betterment/jmileham/custom_allowed_origins
Browse files Browse the repository at this point in the history
Allow custom CORS origins
  • Loading branch information
jmileham authored May 9, 2019
2 parents 587ddb5 + c40fcf4 commit d05643d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SHELL = /bin/sh

VERSION=0.9.5
VERSION=0.9.6
BUILD=`git rev-parse HEAD`

LDFLAGS=-ldflags "-w -s \
Expand Down
25 changes: 22 additions & 3 deletions fakeserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -43,10 +44,28 @@ func Start() {
AllowCredentials: true,
AllowedHeaders: []string{"authorization"},
AllowOriginFunc: func(origin string) bool {
dotTest := strings.HasSuffix(origin, ".test")
localhost := origin == "localhost"
allowedOrigins, ok := os.LookupEnv("TESTTRACK_ALLOWED_ORIGINS")
if ok {
for _, allowedOrigin := range strings.Split(allowedOrigins, ",") {
allowedOrigin = strings.Trim(allowedOrigin, " ")
if strings.HasSuffix(origin, allowedOrigin) {
return true
}
}
} else {
// .test cannot be registered so we allow it by default
if strings.HasSuffix(origin, ".test") {
return true
}
}
if origin == "localhost" {
return true
}
ip := net.ParseIP(origin)
return dotTest || localhost || (ip != nil && ip.IsLoopback())
if ip != nil && ip.IsLoopback() {
return true
}
return false
},
}).Handler(r)))
}
Expand Down

0 comments on commit d05643d

Please sign in to comment.