Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/OPERATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ cuttle pw detach # optional; the browser stays up
(the first 40 lines follow the link). In the same exec, `cuttle pw` fetches
the file through the daemon's loopback `GET /snapshot` route into
`$XDG_STATE_HOME/cuttle/<instance>/snapshots/` (default `~/.local/state`; dir
0700, files 0600, the newest 50 kept) and rewrites the link to that path. The
0700, files 0600, the newest 50 kept) and rewrites the link to that path.
`<instance>` is the `--name` (the context name on k8s), so the default
instance's copies sit under `cuttle/cuttle/` beside the other instances';
discarding the profile (`down --purge`, `purge-profile`, `up
--purge-profile`) removes that instance's dir with it. The
daemon replaces every value its secret store holds for the session with its
`{{cuttle:NAME}}` sentinel first, and the current value of every
`<input type=password>` on the session's open pages (same-origin iframes
Expand Down
25 changes: 19 additions & 6 deletions packages/cuttle/internal/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func newUpCmd() *cobra.Command {
cmd.Flags().Lookup("keep-profile").NoOptDefVal = noOptDefTrue
_ = cmd.Flags().MarkHidden("keep-profile")
cmd.Flags().BoolVar(&uf.ephemeral, "ephemeral", false, "use a disposable profile: no persistent volume, discarded on recreate/down --purge (opt out of the default persistent profile)")
cmd.Flags().BoolVar(&uf.purgeProfile, "purge-profile", false, "remove the persistent profile (volume on local/ssh, PVC on k8s) before starting, so it comes up with a fresh profile (implies --recreate)")
cmd.Flags().BoolVar(&uf.purgeProfile, "purge-profile", false, "remove the persistent profile (volume on local/ssh, PVC on k8s) and this host's copies of its snapshots before starting, so it comes up with a fresh profile (implies --recreate)")
cmd.Flags().BoolVar(&uf.recreate, "recreate", false, "destroy any existing container and start fresh (the persistent profile survives; add --purge-profile to also reset it)")
cmd.Flags().StringVar(&uf.idleTimeout, "idle-timeout", "", `seconds of no CDP client activity after which an idle per-seed browser is closed; "0" = off (default off)`)
cmd.Flags().StringVar(&uf.screen, "screen", "", `screen size the browser claims and is sized to, "WxH" from the image persona's table (default: the context's "screen", else the persona's largest; cuttle serve --help in the image lists the choices)`)
Expand Down Expand Up @@ -584,6 +584,9 @@ func runUp(cmd *cobra.Command, uf *upFlags) error {
if err = b.Start(cmd.Context(), opts); err != nil {
return err
}
if uf.purgeProfile {
purgeHostSnapshots(name)
}

ep, release, err := reachStable(cmd.Context(), b, uf.common)
if err != nil {
Expand Down Expand Up @@ -713,11 +716,17 @@ func newDownCmd() *cobra.Command {
if err := b.Stop(cmd.Context(), purge); err != nil {
return err
}
// The host copies of the instance's snapshots derive from the profile
// just discarded, so they go with it.
snapshots := ""
if purge && purgeHostSnapshots(name) {
snapshots = " and host snapshots"
}
switch {
case purge && state == backend.StateAbsent:
fmt.Fprintf(cmd.OutOrStdout(), "cuttle: %s was already gone; discarded any profile it left\n", locationLabel(ctxName, ctx, name))
fmt.Fprintf(cmd.OutOrStdout(), "cuttle: %s was already gone; discarded any profile%s it left\n", locationLabel(ctxName, ctx, name), snapshots)
case purge:
fmt.Fprintf(cmd.OutOrStdout(), "cuttle: removed %s (profile discarded)\n", locationLabel(ctxName, ctx, name))
fmt.Fprintf(cmd.OutOrStdout(), "cuttle: removed %s (profile%s discarded)\n", locationLabel(ctxName, ctx, name), snapshots)
default:
// An --ephemeral daemon deletes its profile as it shuts down.
profile := "profile kept"
Expand All @@ -730,7 +739,7 @@ func newDownCmd() *cobra.Command {
},
}
addCommonFlags(cmd, &cf)
cmd.Flags().BoolVar(&purge, "purge", false, "also remove the container/release and discard the persistent profile (deletes its volume/PVC)")
cmd.Flags().BoolVar(&purge, "purge", false, "also remove the container/release and discard the persistent profile (deletes its volume/PVC) and this host's copies of its snapshots")
return cmd
}

Expand All @@ -745,7 +754,7 @@ func newPurgeProfileCmd() *cobra.Command {
Short: "reset the persistent profile so the next `up` starts fresh",
Long: `Remove the persistent profile's backing store - the named Docker volume, or
the PVC on the k8s backend - so the next 'cuttle up' starts from a clean profile
with all cookies and logins discarded.
with all cookies and logins discarded; this host's copies of its snapshots go too.

The container/release is torn down so the volume can be removed; run 'cuttle up'
afterwards for a fresh session. To reset and start again in one step, use
Expand Down Expand Up @@ -783,7 +792,11 @@ func runPurgeProfile(cmd *cobra.Command, cf commonFlags) error {
} else if err := purger.PurgeProfileVolume(cmd.Context()); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "cuttle: purged the profile for %s - run `%s up` for a fresh session\n", locationLabel(ctxName, ctx, name), cuttleCmd(ctxName, ctx, name))
snapshots := ""
if purgeHostSnapshots(name) {
snapshots = " and host snapshots"
}
fmt.Fprintf(cmd.OutOrStdout(), "cuttle: purged the profile%s for %s - run `%s up` for a fresh session\n", snapshots, locationLabel(ctxName, ctx, name), cuttleCmd(ctxName, ctx, name))
return nil
}

Expand Down
49 changes: 49 additions & 0 deletions packages/cuttle/internal/cli/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ func runFakeInstance(t *testing.T, fi fakeInstance, serve func(log string), args
t.Setenv("FAKE_DOCKER_BINDINGS", bindings)
t.Setenv("FAKE_DOCKER_PORTS", ports)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_STATE_HOME", t.TempDir()) // a purge must never reach the developer's own snapshots
setSelectorEnv(t, config.EnvContext, "")
setSelectorEnv(t, config.EnvName, "")
withInstance(t, instanceFlags{})
Expand Down Expand Up @@ -665,6 +666,54 @@ func TestDownPurgeOnAnAbsentInstance(t *testing.T) {
}
}

// Every path that discards the profile takes the instance's host snapshot dir
// with it, and only that instance's; a plain `down` leaves it.
func TestProfilePurgeRemovesHostSnapshots(t *testing.T) {
// runFakeInstance isolates the state dir per call, so the dirs are seeded
// inside it, once the env is set.
var mine, other string
seed := func(string) {
mine, other = instanceSnapshotDir("fs-x"), instanceSnapshotDir("other")
for _, dir := range []string{mine, other} {
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "page-2026-01-01T00-00-00-000Z.yml"), []byte("- x\n"), 0o600); err != nil {
t.Fatal(err)
}
}
}
out, _, err := runFakeInstance(t, fakeInstance{state: "running"}, seed, "down")
if _, statErr := os.Stat(mine); err != nil || statErr != nil || strings.Contains(out, "snapshots") {
t.Fatalf("plain down: err=%v stat=%v out=%q", err, statErr, out)
}
cdp, _ := serveDaemon(t)
for _, tc := range []struct {
args []string
fi fakeInstance
want string
}{
{args: []string{"down", "--purge"}, fi: fakeInstance{state: "running"}, want: "profile and host snapshots discarded"},
{args: []string{"purge-profile"}, fi: fakeInstance{state: "running"}, want: "purged the profile and host snapshots"},
{args: []string{"up", "--purge-profile"}, fi: fakeInstance{state: "running", cdp: cdp, vnc: freePort(t)}, want: "fresh identity"},
} {
out, _, err = runFakeInstance(t, tc.fi, seed, tc.args...)
if err != nil || !strings.Contains(out, tc.want) {
t.Fatalf("%v: err=%v out=%q", tc.args, err, out)
}
if _, statErr := os.Stat(filepath.Dir(mine)); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("%v: %s survived: %v", tc.args, filepath.Dir(mine), statErr)
}
if _, statErr := os.Stat(other); statErr != nil {
t.Fatalf("%v: another instance's snapshots were purged: %v", tc.args, statErr)
}
}
out, _, err = runFakeInstance(t, fakeInstance{}, nil, "down", "--purge")
if err != nil || strings.Contains(out, "snapshots") {
t.Fatalf("down --purge with none left: err=%v out=%q", err, out)
}
}

// `up --recreate` without --image moves the container to this CLI's image - the
// upgrade path - and says so rather than switching silently.
func TestUpRecreateNamesAnImageChange(t *testing.T) {
Expand Down
42 changes: 39 additions & 3 deletions packages/cuttle/internal/cli/playwright.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,12 +739,48 @@ func snapshotArgv(argv []string) []string {
// snapshotHostDir is where this instance's snapshots land on the host, or ""
// when no state dir resolves.
func snapshotHostDir() string {
state := xdg.StateDir()
name, _, _, _, err := resolve(commonFlags{}, defaultImage())
if state == "" || err != nil {
if err != nil {
return ""
}
return instanceSnapshotDir(name)
}

// instanceSnapshotDir is $XDG_STATE_HOME/cuttle/<name>/snapshots - the instance
// name is a level of its own, so the default instance lands under
// cuttle/cuttle/ - or "" when no state dir resolves. On k8s the name is the
// context name, which unlike a container name is never validated, so a name
// with a `..` element that would leave cuttle/ resolves to "" rather than
// steer the write or the purge elsewhere.
func instanceSnapshotDir(name string) string {
state := xdg.StateDir()
if state == "" {
return ""
}
base := filepath.Join(state, "cuttle")
dir := filepath.Join(base, name, "snapshots")
if !strings.HasPrefix(dir, base+string(filepath.Separator)) {
return ""
}
return filepath.Join(state, "cuttle", name, "snapshots")
return dir
}

// purgeHostSnapshots removes an instance's host snapshot dir - derived data
// that goes with the profile, so every path that discards the profile calls
// it - and its then-empty parent, reporting whether there was one to remove.
func purgeHostSnapshots(name string) bool {
dir := instanceSnapshotDir(name)
if dir == "" {
return false
}
if _, err := os.Stat(dir); err != nil {
return false
}
if err := os.RemoveAll(dir); err != nil {
return false
}
_ = os.Remove(filepath.Dir(dir)) // only when nothing else is in it
return true
}

// replayHost writes a successful verb's output as this host shows it: the
Expand Down
15 changes: 15 additions & 0 deletions packages/cuttle/internal/cli/playwright_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,21 @@ func TestSnapshotArgv(t *testing.T) {
}
}

// A k8s context name is never validated, so one that would leave cuttle/ gets no
// host snapshot dir at all: nothing is written there and nothing purged.
func TestInstanceSnapshotDirStaysUnderCuttle(t *testing.T) {
state := t.TempDir()
t.Setenv("XDG_STATE_HOME", state)
if got, want := instanceSnapshotDir("fs-x"), filepath.Join(state, "cuttle", "fs-x", "snapshots"); got != want {
t.Fatalf("got %q, want %q", got, want)
}
for _, name := range []string{"..", "../../x", "a/../.."} {
if got := instanceSnapshotDir(name); got != "" {
t.Fatalf("%q: got %q, want none", name, got)
}
}
}

// The appended snapshot is kept on the host with owner-only modes and a bounded
// history, and the printed link points at the copy; anything short of that
// leaves the driver's output as it printed it.
Expand Down
Loading