cli: releases pull locks its directory and refuses Windows aliases
#1389 final review, findings 1 and 2. A pull now creates <out>/.carlos-pull.lock exclusively before it checks for a manifest and removes it on every exit, so two pulls never write into one directory. A lock already there refuses the pull and is left alone. validPulledFile also refuses, on every system, names Windows reads as another file or a device: a trailing dot or space, < > : " | ? *, control characters, CON PRN AUX NUL COM1-9 LPT1-9 with or without an extension, and the pull's lock. The duplicate check compares names case-folded with trailing dots and spaces trimmed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
release_pull.go+67 −12release_pull_test.go+97 −0ship_from_release_test.go+20 −0
Intent
<task-notification> <task-id>a925b95c13e55a4c3</task-id> <tool-use-id>toolu_014SEjK8yqpRbydGmMD4wBMb</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/a88005ac-72e4-481c-884f-581940b8c2c5/tasks/a925b95c13e55a4c3.output</output-file> <status>completed</status> <summary>Agent "Apply Luna copy changes" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: done. Rows 1–14 and 16 are applied verbatim, and rows 15 and 17 are skipped as instructed. Commit: 108f5fd7 "copy: plain refusals and docs for release copy". The packed-refs.lock warning appeared, but the commit landed and the tree is clean. Tests: `go vet`, the console `TestAPIShip|TestAPIRelease` run, the root targeted tests, `internal/docsite`, `internal/parity` and `make code-health` all pass. The darwin and windows builds succeed. `make parity-doc` regenerated `docs/parity.md`. Not verbatim: - Rows 9–11 landed on the successor lines in `internal/console/apiship_expect.go` (76, 78, 80), since the code had moved into the helpers. - Row 2 dropped the `len(m.Artifacts)` argument at the call site, so `m` is now `_` in `release_pull.go`. Report: `.superpowers/sdd/2026-11-25-release-copy-across-consoles/luna-apply-report.md`</result> <usage><subagent_tokens>53478</subagent_tokens><tool_uses>11</tool_uses><duration_ms>285985</duration_ms></usage> </task-notification>
diff --git a/release_pull.go b/release_pull.go| index dbde66ac7..049391e33 100644 |
| --- a/release_pull.go |
| +++ b/release_pull.go |
| @@ -39,6 +39,10 @@ const ( |
| releasesPullUsage = "usage: carlos releases pull <version> --app <a> --out <dir> [--account <sqid-or-name>] [--account-id <sqid>] [--console <origin>]" |
| releasesPullNeedsConsole = "carlos releases pull needs a console. unset CARLOS_DEPLOYMENT_BUCKET and CARLOS_DEPLOYMENT_DIR, then set CARLOS_CONSOLE or run carlos login" |
| releasesPullDone = "pulled %s %s into %s. every file matches its digest" |
| + releasesPullLocked = "another pull is writing to %s. if none is running, remove %s and pull again" |
| + |
| + // pullLockName is the file a pull holds its directory with. |
| + pullLockName = ".carlos-pull.lock" |
| ) |
| // runReleasesPull is `carlos releases pull <version> --app <a> --out <dir>`. |
| @@ -96,15 +100,20 @@ func runReleasesPull(args []string) error { |
| // a directory without it is an interrupted pull, which a second pull |
| // finishes. |
| func pullRelease(ctx context.Context, sess *apiSession, a apiAcct, app, version, out string) (*release.Manifest, error) { |
| + if err := os.MkdirAll(out, 0o755); err != nil { |
| + return nil, err |
| + } |
| + unlock, err := lockPullDir(out) |
| + if err != nil { |
| + return nil, err |
| + } |
| + defer unlock() |
| manifestPath := filepath.Join(out, "manifest.json") |
| if _, err := os.Lstat(manifestPath); err == nil { |
| return nil, alreadyHoldsRelease(out) |
| } else if !errors.Is(err, os.ErrNotExist) { |
| return nil, err |
| } |
| - if err := os.MkdirAll(out, 0o755); err != nil { |
| - return nil, err |
| - } |
| raw, err := sess.releaseManifestRaw(ctx, a, app, version) |
| if err != nil { |
| return nil, err |
| @@ -125,9 +134,9 @@ func pullRelease(ctx context.Context, sess *apiSession, a apiAcct, app, version, |
| if !validPulledFile(art.File) { |
| return nil, fmt.Errorf("the release names a file %q that cannot be written safely", art.File) |
| } |
| - // Folded, because macOS and Windows file systems are: two names |
| - // differing only in case would land on one file there. |
| - fold := strings.ToLower(art.File) |
| + // Normalised as macOS and Windows file systems do: two names that |
| + // differ only in case would land on one file there. |
| + fold := pulledFileKey(art.File) |
| if files[fold] { |
| return nil, fmt.Errorf("the release names the file %q twice", art.File) |
| } |
| @@ -151,6 +160,26 @@ func pullRelease(ctx context.Context, sess *apiSession, a apiAcct, app, version, |
| return &m, nil |
| } |
| +// lockPullDir takes out's pull lock: it creates out/.carlos-pull.lock |
| +// exclusively, so two pulls never write into one directory at once, and |
| +// refuses when the lock already exists. unlock removes the lock this pull |
| +// created, and only that. |
| +func lockPullDir(out string) (unlock func(), err error) { |
| + lock := filepath.Join(out, pullLockName) |
| + f, err := os.OpenFile(lock, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644) |
| + if errors.Is(err, fs.ErrExist) { |
| + return nil, fmt.Errorf(releasesPullLocked, out, lock) |
| + } |
| + if err != nil { |
| + return nil, err |
| + } |
| + if err := f.Close(); err != nil { |
| + _ = os.Remove(lock) |
| + return nil, err |
| + } |
| + return func() { _ = os.Remove(lock) }, nil |
| +} |
| + |
| // alreadyHoldsRelease is the refusal of a directory with a manifest.json. |
| func alreadyHoldsRelease(out string) error { |
| return fmt.Errorf("%s already holds a release. pull into a new or empty directory", out) |
| @@ -208,20 +237,46 @@ func pullArtifactBounded(ctx context.Context, sess *apiSession, a apiAcct, app, |
| } |
| // validPulledFile reports whether a manifest's File name can be written |
| -// into the pull directory as itself: one plain name, not the manifest, not |
| -// a partial download's name (<file>.partial, or a temp file's |
| +// into the pull directory as itself, on every system a pull or a ship may |
| +// run on: one plain name, not the manifest, not the pull's lock, not a |
| +// partial download's name (<file>.partial, or a temp file's |
| // <file>.partial-*). Those compare without case, as macOS and Windows file |
| -// systems do. |
| +// systems do. Names Windows would read as another file or a device are |
| +// refused everywhere: a trailing dot or space (Windows drops them, so |
| +// "manifest.json." is manifest.json), the characters < > : " | ? *, |
| +// control characters, and the device names CON, PRN, AUX, NUL, COM1-9 and |
| +// LPT1-9, alone or with an extension. |
| func validPulledFile(name string) bool { |
| lower := strings.ToLower(name) |
| return name != "" && name == filepath.Base(name) && |
| - !strings.ContainsAny(name, `/\`) && |
| - name != "." && name != ".." && |
| - lower != "manifest.json" && |
| + !strings.ContainsAny(name, `/\<>:"|?*`) && |
| + !strings.ContainsFunc(name, func(r rune) bool { return r < 0x20 }) && |
| + !strings.HasSuffix(name, ".") && !strings.HasSuffix(name, " ") && |
| + !windowsDeviceName(lower) && |
| + lower != "manifest.json" && lower != pullLockName && |
| !strings.HasSuffix(lower, ".partial") && |
| !strings.Contains(lower, ".partial-") |
| } |
| +// windowsDeviceName reports whether the lower-case name is a Windows device |
| +// (con, prn, aux, nul, com1-9, lpt1-9), alone or with an extension. |
| +func windowsDeviceName(lower string) bool { |
| + stem, _, _ := strings.Cut(lower, ".") |
| + stem = strings.TrimRight(stem, " ") |
| + switch stem { |
| + case "con", "prn", "aux", "nul": |
| + return true |
| + } |
| + return len(stem) == 4 && (strings.HasPrefix(stem, "com") || strings.HasPrefix(stem, "lpt")) && |
| + stem[3] >= '1' && stem[3] <= '9' |
| +} |
| + |
| +// pulledFileKey is name as macOS and Windows file systems compare it: case |
| +// folded, trailing dots and spaces dropped. |
| +func pulledFileKey(name string) string { |
| + return strings.ToLower(strings.TrimRight(name, ". ")) |
| +} |
| + |
| // releasePath is the read doors' path for one release of app. |
| func releasePath(a apiAcct, app, version, tail string) string { |
| return apiPath(a, app, "/releases/"+url.PathEscape(version)+tail) |
diff --git a/release_pull_test.go b/release_pull_test.go| index 3cb35d4f1..4ed5372fa 100644 |
| --- a/release_pull_test.go |
| +++ b/release_pull_test.go |
| @@ -127,6 +127,64 @@ func TestReleasesPullFinishesAnIncompleteDir(t *testing.T) { |
| noPartials(t, dir) |
| } |
| +// noLock fails when dir still holds a pull's lock. |
| +func noLock(t *testing.T, dir string) { |
| + t.Helper() |
| + if _, err := os.Lstat(filepath.Join(dir, pullLockName)); !os.IsNotExist(err) { |
| + t.Fatalf("%s left behind in %s (stat: %v)", pullLockName, dir, err) |
| + } |
| +} |
| + |
| +// A lock already in the directory refuses the pull before anything is |
| +// written, and the lock is left as it was. |
| +func TestReleasesPullRefusesALockedDir(t *testing.T) { |
| + useTempCredentials(t) |
| + clearDeploymentEnv(t) |
| + a := shipPullSource(t) |
| + dir := t.TempDir() |
| + lock := filepath.Join(dir, pullLockName) |
| + held := []byte("held by another pull") |
| + if err := os.WriteFile(lock, held, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + err := pullV12(t, a, dir) |
| + want := "another pull is writing to " + dir + ". if none is running, remove " + lock + " and pull again" |
| + if err == nil || err.Error() != want { |
| + t.Fatalf("pull into a locked dir = %v, want %q", err, want) |
| + } |
| + ents, err := os.ReadDir(dir) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(ents) != 1 || ents[0].Name() != pullLockName { |
| + var names []string |
| + for _, e := range ents { |
| + names = append(names, e.Name()) |
| + } |
| + t.Fatalf("%s holds %v after a refused pull, want only the lock", dir, names) |
| + } |
| + if got, _ := os.ReadFile(lock); !bytes.Equal(got, held) { |
| + t.Fatalf("lock = %q, want it untouched", got) |
| + } |
| +} |
| + |
| +func TestReleasesPullRemovesItsLock(t *testing.T) { |
| + useTempCredentials(t) |
| + clearDeploymentEnv(t) |
| + a := shipPullSource(t) |
| + dir := filepath.Join(t.TempDir(), "release") |
| + if err := pullV12(t, a, dir); err != nil { |
| + t.Fatalf("pull: %v", err) |
| + } |
| + noLock(t, dir) |
| + // A refused second pull takes the lock, finds the manifest and lets go. |
| + err := runReleasesPull([]string{"v12", "--app", "shop", "--out", dir}) |
| + if err == nil || !strings.Contains(err.Error(), "already holds a release") { |
| + t.Fatalf("second pull = %v, want it refused as already holding a release", err) |
| + } |
| + noLock(t, dir) |
| +} |
| + |
| func TestReleasesPullRemovesACorruptDownload(t *testing.T) { |
| useTempCredentials(t) |
| clearDeploymentEnv(t) |
| @@ -148,6 +206,7 @@ func TestReleasesPullRemovesACorruptDownload(t *testing.T) { |
| } |
| } |
| noPartials(t, dir) |
| + noLock(t, dir) |
| } |
| // A <file>.partial symlink left in the directory is never followed: the |
| @@ -195,6 +254,12 @@ func TestReleasesPullNeedsAConsole(t *testing.T) { |
| } |
| } |
| +func TestPulledFileKeyCollides(t *testing.T) { |
| + if pulledFileKey("linux-arm64") != pulledFileKey("Linux-ARM64. ") { |
| + t.Fatalf("pulledFileKey keeps %q and %q apart; Windows would not", "linux-arm64", "Linux-ARM64. ") |
| + } |
| +} |
| + |
| func TestValidPulledFile(t *testing.T) { |
| for _, tc := range []struct { |
| name string |
| @@ -214,6 +279,38 @@ func TestValidPulledFile(t *testing.T) { |
| {"Manifest.JSON", false}, |
| {"linux-arm64.PARTIAL", false}, |
| {"linux-arm64.partial-123", false}, |
| + {".carlos-pull.lock", false}, |
| + {".Carlos-Pull.LOCK", false}, |
| + {"manifest.json.", false}, |
| + {"manifest.json ", false}, |
| + {"linux-arm64.", false}, |
| + {"linux-arm64 ", false}, |
| + {"a<b", false}, |
| + {"a>b", false}, |
| + {"a:b", false}, |
| + {`a"b`, false}, |
| + {"a|b", false}, |
| + {"a?b", false}, |
| + {"a*b", false}, |
| + {"a\x00b", false}, |
| + {"a\x1fb", false}, |
| + {"a\tb", false}, |
| + {"con", false}, |
| + {"PRN", false}, |
| + {"Aux", false}, |
| + {"nul", false}, |
| + {"nul.txt", false}, |
| + {"NUL.tar.gz", false}, |
| + {"COM1.tar", false}, |
| + {"com9", false}, |
| + {"lpt1", false}, |
| + {"LPT9.log", false}, |
| + {"com0", true}, |
| + {"com10", true}, |
| + {"console", true}, |
| + {"nullable.bin", true}, |
| + {"auxiliary", true}, |
| + {".hidden", true}, |
| } { |
| if got := validPulledFile(tc.name); got != tc.want { |
| t.Errorf("validPulledFile(%q) = %v, want %v", tc.name, got, tc.want) |
diff --git a/ship_from_release_test.go b/ship_from_release_test.go| index 96b9fa318..1d4e1aa3d 100644 |
| --- a/ship_from_release_test.go |
| +++ b/ship_from_release_test.go |
| @@ -82,6 +82,26 @@ func TestLoadReleaseDirRefusesUnsafeNames(t *testing.T) { |
| } |
| } |
| +// manifest.json. is manifest.json on Windows. It is refused before any |
| +// file is read, even here, where it is a separate file whose bytes match. |
| +func TestLoadReleaseDirRefusesWindowsAliases(t *testing.T) { |
| + dir := t.TempDir() |
| + alias := []byte("an alias of the manifest") |
| + if err := os.WriteFile(filepath.Join(dir, "manifest.json."), alias, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + sum := sha256.Sum256(alias) |
| + manifest := `{"schema":1,"app":"shop","version":"v12","kind":"binary","artifacts":{"linux-arm64":{"file":"manifest.json.","sha256":"` + |
| + hex.EncodeToString(sum[:]) + `","size":` + strconv.Itoa(len(alias)) + `}}}` |
| + if err := os.WriteFile(filepath.Join(dir, "manifest.json"), []byte(manifest), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + _, _, err := loadReleaseDir(dir) |
| + if err == nil || !strings.Contains(err.Error(), `names a file "manifest.json." that cannot be read safely`) { |
| + t.Fatalf("loadReleaseDir of a manifest naming manifest.json. = %v, want the unsafe-name refusal", err) |
| + } |
| +} |
| + |
| func TestCheckFromReleaseAnswerAssets(t *testing.T) { |
| sha := strings.Repeat("a", 64) |
| m := &release.Manifest{ |