Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=1.6.0
VERSION=1.6.1
BUILD=`git rev-parse HEAD`

LDFLAGS=-ldflags "-w -s \
Expand Down
19 changes: 13 additions & 6 deletions fakeserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -50,13 +51,19 @@ func createCors() *cors.Cors {
return true
}
}
if origin == "localhost" {
return true
}
ip := net.ParseIP(origin)
if ip != nil && ip.IsLoopback() {
return true

if parsed, err := url.Parse(origin); err == nil {
hostname := parsed.Hostname()

if hostname == "localhost" {
return true
}

if ip := net.ParseIP(hostname); ip != nil && ip.IsLoopback() {
return true
}
}

return false
},
})
Expand Down
26 changes: 26 additions & 0 deletions fakeserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,32 @@ func TestCors(t *testing.T) {
require.Equal(t, "http://www.allowed.com", w.HeaderMap.Get("Access-Control-Allow-Origin"))
})

t.Run("it passes cors from localhost", func(t *testing.T) {
w := httptest.NewRecorder()
h := createHandler()

request := httptest.NewRequest("GET", "/api/v2/split_registry", nil)
request.Header.Add("Origin", "http://localhost:3000")

h.ServeHTTP(w, request)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, "http://localhost:3000", w.HeaderMap.Get("Access-Control-Allow-Origin"))
})

t.Run("it passes cors from loopback ip", func(t *testing.T) {
w := httptest.NewRecorder()
h := createHandler()

request := httptest.NewRequest("GET", "/api/v2/split_registry", nil)
request.Header.Add("Origin", "http://127.0.0.1:3000")

h.ServeHTTP(w, request)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, "http://127.0.0.1:3000", w.HeaderMap.Get("Access-Control-Allow-Origin"))
})

os.Unsetenv("TESTTRACK_ALLOWED_ORIGINS")
}

Expand Down