Skip to content

Commit 2676a15

Browse files
committed
start: check YAML file size
Signed-off-by: Akihiro Suda <[email protected]>
1 parent f56f620 commit 2676a15

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

cmd/limactl/start.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/AlecAivazis/survey/v2"
1717
"github.com/containerd/containerd/identifiers"
1818
"github.com/lima-vm/lima/pkg/limayaml"
19-
"github.com/lima-vm/lima/pkg/networks/reconcile"
19+
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
2020
"github.com/lima-vm/lima/pkg/osutil"
2121
"github.com/lima-vm/lima/pkg/start"
2222
"github.com/lima-vm/lima/pkg/store"
@@ -53,6 +53,8 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
5353
err error
5454
)
5555

56+
const yBytesLimit = 4 * 1024 * 1024 // 4MiB
57+
5658
if argSeemsHTTPURL(arg) {
5759
instName, err = instNameFromHTTPURL(arg)
5860
if err != nil {
@@ -64,7 +66,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
6466
return nil, err
6567
}
6668
defer resp.Body.Close()
67-
yBytes, err = io.ReadAll(resp.Body)
69+
yBytes, err = readAtMaximum(resp.Body, yBytesLimit)
6870
if err != nil {
6971
return nil, err
7072
}
@@ -74,7 +76,12 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
7476
return nil, err
7577
}
7678
logrus.Debugf("interpreting argument %q as a file path for instance %q", arg, instName)
77-
yBytes, err = os.ReadFile(arg)
79+
r, err := os.Open(arg)
80+
if err != nil {
81+
return nil, err
82+
}
83+
defer r.Close()
84+
yBytes, err = readAtMaximum(r, yBytesLimit)
7885
if err != nil {
7986
return nil, err
8087
}
@@ -299,3 +306,17 @@ func startBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]
299306
instances, _ := bashCompleteInstanceNames(cmd)
300307
return instances, cobra.ShellCompDirectiveDefault
301308
}
309+
310+
func readAtMaximum(r io.Reader, n int64) ([]byte, error) {
311+
lr := &io.LimitedReader{
312+
R: r,
313+
N: n,
314+
}
315+
b, err := io.ReadAll(lr)
316+
if err != nil {
317+
if errors.Is(err, io.EOF) && lr.N <= 0 {
318+
err = fmt.Errorf("exceeded the limit (%d bytes): %w", n, err)
319+
}
320+
}
321+
return b, err
322+
}

0 commit comments

Comments
 (0)