1
0
mirror of https://github.com/rclone/rclone.git synced 2026-01-06 02:23:24 +00:00

fs: Fix parsing of .. when joining remotes - Fixes #4862

Before this fix setting an alias of `s3:bucket` then using `alias:..`
would use the current working directory!

This fix corrects the path parsing. This parsing is also used in
wrapping backends like crypt, chunker, union etc.

It does not allow looking above the root of the alias, so `alias:..`
now lists `s3:bucket` as you might expect if you did `cd /` then
`ls ..`.
This commit is contained in:
Nick Craig-Wood
2020-12-13 10:26:13 +00:00
parent e45716cac2
commit ea8d13d841
2 changed files with 95 additions and 40 deletions

View File

@@ -130,33 +130,66 @@ func TestSplit(t *testing.T) {
}
}
}
func TestJoinRootPath(t *testing.T) {
func TestMakeAbsolute(t *testing.T) {
for _, test := range []struct {
elements []string
want string
in string
want string
}{
{nil, ""},
{[]string{""}, ""},
{[]string{"/"}, "/"},
{[]string{"/", "/"}, "/"},
{[]string{"/", "//"}, "/"},
{[]string{"/root", ""}, "/root"},
{[]string{"/root", "/"}, "/root"},
{[]string{"/root", "//"}, "/root"},
{[]string{"/a/b"}, "/a/b"},
{[]string{"//", "/"}, "//"},
{[]string{"//server", "path"}, "//server/path"},
{[]string{"//server/sub", "path"}, "//server/sub/path"},
{[]string{"//server", "//path"}, "//server/path"},
{[]string{"//server/sub", "//path"}, "//server/sub/path"},
{[]string{"", "//", "/"}, "//"},
{[]string{"", "//server", "path"}, "//server/path"},
{[]string{"", "//server/sub", "path"}, "//server/sub/path"},
{[]string{"", "//server", "//path"}, "//server/path"},
{[]string{"", "//server/sub", "//path"}, "//server/sub/path"},
{[]string{"", filepath.FromSlash("//server/sub"), filepath.FromSlash("//path")}, "//server/sub/path"},
{"", ""},
{".", ""},
{"/.", "/"},
{"../potato", "potato"},
{"/../potato", "/potato"},
{"./../potato", "potato"},
{"//../potato", "/potato"},
{"././../potato", "potato"},
{"././potato/../../onion", "onion"},
} {
got := JoinRootPath(test.elements...)
assert.Equal(t, test.want, got)
got := makeAbsolute(test.in)
assert.Equal(t, test.want, got, test)
}
}
func TestJoinRootPath(t *testing.T) {
for _, test := range []struct {
remote string
filePath string
want string
}{
{"", "", ""},
{"", "/", "/"},
{"/", "", "/"},
{"/", "/", "/"},
{"/", "//", "/"},
{"/root", "", "/root"},
{"/root", "/", "/root"},
{"/root", "//", "/root"},
{"/a/b", "", "/a/b"},
{"//", "/", "//"},
{"//server", "path", "//server/path"},
{"//server/sub", "path", "//server/sub/path"},
{"//server", "//path", "//server/path"},
{"//server/sub", "//path", "//server/sub/path"},
{"//", "/", "//"},
{"//server", "path", "//server/path"},
{"//server/sub", "path", "//server/sub/path"},
{"//server", "//path", "//server/path"},
{"//server/sub", "//path", "//server/sub/path"},
{filepath.FromSlash("//server/sub"), filepath.FromSlash("//path"), "//server/sub/path"},
{"s3:", "", "s3:"},
{"s3:", ".", "s3:"},
{"s3:.", ".", "s3:"},
{"s3:", "..", "s3:"},
{"s3:dir", "sub", "s3:dir/sub"},
{"s3:dir", "/sub", "s3:dir/sub"},
{"s3:dir", "./sub", "s3:dir/sub"},
{"s3:/dir", "/sub/", "s3:/dir/sub"},
{"s3:dir", "..", "s3:dir"},
{"s3:dir", "/..", "s3:dir"},
{"s3:dir", "/../", "s3:dir"},
} {
got := JoinRootPath(test.remote, test.filePath)
assert.Equal(t, test.want, got, test)
}
}