Skip to content

Commit c658816

Browse files
marefrdsotirakis
authored andcommitted
Security: Fix do not forward login cookie in outgoing requests
(cherry picked from commit 54a32fc83b233f5910495b5fcca0b4f881221538)
1 parent 4dd56e4 commit c658816

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

pkg/api/metrics_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/grafana/grafana/pkg/services/featuremgmt"
1313
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
14+
"github.com/grafana/grafana/pkg/setting"
1415
"github.com/grafana/grafana/pkg/web/webtest"
1516

1617
"golang.org/x/oauth2"
@@ -498,7 +499,7 @@ func TestAPIEndpoint_Metrics_ParseDashboardQueryParams(t *testing.T) {
498499
// `/ds/query` endpoint test
499500
func TestAPIEndpoint_Metrics_QueryMetricsV2(t *testing.T) {
500501
qds := query.ProvideService(
501-
nil,
502+
setting.NewCfg(),
502503
nil,
503504
nil,
504505
&fakePluginRequestValidator{},

pkg/api/pluginproxy/ds_proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (proxy *DataSourceProxy) director(req *http.Request) {
214214
}
215215
}
216216

217-
proxyutil.ClearCookieHeader(req, keepCookieNames)
217+
proxyutil.ClearCookieHeader(req, keepCookieNames, []string{proxy.cfg.LoginCookieName})
218218
req.Header.Set("User-Agent", fmt.Sprintf("Grafana/%s", setting.BuildVersion))
219219

220220
jsonData := make(map[string]interface{})

pkg/api/plugins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ func (hs *HTTPServer) makePluginResourceRequest(w http.ResponseWriter, req *http
555555
}
556556
}
557557

558-
proxyutil.ClearCookieHeader(req, keepCookieModel.KeepCookies)
558+
proxyutil.ClearCookieHeader(req, keepCookieModel.KeepCookies, []string{hs.Cfg.LoginCookieName})
559559
proxyutil.PrepareProxyRequest(req)
560560

561561
body, err := ioutil.ReadAll(req.Body)

pkg/services/query/query_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"net/http"
66
"testing"
77

8+
"github.com/grafana/grafana-plugin-sdk-go/backend"
89
"golang.org/x/oauth2"
910

10-
"github.com/grafana/grafana-plugin-sdk-go/backend"
1111
"github.com/grafana/grafana/pkg/api/dtos"
1212
"github.com/grafana/grafana/pkg/components/simplejson"
1313
"github.com/grafana/grafana/pkg/models"

pkg/util/proxyutil/proxyutil.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package proxyutil
33
import (
44
"net"
55
"net/http"
6+
"sort"
67
)
78

89
// PrepareProxyRequest prepares a request for being proxied.
@@ -26,19 +27,31 @@ func PrepareProxyRequest(req *http.Request) {
2627
}
2728
}
2829

29-
// ClearCookieHeader clear cookie header, except for cookies specified to be kept.
30-
func ClearCookieHeader(req *http.Request, keepCookiesNames []string) {
31-
var keepCookies []*http.Cookie
30+
// ClearCookieHeader clear cookie header, except for cookies specified to be kept (keepCookiesNames) if not in skipCookiesNames.
31+
func ClearCookieHeader(req *http.Request, keepCookiesNames []string, skipCookiesNames []string) {
32+
keepCookies := map[string]*http.Cookie{}
3233
for _, c := range req.Cookies() {
3334
for _, v := range keepCookiesNames {
3435
if c.Name == v {
35-
keepCookies = append(keepCookies, c)
36+
keepCookies[c.Name] = c
3637
}
3738
}
3839
}
3940

41+
for _, v := range skipCookiesNames {
42+
delete(keepCookies, v)
43+
}
44+
4045
req.Header.Del("Cookie")
41-
for _, c := range keepCookies {
46+
47+
sortedCookies := []string{}
48+
for name := range keepCookies {
49+
sortedCookies = append(sortedCookies, name)
50+
}
51+
sort.Strings(sortedCookies)
52+
53+
for _, name := range sortedCookies {
54+
c := keepCookies[name]
4255
req.AddCookie(c)
4356
}
4457
}

pkg/util/proxyutil/proxyutil_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestClearCookieHeader(t *testing.T) {
4949
require.NoError(t, err)
5050
req.AddCookie(&http.Cookie{Name: "cookie"})
5151

52-
ClearCookieHeader(req, nil)
52+
ClearCookieHeader(req, nil, nil)
5353
require.NotContains(t, req.Header, "Cookie")
5454
})
5555

@@ -60,8 +60,20 @@ func TestClearCookieHeader(t *testing.T) {
6060
req.AddCookie(&http.Cookie{Name: "cookie2"})
6161
req.AddCookie(&http.Cookie{Name: "cookie3"})
6262

63-
ClearCookieHeader(req, []string{"cookie1", "cookie3"})
63+
ClearCookieHeader(req, []string{"cookie1", "cookie3"}, nil)
6464
require.Contains(t, req.Header, "Cookie")
6565
require.Equal(t, "cookie1=; cookie3=", req.Header.Get("Cookie"))
6666
})
67+
68+
t.Run("Clear cookie header with cookies to keep and skip should clear Cookie header and keep cookies", func(t *testing.T) {
69+
req, err := http.NewRequest(http.MethodGet, "/", nil)
70+
require.NoError(t, err)
71+
req.AddCookie(&http.Cookie{Name: "cookie1"})
72+
req.AddCookie(&http.Cookie{Name: "cookie2"})
73+
req.AddCookie(&http.Cookie{Name: "cookie3"})
74+
75+
ClearCookieHeader(req, []string{"cookie1", "cookie3"}, []string{"cookie3"})
76+
require.Contains(t, req.Header, "Cookie")
77+
require.Equal(t, "cookie1=", req.Header.Get("Cookie"))
78+
})
6779
}

0 commit comments

Comments
 (0)