Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/limatmpl/abs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path"
"path/filepath"
"runtime"
"slices"
"strings"

"github.com/lima-vm/lima/v2/pkg/localpathutil"
Expand Down Expand Up @@ -126,7 +127,7 @@ func absPath(locator, basePath string) (string, error) {
return "", errors.New("basePath is empty")
case basePath == "-":
return "", errors.New("can't use relative paths when reading template from STDIN")
case strings.Contains(locator, "../"):
case containsParentDir(locator):
return "", fmt.Errorf("relative locator path %q must not contain '../' segments", locator)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a good improvement. Please also update the error message to match:

Suggested change
return "", fmt.Errorf("relative locator path %q must not contain '../' segments", locator)
return "", fmt.Errorf("relative locator path %q must not contain '..' path segments", locator)

case volumeLen != 0:
return "", fmt.Errorf("relative locator path %q must not include a volume name", locator)
Expand All @@ -146,3 +147,9 @@ func absPath(locator, basePath string) (string, error) {
}
return withVolume(locator)
}

func containsParentDir(locator string) bool {
return slices.Contains(strings.FieldsFunc(locator, func(r rune) bool {
return r == '/' || (runtime.GOOS == "windows" && r == '\\')
}), "..")
}
5 changes: 5 additions & 0 deletions pkg/limatmpl/abs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ func TestAbsPath(t *testing.T) {
assert.ErrorContains(t, err, "'../'")
})

t.Run("Relative parent directory locator must be underneath the basePath", func(t *testing.T) {
_, err = absPath("..", volume+"/root")
assert.ErrorContains(t, err, "'../'")
})

t.Run("locator must not be empty", func(t *testing.T) {
_, err = absPath("", "foo")
assert.ErrorContains(t, err, "locator is empty")
Expand Down
Loading