Validate url_pattern format in control API append operation

Closes: #38

Co-authored-by: César Gallego Rodríguez <cesar.gallego.next@bbva.com>
This commit is contained in:
pancho horrillo
2019-10-24 14:10:58 +02:00
parent 84a71815e4
commit b0c16ca684
2 changed files with 57 additions and 0 deletions
+10
View File
@@ -70,6 +70,10 @@ func listRoutes(res http.ResponseWriter, req *http.Request) {
var funcAdd func(model.Route) model.Route = user.Routes.Append
var idGenerator = uuid.NewUUID
var pathValidator func(string) error = func(path string) error {
return mux.NewRouter().NewRoute().BuildOnly().Path(path).GetError()
}
func addRoute(res http.ResponseWriter, req *http.Request) {
var route model.Route
@@ -88,6 +92,12 @@ func addRoute(res http.ResponseWriter, req *http.Request) {
return
}
err = pathValidator(route.Pattern)
if err != nil {
res.WriteHeader(http.StatusUnprocessableEntity)
return
}
id, err := idGenerator()
if err != nil {
res.WriteHeader(http.StatusInternalServerError)