Skip to content

Commit

Permalink
dont modify regex, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scarlettperry committed Jul 18, 2023
1 parent 8e26f7b commit 861a3d0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
19 changes: 7 additions & 12 deletions backend/module/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,17 @@ func New(cfg *any.Any, log *zap.Logger, scope tally.Scope) (module.Module, error
return nil, err
}

// Validate that each services constructs a parsable URL
for _, service := range config.Services {
for _, ar := range service.AllowedRequests {
var path string
switch t := ar.PathType.(type) {
case *proxyv1cfg.AllowRequest_Path:
path = t.Path
case *proxyv1cfg.AllowRequest_PathRegex:
path = t.PathRegex
// For exact path type, validate that each services constructs a parsable URL
_, err := url.Parse(fmt.Sprintf("%s%s", service.Host, t.Path))
if err != nil {
return nil, fmt.Errorf("unable to parse the configured URL for service [%s]", service.Name)
}
default:
return nil, fmt.Errorf("path type not supported: %s", t)
}
_, err := url.Parse(fmt.Sprintf("%s%s", service.Host, path))
if err != nil {
return nil, fmt.Errorf("unable to parse the configured URL for service [%s]", service.Name)
break
}
}
}
Expand Down Expand Up @@ -225,8 +221,7 @@ func isAllowedRequest(services []*proxyv1cfg.Service, service, path, method stri
return true, nil
}
case *proxyv1cfg.AllowRequest_PathRegex:
// MatchString reports whether the string contains any match of the regular expression so we add the ‘^’ and ‘$’ to ensure the regex matches the full string
r := regexp.MustCompile(fmt.Sprintf("^%s$", t.PathRegex))
r := regexp.MustCompile(t.PathRegex)
if r.MatchString(path) {
return true, nil
}
Expand Down
35 changes: 34 additions & 1 deletion backend/module/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func generateServicesConfig(host string) []*proxyv1cfg.Service {
AllowedRequests: []*proxyv1cfg.AllowRequest{
{PathType: &proxyv1cfg.AllowRequest_Path{Path: "/meow"}, Method: "GET"},
{PathType: &proxyv1cfg.AllowRequest_Path{Path: "/nom"}, Method: "POST"},
{PathType: &proxyv1cfg.AllowRequest_PathRegex{PathRegex: `^/meow/\w+/\w+$`}, Method: "GET"},
},
},
}
Expand Down Expand Up @@ -270,7 +271,7 @@ func TestIsAllowedRequest(t *testing.T) {
shouldError: false,
},
{
id: "Regex matches request path string",
id: "Path string matches regex",
service: "cat",
path: "/cat/bengal/1",
method: "POST",
Expand All @@ -293,6 +294,38 @@ func TestIsAllowedRequest(t *testing.T) {
expect: false,
shouldError: false,
},
{
id: "Regex does not have a rule for end of string",
service: "cat",
path: "/cat/bengal/1/comments",
method: "POST",
expect: true,
shouldError: false,
},
{
id: "Path string matches regex",
service: "meow",
path: "/meow/foo/sound",
method: "POST",
expect: true,
shouldError: false,
},
{
id: "Path string does not match regex last character",
service: "meow",
path: "/meow/foo/sound/play",
method: "POST",
expect: false,
shouldError: false,
},
{
id: "Path string does not match regex first character",
service: "meow",
path: "animals/meow/foo/sound/play",
method: "POST",
expect: false,
shouldError: false,
},
}

services := generateServicesConfig("http://test.test")
Expand Down

0 comments on commit 861a3d0

Please sign in to comment.