@@ -3,18 +3,14 @@ package sessions
33import (
44 "bytes"
55 "context"
6− "errors"
76 "fmt"
8− "io"
9− "io/ioutil"
10− "reflect"
11− "time"
12− "unicode/utf8"
13−
147 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/clock"
158 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
169 "github.com/pierrec/lz4"
1710 "github.com/vmihailenco/msgpack/v4"
11+ "io"
12+ "io/ioutil"
13+ "time"
1814)
1915
2016// SessionState is used to store information about the currently authenticated user session
@@ -200,11 +196,6 @@ func DecodeSessionState(data []byte, c encryption.Cipher, compressed bool) (*Ses
200196 return nil, fmt.Errorf("error unmarshalling data to session state: %w", err)
201197 }
202198
203− err = ss.validate()
204− if err != nil {
205− return nil, err
206− }
207−
208199 return &ss, nil
209200}
210201
@@ -258,35 +249,3 @@ func lz4Decompress(compressed []byte) ([]byte, error) {
258249
259250 return payload, nil
260251}
261−
262−// validate ensures the decoded session is non-empty and contains valid data
263−//
264−// Non-empty check is needed due to ensure the non-authenticated AES-CFB
265−// decryption doesn't result in garbage data that collides with a valid
266−// MessagePack header bytes (which MessagePack will unmarshal to an empty
267−// default SessionState). <1% chance, but observed with random test data.
268−//
269−// UTF-8 check ensures the strings are valid and not raw bytes overloaded
270−// into Latin-1 encoding. The occurs when legacy unencrypted fields are
271−// decrypted with AES-CFB which results in random bytes.
272−func (s *SessionState) validate() error {
273− for _, field := range []string{
274− s.User,
275− s.Email,
276− s.PreferredUsername,
277− s.AccessToken,
278− s.IDToken,
279− s.RefreshToken,
280− } {
281− if !utf8.ValidString(field) {
282− return errors.New("invalid non-UTF8 field in session")
283− }
284− }
285−
286− empty := new(SessionState)
287− if reflect.DeepEqual(*s, *empty) {
288− return errors.New("invalid empty session unmarshalled")
289− }
290−
291− return nil
292−}