Skip to content

Commit

Permalink
remove trailing slash while validating origin (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJenicek authored Feb 21, 2024
1 parent 530afc3 commit 922ba22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,21 @@ func (t *AccessKey) ValidateOrigin(rawOrigin string) bool {
return true
}

origin := strings.ToLower(rawOrigin)
origin := normalizeOrigin(rawOrigin)
for _, o := range t.AllowedOrigins {
if matchDomain(origin, o) {
if matchDomain(origin, normalizeOrigin(o)) {
return true
}
}
return false
}

func normalizeOrigin(rawOrigin string) string {
origin := strings.ToLower(rawOrigin)
origin = strings.TrimRight(origin, "/")
return origin
}

func (t *AccessKey) ValidateService(service *Service) bool {
if len(t.AllowedServices) == 0 {
return true
Expand Down
5 changes: 3 additions & 2 deletions proto/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ func TestAccessKeyValidateOrigin(t *testing.T) {

t.Run("allowed origins", func(t *testing.T) {
tk := &proto.AccessKey{
AllowedOrigins: []string{"http://localhost:8080", "http://localhost:8081"},
AllowedOrigins: []string{"http://localhost:8080", "http://localhost:8081", "http://localhost:8082/"},
}
assert.True(t, tk.ValidateOrigin("http://localhost:8080"))
assert.True(t, tk.ValidateOrigin("http://localhost:8081"))
assert.False(t, tk.ValidateOrigin("http://localhost:8082"))
assert.True(t, tk.ValidateOrigin("http://localhost:8082"))
assert.False(t, tk.ValidateOrigin("http://localhost:8083"))
})

t.Run("match any", func(t *testing.T) {
Expand Down

0 comments on commit 922ba22

Please sign in to comment.