Skip to content

Commit a887af7

Browse files
committed
Don't use securejoin(templateDir, templateName)
The final filename might be a symlink to somewhere outside `templateDir` (homebrew). Instead make sure that `templateName` is not using `../` path references. Signed-off-by: Jan Dubois <[email protected]>
1 parent 353be89 commit a887af7

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

cmd/limactl/start.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/containerd/containerd/identifiers"
15+
securejoin "github.com/cyphar/filepath-securejoin"
1516
"github.com/lima-vm/lima/cmd/limactl/editflags"
1617
"github.com/lima-vm/lima/pkg/editutil"
1718
"github.com/lima-vm/lima/pkg/instance"
@@ -118,8 +119,10 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
118119
return nil, err
119120
}
120121
if isTemplateURL, templateURL := limatmpl.SeemsTemplateURL(arg); isTemplateURL {
121-
// No need to use SecureJoin here. https://github.com/lima-vm/lima/pull/805#discussion_r853411702
122-
templateName := filepath.Join(templateURL.Host, templateURL.Path)
122+
templateName, err := securejoin.SecureJoin(templateURL.Host, templateURL.Path)
123+
if err != nil {
124+
return nil, err
125+
}
123126
switch templateName {
124127
case "experimental/vz":
125128
logrus.Warn("template://experimental/vz was merged into the default template in Lima v1.0. See also <https://lima-vm.io/docs/config/vmtype/>.")

pkg/limatmpl/locator.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"unicode"
1919

2020
"github.com/containerd/containerd/identifiers"
21+
securejoin "github.com/cyphar/filepath-securejoin"
2122
"github.com/lima-vm/lima/pkg/ioutilx"
2223
"github.com/lima-vm/lima/pkg/limayaml"
2324
"github.com/lima-vm/lima/pkg/templatestore"
@@ -40,8 +41,10 @@ func Read(ctx context.Context, name, locator string) (*Template, error) {
4041
isTemplateURL, templateURL := SeemsTemplateURL(locator)
4142
switch {
4243
case isTemplateURL:
43-
// No need to use SecureJoin here. https://github.com/lima-vm/lima/pull/805#discussion_r853411702
44-
templateName := filepath.Join(templateURL.Host, templateURL.Path)
44+
templateName, err := securejoin.SecureJoin(templateURL.Host, templateURL.Path)
45+
if err != nil {
46+
return nil, err
47+
}
4548
logrus.Debugf("interpreting argument %q as a template name %q", locator, templateName)
4649
if tmpl.Name == "" {
4750
// e.g., templateName = "deprecated/centos-7.yaml" , tmpl.Name = "centos-7"

pkg/templatestore/templatestore.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"strings"
1515
"unicode"
1616

17-
securejoin "github.com/cyphar/filepath-securejoin"
1817
"github.com/lima-vm/lima/pkg/store/dirnames"
1918
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
2019
)
@@ -54,10 +53,7 @@ func Read(name string) ([]byte, error) {
5453
name += ".yaml"
5554
}
5655
for _, templatesDir := range paths {
57-
filePath, err := securejoin.SecureJoin(templatesDir, name)
58-
if err != nil {
59-
return nil, err
60-
}
56+
filePath := filepath.Join(templatesDir, name)
6157
if b, err := os.ReadFile(filePath); !errors.Is(err, os.ErrNotExist) {
6258
return b, err
6359
}

0 commit comments

Comments
 (0)