Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/snapshot/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (h Handler) Save(cmd *cobra.Command, args []string) error {
name, _ := cmd.Flags().GetString("name")
description, _ := cmd.Flags().GetString("description")

if err = (&types.SnapshotConfig{Name: name}).Validate(); err != nil {
return err
}
if name != "" {
if _, inspectErr := snapBackend.Inspect(ctx, name); inspectErr == nil {
return fmt.Errorf("snapshot name %q already exists", name)
Expand Down Expand Up @@ -251,6 +254,10 @@ func (h Handler) Import(cmd *cobra.Command, args []string) error {
name, _ := cmd.Flags().GetString("name")
description, _ := cmd.Flags().GetString("description")

if err = (&types.SnapshotConfig{Name: name}).Validate(); err != nil {
return err
}

var r io.Reader
if len(args) > 0 {
f, openErr := os.Open(args[0]) //nolint:gosec
Expand Down
4 changes: 4 additions & 0 deletions snapshot/localfile/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (lf *LocalFile) Import(ctx context.Context, r io.Reader, name, description
cfg.Name = cmp.Or(name, cfg.Name)
cfg.Description = cmp.Or(description, cfg.Description)

if err = cfg.Validate(); err != nil {
return "", err
}

size, sizeErr := utils.DirSize(dataDir)
if sizeErr != nil {
return "", fmt.Errorf("compute data dir size: %w", sizeErr)
Expand Down
3 changes: 3 additions & 0 deletions snapshot/localfile/localfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func (lf *LocalFile) Create(ctx context.Context, cfg *types.SnapshotConfig, stre
if id == "" {
return "", fmt.Errorf("snapshot ID is required (must be set by caller)")
}
if err = cfg.Validate(); err != nil {
return "", err
}

dataDir := lf.conf.SnapshotDataDir(id)
now := time.Now()
Expand Down
13 changes: 12 additions & 1 deletion types/snapshot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import "time"
import (
"fmt"
"time"
)

// SnapshotConfig carries the parameters for creating a snapshot.
// The hypervisor fills ID, Image, ImageBlobIDs, Hypervisor, and resource fields; the CLI adds Name and Description.
Expand All @@ -15,6 +18,14 @@ type SnapshotConfig struct {
NICs int `json:"nics,omitempty"`
}

// Validate checks SnapshotConfig caller-controlled fields. Empty Name is allowed (name is optional).
func (cfg *SnapshotConfig) Validate() error {
if cfg.Name != "" && !validName.MatchString(cfg.Name) {
return fmt.Errorf("snapshot name %q is invalid: must match %s (max 63 chars)", cfg.Name, validName.String())
}
return nil
}

// Snapshot is the public record for a snapshot.
type Snapshot struct {
SnapshotConfig
Expand Down
32 changes: 32 additions & 0 deletions types/snapshot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package types

import (
"strings"
"testing"
)

func TestSnapshotConfig_Validate(t *testing.T) {
cases := []struct {
name string
cfgName string
wantErr bool
}{
{"empty allowed", "", false},
{"simple", "my-snap", false},
{"with dot underscore", "my.snap_v1", false},
{"max 63", strings.Repeat("a", 63), false},
{"over 63", strings.Repeat("a", 64), true},
{"leading hyphen", "-bad", true},
{"space", "bad name", true},
{"slash", "bad/name", true},
{"control char", "bad\x00name", true},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
err := (&SnapshotConfig{Name: tt.cfgName}).Validate()
if (err != nil) != tt.wantErr {
t.Errorf("Validate(%q): err=%v, wantErr=%v", tt.cfgName, err, tt.wantErr)
}
})
}
}
Loading