From c1b01b5bc09f271f907495b063b963fa4dc0c661 Mon Sep 17 00:00:00 2001 From: Andy Thompson Date: Tue, 8 Feb 2022 20:40:40 +0000 Subject: [PATCH] Fix issue with query string allowed group panic on skip methods --- oauthproxy.go | 5 +++ oauthproxy_test.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/oauthproxy.go b/oauthproxy.go index 69bcdbc5..1cd35e65 100644 --- a/oauthproxy.go +++ b/oauthproxy.go @@ -977,6 +977,11 @@ func (p *OAuthProxy) getAuthenticatedSession(rw http.ResponseWriter, req *http.R // //nolint:gosimple func authOnlyAuthorize(req *http.Request, s *sessionsapi.SessionState) bool { + // Allow requests previously allowed to be bypassed + if s == nil { + return true + } + // Allow secondary group restrictions based on the `allowed_groups` // querystring parameter if !checkAllowedGroups(req, s) { diff --git a/oauthproxy_test.go b/oauthproxy_test.go index 16b3959b..14f1a022 100644 --- a/oauthproxy_test.go +++ b/oauthproxy_test.go @@ -2594,3 +2594,92 @@ func TestAuthOnlyAllowedGroups(t *testing.T) { }) } } + +func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) { + testCases := []struct { + name string + groups []string + method string + ip string + withSession bool + expectedStatusCode int + }{ + { + name: "UserWithGroupSkipAuthPreflight", + groups: []string{"a", "c"}, + method: "OPTIONS", + ip: "1.2.3.5:43670", + withSession: true, + expectedStatusCode: http.StatusAccepted, + }, + { + name: "UserWithGroupTrustedIp", + groups: []string{"a", "c"}, + method: "GET", + ip: "1.2.3.4:43670", + withSession: true, + expectedStatusCode: http.StatusAccepted, + }, + { + name: "UserWithoutGroupSkipAuthPreflight", + groups: []string{"c"}, + method: "OPTIONS", + ip: "1.2.3.5:43670", + withSession: true, + expectedStatusCode: http.StatusForbidden, + }, + { + name: "UserWithoutGroupTrustedIp", + groups: []string{"c"}, + method: "GET", + ip: "1.2.3.4:43670", + withSession: true, + expectedStatusCode: http.StatusForbidden, + }, + { + name: "UserWithoutSessionSkipAuthPreflight", + method: "OPTIONS", + ip: "1.2.3.5:43670", + withSession: false, + expectedStatusCode: http.StatusAccepted, + }, + { + name: "UserWithoutSessionTrustedIp", + method: "GET", + ip: "1.2.3.4:43670", + withSession: false, + expectedStatusCode: http.StatusAccepted, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + test, err := NewAuthOnlyEndpointTest("?allowed_groups=a,b", func(opts *options.Options) { + opts.SkipAuthPreflight = true + opts.TrustedIPs = []string{"1.2.3.4"} + }) + if err != nil { + t.Fatal(err) + } + + test.req.Method = tc.method + test.req.RemoteAddr = tc.ip + + if tc.withSession { + created := time.Now() + session := &sessions.SessionState{ + Groups: tc.groups, + Email: "test", + AccessToken: "oauth_token", + CreatedAt: &created, + } + err = test.SaveSession(session) + } + assert.NoError(t, err) + + test.proxy.ServeHTTP(test.rw, test.req) + + assert.Equal(t, tc.expectedStatusCode, test.rw.Code) + }) + } +}