API: Fix redirect issue when configured to use a subpath (#21652)

* request uri will contain the subpath
This commit is contained in:
Brian Gann
2020-02-14 14:51:35 +01:00
committed by GitHub
parent 0cde7decd7
commit 0e2d874ecf
3 changed files with 40 additions and 4 deletions
+31 -2
View File
@@ -72,6 +72,7 @@ type redirectCase struct {
err error
appURL string
appSubURL string
path string
}
func TestLoginErrorCookieApiEndpoint(t *testing.T) {
@@ -154,6 +155,7 @@ func TestLoginViewRedirect(t *testing.T) {
desc: "grafana relative url without subpath",
url: "/profile",
appURL: "http://localhost:3000",
path: "/",
status: 302,
},
{
@@ -161,6 +163,15 @@ func TestLoginViewRedirect(t *testing.T) {
url: "/grafana/profile",
appURL: "http://localhost:3000",
appSubURL: "grafana",
path: "grafana/",
status: 302,
},
{
desc: "grafana slashed relative url with subpath",
url: "/grafana/profile",
appURL: "http://localhost:3000",
appSubURL: "grafana",
path: "/grafana/",
status: 302,
},
{
@@ -168,13 +179,23 @@ func TestLoginViewRedirect(t *testing.T) {
url: "/profile",
appURL: "http://localhost:3000",
appSubURL: "grafana",
path: "grafana/",
status: 200,
err: login.ErrInvalidRedirectTo,
},
{
desc: "grafana subpath absolute url",
url: "http://localhost:3000/grafana/profile",
appURL: "http://localhost:3000",
appSubURL: "grafana",
path: "/grafana/profile",
status: 200,
},
{
desc: "grafana absolute url",
url: "http://localhost:3000/profile",
appURL: "http://localhost:3000",
path: "/",
status: 200,
err: login.ErrAbsoluteRedirectTo,
},
@@ -182,6 +203,7 @@ func TestLoginViewRedirect(t *testing.T) {
desc: "non grafana absolute url",
url: "http://example.com",
appURL: "http://localhost:3000",
path: "/",
status: 200,
err: login.ErrAbsoluteRedirectTo,
},
@@ -189,6 +211,7 @@ func TestLoginViewRedirect(t *testing.T) {
desc: "invalid url",
url: ":foo",
appURL: "http://localhost:3000",
path: "/",
status: 200,
err: login.ErrInvalidRedirectTo,
},
@@ -203,7 +226,7 @@ func TestLoginViewRedirect(t *testing.T) {
MaxAge: 60,
Value: c.url,
HttpOnly: true,
Path: hs.Cfg.AppSubUrl + "/",
Path: c.path,
Secure: hs.Cfg.CookieSecure,
SameSite: hs.Cfg.CookieSameSiteMode,
}
@@ -219,7 +242,7 @@ func TestLoginViewRedirect(t *testing.T) {
assert.True(t, ok, "Set-Cookie exists")
assert.Greater(t, len(setCookie), 0)
var redirectToCookieFound bool
expCookieValue := fmt.Sprintf("redirect_to=%v; Path=%v; Max-Age=60; HttpOnly; Secure", c.url, hs.Cfg.AppSubUrl+"/")
expCookieValue := fmt.Sprintf("redirect_to=%v; Path=%v; Max-Age=60; HttpOnly; Secure", c.url, c.path)
for _, cookieValue := range setCookie {
if cookieValue == expCookieValue {
redirectToCookieFound = true
@@ -281,6 +304,12 @@ func TestLoginPostRedirect(t *testing.T) {
appURL: "https://localhost:3000",
appSubURL: "grafana",
},
{
desc: "grafana no slash relative url with subpath",
url: "grafana/profile",
appURL: "https://localhost:3000",
appSubURL: "grafana",
},
{
desc: "relative url with missing subpath",
url: "/profile",