2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-18 02:55:18 +00:00

Fix panic when parsing sftp URIs

Closes #587
This commit is contained in:
Alexander Neumann 2016-08-28 12:02:07 +02:00
parent d097d40237
commit ed09887d9e
2 changed files with 21 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package sftp
import ( import (
"errors" "errors"
"fmt"
"net/url" "net/url"
"path" "path"
"strings" "strings"
@ -31,7 +32,12 @@ func ParseConfig(s string) (interface{}, error) {
user = url.User.Username() user = url.User.Username()
} }
host = url.Host 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:"): case strings.HasPrefix(s, "sftp:"):
// parse the sftp:user@host:path format, which means we'll get // parse the sftp:user@host:path format, which means we'll get
// "user@host:path" in s // "user@host:path" in s

View File

@ -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
}
}
}