4
4
"errors"
5
5
"fmt"
6
6
"io"
7
+ "io/fs"
7
8
"net/http"
8
9
"net/url"
9
10
"os"
@@ -14,6 +15,7 @@ import (
14
15
15
16
"github.com/AlecAivazis/survey/v2"
16
17
"github.com/containerd/containerd/identifiers"
18
+ securejoin "github.com/cyphar/filepath-securejoin"
17
19
"github.com/lima-vm/lima/pkg/limayaml"
18
20
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
19
21
"github.com/lima-vm/lima/pkg/osutil"
@@ -63,10 +65,10 @@ func readTemplate(name string) ([]byte, error) {
63
65
if err != nil {
64
66
return nil , err
65
67
}
66
- if strings .Contains (name , string (os .PathSeparator )) {
67
- return nil , fmt .Errorf ("invalid template name %q" , name )
68
+ defaultYAMLPath , err := securejoin .SecureJoin (filepath .Join (dir , "examples" ), name + ".yaml" )
69
+ if err != nil {
70
+ return nil , err
68
71
}
69
- defaultYAMLPath := filepath .Join (dir , "examples" , name + ".yaml" )
70
72
return os .ReadFile (defaultYAMLPath )
71
73
}
72
74
@@ -91,10 +93,12 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
91
93
const yBytesLimit = 4 * 1024 * 1024 // 4MiB
92
94
93
95
if ok , u := argSeemsTemplateURL (arg ); ok {
94
- templateName := u .Host
96
+ // No need to use SecureJoin here. https://github.com/lima-vm/lima/pull/805#discussion_r853411702
97
+ templateName := filepath .Join (u .Host , u .Path )
95
98
logrus .Debugf ("interpreting argument %q as a template name %q" , arg , templateName )
96
99
if st .instName == "" {
97
- st .instName = templateName
100
+ // e.g., templateName = "deprecated/centos-7" , st.instName = "centos-7"
101
+ st .instName = filepath .Base (templateName )
98
102
}
99
103
st .yBytes , err = readTemplate (templateName )
100
104
if err != nil {
@@ -340,21 +344,26 @@ func listTemplateYAMLs() ([]TemplateYAML, error) {
340
344
return nil , err
341
345
}
342
346
examplesDir := filepath .Join (usrlocalsharelimaDir , "examples" )
343
- glob := filepath .Join (examplesDir , "*.yaml" )
344
- globbed , err := filepath .Glob (glob )
345
- if err != nil {
346
- return nil , err
347
- }
347
+
348
348
var res []TemplateYAML
349
- for _ , f := range globbed {
350
- base := filepath .Base (f )
351
- if strings .HasPrefix (base , "." ) {
352
- continue
349
+ walkDirFn := func (p string , d fs.DirEntry , err error ) error {
350
+ if err != nil {
351
+ return err
352
+ }
353
+ base := filepath .Base (p )
354
+ if strings .HasPrefix (base , "." ) || ! strings .HasSuffix (base , ".yaml" ) {
355
+ return nil
356
+ }
357
+ x := TemplateYAML {
358
+ // Name is like "default", "debian", "deprecated/centos-7", ...
359
+ Name : strings .TrimSuffix (strings .TrimPrefix (p , examplesDir + "/" ), ".yaml" ),
360
+ Location : p ,
353
361
}
354
- res = append (res , TemplateYAML {
355
- Name : strings .TrimSuffix (filepath .Base (f ), ".yaml" ),
356
- Location : f ,
357
- })
362
+ res = append (res , x )
363
+ return nil
364
+ }
365
+ if err = filepath .WalkDir (examplesDir , walkDirFn ); err != nil {
366
+ return nil , err
358
367
}
359
368
return res , nil
360
369
}
0 commit comments