Merge pull request #26533 from ArthurWuTW/26506

volume export: refuse to write to terminal (TTY)
This commit is contained in:
openshift-merge-bot[bot]
2025-07-14 15:58:56 +00:00
committed by GitHub
2 changed files with 25 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/containers/podman/v5/cmd/podman/registry"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/spf13/cobra"
"golang.org/x/term"
)
var (
@ -47,11 +48,6 @@ func init() {
func export(cmd *cobra.Command, args []string) error {
containerEngine := registry.ContainerEngine()
ctx := context.Background()
if targetPath == "" && cmd.Flag("output").Changed {
return errors.New("must provide valid path for file to write to")
}
exportOpts := entities.VolumeExportOptions{}
if targetPath != "" {
@ -62,6 +58,12 @@ func export(cmd *cobra.Command, args []string) error {
defer targetFile.Close()
exportOpts.Output = targetFile
} else {
if cmd.Flag("output").Changed {
return errors.New("must provide valid path for file to write to")
}
if term.IsTerminal(int(os.Stdout.Fd())) {
return errors.New("cannot write to terminal, use command-line redirection or the --output flag")
}
exportOpts.Output = os.Stdout
}

View File

@ -124,4 +124,22 @@ function teardown() {
is "$output" "$expected_tty" "passthrough-tty: tty matches"
}
@test "podman volume export should fail when stdout is a tty" {
run_podman volume create testVol
run_podman run --rm -v testVol:/data $IMAGE sh -c "echo data > /data/file.txt"
# Positive Case
$PODMAN volume export testVol --output=/dev/null >$PODMAN_TEST_PTY ||
die "$PODMAN volume export testVol --output=/dev/null failed when connected to terminal."
# Negative Case
local rc=0
$PODMAN volume export testVol >$PODMAN_TEST_PTY 2>$PODMAN_TMPDIR/out || rc=$?
is "$rc" "125" "Exit code should be 125"
is "$(<$PODMAN_TMPDIR/out)" "Error: cannot write to terminal, use command-line redirection or the --output flag" "Should refuse to export to terminal."
run_podman volume rm testVol --force
}
# vim: filetype=sh