Skip to content

Commit

Permalink
machine init: validate machine name and username
Browse files Browse the repository at this point in the history
Validate the names with our name regex that we also use for
containers/pods. While we technically do not need to be that strict, I
think it makes sense to match containers. The most important bit of this
validation is that we exclude the use of / and \ which breaks all our
file paths as we just use this in the name an when machine write the
file it ends up being in a subdir which breaks the reading side. Also
other special characters could cause trouble for the URL parsing in the
machine connection URL.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Feb 16, 2024
1 parent 30a18fc commit 2846027
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/podman/machine/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v5/cmd/podman/registry"
ldefine "github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/libpod/events"
"github.com/containers/podman/v5/pkg/machine"
"github.com/containers/podman/v5/pkg/machine/define"
Expand Down Expand Up @@ -136,13 +137,21 @@ func initMachine(cmd *cobra.Command, args []string) error {
return fmt.Errorf("machine name %q must be %d characters or less", args[0], maxMachineNameSize)
}
initOpts.Name = args[0]

if !ldefine.NameRegex.MatchString(initOpts.Name) {
return fmt.Errorf("invalid name %q: %w", initOpts.Name, ldefine.RegexError)
}
}

// The vmtype names need to be reserved and cannot be used for podman machine names
if _, err := define.ParseVMType(initOpts.Name, define.UnknownVirt); err == nil {
return fmt.Errorf("cannot use %q for a machine name", initOpts.Name)
}

if !ldefine.NameRegex.MatchString(initOpts.Username) {
return fmt.Errorf("invalid username %q: %w", initOpts.Username, ldefine.RegexError)
}

// Check if machine already exists
_, exists, err := shim.VMExists(initOpts.Name, []vmconfigs.VMProvider{provider})
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/machine/e2e/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ var _ = Describe("podman machine init", func() {
Expect(badInit).To(Exit(125))
Expect(badInit.errorToString()).To(ContainSubstring(want))

invalidName := "ab/cd"
session, err = mb.setName(invalidName).setCmd(&i).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(125))
Expect(session.errorToString()).To(ContainSubstring(`invalid name "ab/cd": names must match [a-zA-Z0-9][a-zA-Z0-9_.-]*: invalid argument`))

i.username = "-/a"
session, err = mb.setName("").setCmd(&i).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(125))
Expect(session.errorToString()).To(ContainSubstring(`invalid username "-/a": names must match [a-zA-Z0-9][a-zA-Z0-9_.-]*: invalid argument`))
})

It("simple init", func() {
i := new(initMachine)
session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run()
Expand Down

0 comments on commit 2846027

Please sign in to comment.