From 2518cd4a75d9c5456e850ca46c3d90b8aebf3bd3 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 22 Nov 2026 14:13:28 +0100 Subject: [PATCH] data / repository: quote fields in String() methods --- cmd/restic/cmd_forget.go | 2 +- cmd/restic/cmd_forget_integration_test.go | 2 +- cmd/restic/format.go | 6 +++--- cmd/restic/format_test.go | 22 ++++++++++++++++++++++ internal/data/node.go | 2 +- internal/data/node_test.go | 10 ++++++++++ internal/data/snapshot.go | 3 ++- internal/data/snapshot_group.go | 8 +++++--- internal/data/snapshot_group_test.go | 13 +++++++++++++ internal/data/snapshot_test.go | 12 ++++++++++++ internal/data/tag_list.go | 4 +++- internal/data/tag_list_test.go | 6 ++++++ internal/repository/key.go | 3 ++- internal/repository/lock_file.go | 3 ++- internal/repository/lock_file_test.go | 23 +++++++++++++++++++++++ 15 files changed, 106 insertions(+), 13 deletions(-) diff --git a/cmd/restic/cmd_forget.go b/cmd/restic/cmd_forget.go index 01568f22a..8368c1b96 100644 --- a/cmd/restic/cmd_forget.go +++ b/cmd/restic/cmd_forget.go @@ -280,7 +280,7 @@ func runForget(ctx context.Context, opts ForgetOptions, pruneOptions PruneOption keep, remove, reasons := data.ApplyPolicy(snapshotGroup, policy) if !policy.Empty() && len(keep) == 0 { - return fmt.Errorf("refusing to delete last snapshot of snapshot group \"%v\"", key.String()) + return fmt.Errorf("refusing to delete last snapshot of snapshot group [%s]", key.String()) } if len(keep) != 0 && !gopts.Quiet && !gopts.JSON { printer.P("keep %d snapshots:\n", len(keep)) diff --git a/cmd/restic/cmd_forget_integration_test.go b/cmd/restic/cmd_forget_integration_test.go index 93a178481..38d107b29 100644 --- a/cmd/restic/cmd_forget_integration_test.go +++ b/cmd/restic/cmd_forget_integration_test.go @@ -42,7 +42,7 @@ func TestRunForgetSafetyNet(t *testing.T) { KeepTags: data.TagLists{data.TagList{"invalid"}}, GroupBy: data.SnapshotGroupByOptions{Host: true, Path: true}, }) - rtest.Assert(t, strings.Contains(err.Error(), `refusing to delete last snapshot of snapshot group "host example, path`), "wrong error message got %v", err) + rtest.Assert(t, strings.Contains(err.Error(), `refusing to delete last snapshot of snapshot group [host example, path`), "wrong error message got %v", err) // disallow `forget --unsafe-allow-remove-all` err = testRunForgetMayFail(t, env.gopts, ForgetOptions{ diff --git a/cmd/restic/format.go b/cmd/restic/format.go index 79e55570a..4614a4629 100644 --- a/cmd/restic/format.go +++ b/cmd/restic/format.go @@ -11,7 +11,7 @@ import ( func formatNodeOutput(n lsNodeOutput, long bool, human bool) string { if !long { - return n.Path + return ui.Quote(n.Path) } var mode os.FileMode @@ -31,7 +31,7 @@ func formatNodeOutput(n lsNodeOutput, long bool, human bool) string { mode = os.ModeDir case data.NodeTypeSymlink: mode = os.ModeSymlink - target = fmt.Sprintf(" -> %v", n.LinkTarget) + target = fmt.Sprintf(" -> %s", ui.Quote(n.LinkTarget)) case data.NodeTypeDev: mode = os.ModeDevice case data.NodeTypeCharDev: @@ -44,7 +44,7 @@ func formatNodeOutput(n lsNodeOutput, long bool, human bool) string { return fmt.Sprintf("%s %5d %5d %s %s %s%s", mode|n.Mode, n.UID, n.GID, size, - n.ModTime.Local().Format(global.TimeFormat), n.Path, + n.ModTime.Local().Format(global.TimeFormat), ui.Quote(n.Path), target) } diff --git a/cmd/restic/format_test.go b/cmd/restic/format_test.go index 9d19a70f4..f584ba3d9 100644 --- a/cmd/restic/format_test.go +++ b/cmd/restic/format_test.go @@ -1,6 +1,8 @@ package main import ( + "os" + "strconv" "testing" "time" @@ -54,6 +56,26 @@ func TestFormatNode(t *testing.T) { human: true, expect: "---------- 1000 2000 14.000 MiB 2020-01-02 03:04:05 " + testPath, }, + { + path: "dir\nfile", + Node: node, + long: false, + expect: strconv.Quote("dir\nfile"), + }, + { + path: "dir\nfile", + Node: data.Node{ + Name: "link", + Type: data.NodeTypeSymlink, + Mode: os.ModeSymlink | 0777, + LinkTarget: "target\x1b", + UID: 1000, + GID: 2000, + ModTime: time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC), + }, + long: true, + expect: "Lrwxrwxrwx 1000 2000 0 2020-01-02 03:04:05 " + strconv.Quote("dir\nfile") + " -> " + strconv.Quote("target\x1b"), + }, } { r := formatNode(c.path, &c.Node, c.long, c.human) rtest.Equals(t, c.expect, r) diff --git a/internal/data/node.go b/internal/data/node.go index 7f2154ebb..b9a27f388 100644 --- a/internal/data/node.go +++ b/internal/data/node.go @@ -135,7 +135,7 @@ func (node Node) String() string { } return fmt.Sprintf("%s %5d %5d %6d %s %s", - mode|node.Mode, node.UID, node.GID, node.Size, node.ModTime, node.Name) + mode|node.Mode, node.UID, node.GID, node.Size, node.ModTime, ui.Quote(node.Name)) } // GetExtendedAttribute gets the extended attribute. diff --git a/internal/data/node_test.go b/internal/data/node_test.go index 06a527cfd..675a1cf95 100644 --- a/internal/data/node_test.go +++ b/internal/data/node_test.go @@ -2,6 +2,8 @@ package data import ( "encoding/json" + "strconv" + "strings" "testing" "time" @@ -93,3 +95,11 @@ func TestSymlinkSerializationFormat(t *testing.T) { test.Assert(t, n2.LinkTargetRaw == nil, "quoted link target is just a helper field and must be unset after decoding") } } + +func TestNodeStringQuotesName(t *testing.T) { + ordinary := Node{Name: "file", UID: 1, GID: 2}.String() + test.Assert(t, strings.HasSuffix(ordinary, " file"), "ordinary name was quoted: %s", ordinary) + + quoted := Node{Name: "a\nb", UID: 1, GID: 2}.String() + test.Assert(t, strings.HasSuffix(quoted, " "+strconv.Quote("a\nb")), "hostile name was not quoted: %s", quoted) +} diff --git a/internal/data/snapshot.go b/internal/data/snapshot.go index 18972ff79..f27543737 100644 --- a/internal/data/snapshot.go +++ b/internal/data/snapshot.go @@ -11,6 +11,7 @@ import ( "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/restic" + "github.com/restic/restic/internal/ui" ) // Snapshot is the state of a resource at one point in time. @@ -119,7 +120,7 @@ func ForAllSnapshots(ctx context.Context, be restic.Lister, loader restic.Loader func (sn Snapshot) String() string { return fmt.Sprintf("snapshot %s of %v at %s by %s@%s", - sn.id.Str(), sn.Paths, sn.Time, sn.Username, sn.Hostname) + sn.id.Str(), ui.QuoteSlice(sn.Paths), sn.Time, ui.Quote(sn.Username), ui.Quote(sn.Hostname)) } // ID returns the snapshot's ID. diff --git a/internal/data/snapshot_group.go b/internal/data/snapshot_group.go index 620e153f5..9033781d1 100644 --- a/internal/data/snapshot_group.go +++ b/internal/data/snapshot_group.go @@ -5,6 +5,8 @@ import ( "fmt" "sort" "strings" + + "github.com/restic/restic/internal/ui" ) type SnapshotGroupByOptions struct { @@ -69,13 +71,13 @@ type SnapshotGroupKey struct { func (s *SnapshotGroupKey) String() string { var parts []string if s.Hostname != "" { - parts = append(parts, fmt.Sprintf("host %v", s.Hostname)) + parts = append(parts, fmt.Sprintf("host %v", ui.Quote(s.Hostname))) } if len(s.Paths) != 0 { - parts = append(parts, fmt.Sprintf("path %v", s.Paths)) + parts = append(parts, fmt.Sprintf("path %v", ui.QuoteSlice(s.Paths))) } if len(s.Tags) != 0 { - parts = append(parts, fmt.Sprintf("tags %v", s.Tags)) + parts = append(parts, fmt.Sprintf("tags %v", ui.QuoteSlice(s.Tags))) } return strings.Join(parts, ", ") } diff --git a/internal/data/snapshot_group_test.go b/internal/data/snapshot_group_test.go index 3cbbc7862..e009d02ae 100644 --- a/internal/data/snapshot_group_test.go +++ b/internal/data/snapshot_group_test.go @@ -1,6 +1,7 @@ package data_test import ( + "strconv" "testing" "github.com/google/go-cmp/cmp" @@ -48,3 +49,15 @@ func TestGroupByOptions(t *testing.T) { test.Assert(t, err != nil, "missing error on invalid tags") test.Assert(t, !opts.Host && !opts.Path && !opts.Tag, "unexpected opts %s %s %s", opts.Host, opts.Path, opts.Tag) } + +func TestSnapshotGroupKeyStringQuotes(t *testing.T) { + ordinary := (&data.SnapshotGroupKey{Hostname: "host", Paths: []string{"/ok"}, Tags: []string{"tag"}}).String() + test.Equals(t, "host host, path [/ok], tags [tag]", ordinary) + + hostile := (&data.SnapshotGroupKey{ + Hostname: "a\nb", + Paths: []string{"/ok", "p\nq"}, + Tags: []string{"t\tu"}, + }).String() + test.Equals(t, "host "+strconv.Quote("a\nb")+", path [/ok "+strconv.Quote("p\nq")+"], tags ["+strconv.Quote("t\tu")+"]", hostile) +} diff --git a/internal/data/snapshot_test.go b/internal/data/snapshot_test.go index 84c93228e..2aeae6cb4 100644 --- a/internal/data/snapshot_test.go +++ b/internal/data/snapshot_test.go @@ -66,3 +66,15 @@ func testLoadJSONUnpacked(t *testing.T, version uint) { rtest.Equals(t, sn.Hostname, sn2.Hostname) rtest.Equals(t, sn.Username, sn2.Username) } + +func TestSnapshotStringQuotesFields(t *testing.T) { + ordinary := data.Snapshot{Paths: []string{"/home"}, Username: "alice", Hostname: "box"}.String() + rtest.Equals(t, "snapshot [nil] of [/home] at 0001-01-01 00:00:00 +0000 UTC by alice@box", ordinary) + + hostile := data.Snapshot{ + Paths: []string{"/ok", "a\nb"}, + Username: "u\nv", + Hostname: "host", + }.String() + rtest.Equals(t, `snapshot [nil] of [/ok "a\nb"] at 0001-01-01 00:00:00 +0000 UTC by "u\nv"@host`, hostile) +} diff --git a/internal/data/tag_list.go b/internal/data/tag_list.go index 4139e5624..7d5a26c88 100644 --- a/internal/data/tag_list.go +++ b/internal/data/tag_list.go @@ -4,6 +4,8 @@ import ( "fmt" "slices" "strings" + + "github.com/restic/restic/internal/ui" ) // TagList is a list of tags. @@ -25,7 +27,7 @@ func splitTagList(s string) (TagList, error) { } func (l TagList) String() string { - return "[" + strings.Join(l, ", ") + "]" + return "[" + strings.Join(ui.QuoteSlice(l), ", ") + "]" } // Set updates the TagList's value. diff --git a/internal/data/tag_list_test.go b/internal/data/tag_list_test.go index 17f9df669..c463021f7 100644 --- a/internal/data/tag_list_test.go +++ b/internal/data/tag_list_test.go @@ -1,6 +1,7 @@ package data import ( + "strconv" "testing" rtest "github.com/restic/restic/internal/test" @@ -78,3 +79,8 @@ func TestTagListSetInvalid(t *testing.T) { rtest.Equals(t, TagLists(nil), ls) } } + +func TestTagListStringQuotes(t *testing.T) { + rtest.Equals(t, "[ok, plain]", TagList{"ok", "plain"}.String()) + rtest.Equals(t, "[ok, "+strconv.Quote("a\nb")+"]", TagList{"ok", "a\nb"}.String()) +} diff --git a/internal/repository/key.go b/internal/repository/key.go index 279fbf2b6..ee1d1630f 100644 --- a/internal/repository/key.go +++ b/internal/repository/key.go @@ -11,6 +11,7 @@ import ( "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/restic" + "github.com/restic/restic/internal/ui" "github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/debug" @@ -299,7 +300,7 @@ func (k *Key) String() string { if k == nil { return "" } - return fmt.Sprintf("", k.Username, k.Hostname, k.Created) + return fmt.Sprintf("", ui.Quote(k.Username), ui.Quote(k.Hostname), k.Created) } // ID returns an identifier for the key. diff --git a/internal/repository/lock_file.go b/internal/repository/lock_file.go index b05bf286a..654d57149 100644 --- a/internal/repository/lock_file.go +++ b/internal/repository/lock_file.go @@ -12,6 +12,7 @@ import ( "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/restic" + "github.com/restic/restic/internal/ui" ) // unlockCancelDelay bounds the duration how long lock cleanup operations will wait @@ -376,7 +377,7 @@ func (l *lockHandle) checkExistence(ctx context.Context) (bool, error) { func (l *lockHandle) String() string { text := fmt.Sprintf("PID %d on %s by %s (UID %d, GID %d)\nlock was created at %s (%s ago)\nstorage ID %v", - l.PID, l.Hostname, l.Username, l.UID, l.GID, + l.PID, ui.Quote(l.Hostname), ui.Quote(l.Username), l.UID, l.GID, l.Time.Format("2006-01-02 15:04:05"), time.Since(l.Time), l.lockID.Str()) diff --git a/internal/repository/lock_file_test.go b/internal/repository/lock_file_test.go index c8b2984f2..54bcd7bee 100644 --- a/internal/repository/lock_file_test.go +++ b/internal/repository/lock_file_test.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "regexp" "testing" "time" @@ -246,3 +247,25 @@ func TestLockRefreshStaleMissing(t *testing.T) { err = lock.refreshStaleLock(context.TODO()) rtest.Assert(t, err == errRemovedLock, "unexpected error, expected %v, got %v", errRemovedLock, err) } + +func TestLockStringQuotesHost(t *testing.T) { + created := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC) + age := regexp.MustCompile(`\([^)]* ago\)`) + lockText := func(host string) string { + return age.ReplaceAllString((&lockHandle{Lock: Lock{PID: 7, Hostname: host, Username: "user", UID: 1, GID: 2, Time: created}}).String(), "(AGE ago)") + } + + rtest.Equals(t, "PID 7 on host by user (UID 1, GID 2)\nlock was created at 2020-01-02 03:04:05 (AGE ago)\nstorage ID [nil]", lockText("host")) + rtest.Equals(t, "PID 7 on \"h\\x1b\" by user (UID 1, GID 2)\nlock was created at 2020-01-02 03:04:05 (AGE ago)\nstorage ID [nil]", lockText("h\x1b")) +} + +func TestKeyStringQuotesIdentity(t *testing.T) { + rtest.Equals(t, "", (*Key)(nil).String()) + + created := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC) + ordinary := (&Key{Username: "alice", Hostname: "box", Created: created}).String() + rtest.Equals(t, "", ordinary) + + hostile := (&Key{Username: "a\nb", Hostname: "box", Created: created}).String() + rtest.Equals(t, ``, hostile) +}