Skip to content

Commit

Permalink
seed: validate UC20 seed system label
Browse files Browse the repository at this point in the history
When opening the UC20 seed we add the label to the path without any validation.
The patch adds basic validation of the system labels.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
  • Loading branch information
bboozzoo committed Mar 26, 2020
1 parent 1ca48cd commit 33d3066
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions seed/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ type InternalSnap16 = internal.Snap16

var (
LoadAssertions = loadAssertions

ValidateUC20SeedSystemLabel = validateUC20SeedSystemLabel
)
3 changes: 3 additions & 0 deletions seed/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ type EssentialMetaLoaderSeed interface {
// label if not empty is used to identify a Core 20 recovery system seed.
func Open(seedDir, label string) (Seed, error) {
if label != "" {
if err := validateUC20SeedSystemLabel(label); err != nil {
return nil, err
}
return &seed20{systemDir: filepath.Join(seedDir, "systems", label)}, nil
}
// TODO: consider if systems is present to open the Core 20
Expand Down
16 changes: 16 additions & 0 deletions seed/seed20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package seed_test

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -1800,3 +1801,18 @@ func (s *seed20Suite) TestLoadMetaCore20LocalAssertedSnaps(c *C) {
},
})
}

func (s *seed20Suite) TestOpenInvalidLabel(c *C) {
invalid := []string{
// empty string not included, as it's not a UC20 seed
"/bin",
"../../bin/bar",
":invalid:",
"日本語",
}
for _, label := range invalid {
seed20, err := seed.Open(s.SeedDir, label)
c.Assert(err, ErrorMatches, fmt.Sprintf("invalid seed system label: %q", label))
c.Assert(seed20, IsNil)
}
}
12 changes: 12 additions & 0 deletions seed/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"bytes"
"fmt"
"path/filepath"
"regexp"

"github.com/snapcore/snapd/seed/internal"
"github.com/snapcore/snapd/snap"
Expand Down Expand Up @@ -78,3 +79,14 @@ func ValidateFromYaml(seedYamlFile string) error {

return nil
}

var validSeedSystemLabel = regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9-]+$")

// validateSeedSystemLabel checks whether the string is a valid UC20 seed system
// label.
func validateUC20SeedSystemLabel(label string) error {
if !validSeedSystemLabel.MatchString(label) {
return fmt.Errorf("invalid seed system label: %q", label)
}
return nil
}
29 changes: 29 additions & 0 deletions seed/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,32 @@ snaps:
c.Assert(err, ErrorMatches, `cannot validate seed:
- cannot use snap /.*/snaps/some-snap-invalid-yaml_1.snap: invalid snap version: cannot be empty`)
}

func (s *validateSuite) TestValidateSeedSystemLabel(c *C) {
valid := []string{
"20191119",
"foobar",
"MYSYSTEM",
"mySystem",
"my-system",
"brand-system-date-1234",
}
for _, label := range valid {
c.Logf("trying valid label: %q", label)
err := seed.ValidateUC20SeedSystemLabel(label)
c.Check(err, IsNil)
}

invalid := []string{
"",
"/bin",
"../../bin/bar",
":invalid:",
"日本語",
}
for _, label := range invalid {
c.Logf("trying invalid label: %q", label)
err := seed.ValidateUC20SeedSystemLabel(label)
c.Check(err, ErrorMatches, fmt.Sprintf("invalid seed system label: %q", label))
}
}

0 comments on commit 33d3066

Please sign in to comment.