diff --git a/changelog/unreleased/issue-22051 b/changelog/unreleased/issue-22051 new file mode 100644 index 000000000..beca8cd99 --- /dev/null +++ b/changelog/unreleased/issue-22051 @@ -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 diff --git a/doc/030_preparing_a_new_repo.rst b/doc/030_preparing_a_new_repo.rst index 0ba592051..2b6b2ecba 100644 --- a/doc/030_preparing_a_new_repo.rst +++ b/doc/030_preparing_a_new_repo.rst @@ -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: diff --git a/internal/global/global.go b/internal/global/global.go index 64d02cf0e..49f2fe96c 100644 --- a/internal/global/global.go +++ b/internal/global/global.go @@ -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 diff --git a/internal/global/global_test.go b/internal/global/global_test.go index 90ec972c3..62a5cecfb 100644 --- a/internal/global/global_test.go +++ b/internal/global/global_test.go @@ -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")