From eb6e9b194a33ca452f4105a184c713d68cca12bf Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 19 May 2020 12:34:23 +0100 Subject: [PATCH] fspath: Stop empty strings being a valid path - fixes #4239 Before this change you could use "" as a valid remote, so `rclone lsf ""` would work. This was treated as the current directory. This is unexpected and creates a footgun for scripting when an empty variable is passed to rclone by accident. This fix returns the error "can't use empty string as a path" instead of allowing it. --- fs/fspath/path.go | 7 ++++++- fs/fspath/path_test.go | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/fs/fspath/path.go b/fs/fspath/path.go index d0684f941..fd6034181 100644 --- a/fs/fspath/path.go +++ b/fs/fspath/path.go @@ -18,6 +18,7 @@ const ( var ( errInvalidCharacters = errors.New("config name contains invalid characters - may only contain 0-9, A-Z ,a-z ,_ , - and space ") + errCantBeEmpty = errors.New("can't use empty string as a path") // urlMatcher is a pattern to match an rclone URL // note that this matches invalid remoteNames @@ -55,8 +56,12 @@ func CheckRemoteName(remoteName string) error { // // Note that this will turn \ into / in the fsPath on Windows // -// An error may be returned if the remote name has invalid characters in it. +// An error may be returned if the remote name has invalid characters +// in it or if the path is empty. func Parse(path string) (configName, fsPath string, err error) { + if path == "" { + return "", "", errCantBeEmpty + } parts := urlMatcher.FindStringSubmatch(path) configName, fsPath = "", path if parts != nil && !driveletter.IsDriveLetter(parts[1]) { diff --git a/fs/fspath/path_test.go b/fs/fspath/path_test.go index 5b85df215..5e8cba2e5 100644 --- a/fs/fspath/path_test.go +++ b/fs/fspath/path_test.go @@ -55,7 +55,7 @@ func TestParse(t *testing.T) { in, wantConfigName, wantFsPath string wantErr error }{ - {"", "", "", nil}, + {"", "", "", errCantBeEmpty}, {":", "", "", errInvalidCharacters}, {"::", ":", "", errInvalidCharacters}, {":/:", "", "/:", errInvalidCharacters}, @@ -91,7 +91,7 @@ func TestSplit(t *testing.T) { remote, wantParent, wantLeaf string wantErr error }{ - {"", "", "", nil}, + {"", "", "", errCantBeEmpty}, {"remote:", "remote:", "", nil}, {"remote:potato", "remote:", "potato", nil},