console: refuse imports and paths that cross boxes mid-import (#186)
Paul's rule-2 ruling of 2026-11-21 on whole-branch review R2 P1: a box that has not refreshed keeps an import-gated front's path_front mark and serves its cross-box prefixes without the gate, so the state is refused up front. The import door's begin refuses a host whose claim sends a prefix to an app on another box. While the import is open (any state but committed), the paths door refuses a publish that adds such a prefix; removals and same-box prefixes are published. "Another box" is judged from the published edges files: a target is on the front's box only when some box homes it and every box that homes it also homes the front.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+359
−0
internal/console/apidomains.go+10 −0internal/console/apiinstanceimport.go+16 −0internal/console/importpaths.go+175 −0internal/console/importpaths_test.go+158 −0
diff --git a/internal/console/apidomains.go b/internal/console/apidomains.go| index 503f47511..1559ee361 100644 |
| --- a/internal/console/apidomains.go |
| +++ b/internal/console/apidomains.go |
| @@ -1045,6 +1045,16 @@ func (s *Server) handleAPIDomainPaths(w http.ResponseWriter, r *http.Request) { |
| paths := *in.Paths |
| ctx, cancel := context.WithTimeout(r.Context(), instanceStoreTimeout) |
| defer cancel() |
| + // Path routes across boxes §7.6: while the host's import is open, no |
| + // prefix to an app on another box is added. |
| + if ref, err := s.openImportPathsRefusal(ctx, acct.Sqid, app, host, paths); err != nil { |
| + log.Printf("console: api domains paths %s/%s %s: import check: %v", acct.Sqid, app, host, err) |
| + jsonError(w, http.StatusBadGateway, "could not update the hostname's path routes — try again") |
| + return |
| + } else if ref != nil { |
| + jsonError(w, ref.Status, ref.Msg) |
| + return |
| + } |
| // SetClaimPaths copies the claim it reads, Catchall included, and writes |
| // it back against that read's ETag. A write that leaves a catch-all on |
| // takes regionalMu (#447, Paul's ruling 8), so its read and write both |
diff --git a/internal/console/apiinstanceimport.go b/internal/console/apiinstanceimport.go| index 325b29e20..57dd0a2f2 100644 |
| --- a/internal/console/apiinstanceimport.go |
| +++ b/internal/console/apiinstanceimport.go |
| @@ -50,6 +50,16 @@ func importStepFailure(host string) string { |
| return fmt.Sprintf("could not update the import of %s. Try again.", host) |
| } |
| +// importPathsAcrossBoxes is begin's refusal of a host that sends a path |
| +// prefix to an app on another box (path routes across boxes spec §7.6, |
| +// Paul's ruling of 2026-11-21). Pending Paul's copy review. |
| +const importPathsAcrossBoxes = "This address sends some of its paths to apps on other boxes, so it can’t be imported while they’re there. Remove those paths, import, then add them back." |
| + |
| +// importOpenPathsAcrossBoxes is the paths door's refusal, while the host's |
| +// import is open, of a publish that adds a prefix to an app on another box |
| +// (spec §7.6, Paul's ruling of 2026-11-21). Pending Paul's copy review. |
| +const importOpenPathsAcrossBoxes = "This address is being imported. Paths that go to apps on other boxes can be added once the import is committed." |
| + |
| // importSizeRule is SD-N48, answered 400 below the range and 413 above it. |
| const importSizeRule = "bytes must be more than 0 and at most 4 GiB" |
| @@ -141,6 +151,12 @@ func (s *Server) beginImport(w http.ResponseWriter, r *http.Request, c seedCreat |
| jsonError(w, ref.Status, ref.Msg) |
| return |
| } |
| + // Path routes across boxes §7.6: no import while the host sends a path |
| + // prefix to an app on another box. |
| + if ref := s.importPathsRefusal(checks, host); ref != nil { |
| + jsonError(w, ref.Status, ref.Msg) |
| + return |
| + } |
| id, err := seeds.NewID() |
| if err != nil { |
| log.Printf("console: import %s: seed id: %v", host, err) |
diff --git a/internal/console/importpaths.go b/internal/console/importpaths.go| new file mode 100644 |
| index 000000000..3f9eed16d |
| --- /dev/null |
| +++ b/internal/console/importpaths.go |
| @@ -0,0 +1,175 @@ |
| +package console |
| + |
| +// Imports and path routes across boxes (path routes across boxes spec §7.6, |
| +// Paul's ruling of 2026-11-21, whole-branch review R2 P1). |
| +// |
| +// An import's gate holds a front's prefixes closed on the front's own box, |
| +// and registry.PathFront stops that box publishing the front as one. But a |
| +// box that still holds the front's last publication (it has not refreshed, |
| +// or its refresh failed) and serves a prefix's target selects that target |
| +// for the front's name, and its import gate, which cannot see the front's |
| +// row, admits it. Nothing bounds how long that lasts, so the ruling refuses |
| +// the state up front: an import never begins while its host sends a prefix |
| +// to an app on another box, and while it is open the paths door adds no such |
| +// prefix. Same-box prefixes stay, and the front's own gate holds them. |
| +// |
| +// "On another box" is judged from the edges files every box publishes, |
| +// which is what the edges route by: a target is on the front's box only when |
| +// some box homes it and every box that homes it also homes the front. A |
| +// target no box homes is not provably on the front's box, so it counts as on |
| +// another (the stricter reading the ruling asks for). Every file counts, |
| +// fresh or not: a retired box's file may still list a target, which refuses |
| +// until the prefix is removed, as the refusal says. |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "log" |
| + "net/http" |
| + "os" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/edgesfile" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/seeds" |
| +) |
| + |
| +// pathTargetsLeaveFront reports whether any of targets is not provably homed |
| +// on front's own box, by snap's edges files (this file's comment). |
| +func pathTargetsLeaveFront(snap edgesSnapshot, front string, targets []string) bool { |
| + want := make(map[string]bool, len(targets)) |
| + for _, t := range targets { |
| + want[t] = true |
| + } |
| + fronts := map[string]bool{} |
| + for _, f := range snap.Files { |
| + if publishesHome(f, front) { |
| + fronts[f.Box] = true |
| + } |
| + } |
| + homed := make(map[string]bool, len(want)) |
| + for _, f := range snap.Files { |
| + for host := range f.Hosts { |
| + if !want[host] || !publishesHome(f, host) { |
| + continue |
| + } |
| + if !fronts[f.Box] { |
| + return true |
| + } |
| + homed[host] = true |
| + } |
| + } |
| + return len(homed) < len(want) |
| +} |
| + |
| +// publishesHome reports whether f publishes host as a route its box serves. |
| +// Any other role is a cert-only or newer role, which serves nothing. |
| +func publishesHome(f edgesfile.PlacementView, host string) bool { |
| + raw, ok := f.Hosts[host] |
| + if !ok { |
| + return false |
| + } |
| + var h edgesfile.Host |
| + return json.Unmarshal(raw, &h) == nil && h.Role == edgesfile.RoleHome |
| +} |
| + |
| +// importPathsRefusal is begin's rule 1: host's claim, whoever holds it, |
| +// sends no prefix to an app on another box. A read that fails refuses with |
| +// begin's retry sentence; nothing has been written yet. |
| +func (s *Server) importPathsRefusal(ctx context.Context, host string) *doorRefusal { |
| + c, err := domains.GetClaim(ctx, s.cfg.Releases, host) |
| + if errors.Is(err, os.ErrNotExist) { |
| + return nil |
| + } |
| + if err != nil { |
| + log.Printf("console: import %s: read the host's claim: %v", host, err) |
| + return &doorRefusal{Status: http.StatusBadGateway, Msg: importFailed} |
| + } |
| + if len(c.Paths) == 0 { |
| + return nil |
| + } |
| + snap, err := s.liveEdges(ctx) |
| + if err != nil { |
| + log.Printf("console: import %s: read the edges files: %v", host, err) |
| + return &doorRefusal{Status: http.StatusBadGateway, Msg: importFailed} |
| + } |
| + targets := make([]string, 0, len(c.Paths)) |
| + for _, t := range c.Paths { |
| + targets = append(targets, t) |
| + } |
| + if pathTargetsLeaveFront(snap, host, targets) { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: importPathsAcrossBoxes} |
| + } |
| + return nil |
| +} |
| + |
| +// openImportPathsRefusal is the paths door's rule 2: while host's import is |
| +// open, a publish adds no prefix to an app on another box. Adding is a prefix |
| +// the stored map lacks or maps elsewhere; removals and unchanged prefixes |
| +// pass. It judges only a claim the caller holds, so a stranger's publish |
| +// reaches SetClaimPaths' neutral 404 as before, and it reads nothing more |
| +// when the publish adds nothing (a republish as a repair). An error is a |
| +// read that failed. |
| +func (s *Server) openImportPathsRefusal(ctx context.Context, acct, app, host string, paths map[string]string) (*doorRefusal, error) { |
| + cur, err := domains.GetClaim(ctx, s.cfg.Releases, host) |
| + if errors.Is(err, os.ErrNotExist) { |
| + return nil, nil |
| + } |
| + if err != nil { |
| + return nil, err |
| + } |
| + if cur.Account != acct || cur.App != app || cur.Instances { |
| + return nil, nil |
| + } |
| + var added []string |
| + for prefix, target := range paths { |
| + if cur.Paths[prefix] != target { |
| + added = append(added, target) |
| + } |
| + } |
| + if len(added) == 0 { |
| + return nil, nil |
| + } |
| + if open, err := s.importOpenAt(ctx, acct, app, host); err != nil || !open { |
| + return nil, err |
| + } |
| + snap, err := s.liveEdges(ctx) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if pathTargetsLeaveFront(snap, host, added) { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: importOpenPathsAcrossBoxes}, nil |
| + } |
| + return nil, nil |
| +} |
| + |
| +// importOpenAt reports whether host has an import that is not committed: |
| +// its record under the caller's app, or under the app its lifetime slot |
| +// names. Aborted, rolled-back and refused imports count as open (their row |
| +// may still carry a closed gate, which PathFront also excludes). |
| +func (s *Server) importOpenAt(ctx context.Context, acct, app, host string) (bool, error) { |
| + keys := [][2]string{{acct, app}} |
| + if provision.HostIncarnationKey(host) != "" { |
| + slot, _, err := provision.ReadHostIncarnation(ctx, s.cfg.Releases, host) |
| + if err != nil { |
| + return false, err |
| + } |
| + if slot != nil && slot.State == provision.SlotLive && (slot.Account != acct || slot.App != app) { |
| + keys = append(keys, [2]string{slot.Account, slot.App}) |
| + } |
| + } |
| + for _, k := range keys { |
| + rec, _, err := s.readRecord(ctx, k[0], k[1], host) |
| + if errors.Is(err, os.ErrNotExist) { |
| + continue |
| + } |
| + if err != nil { |
| + return false, err |
| + } |
| + if rec.Seed.Import() && rec.Seed.State != seeds.StateCommitted { |
| + return true, nil |
| + } |
| + } |
| + return false, nil |
| +} |
diff --git a/internal/console/importpaths_test.go b/internal/console/importpaths_test.go| new file mode 100644 |
| index 000000000..aa2e61e14 |
| --- /dev/null |
| +++ b/internal/console/importpaths_test.go |
| @@ -0,0 +1,158 @@ |
| +package console |
| + |
| +// Imports and path routes across boxes (path routes across boxes spec §7.6, |
| +// Paul's ruling of 2026-11-21). An import's gate holds a front's prefixes |
| +// closed only on the front's own box; a box that still holds the front's |
| +// last published eligibility, and serves a prefix's target, would serve it |
| +// ungated. So an import never starts while the front sends a prefix to an |
| +// app on another box, and while it is open the paths door adds none. |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "net/http" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/provision" |
| +) |
| + |
| +const ( |
| + nearTarget = "near.shop.example.com" // homed on the import's box |
| + farTarget = "far.shop.example.com" // homed on a box in another region |
| +) |
| + |
| +// putPathsBox publishes label's edges file, ready for seeds as seedReadyBox |
| +// writes it, homing hosts, with a fresh heartbeat beside it. |
| +func putPathsBox(t *testing.T, fx *consoleFixture, label, placement string, hosts ...string) { |
| + t.Helper() |
| + m := map[string]any{} |
| + for _, h := range hosts { |
| + m[h] = map[string]any{"role": "home", "platform": false} |
| + } |
| + raw, err := json.Marshal(m) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + putSeedEdges(t, fx, label, `{"v":1,"box":"`+label+`","eip":"1.2.3.4","placement":"`+placement+ |
| + `","hibernation_configured":true,"capabilities":["lifetimes-1"],"hosts":`+string(raw)+ |
| + `,"seeds_protocol":1,"seeds_floor_min_release":"v0.27.0","prev_release":"v0.27.1","prev_seeds_allowed_for":"v0.27.0"}`) |
| + putSeedHeartbeat(t, fx, label, "nested", false, time.Now()) |
| +} |
| + |
| +// pathsAcrossFixture is the import fixture with the import's host claimed |
| +// by shop, carrying paths, and two boxes: kass-1, the import's one-box |
| +// region, homes the import's host and nearTarget; kass-2, in eu, homes |
| +// farTarget. |
| +func pathsAcrossFixture(t *testing.T, paths map[string]string) fromFixture { |
| + t.Helper() |
| + f := newImportFixture(t) |
| + putPathsBox(t, f.fx, "kass-1", "", importHost, nearTarget) |
| + putPathsBox(t, f.fx, "kass-2", "eu", farTarget) |
| + ctx := context.Background() |
| + if _, _, err := domains.AttachClaim(ctx, f.fx.rel, domains.Claim{Schema: 1, Host: importHost, Account: "bab", App: fromApp, |
| + CreatedAt: time.Now().UTC().Format(time.RFC3339)}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(paths) > 0 { |
| + if _, err := domains.SetClaimPaths(ctx, f.fx.rel, importHost, "bab", fromApp, paths); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + return f |
| +} |
| + |
| +// TestImportBeginRefusesPathsOnAnotherBox: a front with a prefix whose |
| +// target is homed on another box, or on no box at all, is not imported, |
| +// and nothing is written. |
| +func TestImportBeginRefusesPathsOnAnotherBox(t *testing.T) { |
| + for name, paths := range map[string]map[string]string{ |
| + "another box": {"near": nearTarget, "far": farTarget}, |
| + "no box": {"near": nearTarget, "gone": "gone.shop.example.com"}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + f := pathsAcrossFixture(t, paths) |
| + code, out := beginImport(t, f, importHost, importSHA, 10) |
| + if code != http.StatusConflict || str(t, out, "error") != importPathsAcrossBoxes { |
| + t.Fatalf("begin = %d %v, want 409 %q", code, out, importPathsAcrossBoxes) |
| + } |
| + f.requireNoRecord(t, importHost) |
| + }) |
| + } |
| +} |
| + |
| +// TestImportBeginAllowsSameBoxPaths: prefixes whose targets are homed on |
| +// the import's own box, or no prefixes, leave the import to begin. |
| +func TestImportBeginAllowsSameBoxPaths(t *testing.T) { |
| + for name, paths := range map[string]map[string]string{ |
| + "same box": {"near": nearTarget}, |
| + "no paths": nil, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + f := pathsAcrossFixture(t, paths) |
| + code, out := beginImport(t, f, importHost, importSHA, 10) |
| + f.requireBegun(t, code, out, importHost) |
| + }) |
| + } |
| +} |
| + |
| +// putShopPaths publishes paths for the import's host through the paths |
| +// door as shop's owner, answering the status and the body. |
| +func putShopPaths(t *testing.T, f fromFixture, paths map[string]string) (int, map[string]any) { |
| + t.Helper() |
| + code, _, raw := rawReq(t, http.MethodPut, domainsURL(f.fx, "bab", fromApp)+"/"+importHost+"/paths", f.token, pathsBody(t, paths)) |
| + var out map[string]any |
| + _ = json.Unmarshal([]byte(raw), &out) |
| + return code, out |
| +} |
| + |
| +// TestPathsDoorDuringAnOpenImport: while the import is open, a republish |
| +// that adds a prefix to another box is refused and leaves the map alone; |
| +// one that adds a same-box prefix, or removes a prefix, is published. Once |
| +// the import is committed, a prefix to another box is published. |
| +func TestPathsDoorDuringAnOpenImport(t *testing.T) { |
| + f := pathsAcrossFixture(t, map[string]string{"near": nearTarget}) |
| + code, out := beginImport(t, f, importHost, importSHA, 10) |
| + begun, _ := f.requireBegun(t, code, out, importHost) |
| + |
| + if code, out := putShopPaths(t, f, map[string]string{"near": nearTarget, "far": farTarget}); code != http.StatusConflict || |
| + str(t, out, "error") != importOpenPathsAcrossBoxes { |
| + t.Fatalf("adding a prefix to another box during the import = %d %v, want 409 %q", code, out, importOpenPathsAcrossBoxes) |
| + } |
| + // Retargeting a prefix is adding it. |
| + if code, out := putShopPaths(t, f, map[string]string{"near": farTarget}); code != http.StatusConflict { |
| + t.Fatalf("retargeting a prefix to another box during the import = %d %v, want 409", code, out) |
| + } |
| + if c := claimOf(t, f.fx, importHost); len(c.Paths) != 1 || c.Paths["near"] != nearTarget { |
| + t.Fatalf("claim paths after the refusals = %v, want near alone", c.Paths) |
| + } |
| + if code, out := putShopPaths(t, f, map[string]string{"near": nearTarget, "also": nearTarget}); code != http.StatusOK { |
| + t.Fatalf("adding a same-box prefix during the import = %d %v, want 200", code, out) |
| + } |
| + if code, out := putShopPaths(t, f, map[string]string{"also": nearTarget}); code != http.StatusOK { |
| + t.Fatalf("removing a prefix during the import = %d %v, want 200", code, out) |
| + } |
| + |
| + for _, step := range []func(*provision.Record) error{ |
| + provision.SeedUploaded(begun.Seed.ID), provision.SeedStopped(begun.Seed.ID), provision.SeedSwapped(begun.Seed.ID), |
| + provision.SeedPlace(begun.Seed.ID), provision.SeedCommit(begun.Seed.ID), |
| + } { |
| + f.setSeed(t, step) |
| + } |
| + if code, out := putShopPaths(t, f, map[string]string{"also": nearTarget, "far": farTarget}); code != http.StatusOK { |
| + t.Fatalf("adding a prefix to another box after the commit = %d %v, want 200", code, out) |
| + } |
| + if c := claimOf(t, f.fx, importHost); c.Paths["far"] != farTarget { |
| + t.Fatalf("claim paths after the commit = %v, want far published", c.Paths) |
| + } |
| +} |
| + |
| +// TestPathsDoorWithoutAnImport: a front with no import open publishes a |
| +// prefix to another box, as before the ruling. |
| +func TestPathsDoorWithoutAnImport(t *testing.T) { |
| + f := pathsAcrossFixture(t, nil) |
| + if code, out := putShopPaths(t, f, map[string]string{"far": farTarget}); code != http.StatusOK { |
| + t.Fatalf("adding a prefix to another box with no import = %d %v, want 200", code, out) |
| + } |
| +} |