diff --git a/cmd/restic/main.go b/cmd/restic/main.go index 5c6e46334..3a192e9df 100644 --- a/cmd/restic/main.go +++ b/cmd/restic/main.go @@ -17,6 +17,7 @@ import ( "go.uber.org/automaxprocs/maxprocs" "github.com/restic/restic/internal/backend/all" + "github.com/restic/restic/internal/backend/location" "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/feature" @@ -178,6 +179,22 @@ func printExitError(globalOptions global.Options, code int, message string) { } } +// redactLoggedArgs heuristically hides repository passwords from the debug log. +func redactLoggedArgs(args []string, registry *location.Registry) []string { + redacted := make([]string, 0, len(args)) + for _, arg := range args { + var cleaned string + if flag, value, ok := strings.Cut(arg, "="); ok && strings.HasPrefix(flag, "-") { + cleaned = flag + "=" + location.StripPassword(registry, value) + } else { + cleaned = location.StripPassword(registry, arg) + } + + redacted = append(redacted, cleaned) + } + return redacted +} + func main() { tweakGoGC() // install custom global logger into a buffer, if an error occurs @@ -193,12 +210,13 @@ func main() { Exit(1) } - debug.Log("main %#v", os.Args) + backends := all.Backends() + debug.Log("main %#v", redactLoggedArgs(os.Args, backends)) debug.Log("restic %s compiled with %v on %v/%v", global.Version, runtime.Version(), runtime.GOOS, runtime.GOARCH) globalOptions := global.Options{ - Backends: all.Backends(), + Backends: backends, } func() { term, cancel := termstatus.Setup(os.Stdin, os.Stdout, os.Stderr, globalOptions.Quiet) diff --git a/cmd/restic/main_test.go b/cmd/restic/main_test.go new file mode 100644 index 000000000..a070ecfcc --- /dev/null +++ b/cmd/restic/main_test.go @@ -0,0 +1,33 @@ +package main + +import ( + "slices" + "testing" + + "github.com/restic/restic/internal/backend/all" + rtest "github.com/restic/restic/internal/test" +) + +func TestRedactLoggedArgs(t *testing.T) { + registry := all.Backends() + args := []string{ + "restic", + "-r", + "rest:https://user:secret@example.com/repo", + "--repo=rest:https://user:secret@example.com/repo", + "backup", + "/srv/data", + } + copy := slices.Clone(args) + got := redactLoggedArgs(args, registry) + rtest.Equals(t, []string{ + "restic", + "-r", + "rest:https://user:***@example.com/repo/", + "--repo=rest:https://user:***@example.com/repo/", + "backup", + "/srv/data", + }, got) + // ensure that the original arguments are not modified + rtest.Equals(t, copy, args) +}