carlos protection: a member can read the record-only switch (#491)
A follow-up on #491: `carlos protection` with no flag read the app's telemetry object, whose GET kept the PUT's owner-only rule, so a member got 403 for a switch the Protection tab already shows every member. The GET now answers a member who is not an owner with the protection half alone; the errors and analytics halves stay the owner's, and the PUT is unchanged. Owners and Operate service credentials read the whole object as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+38
−10
internal/console/apierrors.go+14 −5internal/console/protectionsetting_test.go+22 −4internal/console/routedecls.go+2 −1
diff --git a/internal/console/apierrors.go b/internal/console/apierrors.go| index f8d6b759f..ec66f3ed6 100644 |
| --- a/internal/console/apierrors.go |
| +++ b/internal/console/apierrors.go |
| @@ -98,9 +98,9 @@ func (s *Server) handleAPIAvailability(w http.ResponseWriter, r *http.Request) { |
| writeJSON(w, http.StatusOK, resp) |
| } |
| -// telemetryGate is the instance gate plus the owner rule both telemetry |
| -// doors keep: the browser forms for these settings are owner-only, so the |
| -// person path here is too. A SERVICE holds no account role (AccountByID does |
| +// telemetryGate is the instance gate plus the owner rule the telemetry PUT |
| +// keeps: the browser forms for these settings are owner-only, so the person |
| +// path here is too. A SERVICE holds no account role (AccountByID does |
| // not populate Role), which is why this reads the kind first. |
| func (s *Server) telemetryGate(w http.ResponseWriter, r *http.Request) (string, *Account, string, bool) { |
| principal, acct, app, ok := s.instanceGate(w, r) |
| @@ -117,12 +117,18 @@ func (s *Server) telemetryGate(w http.ResponseWriter, r *http.Request) (string, |
| // handleAPITelemetryGet answers the app-level telemetry object as stored |
| // (absent halves absent, so the caller sees what is set on the app itself, |
| -// not what it inherits). The PUT's gate: it is that door's read. |
| +// not what it inherits). An owner, or a service credential the PUT would |
| +// take, reads the whole object. A member who is not an owner reads the |
| +// protection half alone (#491): the Protection tab already shows every |
| +// member whether the app records only, so `carlos protection` shows them |
| +// the same; the errors and analytics halves stay the owner's. |
| func (s *Server) handleAPITelemetryGet(w http.ResponseWriter, r *http.Request) { |
| - _, acct, app, ok := s.telemetryGate(w, r) |
| + _, acct, app, ok := s.instanceGate(w, r) |
| if !ok { |
| return |
| } |
| + p, _ := principalFrom(r.Context()) |
| + protectionOnly := p.Kind == PrincipalUser && acct.Role != RoleOwner |
| ctx, cancel := context.WithTimeout(r.Context(), instanceStoreTimeout) |
| defer cancel() |
| st, _, err := telemetry.Read(ctx, s.cfg.Releases, telemetry.AppKey(acct.Sqid, app)) |
| @@ -131,6 +137,9 @@ func (s *Server) handleAPITelemetryGet(w http.ResponseWriter, r *http.Request) { |
| jsonError(w, http.StatusBadGateway, "could not read the settings") |
| return |
| } |
| + if protectionOnly { |
| + st = telemetry.Settings{Protection: st.Protection} |
| + } |
| writeJSON(w, http.StatusOK, st) |
| } |
diff --git a/internal/console/protectionsetting_test.go b/internal/console/protectionsetting_test.go| index 344de2c57..9bf3b49eb 100644 |
| --- a/internal/console/protectionsetting_test.go |
| +++ b/internal/console/protectionsetting_test.go |
| @@ -264,8 +264,9 @@ func TestAPITelemetryProtectionAuditLine(t *testing.T) { |
| } |
| } |
| -// The read beside the PUT: the app's stored settings as stored, under the |
| -// PUT's gate (Operate, and a person must own the account). |
| +// The read beside the PUT: the app's stored settings as stored, for an |
| +// Operate credential or an owner. A member reads the protection half alone, |
| +// as the Protection tab shows it to them (#491). |
| func TestAPITelemetryGet(t *testing.T) { |
| fx, _ := ptFixture(t) |
| getURL := fx.ts.URL + "/api/cli/apps/" + fx.sqid + "/shop/telemetry" |
| @@ -297,7 +298,24 @@ func TestAPITelemetryGet(t *testing.T) { |
| t.Fatal(err) |
| } |
| member := cliToken(t, fx, "kim@keymail.dev", "code-ps-get-member") |
| - if status, _, body := rawReq(t, http.MethodGet, getURL, member, ""); status != http.StatusForbidden { |
| - t.Fatalf("member GET = %d %s, want 403", status, body) |
| + status, _, body = rawReq(t, http.MethodGet, getURL, member, "") |
| + var st telemetry.Settings |
| + if status != http.StatusOK || json.Unmarshal([]byte(body), &st) != nil || !telemetry.RecordOnly(st) || |
| + st.Errors != nil || st.Analytics != nil || strings.Contains(body, "log_level") { |
| + t.Fatalf("member GET = %d %s, want the protection half alone", status, body) |
| + } |
| + // Nothing stored for protection: a member sees nothing at all. |
| + if err := telemetry.Update(context.Background(), fx.rel, telemetry.AppKey(fx.sqid, "shop"), func(st *telemetry.Settings) { |
| + st.Protection = nil |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + if status, _, body := rawReq(t, http.MethodGet, getURL, member, ""); status != http.StatusOK || strings.TrimSpace(body) != "{}" { |
| + t.Fatalf("member GET with no protection stored = %d %s, want {}", status, body) |
| + } |
| + // A stranger to the account gets nothing. |
| + stranger := cliToken(t, fx, "sam@keymail.dev", "code-ps-get-stranger") |
| + if status, _, body := rawReq(t, http.MethodGet, getURL, stranger, ""); status == http.StatusOK { |
| + t.Fatalf("non-member GET = %d %s, want a refusal", status, body) |
| } |
| } |
diff --git a/internal/console/routedecls.go b/internal/console/routedecls.go| index dbc1872b7..4e9e73ac8 100644 |
| --- a/internal/console/routedecls.go |
| +++ b/internal/console/routedecls.go |
| @@ -666,7 +666,8 @@ var routeDecls = []routeDecl{ |
| // widen or narrow what its owner is shown. |
| {Pattern: "PUT /api/cli/apps/{account}/{app}/telemetry", Kind: kindCLI, Allow: svcrole.Allow(svcrole.Operate), Handler: (*Server).handleAPITelemetryPut}, |
| // Its read (`carlos protection` with no flag): the stored settings under |
| - // the PUT's own gate, since they are the settings that PUT writes. |
| + // the PUT's service role. An owner reads them all; a member reads only |
| + // the protection half, which the Protection tab shows them (#491). |
| {Pattern: "GET /api/cli/apps/{account}/{app}/telemetry", Kind: kindCLI, Allow: svcrole.Allow(svcrole.Operate), Handler: (*Server).handleAPITelemetryGet}, |
| {Pattern: "POST /api/cli/apps/{account}/{app}/instances", Kind: kindCLIScoped, Allow: svcrole.Allow(svcrole.Instance), Target: scopeCreateBody, Handler: (*Server).handleAPIInstanceCreate}, |
| {Pattern: "GET /api/cli/apps/{account}/{app}/instances/{host}", Kind: kindCLIScoped, Allow: svcrole.Allow(svcrole.Instance), Target: scopeHost, Handler: (*Server).handleAPIInstanceGet}, |