Skip to content

Commit

Permalink
Validate inventory path before save
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Mar 8, 2018
1 parent de7cb34 commit a63fd9d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion api/projects/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/castawaylabs/mulekick"
"github.com/gorilla/context"
"github.com/masterminds/squirrel"
"path/filepath"
"strings"
)

func InventoryMiddleware(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -123,6 +125,19 @@ func AddInventory(w http.ResponseWriter, r *http.Request) {
mulekick.WriteJSON(w, http.StatusCreated, inv)
}

func isValidInventoryPath(path string) bool {

This comment has been minimized.

Copy link
@twhiston

twhiston Mar 8, 2018

Contributor

can you add a test for this function please?

if currentPath, err := filepath.Abs("./"); err != nil {
return false
} else if absPath, err := filepath.Abs(path); err != nil {
return false
} else if relPath, err := filepath.Rel(currentPath, absPath); err != nil {
return false
} else {
ret := !strings.HasPrefix(relPath, "..")
return ret
}
}

func UpdateInventory(w http.ResponseWriter, r *http.Request) {
oldInventory := context.Get(r, "inventory").(db.Inventory)

Expand All @@ -139,7 +154,12 @@ func UpdateInventory(w http.ResponseWriter, r *http.Request) {
}

switch inventory.Type {
case "static", "file":
case "static":
break
case "file":
if !isValidInventoryPath(inventory.Inventory) {
panic("Invalid inventory path")
}
break
default:
w.WriteHeader(http.StatusBadRequest)
Expand Down

0 comments on commit a63fd9d

Please sign in to comment.