mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-11-03 15:25:19 +00:00
fix paths w shortcuts when copying from pods
Addresses an issue where copying from a remote location containing path shortcuts (podName:../../../tmp/foo) causes an index out of range panic.
This commit is contained in:
@@ -303,6 +303,18 @@ func (o *CopyOptions) copyFromPod(src, dest fileSpec) error {
|
||||
// stripPathShortcuts removes any leading or trailing "../" from a given path
|
||||
func stripPathShortcuts(p string) string {
|
||||
newPath := path.Clean(p)
|
||||
trimmed := strings.TrimPrefix(newPath, "../")
|
||||
|
||||
for trimmed != newPath {
|
||||
newPath = trimmed
|
||||
trimmed = strings.TrimPrefix(newPath, "../")
|
||||
}
|
||||
|
||||
// trim leftover ".."
|
||||
if newPath == ".." {
|
||||
newPath = ""
|
||||
}
|
||||
|
||||
if len(newPath) > 0 && string(newPath[0]) == "/" {
|
||||
return newPath[1:]
|
||||
}
|
||||
|
||||
@@ -127,6 +127,62 @@ func TestGetPrefix(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripPathShortcuts(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "test single path shortcut prefix",
|
||||
input: "../foo/bar",
|
||||
expected: "foo/bar",
|
||||
},
|
||||
{
|
||||
name: "test multiple path shortcuts",
|
||||
input: "../../foo/bar",
|
||||
expected: "foo/bar",
|
||||
},
|
||||
{
|
||||
name: "test multiple path shortcuts with absolute path",
|
||||
input: "/tmp/one/two/../../foo/bar",
|
||||
expected: "tmp/foo/bar",
|
||||
},
|
||||
{
|
||||
name: "test multiple path shortcuts with no named directory",
|
||||
input: "../../",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "test multiple path shortcuts with no named directory and no trailing slash",
|
||||
input: "../..",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "test multiple path shortcuts with absolute path and filename containing leading dots",
|
||||
input: "/tmp/one/two/../../foo/..bar",
|
||||
expected: "tmp/foo/..bar",
|
||||
},
|
||||
{
|
||||
name: "test multiple path shortcuts with no named directory and filename containing leading dots",
|
||||
input: "../...foo",
|
||||
expected: "...foo",
|
||||
},
|
||||
{
|
||||
name: "test filename containing leading dots",
|
||||
input: "...foo",
|
||||
expected: "...foo",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
out := stripPathShortcuts(test.input)
|
||||
if out != test.expected {
|
||||
t.Errorf("expected: %s, saw: %s", test.expected, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTarUntar(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "input")
|
||||
dir2, err2 := ioutil.TempDir("", "output")
|
||||
|
||||
Reference in New Issue
Block a user