Skip to content

Commit ca747c5

Browse files
committed
fix: reject parent directory template locators
Signed-off-by: immanuwell <pchpr.00@list.ru>
1 parent ee89718 commit ca747c5

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

pkg/limatmpl/abs.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path"
1212
"path/filepath"
1313
"runtime"
14+
"slices"
1415
"strings"
1516

1617
"github.com/lima-vm/lima/v2/pkg/localpathutil"
@@ -126,7 +127,7 @@ func absPath(locator, basePath string) (string, error) {
126127
return "", errors.New("basePath is empty")
127128
case basePath == "-":
128129
return "", errors.New("can't use relative paths when reading template from STDIN")
129-
case strings.Contains(locator, "../"):
130+
case containsParentDir(locator):
130131
return "", fmt.Errorf("relative locator path %q must not contain '../' segments", locator)
131132
case volumeLen != 0:
132133
return "", fmt.Errorf("relative locator path %q must not include a volume name", locator)
@@ -146,3 +147,9 @@ func absPath(locator, basePath string) (string, error) {
146147
}
147148
return withVolume(locator)
148149
}
150+
151+
func containsParentDir(locator string) bool {
152+
return slices.Contains(strings.FieldsFunc(locator, func(r rune) bool {
153+
return r == '/' || (runtime.GOOS == "windows" && r == '\\')
154+
}), "..")
155+
}

pkg/limatmpl/abs_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ func TestAbsPath(t *testing.T) {
249249
assert.ErrorContains(t, err, "'../'")
250250
})
251251

252+
t.Run("Relative parent directory locator must be underneath the basePath", func(t *testing.T) {
253+
_, err = absPath("..", volume+"/root")
254+
assert.ErrorContains(t, err, "'../'")
255+
})
256+
252257
t.Run("locator must not be empty", func(t *testing.T) {
253258
_, err = absPath("", "foo")
254259
assert.ErrorContains(t, err, "locator is empty")

0 commit comments

Comments
 (0)