From dca6912c54ff3d32abe7446adbb899f06c97b970 Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Thu, 31 May 2018 16:22:24 -0400 Subject: [PATCH] remove extra "../" when copying from pod to local --- pkg/kubectl/cmd/cp.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/kubectl/cmd/cp.go b/pkg/kubectl/cmd/cp.go index b7242f5abd5..c2ba1c20795 100644 --- a/pkg/kubectl/cmd/cp.go +++ b/pkg/kubectl/cmd/cp.go @@ -287,9 +287,22 @@ func (o *CopyOptions) copyFromPod(src, dest fileSpec) error { }() prefix := getPrefix(src.File) prefix = path.Clean(prefix) + // remove extraneous path shortcuts - these could occur if a path contained extra "../" + // and attempted to navigate beyond "/" in a remote filesystem + prefix = stripPathShortcuts(prefix) return untarAll(reader, dest.File, prefix) } +// stripPathShortcuts removes any leading or trailing "../" from a given path +func stripPathShortcuts(p string) string { + newPath := path.Clean(p) + if len(newPath) > 0 && string(newPath[0]) == "/" { + return newPath[1:] + } + + return newPath +} + func makeTar(srcPath, destPath string, writer io.Writer) error { // TODO: use compression here? tarWriter := tar.NewWriter(writer)