mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-19 03:57:17 +02:00
sessions: check idp id to detect provider changes to force session invalidation (#3707)
* sessions: check idp id to detect provider changes to force session invalidation * remove dead code * fix test
This commit is contained in:
parent
3f7a482815
commit
30bdae3d9e
14 changed files with 265 additions and 193 deletions
128
config/session_test.go
Normal file
128
config/session_test.go
Normal file
|
@ -0,0 +1,128 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/encoding/jws"
|
||||
"github.com/pomerium/pomerium/internal/httputil"
|
||||
"github.com/pomerium/pomerium/internal/sessions"
|
||||
"github.com/pomerium/pomerium/internal/urlutil"
|
||||
"github.com/pomerium/pomerium/pkg/cryptutil"
|
||||
)
|
||||
|
||||
func TestSessionStore_LoadSessionState(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sharedKey := cryptutil.NewKey()
|
||||
options := NewDefaultOptions()
|
||||
options.SharedKey = base64.StdEncoding.EncodeToString(sharedKey)
|
||||
options.Provider = "oidc"
|
||||
options.ProviderURL = "https://oidc.example.com"
|
||||
options.ClientID = "client_id"
|
||||
options.ClientSecret = "client_secret"
|
||||
options.Policies = append(options.Policies,
|
||||
Policy{
|
||||
From: "https://p1.example.com",
|
||||
To: mustParseWeightedURLs(t, "https://p1"),
|
||||
IDPClientID: "client_id_1",
|
||||
IDPClientSecret: "client_secret_1",
|
||||
},
|
||||
Policy{
|
||||
From: "https://p2.example.com",
|
||||
To: mustParseWeightedURLs(t, "https://p2"),
|
||||
IDPClientID: "client_id_2",
|
||||
IDPClientSecret: "client_secret_2",
|
||||
})
|
||||
require.NoError(t, options.Validate())
|
||||
|
||||
store, err := NewSessionStore(options)
|
||||
require.NoError(t, err)
|
||||
|
||||
idp1, err := options.GetIdentityProviderForPolicy(nil)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, idp1)
|
||||
|
||||
idp2, err := options.GetIdentityProviderForPolicy(&options.Policies[0])
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, idp2)
|
||||
|
||||
idp3, err := options.GetIdentityProviderForPolicy(&options.Policies[1])
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, idp3)
|
||||
|
||||
makeJWS := func(t *testing.T, state *sessions.State) string {
|
||||
e, err := jws.NewHS256Signer(sharedKey)
|
||||
require.NoError(t, err)
|
||||
|
||||
rawJWS, err := e.Marshal(state)
|
||||
require.NoError(t, err)
|
||||
|
||||
return string(rawJWS)
|
||||
}
|
||||
|
||||
t.Run("mssing", func(t *testing.T) {
|
||||
r, err := http.NewRequest(http.MethodGet, "https://p1.example.com", nil)
|
||||
require.NoError(t, err)
|
||||
s, err := store.LoadSessionState(r)
|
||||
assert.ErrorIs(t, err, sessions.ErrNoSessionFound)
|
||||
assert.Nil(t, s)
|
||||
})
|
||||
t.Run("query", func(t *testing.T) {
|
||||
rawJWS := makeJWS(t, &sessions.State{
|
||||
Issuer: "authenticate.example.com",
|
||||
ID: "example",
|
||||
IdentityProviderID: idp2.GetId(),
|
||||
})
|
||||
|
||||
r, err := http.NewRequest(http.MethodGet, "https://p1.example.com?"+url.Values{
|
||||
urlutil.QuerySession: {rawJWS},
|
||||
}.Encode(), nil)
|
||||
require.NoError(t, err)
|
||||
s, err := store.LoadSessionState(r)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, cmp.Diff(&sessions.State{
|
||||
Issuer: "authenticate.example.com",
|
||||
ID: "example",
|
||||
IdentityProviderID: idp2.GetId(),
|
||||
}, s))
|
||||
})
|
||||
t.Run("header", func(t *testing.T) {
|
||||
rawJWS := makeJWS(t, &sessions.State{
|
||||
Issuer: "authenticate.example.com",
|
||||
ID: "example",
|
||||
IdentityProviderID: idp3.GetId(),
|
||||
})
|
||||
|
||||
r, err := http.NewRequest(http.MethodGet, "https://p2.example.com", nil)
|
||||
require.NoError(t, err)
|
||||
r.Header.Set(httputil.HeaderPomeriumAuthorization, rawJWS)
|
||||
s, err := store.LoadSessionState(r)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, cmp.Diff(&sessions.State{
|
||||
Issuer: "authenticate.example.com",
|
||||
ID: "example",
|
||||
IdentityProviderID: idp3.GetId(),
|
||||
}, s))
|
||||
})
|
||||
t.Run("wrong idp", func(t *testing.T) {
|
||||
rawJWS := makeJWS(t, &sessions.State{
|
||||
Issuer: "authenticate.example.com",
|
||||
ID: "example",
|
||||
IdentityProviderID: idp1.GetId(),
|
||||
})
|
||||
|
||||
r, err := http.NewRequest(http.MethodGet, "https://p2.example.com", nil)
|
||||
require.NoError(t, err)
|
||||
r.Header.Set(httputil.HeaderPomeriumAuthorization, rawJWS)
|
||||
s, err := store.LoadSessionState(r)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, s)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue