From f6e15168bd28af741fbc76656da0de58ab8e53b5 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Mon, 2 Nov 2026 11:25:17 +1100 Subject: [PATCH 1/4] feat(rsync): Exclude private files from downloads by default (PSEC-22) Downloading from /mnt/private now requires --include-private and a confirmation prompt (--force skips it). Syncing from a parent such as /mnt/ adds an anchored exclude for the private filesystem. --- cmd/skpr/rsync/command.go | 17 ++++- internal/command/rsync/command.go | 81 ++++++++++++++++++-- internal/command/rsync/command_test.go | 68 ++++++++++++++++- internal/command/rsync/private.go | 101 +++++++++++++++++++++++++ 4 files changed, 254 insertions(+), 13 deletions(-) create mode 100644 internal/command/rsync/private.go diff --git a/cmd/skpr/rsync/command.go b/cmd/skpr/rsync/command.go index 6d9bfc6..bcd261c 100644 --- a/cmd/skpr/rsync/command.go +++ b/cmd/skpr/rsync/command.go @@ -8,11 +8,22 @@ import ( ) var ( - cmdLong = "Sync files from remote and local directories" + cmdLong = `Sync files from remote and local directories. + +Private files (/mnt/private) may contain personal information and are not downloaded by default. +Syncing from a parent directory, such as /mnt/, excludes them automatically. Syncing from the +private filesystem itself fails unless --include-private is set. Only download private files to +investigate a specific issue, with approval.` cmdExample = ` # Sync from local to a remote environment - skpr rsync ./foo/ dev:/mnt/temporary/foo/` + skpr rsync ./foo/ dev:/mnt/temporary/foo/ + + # Download public files from a remote environment + skpr rsync dev:/mnt/public/ ./files/ + + # Download private files from a remote environment (asks for confirmation) + skpr rsync --include-private dev:/mnt/private/ ./private/` ) // NewCommand creates a new cobra.Command for 'rsync' sub command @@ -37,6 +48,8 @@ func NewCommand() *cobra.Command { cmd.Flags().StringSliceVar(&command.Excludes, "exclude", []string{}, "Exclude files matching PATTERN.") cmd.Flags().StringVar(&command.ExcludeFrom, "exclude-from", "", "Exclude files matching patterns in FILE.") cmd.Flags().BoolVar(&command.DryRun, "dry-run", false, "Skpr will not request a confirmation for configuration changes.") + cmd.Flags().BoolVar(&command.IncludePrivate, "include-private", false, "Download private files ("+v1rsync.PrivateFilesPath+"). Asks for confirmation.") + cmd.Flags().BoolVarP(&command.Force, "force", "f", false, "Skip the confirmation prompt when downloading private files.") return cmd } diff --git a/internal/command/rsync/command.go b/internal/command/rsync/command.go index 23ebe2c..19737c7 100644 --- a/internal/command/rsync/command.go +++ b/internal/command/rsync/command.go @@ -7,15 +7,24 @@ import ( "os/exec" "github.com/pkg/errors" + + "github.com/skpr/cli/internal/confirmation" ) +// ErrPrivateFilesNotIncluded is returned when a sync would download private files without the flag to allow it. +var ErrPrivateFilesNotIncluded = errors.New("this sync would download private files, which may contain personal information. " + + "Only download private files to investigate a specific issue, with approval. " + + "Use --include-private to download them") + // Command to rsync files. type Command struct { - Source string - Destination string - Excludes []string - ExcludeFrom string - DryRun bool + Source string + Destination string + Excludes []string + ExcludeFrom string + DryRun bool + IncludePrivate bool + Force bool } // Run the command. @@ -25,20 +34,78 @@ func (c *Command) Run(_ context.Context) error { return errors.Wrap(err, "failed to lookup binary path") } - args := c.generateArgs(executable) + plan, err := c.planPrivateFiles() + if err != nil { + return err + } + + if plan.confirm { + msg := fmt.Sprintf("You are about to download private files from %s. Private files may contain personal information. "+ + "Only download them to investigate a specific issue, with approval. Continue? [yes/no]", plan.environment) + if ok := confirmation.Confirm(c.Force, msg); !ok { + return errors.New("aborted") + } + } + + if plan.exclude != "" { + fmt.Fprintf(os.Stderr, "Excluding private files (%s). Use --include-private to download them.\n", PrivateFilesPath) + } + + args := c.generateArgs(executable, plan.exclude) cmd := exec.Command("rsync", args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr return cmd.Run() } +// privateFilesPlan describes how a sync handles private files. +type privateFilesPlan struct { + // environment the files are downloaded from. + environment string + // confirm is true when the user must confirm a private file download. + confirm bool + // exclude is an rsync exclude pattern that skips private files, or empty when none is needed. + exclude string +} + +// planPrivateFiles decides how the sync handles private files. Private files are only +// downloaded with IncludePrivate set, and only after confirmation. +func (c *Command) planPrivateFiles() (privateFilesPlan, error) { + env, remotePath, ok := splitRemote(c.Source) + if !ok { + return privateFilesPlan{}, nil + } + + plan := privateFilesPlan{environment: env} + + switch classifyRemotePath(remotePath) { + case privateScopeInside: + if !c.IncludePrivate { + return plan, ErrPrivateFilesNotIncluded + } + + plan.confirm = true + case privateScopeParent: + if c.IncludePrivate { + plan.confirm = true + } else { + plan.exclude = privateExcludePattern(remotePath) + } + } + + return plan, nil +} + // generateArgs generates the args for the rsync command. -func (c *Command) generateArgs(executable string) []string { +func (c *Command) generateArgs(executable, privateExclude string) []string { args := []string{ "-avz", "--progress", "-e", fmt.Sprintf("%s-rsh", executable), } + if privateExclude != "" { + args = append(args, "--exclude", privateExclude) + } for _, ex := range c.Excludes { args = append(args, "--exclude", ex) } diff --git a/internal/command/rsync/command_test.go b/internal/command/rsync/command_test.go index 65c503c..6c2c569 100644 --- a/internal/command/rsync/command_test.go +++ b/internal/command/rsync/command_test.go @@ -12,7 +12,7 @@ func TestGenerateArgs(t *testing.T) { Destination: "destination", } - args := c.generateArgs("skpr") + args := c.generateArgs("skpr", "") assert.Equal(t, []string{ "-avz", @@ -34,7 +34,7 @@ func TestGenerateArgsExcludes(t *testing.T) { }, } - args := c.generateArgs("skpr") + args := c.generateArgs("skpr", "") assert.Equal(t, []string{ "-avz", @@ -54,7 +54,7 @@ func TestGenerateArgsExcludeFrom(t *testing.T) { Destination: "destination", ExcludeFrom: "foo.txt", } - args := c.generateArgs("skpr") + args := c.generateArgs("skpr", "") assert.Equal(t, []string{ "-avz", "--progress", @@ -71,7 +71,7 @@ func TestGenerateDryRun(t *testing.T) { Destination: "destination", DryRun: true, } - args := c.generateArgs("skpr") + args := c.generateArgs("skpr", "") assert.Equal(t, []string{ "-avz", "--progress", @@ -81,3 +81,63 @@ func TestGenerateDryRun(t *testing.T) { "destination", }, args) } + +func TestGenerateArgsPrivateExclude(t *testing.T) { + c := Command{ + Source: "dev:/mnt/", + Destination: "destination", + Excludes: []string{"js/*"}, + } + args := c.generateArgs("skpr", "/private/") + assert.Equal(t, []string{ + "-avz", + "--progress", + "-e", "skpr-rsh", + "--exclude", "/private/", + "--exclude", "js/*", + "dev:/mnt/", + "destination", + }, args) +} + +func TestPlanPrivateFiles(t *testing.T) { + tests := []struct { + name string + source string + includePrivate bool + want privateFilesPlan + wantErr error + }{ + {name: "upload", source: "./private/", want: privateFilesPlan{}}, + {name: "public download", source: "dev:/mnt/public/", want: privateFilesPlan{environment: "dev"}}, + {name: "relative download", source: "dev:files/", want: privateFilesPlan{environment: "dev"}}, + {name: "similar name", source: "dev:/mnt/private-old/", want: privateFilesPlan{environment: "dev"}}, + {name: "private without flag", source: "dev:/mnt/private/", wantErr: ErrPrivateFilesNotIncluded}, + {name: "private subdirectory without flag", source: "dev:/mnt/private/webform", wantErr: ErrPrivateFilesNotIncluded}, + {name: "private via traversal without flag", source: "dev:/mnt/public/../private", wantErr: ErrPrivateFilesNotIncluded}, + {name: "private glob without flag", source: "dev:/mnt/*", wantErr: ErrPrivateFilesNotIncluded}, + {name: "parent ending in dot without flag", source: "dev:/mnt/.", wantErr: ErrPrivateFilesNotIncluded}, + {name: "private with flag", source: "dev:/mnt/private/", includePrivate: true, want: privateFilesPlan{environment: "dev", confirm: true}}, + {name: "parent with trailing slash", source: "dev:/mnt/", want: privateFilesPlan{environment: "dev", exclude: "/private/"}}, + {name: "parent without trailing slash", source: "dev:/mnt", want: privateFilesPlan{environment: "dev", exclude: "/mnt/private/"}}, + {name: "root", source: "dev:/", want: privateFilesPlan{environment: "dev", exclude: "/mnt/private/"}}, + {name: "parent with flag", source: "dev:/mnt/", includePrivate: true, want: privateFilesPlan{environment: "dev", confirm: true}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := Command{ + Source: tt.source, + Destination: "destination", + IncludePrivate: tt.includePrivate, + } + got, err := c.planPrivateFiles() + if tt.wantErr != nil { + assert.ErrorIs(t, err, tt.wantErr) + return + } + assert.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +} diff --git a/internal/command/rsync/private.go b/internal/command/rsync/private.go new file mode 100644 index 0000000..cf614c4 --- /dev/null +++ b/internal/command/rsync/private.go @@ -0,0 +1,101 @@ +package rsync + +import ( + "path" + "strings" +) + +// PrivateFilesPath is where the private filesystem is mounted in an environment. +const PrivateFilesPath = "/mnt/private" + +// privateScope describes how a remote rsync source relates to the private filesystem. +type privateScope int + +const ( + // privateScopeNone means the source does not include private files. + privateScopeNone privateScope = iota + // privateScopeInside means the source is the private filesystem or a path within it. + privateScopeInside + // privateScopeParent means the source is a parent directory of the private filesystem. + privateScopeParent +) + +// splitRemote splits an rsync remote location ("env:path") into its environment and path. +// The boolean is false when the location is a local path. +func splitRemote(location string) (string, string, bool) { + env, remotePath, found := strings.Cut(location, ":") + if !found || env == "" || strings.Contains(env, "/") { + return "", "", false + } + + return env, remotePath, true +} + +// classifyRemotePath reports how a remote source path relates to the private filesystem. +// Relative paths are resolved by the remote shell and cannot be checked here, so they are +// treated as not including private files. +func classifyRemotePath(remotePath string) privateScope { + if !path.IsAbs(remotePath) { + return privateScopeNone + } + + // A glob such as "/mnt/*" can expand to the private filesystem on the remote side. + // Only its literal prefix is known here, so treat any glob that could reach the + // private filesystem as being inside it. + if i := strings.IndexAny(remotePath, "*?["); i >= 0 { + prefix := path.Dir(remotePath[:i+1]) + if isWithin(prefix, PrivateFilesPath) || isWithin(PrivateFilesPath, prefix) { + return privateScopeInside + } + + return privateScopeNone + } + + cleaned := path.Clean(remotePath) + + switch { + case isWithin(cleaned, PrivateFilesPath): + return privateScopeInside + case isWithin(PrivateFilesPath, cleaned): + // rsync picks the transfer root from the last path element, and "." or ".." make + // that root hard to predict. Without a known root an exclude can't be anchored + // safely, so require the private files flag instead. + if trimmed := strings.TrimRight(remotePath, "/"); trimmed != "" { + if base := path.Base(trimmed); base == "." || base == ".." { + return privateScopeInside + } + } + + return privateScopeParent + default: + return privateScopeNone + } +} + +// isWithin reports whether child is the same path as parent or is below it. +func isWithin(child, parent string) bool { + if child == parent || parent == "/" { + return true + } + + return strings.HasPrefix(child, parent+"/") +} + +// privateExcludePattern returns an anchored rsync exclude pattern that matches the private +// filesystem when syncing from remotePath, which must be a parent of the private filesystem. +// +// rsync anchors patterns to the root of the transfer. With a trailing slash ("/mnt/") the +// root is the directory itself, otherwise ("/mnt") it is the directory's parent. +func privateExcludePattern(remotePath string) string { + root := path.Clean(remotePath) + if !strings.HasSuffix(remotePath, "/") { + root = path.Dir(root) + } + + rel := strings.TrimPrefix(PrivateFilesPath, root) + if !strings.HasPrefix(rel, "/") { + rel = "/" + rel + } + + return rel + "/" +} From cc3373fae221d59af107b16c124f32b126fca766 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Mon, 2 Nov 2026 11:36:55 +1100 Subject: [PATCH 2/4] fix(rsync): Tidy private file handling from review (PSEC-22) - Treat an empty or unreadable confirmation answer as no instead of panicking - Return nil when a private download is declined, matching other commands - Skip the confirmation prompt for --dry-run - Match globs per path element so globs that can't reach private files are allowed - Rename privateScopeInside to privateScopeRequiresFlag - Add tests for globs, local paths with colons, dry runs and declining --- internal/command/rsync/command.go | 32 +++++++----- internal/command/rsync/command_test.go | 30 ++++++++++++ internal/command/rsync/private.go | 57 +++++++++++++++++----- internal/confirmation/confirmation.go | 20 +++++--- internal/confirmation/confirmation_test.go | 31 ++++++++++++ 5 files changed, 139 insertions(+), 31 deletions(-) create mode 100644 internal/confirmation/confirmation_test.go diff --git a/internal/command/rsync/command.go b/internal/command/rsync/command.go index 19737c7..e71aee6 100644 --- a/internal/command/rsync/command.go +++ b/internal/command/rsync/command.go @@ -25,28 +25,37 @@ type Command struct { DryRun bool IncludePrivate bool Force bool + + // confirm prompts the user; it defaults to confirmation.Confirm and is replaced in tests. + confirm func(force bool, message string) bool } // Run the command. func (c *Command) Run(_ context.Context) error { - executable, err := os.Executable() - if err != nil { - return errors.Wrap(err, "failed to lookup binary path") - } - plan, err := c.planPrivateFiles() if err != nil { return err } if plan.confirm { + confirm := c.confirm + if confirm == nil { + confirm = confirmation.Confirm + } + msg := fmt.Sprintf("You are about to download private files from %s. Private files may contain personal information. "+ "Only download them to investigate a specific issue, with approval. Continue? [yes/no]", plan.environment) - if ok := confirmation.Confirm(c.Force, msg); !ok { - return errors.New("aborted") + if ok := confirm(c.Force, msg); !ok { + fmt.Fprintln(os.Stderr, "Download cancelled.") + return nil } } + executable, err := os.Executable() + if err != nil { + return errors.Wrap(err, "failed to lookup binary path") + } + if plan.exclude != "" { fmt.Fprintf(os.Stderr, "Excluding private files (%s). Use --include-private to download them.\n", PrivateFilesPath) } @@ -69,7 +78,8 @@ type privateFilesPlan struct { } // planPrivateFiles decides how the sync handles private files. Private files are only -// downloaded with IncludePrivate set, and only after confirmation. +// downloaded with IncludePrivate set, and only after confirmation. A dry run transfers +// nothing, so it needs the flag but not the confirmation. func (c *Command) planPrivateFiles() (privateFilesPlan, error) { env, remotePath, ok := splitRemote(c.Source) if !ok { @@ -79,15 +89,15 @@ func (c *Command) planPrivateFiles() (privateFilesPlan, error) { plan := privateFilesPlan{environment: env} switch classifyRemotePath(remotePath) { - case privateScopeInside: + case privateScopeRequiresFlag: if !c.IncludePrivate { return plan, ErrPrivateFilesNotIncluded } - plan.confirm = true + plan.confirm = !c.DryRun case privateScopeParent: if c.IncludePrivate { - plan.confirm = true + plan.confirm = !c.DryRun } else { plan.exclude = privateExcludePattern(remotePath) } diff --git a/internal/command/rsync/command_test.go b/internal/command/rsync/command_test.go index 6c2c569..07a0875 100644 --- a/internal/command/rsync/command_test.go +++ b/internal/command/rsync/command_test.go @@ -1,6 +1,7 @@ package rsync import ( + "context" "testing" "github.com/stretchr/testify/assert" @@ -105,10 +106,12 @@ func TestPlanPrivateFiles(t *testing.T) { name string source string includePrivate bool + dryRun bool want privateFilesPlan wantErr error }{ {name: "upload", source: "./private/", want: privateFilesPlan{}}, + {name: "local path with colon", source: "./a:b/", want: privateFilesPlan{}}, {name: "public download", source: "dev:/mnt/public/", want: privateFilesPlan{environment: "dev"}}, {name: "relative download", source: "dev:files/", want: privateFilesPlan{environment: "dev"}}, {name: "similar name", source: "dev:/mnt/private-old/", want: privateFilesPlan{environment: "dev"}}, @@ -116,12 +119,20 @@ func TestPlanPrivateFiles(t *testing.T) { {name: "private subdirectory without flag", source: "dev:/mnt/private/webform", wantErr: ErrPrivateFilesNotIncluded}, {name: "private via traversal without flag", source: "dev:/mnt/public/../private", wantErr: ErrPrivateFilesNotIncluded}, {name: "private glob without flag", source: "dev:/mnt/*", wantErr: ErrPrivateFilesNotIncluded}, + {name: "matching glob without flag", source: "dev:/mnt/priv*/", wantErr: ErrPrivateFilesNotIncluded}, + {name: "glob within private without flag", source: "dev:/mnt/private/*.pdf", wantErr: ErrPrivateFilesNotIncluded}, + {name: "glob parent without flag", source: "dev:/m*", wantErr: ErrPrivateFilesNotIncluded}, + {name: "glob with traversal without flag", source: "dev:/var/*/../mnt/private", wantErr: ErrPrivateFilesNotIncluded}, + {name: "public glob", source: "dev:/mnt/pub*/", want: privateFilesPlan{environment: "dev"}}, + {name: "unrelated glob", source: "dev:/var/*", want: privateFilesPlan{environment: "dev"}}, {name: "parent ending in dot without flag", source: "dev:/mnt/.", wantErr: ErrPrivateFilesNotIncluded}, {name: "private with flag", source: "dev:/mnt/private/", includePrivate: true, want: privateFilesPlan{environment: "dev", confirm: true}}, {name: "parent with trailing slash", source: "dev:/mnt/", want: privateFilesPlan{environment: "dev", exclude: "/private/"}}, {name: "parent without trailing slash", source: "dev:/mnt", want: privateFilesPlan{environment: "dev", exclude: "/mnt/private/"}}, {name: "root", source: "dev:/", want: privateFilesPlan{environment: "dev", exclude: "/mnt/private/"}}, {name: "parent with flag", source: "dev:/mnt/", includePrivate: true, want: privateFilesPlan{environment: "dev", confirm: true}}, + {name: "private dry run with flag", source: "dev:/mnt/private/", includePrivate: true, dryRun: true, want: privateFilesPlan{environment: "dev"}}, + {name: "private dry run without flag", source: "dev:/mnt/private/", dryRun: true, wantErr: ErrPrivateFilesNotIncluded}, } for _, tt := range tests { @@ -130,6 +141,7 @@ func TestPlanPrivateFiles(t *testing.T) { Source: tt.source, Destination: "destination", IncludePrivate: tt.includePrivate, + DryRun: tt.dryRun, } got, err := c.planPrivateFiles() if tt.wantErr != nil { @@ -141,3 +153,21 @@ func TestPlanPrivateFiles(t *testing.T) { }) } } + +func TestRunPrivateDeclined(t *testing.T) { + var prompted bool + c := Command{ + Source: "dev:/mnt/private/", + Destination: "destination", + IncludePrivate: true, + confirm: func(force bool, message string) bool { + prompted = true + assert.Contains(t, message, "dev") + return false + }, + } + + // Declining returns before rsync runs. + assert.NoError(t, c.Run(context.Background())) + assert.True(t, prompted) +} diff --git a/internal/command/rsync/private.go b/internal/command/rsync/private.go index cf614c4..db03c4f 100644 --- a/internal/command/rsync/private.go +++ b/internal/command/rsync/private.go @@ -14,12 +14,17 @@ type privateScope int const ( // privateScopeNone means the source does not include private files. privateScopeNone privateScope = iota - // privateScopeInside means the source is the private filesystem or a path within it. - privateScopeInside - // privateScopeParent means the source is a parent directory of the private filesystem. + // privateScopeRequiresFlag means the source is, or may expand to, the private filesystem + // or a path within it, so downloading it requires the private files flag. + privateScopeRequiresFlag + // privateScopeParent means the source is a parent directory of the private filesystem, + // which can be synced with the private filesystem excluded. privateScopeParent ) +// privateFilesElements are the path elements of PrivateFilesPath. +var privateFilesElements = strings.Split(strings.TrimPrefix(PrivateFilesPath, "/"), "/") + // splitRemote splits an rsync remote location ("env:path") into its environment and path. // The boolean is false when the location is a local path. func splitRemote(location string) (string, string, bool) { @@ -39,13 +44,12 @@ func classifyRemotePath(remotePath string) privateScope { return privateScopeNone } - // A glob such as "/mnt/*" can expand to the private filesystem on the remote side. - // Only its literal prefix is known here, so treat any glob that could reach the - // private filesystem as being inside it. - if i := strings.IndexAny(remotePath, "*?["); i >= 0 { - prefix := path.Dir(remotePath[:i+1]) - if isWithin(prefix, PrivateFilesPath) || isWithin(PrivateFilesPath, prefix) { - return privateScopeInside + // A glob such as "/mnt/*" is expanded on the remote side and may include the private + // filesystem. An exclude can't be anchored reliably for an expanded list, so require + // the flag whenever the glob could reach it. + if strings.ContainsAny(remotePath, "*?[") { + if globMayReachPrivate(remotePath) { + return privateScopeRequiresFlag } return privateScopeNone @@ -55,14 +59,14 @@ func classifyRemotePath(remotePath string) privateScope { switch { case isWithin(cleaned, PrivateFilesPath): - return privateScopeInside + return privateScopeRequiresFlag case isWithin(PrivateFilesPath, cleaned): // rsync picks the transfer root from the last path element, and "." or ".." make // that root hard to predict. Without a known root an exclude can't be anchored // safely, so require the private files flag instead. if trimmed := strings.TrimRight(remotePath, "/"); trimmed != "" { if base := path.Base(trimmed); base == "." || base == ".." { - return privateScopeInside + return privateScopeRequiresFlag } } @@ -72,6 +76,35 @@ func classifyRemotePath(remotePath string) privateScope { } } +// globMayReachPrivate reports whether an absolute glob could expand to the private filesystem, +// a path within it, or one of its parents. Shell globs don't match across "/", so each element +// is matched against the element of PrivateFilesPath at the same depth. Globs containing "." or +// ".." elements, or invalid patterns, are assumed to reach it. +func globMayReachPrivate(glob string) bool { + var elements []string + for _, e := range strings.Split(glob, "/") { + switch e { + case "": + continue + case ".", "..": + return true + } + elements = append(elements, e) + } + + for i := 0; i < len(elements) && i < len(privateFilesElements); i++ { + matched, err := path.Match(elements[i], privateFilesElements[i]) + if err != nil { + return true + } + if !matched { + return false + } + } + + return true +} + // isWithin reports whether child is the same path as parent or is below it. func isWithin(child, parent string) bool { if child == parent || parent == "/" { diff --git a/internal/confirmation/confirmation.go b/internal/confirmation/confirmation.go index cb9f194..6adf4b2 100644 --- a/internal/confirmation/confirmation.go +++ b/internal/confirmation/confirmation.go @@ -2,6 +2,8 @@ package confirmation import ( "fmt" + "io" + "os" "github.com/skpr/cli/internal/slice" ) @@ -19,6 +21,12 @@ var ( // use with caution. This confirmation prompt is here to // protect you. func Confirm(force bool, message string) bool { + return confirm(os.Stdin, os.Stdout, force, message) +} + +// confirm prompts on out and reads the answer from in. An empty answer, or one that +// can't be read (e.g. no input is attached), counts as "no". +func confirm(in io.Reader, out io.Writer, force bool, message string) bool { if force { return true } @@ -26,13 +34,9 @@ func Confirm(force bool, message string) bool { message = "Are you sure? [yes/no]" } var response string - fmt.Printf("%s: ", message) - _, err := fmt.Scanln(&response) - if err != nil { - panic(err) - } - if slice.Contains(okayResponses, response) { - return true + fmt.Fprintf(out, "%s: ", message) + if _, err := fmt.Fscanln(in, &response); err != nil { + return false } - return false + return slice.Contains(okayResponses, response) } diff --git a/internal/confirmation/confirmation_test.go b/internal/confirmation/confirmation_test.go new file mode 100644 index 0000000..e99fd98 --- /dev/null +++ b/internal/confirmation/confirmation_test.go @@ -0,0 +1,31 @@ +package confirmation + +import ( + "io" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConfirm(t *testing.T) { + tests := []struct { + name string + input string + force bool + want bool + }{ + {name: "yes", input: "yes\n", want: true}, + {name: "short yes", input: "y\n", want: true}, + {name: "no", input: "no\n", want: false}, + {name: "empty line", input: "\n", want: false}, + {name: "no input", input: "", want: false}, + {name: "force", input: "", force: true, want: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, confirm(strings.NewReader(tt.input), io.Discard, tt.force, "")) + }) + } +} From 8ece66f33d9cc360ad243a14cce1e2f5abd78b39 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Mon, 2 Nov 2026 11:40:11 +1100 Subject: [PATCH 3/4] test(rsync): Consolidate rsync tests (PSEC-22) Merge the generateArgs tests into one table and group private file plan cases by outcome, checking each private source with and without the flag and dry run. --- internal/command/rsync/command_test.go | 226 ++++++++++--------------- 1 file changed, 94 insertions(+), 132 deletions(-) diff --git a/internal/command/rsync/command_test.go b/internal/command/rsync/command_test.go index 07a0875..e477f81 100644 --- a/internal/command/rsync/command_test.go +++ b/internal/command/rsync/command_test.go @@ -8,150 +8,112 @@ import ( ) func TestGenerateArgs(t *testing.T) { - c := Command{ - Source: "source", - Destination: "destination", + tests := []struct { + name string + command Command + privateExclude string + want []string + }{ + { + name: "defaults", + command: Command{Source: "source", Destination: "destination"}, + want: []string{"-avz", "--progress", "-e", "skpr-rsh", "source", "destination"}, + }, + { + name: "all options", + command: Command{ + Source: "dev:/mnt/", + Destination: "destination", + Excludes: []string{"js/*", "css/*"}, + ExcludeFrom: "foo.txt", + DryRun: true, + }, + privateExclude: "/private/", + // The private exclude comes first so user patterns can't re-include private files. + want: []string{ + "-avz", "--progress", "-e", "skpr-rsh", + "--exclude", "/private/", + "--exclude", "js/*", + "--exclude", "css/*", + "--exclude-from", "foo.txt", + "--dry-run", + "dev:/mnt/", "destination", + }, + }, } - args := c.generateArgs("skpr", "") - - assert.Equal(t, []string{ - "-avz", - "--progress", - "-e", "skpr-rsh", - "source", - "destination", - }, args) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, tt.command.generateArgs("skpr", tt.privateExclude)) + }) + } } -func TestGenerateArgsExcludes(t *testing.T) { - c := Command{ - Source: "source", - Destination: "destination", - Excludes: []string{ - "js/*", - "css/*", - "styles/*", - }, +func TestPlanPrivateFiles(t *testing.T) { + plan := func(c Command) (privateFilesPlan, error) { + c.Destination = "destination" + return c.planPrivateFiles() } - args := c.generateArgs("skpr", "") + t.Run("not private", func(t *testing.T) { + for source, env := range map[string]string{ + "./private/": "", + "./a:b/": "", + "dev:/mnt/public/": "dev", + "dev:files/": "dev", + "dev:/mnt/private-old/": "dev", + "dev:/mnt/pub*/": "dev", + "dev:/var/*": "dev", + } { + got, err := plan(Command{Source: source}) + assert.NoError(t, err, source) + assert.Equal(t, privateFilesPlan{environment: env}, got, source) + } + }) - assert.Equal(t, []string{ - "-avz", - "--progress", - "-e", "skpr-rsh", - "--exclude", "js/*", - "--exclude", "css/*", - "--exclude", "styles/*", - "source", - "destination", - }, args) -} + t.Run("requires flag", func(t *testing.T) { + for _, source := range []string{ + "dev:/mnt/private/", + "dev:/mnt/private/webform", + "dev:/mnt/public/../private", + "dev:/mnt/.", + "dev:/mnt/*", + "dev:/mnt/priv*/", + "dev:/mnt/private/*.pdf", + "dev:/m*", + "dev:/var/*/../mnt/private", + } { + _, err := plan(Command{Source: source}) + assert.ErrorIs(t, err, ErrPrivateFilesNotIncluded, source) -func TestGenerateArgsExcludeFrom(t *testing.T) { - c := Command{ - Source: "source", - Destination: "destination", - ExcludeFrom: "foo.txt", - } - args := c.generateArgs("skpr", "") - assert.Equal(t, []string{ - "-avz", - "--progress", - "-e", "skpr-rsh", - "--exclude-from", "foo.txt", - "source", - "destination", - }, args) -} + _, err = plan(Command{Source: source, DryRun: true}) + assert.ErrorIs(t, err, ErrPrivateFilesNotIncluded, source) -func TestGenerateDryRun(t *testing.T) { - c := Command{ - Source: "source", - Destination: "destination", - DryRun: true, - } - args := c.generateArgs("skpr", "") - assert.Equal(t, []string{ - "-avz", - "--progress", - "-e", "skpr-rsh", - "--dry-run", - "source", - "destination", - }, args) -} + got, err := plan(Command{Source: source, IncludePrivate: true}) + assert.NoError(t, err, source) + assert.Equal(t, privateFilesPlan{environment: "dev", confirm: true}, got, source) -func TestGenerateArgsPrivateExclude(t *testing.T) { - c := Command{ - Source: "dev:/mnt/", - Destination: "destination", - Excludes: []string{"js/*"}, - } - args := c.generateArgs("skpr", "/private/") - assert.Equal(t, []string{ - "-avz", - "--progress", - "-e", "skpr-rsh", - "--exclude", "/private/", - "--exclude", "js/*", - "dev:/mnt/", - "destination", - }, args) -} + got, err = plan(Command{Source: source, IncludePrivate: true, DryRun: true}) + assert.NoError(t, err, source) + assert.Equal(t, privateFilesPlan{environment: "dev"}, got, source) + } + }) -func TestPlanPrivateFiles(t *testing.T) { - tests := []struct { - name string - source string - includePrivate bool - dryRun bool - want privateFilesPlan - wantErr error - }{ - {name: "upload", source: "./private/", want: privateFilesPlan{}}, - {name: "local path with colon", source: "./a:b/", want: privateFilesPlan{}}, - {name: "public download", source: "dev:/mnt/public/", want: privateFilesPlan{environment: "dev"}}, - {name: "relative download", source: "dev:files/", want: privateFilesPlan{environment: "dev"}}, - {name: "similar name", source: "dev:/mnt/private-old/", want: privateFilesPlan{environment: "dev"}}, - {name: "private without flag", source: "dev:/mnt/private/", wantErr: ErrPrivateFilesNotIncluded}, - {name: "private subdirectory without flag", source: "dev:/mnt/private/webform", wantErr: ErrPrivateFilesNotIncluded}, - {name: "private via traversal without flag", source: "dev:/mnt/public/../private", wantErr: ErrPrivateFilesNotIncluded}, - {name: "private glob without flag", source: "dev:/mnt/*", wantErr: ErrPrivateFilesNotIncluded}, - {name: "matching glob without flag", source: "dev:/mnt/priv*/", wantErr: ErrPrivateFilesNotIncluded}, - {name: "glob within private without flag", source: "dev:/mnt/private/*.pdf", wantErr: ErrPrivateFilesNotIncluded}, - {name: "glob parent without flag", source: "dev:/m*", wantErr: ErrPrivateFilesNotIncluded}, - {name: "glob with traversal without flag", source: "dev:/var/*/../mnt/private", wantErr: ErrPrivateFilesNotIncluded}, - {name: "public glob", source: "dev:/mnt/pub*/", want: privateFilesPlan{environment: "dev"}}, - {name: "unrelated glob", source: "dev:/var/*", want: privateFilesPlan{environment: "dev"}}, - {name: "parent ending in dot without flag", source: "dev:/mnt/.", wantErr: ErrPrivateFilesNotIncluded}, - {name: "private with flag", source: "dev:/mnt/private/", includePrivate: true, want: privateFilesPlan{environment: "dev", confirm: true}}, - {name: "parent with trailing slash", source: "dev:/mnt/", want: privateFilesPlan{environment: "dev", exclude: "/private/"}}, - {name: "parent without trailing slash", source: "dev:/mnt", want: privateFilesPlan{environment: "dev", exclude: "/mnt/private/"}}, - {name: "root", source: "dev:/", want: privateFilesPlan{environment: "dev", exclude: "/mnt/private/"}}, - {name: "parent with flag", source: "dev:/mnt/", includePrivate: true, want: privateFilesPlan{environment: "dev", confirm: true}}, - {name: "private dry run with flag", source: "dev:/mnt/private/", includePrivate: true, dryRun: true, want: privateFilesPlan{environment: "dev"}}, - {name: "private dry run without flag", source: "dev:/mnt/private/", dryRun: true, wantErr: ErrPrivateFilesNotIncluded}, - } + t.Run("parent", func(t *testing.T) { + for source, exclude := range map[string]string{ + "dev:/mnt/": "/private/", + "dev:/mnt": "/mnt/private/", + "dev:/": "/mnt/private/", + } { + got, err := plan(Command{Source: source}) + assert.NoError(t, err, source) + assert.Equal(t, privateFilesPlan{environment: "dev", exclude: exclude}, got, source) - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - c := Command{ - Source: tt.source, - Destination: "destination", - IncludePrivate: tt.includePrivate, - DryRun: tt.dryRun, - } - got, err := c.planPrivateFiles() - if tt.wantErr != nil { - assert.ErrorIs(t, err, tt.wantErr) - return - } - assert.NoError(t, err) - assert.Equal(t, tt.want, got) - }) - } + got, err = plan(Command{Source: source, IncludePrivate: true}) + assert.NoError(t, err, source) + assert.Equal(t, privateFilesPlan{environment: "dev", confirm: true}, got, source) + } + }) } func TestRunPrivateDeclined(t *testing.T) { From 9160aa5c7c575c88bd60668e10533cab0c6b60c4 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Mon, 2 Nov 2026 11:42:19 +1100 Subject: [PATCH 4/4] docs(rsync): Trim verbose comments (PSEC-22) --- internal/command/rsync/command.go | 14 ++++----- internal/command/rsync/command_test.go | 2 +- internal/command/rsync/private.go | 40 +++++++++----------------- internal/confirmation/confirmation.go | 3 +- 4 files changed, 20 insertions(+), 39 deletions(-) diff --git a/internal/command/rsync/command.go b/internal/command/rsync/command.go index e71aee6..8ca85dd 100644 --- a/internal/command/rsync/command.go +++ b/internal/command/rsync/command.go @@ -11,7 +11,7 @@ import ( "github.com/skpr/cli/internal/confirmation" ) -// ErrPrivateFilesNotIncluded is returned when a sync would download private files without the flag to allow it. +// ErrPrivateFilesNotIncluded is returned when a download includes private files without --include-private. var ErrPrivateFilesNotIncluded = errors.New("this sync would download private files, which may contain personal information. " + "Only download private files to investigate a specific issue, with approval. " + "Use --include-private to download them") @@ -26,7 +26,7 @@ type Command struct { IncludePrivate bool Force bool - // confirm prompts the user; it defaults to confirmation.Confirm and is replaced in tests. + // confirm defaults to confirmation.Confirm. confirm func(force bool, message string) bool } @@ -69,17 +69,13 @@ func (c *Command) Run(_ context.Context) error { // privateFilesPlan describes how a sync handles private files. type privateFilesPlan struct { - // environment the files are downloaded from. environment string - // confirm is true when the user must confirm a private file download. - confirm bool - // exclude is an rsync exclude pattern that skips private files, or empty when none is needed. + confirm bool + // exclude is an rsync pattern that skips private files. exclude string } -// planPrivateFiles decides how the sync handles private files. Private files are only -// downloaded with IncludePrivate set, and only after confirmation. A dry run transfers -// nothing, so it needs the flag but not the confirmation. +// planPrivateFiles decides how the sync handles private files. Dry runs skip confirmation. func (c *Command) planPrivateFiles() (privateFilesPlan, error) { env, remotePath, ok := splitRemote(c.Source) if !ok { diff --git a/internal/command/rsync/command_test.go b/internal/command/rsync/command_test.go index e477f81..3c2d405 100644 --- a/internal/command/rsync/command_test.go +++ b/internal/command/rsync/command_test.go @@ -29,7 +29,7 @@ func TestGenerateArgs(t *testing.T) { DryRun: true, }, privateExclude: "/private/", - // The private exclude comes first so user patterns can't re-include private files. + // The private exclude comes first so user patterns can't override it. want: []string{ "-avz", "--progress", "-e", "skpr-rsh", "--exclude", "/private/", diff --git a/internal/command/rsync/private.go b/internal/command/rsync/private.go index db03c4f..18217b2 100644 --- a/internal/command/rsync/private.go +++ b/internal/command/rsync/private.go @@ -5,28 +5,24 @@ import ( "strings" ) -// PrivateFilesPath is where the private filesystem is mounted in an environment. +// PrivateFilesPath is where the private filesystem is mounted. const PrivateFilesPath = "/mnt/private" // privateScope describes how a remote rsync source relates to the private filesystem. type privateScope int const ( - // privateScopeNone means the source does not include private files. privateScopeNone privateScope = iota - // privateScopeRequiresFlag means the source is, or may expand to, the private filesystem - // or a path within it, so downloading it requires the private files flag. + // privateScopeRequiresFlag means the source is, or may expand to, private files. privateScopeRequiresFlag - // privateScopeParent means the source is a parent directory of the private filesystem, - // which can be synced with the private filesystem excluded. + // privateScopeParent means the source contains the private filesystem, which can be excluded. privateScopeParent ) // privateFilesElements are the path elements of PrivateFilesPath. var privateFilesElements = strings.Split(strings.TrimPrefix(PrivateFilesPath, "/"), "/") -// splitRemote splits an rsync remote location ("env:path") into its environment and path. -// The boolean is false when the location is a local path. +// splitRemote splits "env:path" into its parts. The boolean is false for local paths. func splitRemote(location string) (string, string, bool) { env, remotePath, found := strings.Cut(location, ":") if !found || env == "" || strings.Contains(env, "/") { @@ -36,17 +32,14 @@ func splitRemote(location string) (string, string, bool) { return env, remotePath, true } -// classifyRemotePath reports how a remote source path relates to the private filesystem. -// Relative paths are resolved by the remote shell and cannot be checked here, so they are -// treated as not including private files. +// classifyRemotePath reports how a remote path relates to the private filesystem. +// Relative paths can't be resolved locally, so they aren't checked. func classifyRemotePath(remotePath string) privateScope { if !path.IsAbs(remotePath) { return privateScopeNone } - // A glob such as "/mnt/*" is expanded on the remote side and may include the private - // filesystem. An exclude can't be anchored reliably for an expanded list, so require - // the flag whenever the glob could reach it. + // Globs expand remotely, so an exclude can't be anchored for them. if strings.ContainsAny(remotePath, "*?[") { if globMayReachPrivate(remotePath) { return privateScopeRequiresFlag @@ -61,9 +54,7 @@ func classifyRemotePath(remotePath string) privateScope { case isWithin(cleaned, PrivateFilesPath): return privateScopeRequiresFlag case isWithin(PrivateFilesPath, cleaned): - // rsync picks the transfer root from the last path element, and "." or ".." make - // that root hard to predict. Without a known root an exclude can't be anchored - // safely, so require the private files flag instead. + // A trailing "." or ".." makes the transfer root, and so the exclude, unpredictable. if trimmed := strings.TrimRight(remotePath, "/"); trimmed != "" { if base := path.Base(trimmed); base == "." || base == ".." { return privateScopeRequiresFlag @@ -76,10 +67,8 @@ func classifyRemotePath(remotePath string) privateScope { } } -// globMayReachPrivate reports whether an absolute glob could expand to the private filesystem, -// a path within it, or one of its parents. Shell globs don't match across "/", so each element -// is matched against the element of PrivateFilesPath at the same depth. Globs containing "." or -// ".." elements, or invalid patterns, are assumed to reach it. +// globMayReachPrivate reports whether a glob could expand to the private filesystem, a path +// within it, or a parent. Globs don't cross "/", so elements are matched depth by depth. func globMayReachPrivate(glob string) bool { var elements []string for _, e := range strings.Split(glob, "/") { @@ -105,7 +94,7 @@ func globMayReachPrivate(glob string) bool { return true } -// isWithin reports whether child is the same path as parent or is below it. +// isWithin reports whether child is parent or below it. func isWithin(child, parent string) bool { if child == parent || parent == "/" { return true @@ -114,11 +103,8 @@ func isWithin(child, parent string) bool { return strings.HasPrefix(child, parent+"/") } -// privateExcludePattern returns an anchored rsync exclude pattern that matches the private -// filesystem when syncing from remotePath, which must be a parent of the private filesystem. -// -// rsync anchors patterns to the root of the transfer. With a trailing slash ("/mnt/") the -// root is the directory itself, otherwise ("/mnt") it is the directory's parent. +// privateExcludePattern returns an exclude for the private filesystem, anchored to the +// transfer root: "/mnt/" syncs from /mnt, "/mnt" syncs from /. func privateExcludePattern(remotePath string) string { root := path.Clean(remotePath) if !strings.HasSuffix(remotePath, "/") { diff --git a/internal/confirmation/confirmation.go b/internal/confirmation/confirmation.go index 6adf4b2..378d72b 100644 --- a/internal/confirmation/confirmation.go +++ b/internal/confirmation/confirmation.go @@ -24,8 +24,7 @@ func Confirm(force bool, message string) bool { return confirm(os.Stdin, os.Stdout, force, message) } -// confirm prompts on out and reads the answer from in. An empty answer, or one that -// can't be read (e.g. no input is attached), counts as "no". +// confirm prompts on out and reads from in. An empty or unreadable answer is "no". func confirm(in io.Reader, out io.Writer, force bool, message string) bool { if force { return true