From d01924d4f506efb8c493273ce06b7e5b529b2ca9 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 2 Nov 2026 22:23:12 +0300 Subject: [PATCH] Traces: locked entry and upgrade window for roles without the traces grant - The API context carries traces_locked (the role may not view the project's traces); the menu shows the Traces entry with a lock. - The Traces page answers a 403 with an upgrade card and window instead of an error; its button leads to the embedding product's billing page. - Handoff create accepts return_url and workspace; consume stores them as cookies (coroot_return_url, coroot_workspace) for the SPA. Agent: claude Co-Authored-By: Claude Fable 5.1 --- api/ctx.go | 4 +++ api/forms/auth.go | 5 +++ api/handoff.go | 64 ++++++++++++++++++++++++++++----- api/handoff_test.go | 10 ++++++ front/src/App.vue | 6 +++- front/src/api.js | 2 ++ front/src/views/Traces.vue | 72 ++++++++++++++++++++++++++++++++++++-- 7 files changed, 151 insertions(+), 12 deletions(-) diff --git a/api/ctx.go b/api/ctx.go index 5be81aaec..0e9d085f0 100644 --- a/api/ctx.go +++ b/api/ctx.go @@ -28,6 +28,9 @@ type Context struct { License *License `json:"license,omitempty"` Multicluster bool `json:"multicluster"` MemberProjects []string `json:"member_projects,omitempty"` + // TracesLocked: the user's role may not view the project's traces; the + // menu shows the entry locked and the page an upgrade prompt. + TracesLocked bool `json:"traces_locked"` } type GitOpsStatus struct { @@ -97,6 +100,7 @@ func (api *Api) WithContext(u *db.User, p *db.Project, cacheStatus *cache.Status Argocd: gitOpsStatus(w, gitOpsVisible && w != nil && w.ArgoCD != nil, overview.CountArgoCDIssues), Multicluster: p.Multicluster(), MemberProjects: p.Settings.MemberProjects, + TracesLocked: u != nil && !api.IsAllowed(u, rbac.Actions.Project(string(p.Id)).Traces().View()), }, Data: data, } diff --git a/api/forms/auth.go b/api/forms/auth.go index 5d9b26bd3..77da94041 100644 --- a/api/forms/auth.go +++ b/api/forms/auth.go @@ -63,6 +63,11 @@ type HandoffCreateForm struct { // Theme is light|dark|auto from the embedding product (e.g. Kubero). // Applied on handoff consume so the SPA matches the caller's UI. Theme string `json:"theme"` + // ReturnURL is the embedding product's page to go back to (absolute + // http(s) URL); Workspace its display name. Both are handed to the SPA + // as cookies on consume. + ReturnURL string `json:"return_url"` + Workspace string `json:"workspace"` } func (f *HandoffCreateForm) Valid() bool { diff --git a/api/handoff.go b/api/handoff.go index 8ccfe7c62..04ec3a070 100644 --- a/api/handoff.go +++ b/api/handoff.go @@ -27,13 +27,21 @@ const ( ) type handoffToken struct { - UserId int - Redirect string - Theme string // light|dark|auto — from Kubero (or redirect ?theme=) - Expires time.Time + UserId int + Redirect string + Theme string // light|dark|auto — from Kubero (or redirect ?theme=) + ReturnURL string // the embedding product's page, for links back + Workspace string + Expires time.Time } -const corootThemeCookie = "coroot_theme" +const ( + corootThemeCookie = "coroot_theme" + corootReturnURLCookie = "coroot_return_url" + corootWorkspaceCookie = "coroot_workspace" + maxHandoffReturnURL = 2048 + maxHandoffWorkspace = 200 +) type handoffStore struct { mu sync.Mutex @@ -187,10 +195,12 @@ func (api *Api) CreateHandoff(w http.ResponseWriter, r *http.Request) { theme = themeFromRedirect(redirect) } api.handoffs.put(token, handoffToken{ - UserId: user.Id, - Redirect: redirect, - Theme: theme, - Expires: time.Now().Add(ttl), + UserId: user.Id, + Redirect: redirect, + Theme: theme, + ReturnURL: sanitizeHandoffReturnURL(form.ReturnURL), + Workspace: truncateHandoffField(form.Workspace, maxHandoffWorkspace), + Expires: time.Now().Add(ttl), }) handoffPath := path.Join(api.cfg.UrlBasePath, "api/auth/handoff") @@ -245,6 +255,20 @@ func (api *Api) ConsumeHandoff(w http.ResponseWriter, r *http.Request) { if theme != "" { target = appendQueryParam(target, "theme", theme) } + // Where the embedding product is, readable by the SPA (links back, the + // plan-upgrade prompt on pages the role may not view). + for name, value := range map[string]string{corootReturnURLCookie: ht.ReturnURL, corootWorkspaceCookie: ht.Workspace} { + if value == "" { + continue + } + http.SetCookie(w, &http.Cookie{ + Name: name, + Value: url.QueryEscape(value), + Path: "/", + Expires: time.Now().Add(SessionCookieTTL), + SameSite: http.SameSiteLaxMode, + }) + } http.Redirect(w, r, target, http.StatusFound) } @@ -345,6 +369,28 @@ func (api *Api) checkHandoffSecret(r *http.Request) bool { return subtle.ConstantTimeCompare([]byte(provided), []byte(secret)) == 1 } +// sanitizeHandoffReturnURL keeps an absolute http(s) URL without +// credentials, at most maxHandoffReturnURL long; anything else is dropped. +func sanitizeHandoffReturnURL(raw string) string { + raw = strings.TrimSpace(raw) + if raw == "" || len(raw) > maxHandoffReturnURL { + return "" + } + u, err := url.Parse(raw) + if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil { + return "" + } + return u.String() +} + +func truncateHandoffField(s string, max int) string { + s = strings.TrimSpace(s) + if len(s) > max { + return s[:max] + } + return s +} + func sanitizeHandoffRedirect(redirect string) string { redirect = strings.TrimSpace(redirect) if redirect == "" { diff --git a/api/handoff_test.go b/api/handoff_test.go index 6e38d8a1e..40f6b3373 100644 --- a/api/handoff_test.go +++ b/api/handoff_test.go @@ -167,3 +167,13 @@ func TestCreateHandoffRefusesRolesItDoesNotManage(t *testing.T) { w := create(`{"email":"tenant@example.com","role":"kubero-team-prod",` + perms + `}`) require.Equal(t, http.StatusOK, w.Code, w.Body.String()) } + +func TestSanitizeHandoffReturnURL(t *testing.T) { + assert.Equal(t, "https://dashboard.example.com/apps/x", sanitizeHandoffReturnURL(" https://dashboard.example.com/apps/x ")) + assert.Equal(t, "", sanitizeHandoffReturnURL("/relative")) + assert.Equal(t, "", sanitizeHandoffReturnURL("javascript:alert(1)")) + assert.Equal(t, "", sanitizeHandoffReturnURL("https://user:pw@dashboard.example.com/")) + assert.Equal(t, "", sanitizeHandoffReturnURL("")) + assert.Equal(t, "ab", truncateHandoffField(" ab ", 10)) + assert.Equal(t, "abc", truncateHandoffField("abcdef", 3)) +} diff --git a/front/src/App.vue b/front/src/App.vue index 82fae7f27..f6a038d13 100644 --- a/front/src/App.vue +++ b/front/src/App.vue @@ -50,7 +50,11 @@ :class="{ 'v-list-item--active sb-nav__item--active': id === view }" > - + + + mdi-lock-outline + + {{ v.icon }} diff --git a/front/src/api.js b/front/src/api.js index c08e3bf00..503447fc1 100644 --- a/front/src/api.js +++ b/front/src/api.js @@ -26,6 +26,7 @@ export default class Api { argocd: null, multicluster: false, member_projects: [], + traces_locked: false, }; constructor(router, vuetify, basePath) { @@ -68,6 +69,7 @@ export default class Api { this.context.argocd = response.data.context.argocd; this.context.multicluster = response.data.context.multicluster; this.context.member_projects = response.data.context.member_projects || []; + this.context.traces_locked = !!response.data.context.traces_locked; } try { const data = response.data.data !== undefined ? response.data.data : response.data; diff --git a/front/src/views/Traces.vue b/front/src/views/Traces.vue index 57cf6d6f8..31df7821b 100644 --- a/front/src/views/Traces.vue +++ b/front/src/views/Traces.vue @@ -1,6 +1,43 @@