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:
JUSHUANGHUI LI authored and GitHub committed 2026-11-15 19:03:50 +00:00
1 parent 335d7e38aa
commit d7aa4145e0
4 files changed
+35 -1

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
Bugfix: Reject empty repository location files
An empty file passed via `--repository-file` or `RESTIC_REPOSITORY_FILE`
was treated as the current directory. Restic now reports an error if the
file is empty or contains only whitespace.
https://github.com/restic/restic/issues/22051
https://github.com/restic/restic/pull/22052
+2 -1
View File
@@ -30,7 +30,8 @@ section here.
For automated backups, restic supports specifying the repository location in the
environment variable ``RESTIC_REPOSITORY``. Restic can also read the repository
location from a file specified via the ``--repository-file`` option or the
environment variable ``RESTIC_REPOSITORY_FILE``.
environment variable ``RESTIC_REPOSITORY_FILE``. The file must contain a
repository location; empty or whitespace-only files are rejected.
For automating the supply of the repository password to restic, several options
exist:
+3
View File
@@ -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
+22
View File
@@ -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")