diff --git a/cmd/podman/containers/attach.go b/cmd/podman/containers/attach.go index 52adb1fcb719..3b69154d17e8 100644 --- a/cmd/podman/containers/attach.go +++ b/cmd/podman/containers/attach.go @@ -3,6 +3,7 @@ package containers import ( "errors" "os" + "strings" "github.com/containers/podman/v4/cmd/podman/common" "github.com/containers/podman/v4/cmd/podman/registry" @@ -75,7 +76,7 @@ func attach(cmd *cobra.Command, args []string) error { var name string if len(args) > 0 { - name = args[0] + name = strings.TrimPrefix(args[0], "/") } attachOpts.Stdin = os.Stdin if attachOpts.NoStdin { diff --git a/cmd/podman/containers/checkpoint.go b/cmd/podman/containers/checkpoint.go index 4350e5586d3d..b52dca15bcf9 100644 --- a/cmd/podman/containers/checkpoint.go +++ b/cmd/podman/containers/checkpoint.go @@ -87,6 +87,7 @@ func init() { func checkpoint(cmd *cobra.Command, args []string) error { var errs utils.OutputErrors + args = utils.RemoveSlash(args) podmanStart := time.Now() if cmd.Flags().Changed("compress") { if checkpointOptions.Export == "" { diff --git a/cmd/podman/containers/commit.go b/cmd/podman/containers/commit.go index 77f74395ec56..fa0621a4cc55 100644 --- a/cmd/podman/containers/commit.go +++ b/cmd/podman/containers/commit.go @@ -93,7 +93,7 @@ func init() { } func commit(cmd *cobra.Command, args []string) error { - container := args[0] + container := strings.TrimPrefix(args[0], "/") if len(args) == 2 { commitOptions.ImageName = args[1] } diff --git a/cmd/podman/containers/exec.go b/cmd/podman/containers/exec.go index cbd09085234d..1440c9374827 100644 --- a/cmd/podman/containers/exec.go +++ b/cmd/podman/containers/exec.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "strings" "github.com/containers/common/pkg/completion" "github.com/containers/podman/v4/cmd/podman/common" @@ -112,7 +113,7 @@ func exec(_ *cobra.Command, args []string) error { execOpts.Cmd = args if !execOpts.Latest { execOpts.Cmd = args[1:] - nameOrID = args[0] + nameOrID = strings.TrimPrefix(args[0], "/") } // Validate given environment variables execOpts.Envs = make(map[string]string) diff --git a/cmd/podman/containers/exists.go b/cmd/podman/containers/exists.go index 69f9b85cdc62..11d1f35040c6 100644 --- a/cmd/podman/containers/exists.go +++ b/cmd/podman/containers/exists.go @@ -2,6 +2,7 @@ package containers import ( "context" + "strings" "github.com/containers/podman/v4/cmd/podman/common" "github.com/containers/podman/v4/cmd/podman/registry" @@ -41,7 +42,7 @@ func exists(cmd *cobra.Command, args []string) error { options := entities.ContainerExistsOptions{ External: external, } - response, err := registry.ContainerEngine().ContainerExists(context.Background(), args[0], options) + response, err := registry.ContainerEngine().ContainerExists(context.Background(), strings.TrimPrefix(args[0], "/"), options) if err != nil { return err } diff --git a/cmd/podman/containers/export.go b/cmd/podman/containers/export.go index 96f7c8644f9d..c3ddef39bde1 100644 --- a/cmd/podman/containers/export.go +++ b/cmd/podman/containers/export.go @@ -4,6 +4,7 @@ import ( "context" "errors" "os" + "strings" "github.com/containers/common/pkg/completion" "github.com/containers/podman/v4/cmd/podman/common" @@ -87,5 +88,5 @@ func export(cmd *cobra.Command, args []string) error { defer file.Close() exportOpts.Output = file } - return registry.ContainerEngine().ContainerExport(context.Background(), args[0], exportOpts) + return registry.ContainerEngine().ContainerExport(context.Background(), strings.TrimPrefix(args[0], "/"), exportOpts) } diff --git a/cmd/podman/containers/init.go b/cmd/podman/containers/init.go index 8f53dca4512b..d782382cfc7a 100644 --- a/cmd/podman/containers/init.go +++ b/cmd/podman/containers/init.go @@ -69,6 +69,7 @@ func init() { func initContainer(cmd *cobra.Command, args []string) error { var errs utils.OutputErrors + args = utils.RemoveSlash(args) report, err := registry.ContainerEngine().ContainerInit(registry.GetContext(), args, initOptions) if err != nil { return err diff --git a/cmd/podman/containers/kill.go b/cmd/podman/containers/kill.go index 3c9bf6b719d7..49983f42289c 100644 --- a/cmd/podman/containers/kill.go +++ b/cmd/podman/containers/kill.go @@ -86,6 +86,7 @@ func kill(_ *cobra.Command, args []string) error { err error errs utils.OutputErrors ) + args = utils.RemoveSlash(args) // Check if the signalString provided by the user is valid // Invalid signals will return err sig, err := signal.ParseSignalNameOrNumber(killOptions.Signal) diff --git a/cmd/podman/containers/logs.go b/cmd/podman/containers/logs.go index e285a24d8e57..206928107303 100644 --- a/cmd/podman/containers/logs.go +++ b/cmd/podman/containers/logs.go @@ -8,6 +8,7 @@ import ( "github.com/containers/common/pkg/completion" "github.com/containers/podman/v4/cmd/podman/common" "github.com/containers/podman/v4/cmd/podman/registry" + "github.com/containers/podman/v4/cmd/podman/utils" "github.com/containers/podman/v4/cmd/podman/validate" "github.com/containers/podman/v4/pkg/domain/entities" "github.com/containers/podman/v4/pkg/util" @@ -122,6 +123,7 @@ func logsFlags(cmd *cobra.Command) { } func logs(_ *cobra.Command, args []string) error { + args = utils.RemoveSlash(args) if logsOptions.SinceRaw != "" { // parse time, error out if something is wrong since, err := util.ParseInputTime(logsOptions.SinceRaw, true) diff --git a/cmd/podman/containers/mount.go b/cmd/podman/containers/mount.go index a1f0b0cf3832..a60cb4e77683 100644 --- a/cmd/podman/containers/mount.go +++ b/cmd/podman/containers/mount.go @@ -85,6 +85,8 @@ func mount(cmd *cobra.Command, args []string) error { if len(args) > 0 && mountOpts.Latest { return errors.New("--latest and containers cannot be used together") } + args = utils.RemoveSlash(args) + reports, err := registry.ContainerEngine().ContainerMount(registry.GetContext(), args, mountOpts) if err != nil { return err diff --git a/cmd/podman/containers/pause.go b/cmd/podman/containers/pause.go index ea5dd3a0cc77..3dd65859580d 100644 --- a/cmd/podman/containers/pause.go +++ b/cmd/podman/containers/pause.go @@ -90,6 +90,7 @@ func pause(cmd *cobra.Command, args []string) error { var ( errs utils.OutputErrors ) + args = utils.RemoveSlash(args) for _, cidFile := range pauseCidFiles { content, err := os.ReadFile(cidFile) diff --git a/cmd/podman/containers/port.go b/cmd/podman/containers/port.go index 74bfdf5c614a..42077214d84d 100644 --- a/cmd/podman/containers/port.go +++ b/cmd/podman/containers/port.go @@ -80,7 +80,7 @@ func port(_ *cobra.Command, args []string) error { return errors.New("you must supply a running container name or id") } if !portOpts.Latest && len(args) >= 1 { - container = args[0] + container = strings.TrimPrefix(args[0], "/") } port := "" if len(args) > 2 { diff --git a/cmd/podman/containers/prune.go b/cmd/podman/containers/prune.go index 100bf52c6624..e90bd500f9ff 100644 --- a/cmd/podman/containers/prune.go +++ b/cmd/podman/containers/prune.go @@ -46,7 +46,7 @@ func init() { _ = pruneCommand.RegisterFlagCompletionFunc(filterFlagName, common.AutocompletePruneFilters) } -func prune(cmd *cobra.Command, args []string) error { +func prune(cmd *cobra.Command, _ []string) error { var ( pruneOptions = entities.ContainerPruneOptions{} err error diff --git a/cmd/podman/containers/rename.go b/cmd/podman/containers/rename.go index d78492ae45c0..78a1608922d7 100644 --- a/cmd/podman/containers/rename.go +++ b/cmd/podman/containers/rename.go @@ -5,6 +5,7 @@ import ( "github.com/containers/podman/v4/cmd/podman/common" "github.com/containers/podman/v4/cmd/podman/registry" + "github.com/containers/podman/v4/cmd/podman/utils" "github.com/containers/podman/v4/pkg/domain/entities" "github.com/spf13/cobra" ) @@ -47,6 +48,7 @@ func rename(cmd *cobra.Command, args []string) error { if len(args) > 2 { return errors.New("must provide at least two arguments to rename") } + args = utils.RemoveSlash(args) renameOpts := entities.ContainerRenameOptions{ NewName: args[1], } diff --git a/cmd/podman/containers/restart.go b/cmd/podman/containers/restart.go index db2759f0b401..b4886575a2da 100644 --- a/cmd/podman/containers/restart.go +++ b/cmd/podman/containers/restart.go @@ -99,6 +99,7 @@ func restart(cmd *cobra.Command, args []string) error { var ( errs utils.OutputErrors ) + args = utils.RemoveSlash(args) if cmd.Flag("time").Changed { restartOpts.Timeout = &restartTimeout diff --git a/cmd/podman/containers/restore.go b/cmd/podman/containers/restore.go index d094ac246986..f399324c00a4 100644 --- a/cmd/podman/containers/restore.go +++ b/cmd/podman/containers/restore.go @@ -96,6 +96,8 @@ func restore(cmd *cobra.Command, args []string) error { e error errs utils.OutputErrors ) + args = utils.RemoveSlash(args) + podmanStart := time.Now() if rootless.IsRootless() { return fmt.Errorf("restoring a container requires root") diff --git a/cmd/podman/containers/rm.go b/cmd/podman/containers/rm.go index 41d209332109..fcd9935fc6a8 100644 --- a/cmd/podman/containers/rm.go +++ b/cmd/podman/containers/rm.go @@ -132,7 +132,7 @@ func rm(cmd *cobra.Command, args []string) error { rmOptions.Depend = true } - return removeContainers(args, rmOptions, true) + return removeContainers(utils.RemoveSlash(args), rmOptions, true) } // removeContainers will remove the specified containers (names or IDs). diff --git a/cmd/podman/containers/runlabel.go b/cmd/podman/containers/runlabel.go index 6410e3f1cd62..00f64ec0b9fa 100644 --- a/cmd/podman/containers/runlabel.go +++ b/cmd/podman/containers/runlabel.go @@ -3,6 +3,7 @@ package containers import ( "context" "os" + "strings" "github.com/containers/common/pkg/auth" "github.com/containers/common/pkg/completion" @@ -95,5 +96,5 @@ func runlabel(cmd *cobra.Command, args []string) error { return err } } - return registry.ContainerEngine().ContainerRunlabel(context.Background(), args[0], args[1], args[2:], runlabelOptions.ContainerRunlabelOptions) + return registry.ContainerEngine().ContainerRunlabel(context.Background(), strings.TrimPrefix(args[0], "/"), args[1], args[2:], runlabelOptions.ContainerRunlabelOptions) } diff --git a/cmd/podman/containers/start.go b/cmd/podman/containers/start.go index 061f0953d3eb..b67a5ae547d1 100644 --- a/cmd/podman/containers/start.go +++ b/cmd/podman/containers/start.go @@ -124,7 +124,7 @@ func start(cmd *cobra.Command, args []string) error { startOptions.Stdout = os.Stdout } - containers := args + containers := utils.RemoveSlash(args) for _, f := range filters { split := strings.SplitN(f, "=", 2) if len(split) < 2 { diff --git a/cmd/podman/containers/stats.go b/cmd/podman/containers/stats.go index f29bbf34c52b..79b549a1d835 100644 --- a/cmd/podman/containers/stats.go +++ b/cmd/podman/containers/stats.go @@ -10,6 +10,7 @@ import ( "github.com/containers/common/pkg/report" "github.com/containers/podman/v4/cmd/podman/common" "github.com/containers/podman/v4/cmd/podman/registry" + putils "github.com/containers/podman/v4/cmd/podman/utils" "github.com/containers/podman/v4/cmd/podman/validate" "github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/pkg/domain/entities" @@ -120,6 +121,7 @@ func stats(cmd *cobra.Command, args []string) error { Stream: !statsOptions.NoStream, Interval: statsOptions.Interval, } + args = putils.RemoveSlash(args) statsChan, err := registry.ContainerEngine().ContainerStats(registry.Context(), args, opts) if err != nil { return err diff --git a/cmd/podman/containers/stop.go b/cmd/podman/containers/stop.go index a1e3a0c464f4..a03f681fbe47 100644 --- a/cmd/podman/containers/stop.go +++ b/cmd/podman/containers/stop.go @@ -101,6 +101,8 @@ func stop(cmd *cobra.Command, args []string) error { var ( errs utils.OutputErrors ) + args = utils.RemoveSlash(args) + if cmd.Flag("time").Changed { stopOptions.Timeout = &stopTimeout } diff --git a/cmd/podman/containers/top.go b/cmd/podman/containers/top.go index 9340acd9a45e..659c74c2687a 100644 --- a/cmd/podman/containers/top.go +++ b/cmd/podman/containers/top.go @@ -94,7 +94,7 @@ func top(cmd *cobra.Command, args []string) error { if topOptions.Latest { topOptions.Descriptors = args } else { - topOptions.NameOrID = args[0] + topOptions.NameOrID = strings.TrimPrefix(args[0], "/") topOptions.Descriptors = args[1:] } diff --git a/cmd/podman/containers/unmount.go b/cmd/podman/containers/unmount.go index 6869de2e2f3c..c16ab7d01bc5 100644 --- a/cmd/podman/containers/unmount.go +++ b/cmd/podman/containers/unmount.go @@ -78,6 +78,7 @@ func init() { func unmount(cmd *cobra.Command, args []string) error { var errs utils.OutputErrors + args = utils.RemoveSlash(args) reports, err := registry.ContainerEngine().ContainerUnmount(registry.GetContext(), args, unmountOpts) if err != nil { return err diff --git a/cmd/podman/containers/unpause.go b/cmd/podman/containers/unpause.go index fea05ee22738..00f22944728b 100644 --- a/cmd/podman/containers/unpause.go +++ b/cmd/podman/containers/unpause.go @@ -91,6 +91,8 @@ func unpause(cmd *cobra.Command, args []string) error { var ( errs utils.OutputErrors ) + args = utils.RemoveSlash(args) + if rootless.IsRootless() && !registry.IsRemote() { cgroupv2, _ := cgroups.IsCgroup2UnifiedMode() if !cgroupv2 { diff --git a/cmd/podman/containers/update.go b/cmd/podman/containers/update.go index 33aa25f8e5eb..5992a8ec724f 100644 --- a/cmd/podman/containers/update.go +++ b/cmd/podman/containers/update.go @@ -3,6 +3,7 @@ package containers import ( "context" "fmt" + "strings" "github.com/containers/podman/v4/cmd/podman/common" "github.com/containers/podman/v4/cmd/podman/registry" @@ -71,7 +72,7 @@ func update(cmd *cobra.Command, args []string) error { } opts := &entities.ContainerUpdateOptions{ - NameOrID: args[0], + NameOrID: strings.TrimPrefix(args[0], "/"), Specgen: s, } rep, err := registry.ContainerEngine().ContainerUpdate(context.Background(), opts) diff --git a/cmd/podman/containers/wait.go b/cmd/podman/containers/wait.go index f34e1a18a487..0b6a5dc5f106 100644 --- a/cmd/podman/containers/wait.go +++ b/cmd/podman/containers/wait.go @@ -80,6 +80,7 @@ func wait(cmd *cobra.Command, args []string) error { err error errs utils.OutputErrors ) + args = utils.RemoveSlash(args) if waitOptions.Interval, err = time.ParseDuration(waitInterval); err != nil { var err1 error if waitOptions.Interval, err1 = time.ParseDuration(waitInterval + "ms"); err1 != nil { diff --git a/cmd/podman/utils/utils.go b/cmd/podman/utils/utils.go index 8063f4309e08..4fa16914cc95 100644 --- a/cmd/podman/utils/utils.go +++ b/cmd/podman/utils/utils.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "strings" "github.com/containers/podman/v4/cmd/podman/registry" "github.com/containers/podman/v4/libpod/define" @@ -139,3 +140,11 @@ func IsCheckpointImage(ctx context.Context, namesOrIDs []string) (bool, error) { } return true, nil } + +func RemoveSlash(input []string) []string { + output := make([]string, 0, len(input)) + for _, in := range input { + output = append(output, strings.TrimPrefix(in, "/")) + } + return output +} diff --git a/libpod/options.go b/libpod/options.go index 2f81d9358a36..373cf5aada4e 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -6,6 +6,7 @@ import ( "net" "os" "path/filepath" + "strings" "syscall" "github.com/containers/buildah/pkg/parse" @@ -776,6 +777,7 @@ func WithName(name string) CtrCreateOption { return define.ErrCtrFinalized } + name = strings.TrimPrefix(name, "/") // Check the name against a regex if !define.NameRegex.MatchString(name) { return define.RegexError diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index f8e74c3df166..f44a01f0f79a 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -118,6 +118,7 @@ func (r *Runtime) RenameContainer(ctx context.Context, ctr *Container, newName s return nil, err } + newName = strings.TrimPrefix(newName, "/") if newName == "" || !define.NameRegex.MatchString(newName) { return nil, define.RegexError } diff --git a/pkg/domain/filters/containers.go b/pkg/domain/filters/containers.go index 67f727fd838a..99b11f048f8f 100644 --- a/pkg/domain/filters/containers.go +++ b/pkg/domain/filters/containers.go @@ -30,7 +30,11 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo case "name": // we only have to match one name return func(c *libpod.Container) bool { - return util.StringMatchRegexSlice(c.Name(), filterValues) + var filters []string + for _, f := range filterValues { + filters = append(filters, strings.ReplaceAll(f, "/", "")) + } + return util.StringMatchRegexSlice(c.Name(), filters) }, nil case "exited": var exitCodes []int32 diff --git a/test/system/030-run.bats b/test/system/030-run.bats index 171008393982..3866a8201890 100644 --- a/test/system/030-run.bats +++ b/test/system/030-run.bats @@ -174,7 +174,7 @@ echo $rand | 0 | $rand run_podman 1 image exists $NONLOCAL_IMAGE # Run a container, without --rm; this should block subsequent --rmi - run_podman run --name keepme $NONLOCAL_IMAGE /bin/true + run_podman run --name /keepme $NONLOCAL_IMAGE /bin/true run_podman image exists $NONLOCAL_IMAGE # Now try running with --rmi : it should succeed, but not remove the image @@ -182,7 +182,7 @@ echo $rand | 0 | $rand run_podman image exists $NONLOCAL_IMAGE # Remove the stray container, and run one more time with --rmi. - run_podman rm keepme + run_podman rm /keepme run_podman run --rmi --rm $NONLOCAL_IMAGE /bin/true run_podman 1 image exists $NONLOCAL_IMAGE } @@ -964,4 +964,14 @@ EOF CONTAINERS_CONF="$containersconf" run_podman 1 run --rm --read-only-tmpfs=false $IMAGE touch /tmp/testro } +@test "podman run bad --name" { + randomname=$(random_string 30) + run_podman 125 create --name "$randomname/bad" $IMAGE + run_podman create --name "/$randomname" $IMAGE + run_podman ps -a --filter name="^/$randomname$" --format '{{ .Names }}' + is $output "$randomname" "Should be able to find container by name" + run_podman rm "/$randomname" + run_podman 125 create --name "$randomname/" $IMAGE +} + # vim: filetype=sh