carlos / platform Public

console: unpublish and unlink free the name's bucket objects

Task 5 fix round 2 (Astra). The protection and analytics switches a
linked app keeps write apps/<sqid>/<app>/telemetry.json; unpublish and
unlink now delete that prefix as one app mutation before the row, so
publishing the name again succeeds. Only a row this link holds is
cleaned, so a foreign object under a never-published name still
refuses.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 63c7c3bf800ff4f68560d570ac647207a89fde8a parent 597c10f
2 files changed, +106 −1
  • internal/console/apilinks.go +47 −1
  • internal/console/apilinks_test.go +59 −0
diff --git a/internal/console/apilinks.go b/internal/console/apilinks.go
index 35ada2ee3..65d8b82ba 100644
--- a/internal/console/apilinks.go
+++ b/internal/console/apilinks.go
@@ -384,12 +384,49 @@ func (s *Server) unpublish(ctx context.Context, l *Link, acct *Account, name str
return fmt.Errorf("delete forward row: %w", err)
}
}
+ owner, err := s.store.appLinkID(acct.ID, name)
+ if err != nil {
+ return fmt.Errorf("read app row: %w", err)
+ }
+ if owner != l.ID {
+ return nil // no row of this link: nothing of the name is this link's
+ }
+ if err := s.dropLinkedAppObjects(ctx, acct.ID, acct.Sqid, name); err != nil {
+ return fmt.Errorf("delete bucket objects: %w", err)
+ }
if _, err := s.store.deleteLinkedAppRow(ctx, acct.ID, name, l.ID); err != nil {
return fmt.Errorf("delete app row: %w", err)
}
return nil
}
+// dropLinkedAppObjects deletes every object under a linked app's prefix,
+// apps/<sqid>/<app>/, as one app mutation, before its row goes. A linked
+// app writes nothing there itself; what is there was written by a door the
+// linked-app guard allows (the protection and analytics switches write the
+// app's telemetry.json). Removing it with the row is what releases the name:
+// a later create or publish of it refuses any object under the prefix
+// (claimNamePrefixes). The caller holds the app lock and has seen the row
+// as this link's, so the objects are this app's. The listing sends no delete
+// for an empty prefix.
+func (s *Server) dropLinkedAppObjects(ctx context.Context, accountID int64, sqid, app string) error {
+ if s.cfg.Releases == nil || !release.ValidApp(app) {
+ return nil
+ }
+ keys, err := s.cfg.Releases.List(ctx, release.AppPrefix(sqid, app))
+ if err != nil || len(keys) == 0 {
+ return err
+ }
+ return s.appMutation(ctx, &Account{ID: accountID, Sqid: sqid}, app, "linked-app-remove", func(ctx context.Context) error {
+ for _, k := range keys {
+ if err := s.cfg.Releases.Delete(ctx, k); err != nil {
+ return fmt.Errorf("delete %s: %w", k, err)
+ }
+ }
+ return nil
+ })
+}
+
// linkSyncTimeout bounds a door's own sync after its change committed.
const linkSyncTimeout = 10 * time.Second
@@ -519,9 +556,18 @@ func (s *Server) dropLinkedApps(ctx context.Context, linkID string) error {
}
// dropLinkedApp deletes one apps row of linkID under the app lock a
-// publish of the name holds.
+// publish of the name holds, its bucket objects first (dropLinkedAppObjects),
+// so the name is free once the row is gone.
func (s *Server) dropLinkedApp(ctx context.Context, a linkedApp, linkID string) error {
defer s.lockApp(a.Sqid, a.App)()
+ if owner, err := s.store.appLinkID(a.AccountID, a.App); err != nil || owner != linkID {
+ return err // gone since the listing, or never this link's
+ }
+ if a.Sqid != "" {
+ if err := s.dropLinkedAppObjects(ctx, a.AccountID, a.Sqid, a.App); err != nil {
+ return fmt.Errorf("delete bucket objects: %w", err)
+ }
+ }
_, err := s.store.deleteLinkedAppRow(ctx, a.AccountID, a.App, linkID)
return err
}
diff --git a/internal/console/apilinks_test.go b/internal/console/apilinks_test.go
index 0b015f024..c9fdda1b9 100644
--- a/internal/console/apilinks_test.go
+++ b/internal/console/apilinks_test.go
@@ -1673,3 +1673,62 @@ func TestLinkedAppEdgeSettingsLand(t *testing.T) {
}
}
}
+
+// TestRepublishAfterAnEdgeSetting: removing a publication releases its name,
+// even after an allowed edge setting wrote the app's telemetry.json under
+// its prefix; a name whose prefix holds an object that was never a linked
+// app's still refuses.
+func TestRepublishAfterAnEdgeSetting(t *testing.T) {
+ for _, how := range []string{"unpublish", "unlink"} {
+ t.Run(how, func(t *testing.T) {
+ fx := newLinkFixture(t, "tunnel.example.com")
+ browser := signInPaul(t, fx)
+ l := mintLink(t, fx, browser, "shop-home")
+ if status, out := publish(t, fx, l.token, "shop"); status != http.StatusCreated {
+ t.Fatalf("publish = %d %v", status, out)
+ }
+ csrf := sessionToken(t, fx, browser)
+ if status, _, body := postForm(t, browser, fx.u("/apps/shop/protection/setting"), url.Values{"csrf": {csrf}, "mode": {"record"}}); status != http.StatusSeeOther {
+ t.Fatalf("protection setting = %d %s", status, body)
+ }
+ if _, _, err := fx.rel.Get(context.Background(), telemetry.AppKey(fx.sqid, "shop")); err != nil {
+ t.Fatalf("the setting wrote no telemetry.json: %v", err)
+ }
+ again := l
+ if how == "unpublish" {
+ if status, body := linkDoor(t, fx, http.MethodDelete, "/api/link/publications/shop", l.token, ""); status != http.StatusNoContent {
+ t.Fatalf("unpublish = %d %s", status, body)
+ }
+ } else {
+ if status, body := linkDoor(t, fx, http.MethodDelete, "/api/link", l.token, ""); status != http.StatusNoContent {
+ t.Fatalf("unlink = %d %s", status, body)
+ }
+ again = mintLink(t, fx, browser, "shop-home-again")
+ }
+ if keys, _ := fx.rel.List(context.Background(), release.AppPrefix(fx.sqid, "shop")); len(keys) != 0 {
+ t.Fatalf("objects left under the prefix after %s: %v", how, keys)
+ }
+ if status, out := publish(t, fx, again.token, "shop"); status != http.StatusCreated {
+ t.Fatalf("publish the same name after %s = %d %v, want 201", how, status, out)
+ }
+ })
+ }
+ t.Run("a foreign object still refuses", func(t *testing.T) {
+ fx := newLinkFixture(t, "tunnel.example.com")
+ l := mintLink(t, fx, signInPaul(t, fx), "shop-home")
+ if _, err := fx.rel.Put(context.Background(), telemetry.AppKey(fx.sqid, "blog"), []byte(`{}`), ""); err != nil {
+ t.Fatal(err)
+ }
+ status, out := publish(t, fx, l.token, "blog")
+ if want := linkCopy(copyLD10, "app", "blog", "account", linkOpsAccount(t, fx).Name); status != http.StatusConflict || out["error"] != want {
+ t.Fatalf("publish over a foreign object = %d %v, want 409 LD-C10", status, out)
+ }
+ // Unpublishing a name the link never published deletes nothing.
+ if status, _ := linkDoor(t, fx, http.MethodDelete, "/api/link/publications/blog", l.token, ""); status != http.StatusNoContent {
+ t.Fatalf("unpublish of a name never published = %d", status)
+ }
+ if _, _, err := fx.rel.Get(context.Background(), telemetry.AppKey(fx.sqid, "blog")); err != nil {
+ t.Fatalf("an unpublish removed a foreign object: %v", err)
+ }
+ })
+}