From be1aa196428c9b5155d18c7b0155d34a2487d7f8 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Thu, 13 Mar 2025 10:15:42 +0000 Subject: [PATCH] [release-11.5.3] Org redirection: Fix linking between orgs (#102089) Org redirection: Fix linking between orgs (#102021) * don't trim path * add unit test (cherry picked from commit f3fb9592da11a5f8343699ea307e591e522af627) --- pkg/middleware/org_redirect.go | 3 +-- pkg/middleware/org_redirect_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/middleware/org_redirect.go b/pkg/middleware/org_redirect.go index c4d5623d69e..b794f9acfdf 100644 --- a/pkg/middleware/org_redirect.go +++ b/pkg/middleware/org_redirect.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "strconv" - "strings" "github.com/grafana/grafana/pkg/services/contexthandler" "github.com/grafana/grafana/pkg/services/user" @@ -50,7 +49,7 @@ func OrgRedirect(cfg *setting.Cfg, userSvc user.Service) web.Handler { qs = fmt.Sprintf("%s&kiosk", urlParams.Encode()) } - newURL := fmt.Sprintf("%s%s?%s", cfg.AppSubURL, strings.TrimPrefix(c.Req.URL.Path, "/"), qs) + newURL := fmt.Sprintf("%s%s?%s", cfg.AppSubURL, c.Req.URL.Path, qs) c.Redirect(newURL, 302) } diff --git a/pkg/middleware/org_redirect_test.go b/pkg/middleware/org_redirect_test.go index e2bc6d81075..4087eeb91c6 100644 --- a/pkg/middleware/org_redirect_test.go +++ b/pkg/middleware/org_redirect_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/require" "github.com/grafana/grafana/pkg/services/authn" + "github.com/grafana/grafana/pkg/setting" ) func TestOrgRedirectMiddleware(t *testing.T) { @@ -62,4 +63,17 @@ func TestOrgRedirectMiddleware(t *testing.T) { require.Equal(t, 404, sc.resp.Code) }) + + middlewareScenario(t, "works correctly when grafana is served under a subpath", func(t *testing.T, sc *scenarioContext) { + sc.withIdentity(&authn.Identity{}) + + sc.m.Get("/", sc.defaultHandler) + sc.fakeReq("GET", "/?orgId=3").exec() + + require.Equal(t, 302, sc.resp.Code) + require.Equal(t, "/grafana/?orgId=3", sc.resp.Header().Get("Location")) + }, func(cfg *setting.Cfg) { + cfg.AppURL = "http://localhost:3000/grafana/" + cfg.AppSubURL = "/grafana" + }) }