@@ -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)
@@ -42,7 +41,16 @@ func templatesPaths() ([]string, error) {
4241 }, nil
4342}
4443
44+ // Read searches for template `name` in all template directories and returns the
45+ // contents of the first one found. Template names cannot contain the substring ".."
46+ // to make sure they don't reference files outside the template directories. We are
47+ // not using securejoin.SecureJoin because the actual template may be a symlink to a
48+ // directory elsewhere (e.g. when installed by Homebrew).
4549func Read (name string ) ([]byte , error ) {
50+ doubleDot := ".."
51+ if strings .Contains (name , doubleDot ) {
52+ return nil , fmt .Errorf ("template name %q must not contain %q" , name , doubleDot )
53+ }
4654 paths , err := templatesPaths ()
4755 if err != nil {
4856 return nil , err
@@ -54,10 +62,8 @@ func Read(name string) ([]byte, error) {
5462 name += ".yaml"
5563 }
5664 for _ , templatesDir := range paths {
57- filePath , err := securejoin .SecureJoin (templatesDir , name )
58- if err != nil {
59- return nil , err
60- }
65+ // Normalize filePath for error messages because template names always use forward slashes
66+ filePath := filepath .Clean (filepath .Join (templatesDir , name ))
6167 if b , err := os .ReadFile (filePath ); ! errors .Is (err , os .ErrNotExist ) {
6268 return b , err
6369 }
@@ -67,6 +73,10 @@ func Read(name string) ([]byte, error) {
6773
6874const Default = "default"
6975
76+ // Templates returns a list of Template structures containing the Name and Location for each template.
77+ // It searches all template directories, but only the first template of a given name is recorded.
78+ // Only non-hidden files with a ".yaml" file extension are considered templates.
79+ // The final result is sorted alphabetically by template name.
7080func Templates () ([]Template , error ) {
7181 paths , err := templatesPaths ()
7282 if err != nil {
0 commit comments