mirror of
https://github.com/restic/restic.git
synced 2026-11-21 13:02:42 +00:00
global: reject empty repository location files (#22052)
Return a fatal error before opening or creating a repository when its location file is empty after decoding and trimming whitespace.
This commit is contained in:
1 parent
335d7e38aa
commit
d7aa4145e0
4 files changed
+35
-1
No files matched your search
@@ -289,6 +289,9 @@ func readRepo(gopts Options) (string, error) {
|
||||
}
|
||||
|
||||
repo = strings.TrimSpace(string(s))
|
||||
if repo == "" {
|
||||
return "", errors.Fatalf("repository file %s is empty", gopts.RepositoryFile)
|
||||
}
|
||||
}
|
||||
|
||||
return repo, nil
|
||||
|
||||
@@ -42,6 +42,28 @@ func TestReadRepo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadRepoEmptyFile(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
content string
|
||||
}{
|
||||
{"empty", ""},
|
||||
{"newline", "\n"},
|
||||
{"whitespace", " \t\r\n"},
|
||||
{"utf8-bom", "\xef\xbb\xbf"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
filename := filepath.Join(t.TempDir(), "repository")
|
||||
rtest.OK(t, os.WriteFile(filename, []byte(tc.content), 0600))
|
||||
|
||||
repo, err := readRepo(Options{RepositoryFile: filename})
|
||||
rtest.Assert(t, errors.IsFatal(err), "expected fatal error for empty repository file, got %v", err)
|
||||
rtest.Equals(t, "", repo)
|
||||
rtest.Assert(t, strings.Contains(err.Error(), filename), "error should identify repository file, got %v", err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadEmptyPassword(t *testing.T) {
|
||||
opts := Options{InsecureNoPassword: true}
|
||||
password, err := readPassword(context.TODO(), opts, "test")
|
||||
|
||||
Reference in new issue
Block a user