Skip to content

Commit

Permalink
add validation rule to reject input with relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkpc138 committed Jan 8, 2025
1 parent 31d45f6 commit 4e82494
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/operator/api/v1/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"net/url"
"path/filepath"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -54,6 +55,10 @@ func ValidateTemplateString(templateStr string) error {
return errors.New("mismatch between '{{' and '}}'")
}

if !filepath.IsAbs(templateStr) {
return errors.New("the template should be an absolute path (starting with `/`)")
}

tmpl, err := getTemplate(fmt.Sprintf("validate_%s", templateStr), PathElement{}).Parse(templateStr)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/operator/api/v1/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestInvalidPath(t *testing.T) {
"/{.SourcePath}}", // invalid brace
"/{{.TimeLayout \"2006-01\"}}", // invalid function usage
"/{{TimeLayout}}", // invalid function usage
"{{.Container}}/{{.Pod}}", // invalid relative path
}

for _, tmpl := range templates {
Expand Down

0 comments on commit 4e82494

Please sign in to comment.