Skip to content

Commit cf88ee2

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

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

pkg/limatmpl/abs.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func absPath(locator, basePath string) (string, error) {
126126
return "", errors.New("basePath is empty")
127127
case basePath == "-":
128128
return "", errors.New("can't use relative paths when reading template from STDIN")
129-
case strings.Contains(locator, "../"):
129+
case containsParentDir(locator):
130130
return "", fmt.Errorf("relative locator path %q must not contain '../' segments", locator)
131131
case volumeLen != 0:
132132
return "", fmt.Errorf("relative locator path %q must not include a volume name", locator)
@@ -146,3 +146,14 @@ func absPath(locator, basePath string) (string, error) {
146146
}
147147
return withVolume(locator)
148148
}
149+
150+
func containsParentDir(locator string) bool {
151+
for _, segment := range strings.FieldsFunc(locator, func(r rune) bool {
152+
return r == '/' || (runtime.GOOS == "windows" && r == '\\')
153+
}) {
154+
if segment == ".." {
155+
return true
156+
}
157+
}
158+
return false
159+
}

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)