internal/sessions: error if session too large

This commit is contained in:
Bobby DeSimone 2019-07-06 11:34:49 -07:00
parent 10a1d2fd7e
commit 63043dec9c
No known key found for this signature in database
GPG key ID: AEE4CF12FE86D07E
4 changed files with 53 additions and 3 deletions

View file

@ -1,10 +1,12 @@
package sessions
import (
"crypto/rand"
"reflect"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/pomerium/pomerium/internal/cryptutil"
)
@ -138,3 +140,41 @@ func TestSessionState_Impersonating(t *testing.T) {
})
}
}
func TestMarshalSession(t *testing.T) {
secret := cryptutil.GenerateKey()
c, err := cryptutil.NewCipher([]byte(secret))
if err != nil {
t.Fatalf("expected to be able to create cipher: %v", err)
}
hugeString := make([]byte, 4097)
if _, err := rand.Read(hugeString); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
s *SessionState
wantErr bool
}{
{"simple", &SessionState{}, false},
{"too big", &SessionState{AccessToken: string(hugeString)}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
in, err := MarshalSession(tt.s, c)
if (err != nil) != tt.wantErr {
t.Errorf("MarshalSession() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil {
out, err := UnmarshalSession(in, c)
if err != nil {
t.Fatalf("expected to be decode session: %v", err)
}
if diff := cmp.Diff(tt.s, out); diff != "" {
t.Errorf("MarshalSession() = %s", diff)
}
}
})
}
}