diff --git a/src/restic/backend/sftp/config.go b/src/restic/backend/sftp/config.go index d48525344..e6dfd0d27 100644 --- a/src/restic/backend/sftp/config.go +++ b/src/restic/backend/sftp/config.go @@ -2,6 +2,7 @@ package sftp import ( "errors" + "fmt" "net/url" "path" "strings" @@ -31,7 +32,12 @@ func ParseConfig(s string) (interface{}, error) { user = url.User.Username() } host = url.Host - dir = url.Path[1:] + dir = url.Path + if dir == "" { + return nil, fmt.Errorf("invalid backend %q, no directory specified", s) + } + + dir = dir[1:] case strings.HasPrefix(s, "sftp:"): // parse the sftp:user@host:path format, which means we'll get // "user@host:path" in s diff --git a/src/restic/backend/sftp/config_test.go b/src/restic/backend/sftp/config_test.go index 54ff3b91b..d0350745c 100644 --- a/src/restic/backend/sftp/config_test.go +++ b/src/restic/backend/sftp/config_test.go @@ -74,3 +74,17 @@ func TestParseConfig(t *testing.T) { } } } + +var configTestsInvalid = []string{ + "sftp://host:dir", +} + +func TestParseConfigInvalid(t *testing.T) { + for i, test := range configTestsInvalid { + _, err := ParseConfig(test) + if err == nil { + t.Errorf("test %d: invalid config %s did not return an error", i, test) + continue + } + } +}