Skip to content

Commit 2846027

Browse files
committed
machine init: validate machine name and username
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 <pholzing@redhat.com>
1 parent 30a18fc commit 2846027

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

cmd/podman/machine/init.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/containers/common/pkg/completion"
1010
"github.com/containers/podman/v5/cmd/podman/registry"
11+
ldefine "github.com/containers/podman/v5/libpod/define"
1112
"github.com/containers/podman/v5/libpod/events"
1213
"github.com/containers/podman/v5/pkg/machine"
1314
"github.com/containers/podman/v5/pkg/machine/define"
@@ -136,13 +137,21 @@ func initMachine(cmd *cobra.Command, args []string) error {
136137
return fmt.Errorf("machine name %q must be %d characters or less", args[0], maxMachineNameSize)
137138
}
138139
initOpts.Name = args[0]
140+
141+
if !ldefine.NameRegex.MatchString(initOpts.Name) {
142+
return fmt.Errorf("invalid name %q: %w", initOpts.Name, ldefine.RegexError)
143+
}
139144
}
140145

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

151+
if !ldefine.NameRegex.MatchString(initOpts.Username) {
152+
return fmt.Errorf("invalid username %q: %w", initOpts.Username, ldefine.RegexError)
153+
}
154+
146155
// Check if machine already exists
147156
_, exists, err := shim.VMExists(initOpts.Name, []vmconfigs.VMProvider{provider})
148157
if err != nil {

pkg/machine/e2e/init_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,19 @@ var _ = Describe("podman machine init", func() {
6666
Expect(badInit).To(Exit(125))
6767
Expect(badInit.errorToString()).To(ContainSubstring(want))
6868

69+
invalidName := "ab/cd"
70+
session, err = mb.setName(invalidName).setCmd(&i).run()
71+
Expect(err).ToNot(HaveOccurred())
72+
Expect(session).To(Exit(125))
73+
Expect(session.errorToString()).To(ContainSubstring(`invalid name "ab/cd": names must match [a-zA-Z0-9][a-zA-Z0-9_.-]*: invalid argument`))
74+
75+
i.username = "-/a"
76+
session, err = mb.setName("").setCmd(&i).run()
77+
Expect(err).ToNot(HaveOccurred())
78+
Expect(session).To(Exit(125))
79+
Expect(session.errorToString()).To(ContainSubstring(`invalid username "-/a": names must match [a-zA-Z0-9][a-zA-Z0-9_.-]*: invalid argument`))
6980
})
81+
7082
It("simple init", func() {
7183
i := new(initMachine)
7284
session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run()

0 commit comments

Comments
 (0)