Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oauth2: Resolves possible session fixation attack #770

Merged
merged 1 commit into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions oauth2/consent_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
package oauth2

import (
"time"

"net/http"
"time"

"github.com/gorilla/sessions"
"github.com/ory/fosite"
Expand All @@ -39,18 +38,35 @@ type DefaultConsentStrategy struct {
ConsentManager ConsentRequestManager
}

func checkAntiReplayToken(consent *ConsentRequest, cookie *sessions.Session) error {
func (s *DefaultConsentStrategy) validateSession(req fosite.AuthorizeRequester, consent *ConsentRequest, cookie *sessions.Session) error {
if j, ok := cookie.Values[CookieCSRFKey]; !ok {
return errors.Errorf("Session cookie is missing anti-replay token")
return errors.Errorf("Session cookie is missing CSRF token")
} else if js, ok := j.(string); !ok {
return errors.Errorf("Session cookie anti-replay value is not a string")
return errors.Errorf("CSRF value in session cookie is not a string")
} else if js != consent.CSRF {
return errors.Errorf("Session cookie anti-replay value does not match value from consent response")
return errors.Errorf("CSRF value in session cookie does not match consent CSRF value")
} else if consent.CSRF != req.GetRequestForm().Get("consent_csrf") {
return errors.Errorf("CSRF value from query parameters does not match consent CSRF value")
}

if time.Now().After(consent.ExpiresAt) {
return errors.Errorf("Consent session expired")
}

if consent.ClientID != req.GetClient().GetID() {
return errors.Errorf("ClientID mismatch")
}

if consent.Subject == "" {
return errors.Errorf("Subject key is empty or undefined in consent response, check your payload.")
}

return nil
}

func (s *DefaultConsentStrategy) ValidateConsentRequest(req fosite.AuthorizeRequester, session string, cookie *sessions.Session) (claims *Session, err error) {
defer delete(cookie.Values, CookieCSRFKey)

consent, err := s.ConsentManager.GetConsentRequest(session)
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -67,21 +83,9 @@ func (s *DefaultConsentStrategy) ValidateConsentRequest(req fosite.AuthorizeRequ
}
}

if time.Now().After(consent.ExpiresAt) {
return nil, errors.Errorf("Token expired")
}

if consent.ClientID != req.GetClient().GetID() {
return nil, errors.Errorf("ClientID mismatch")
}

if consent.Subject == "" {
return nil, errors.Errorf("Subject key is empty or undefined in consent response, check your payload.")
}

if err := checkAntiReplayToken(consent, cookie); err != nil {
if err := s.validateSession(req, consent, cookie); err != nil {
if err := s.ConsentManager.RejectConsentRequest(session, &RejectConsentRequestPayload{
Reason: "Session cookie is missing anti-replay token",
Reason: "Unable to validate consent request",
}); err != nil {
return nil, err
}
Expand All @@ -92,8 +96,6 @@ func (s *DefaultConsentStrategy) ValidateConsentRequest(req fosite.AuthorizeRequ
req.GrantScope(scope)
}

delete(cookie.Values, CookieCSRFKey)

return &Session{
DefaultSession: &openid.DefaultSession{
Claims: &ejwt.IDTokenClaims{
Expand Down Expand Up @@ -126,7 +128,7 @@ func (s *DefaultConsentStrategy) CreateConsentRequest(req fosite.AuthorizeReques
RequestedScopes: req.GetRequestedScopes(),
ClientID: req.GetClient().GetID(),
ExpiresAt: time.Now().Add(s.DefaultChallengeLifespan),
RedirectURL: redirectURL + "&consent=" + id,
RedirectURL: redirectURL + "&consent=" + id + "&consent_csrf=" + csrf,
AccessTokenExtra: map[string]interface{}{},
IDTokenExtra: map[string]interface{}{},
}
Expand Down
40 changes: 31 additions & 9 deletions oauth2/consent_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package oauth2

import (
"fmt"
"net/url"
"testing"
"time"

Expand Down Expand Up @@ -43,7 +44,15 @@ func TestConsentStrategy(t *testing.T) {
ExpiresAt: time.Now().Add(time.Hour),
}))
require.NoError(t, strategy.ConsentManager.PersistConsentRequest(&ConsentRequest{
ID: "granted_csrf",
ID: "granted_csrf_cookie",
Consent: ConsentRequestAccepted,
ClientID: "client_id",
Subject: "peter",
CSRF: "csrf_token",
ExpiresAt: time.Now().Add(time.Hour),
}))
require.NoError(t, strategy.ConsentManager.PersistConsentRequest(&ConsentRequest{
ID: "granted_csrf_request",
Consent: ConsentRequestAccepted,
ClientID: "client_id",
Subject: "peter",
Expand Down Expand Up @@ -71,36 +80,49 @@ func TestConsentStrategy(t *testing.T) {
d: "invalid session",
session: "not_granted",
expectErr: true,
cookie: &sessions.Session{Values: map[interface{}]interface{}{CookieCSRFKey: "csrf_token"}},
},
{
d: "session expired",
session: "granted_expired",
expectErr: true,
req: &fosite.AuthorizeRequest{Request: fosite.Request{Client: &fosite.DefaultClient{ID: "client_id"}}},
req: &fosite.AuthorizeRequest{Request: fosite.Request{Client: &fosite.DefaultClient{ID: "client_id"}, Form: url.Values{"consent_csrf": {"csrf_token"}}}},
cookie: &sessions.Session{Values: map[interface{}]interface{}{CookieCSRFKey: "csrf_token"}},
},
{
d: "granted",
session: "granted",
expectErr: false,
req: &fosite.AuthorizeRequest{Request: fosite.Request{Client: &fosite.DefaultClient{ID: "client_id"}}},
req: &fosite.AuthorizeRequest{Request: fosite.Request{Client: &fosite.DefaultClient{ID: "client_id"}, Form: url.Values{"consent_csrf": {"csrf_token"}}}},
cookie: &sessions.Session{Values: map[interface{}]interface{}{CookieCSRFKey: "csrf_token"}},
},
{
d: "client mismatch",
session: "granted",
expectErr: true,
req: &fosite.AuthorizeRequest{Request: fosite.Request{Client: &fosite.DefaultClient{ID: "mismatch_client"}}},
req: &fosite.AuthorizeRequest{Request: fosite.Request{Client: &fosite.DefaultClient{ID: "mismatch_client"}, Form: url.Values{"consent_csrf": {"csrf_token"}}}},
cookie: &sessions.Session{Values: map[interface{}]interface{}{CookieCSRFKey: "csrf_token"}},
},
{
d: "csrf detected",
session: "granted_csrf",
d: "consent request was not initiated by this user agent",
session: "granted_csrf_cookie",
expectErr: true,
req: &fosite.AuthorizeRequest{Request: fosite.Request{Client: &fosite.DefaultClient{ID: "client_id"}}},
cookie: &sessions.Session{Values: map[interface{}]interface{}{CookieCSRFKey: "invalid_csrf"}},
req: &fosite.AuthorizeRequest{Request: fosite.Request{Client: &fosite.DefaultClient{ID: "client_id"}, Form: url.Values{"consent_csrf": {"csrf_token"}}}},
cookie: &sessions.Session{Values: map[interface{}]interface{}{CookieCSRFKey: "very_different_csrf_token"}},
assert: func(t *testing.T, session *Session) {
cr, err := strategy.ConsentManager.GetConsentRequest("granted_csrf_cookie")
require.NoError(t, err)
assert.False(t, cr.IsConsentGranted())
},
},
{
d: "authorize url contains csrf token that does not match token set in consent request",
session: "granted_csrf_request",
expectErr: true,
req: &fosite.AuthorizeRequest{Request: fosite.Request{Client: &fosite.DefaultClient{ID: "client_id"}, Form: url.Values{"consent_csrf": {"very_different_csrf_token"}}}},
cookie: &sessions.Session{Values: map[interface{}]interface{}{CookieCSRFKey: "csrf_token"}},
assert: func(t *testing.T, session *Session) {
cr, err := strategy.ConsentManager.GetConsentRequest("granted_csrf")
cr, err := strategy.ConsentManager.GetConsentRequest("granted_csrf_request")
require.NoError(t, err)
assert.False(t, cr.IsConsentGranted())
},
Expand Down