-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[management] Prevent JWT reuse during peer login #6002
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "errors" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/eko/gocache/lib/v4/cache" | ||
| "github.com/eko/gocache/lib/v4/store" | ||
| ) | ||
|
|
||
| const ( | ||
| usedTokenKeyPrefix = "jwt-used:" | ||
| usedTokenMarker = "1" | ||
| ) | ||
|
|
||
| var ( | ||
| ErrTokenAlreadyUsed = errors.New("JWT already used") | ||
| ErrTokenExpired = errors.New("JWT expired") | ||
| ) | ||
|
|
||
| type SessionStore struct { | ||
| cache *cache.Cache[string] | ||
| } | ||
|
|
||
| func NewSessionStore(cacheStore store.StoreInterface) *SessionStore { | ||
| return &SessionStore{cache: cache.New[string](cacheStore)} | ||
| } | ||
|
|
||
| // RegisterToken records a JWT until its exp time and rejects reuse. | ||
| func (s *SessionStore) RegisterToken(ctx context.Context, token string, expiresAt time.Time) error { | ||
| ttl := time.Until(expiresAt) | ||
| if ttl <= 0 { | ||
| return ErrTokenExpired | ||
| } | ||
|
|
||
| key := usedTokenKeyPrefix + hashToken(token) | ||
| _, err := s.cache.Get(ctx, key) | ||
| if err == nil { | ||
| return ErrTokenAlreadyUsed | ||
| } | ||
|
|
||
| var notFound *store.NotFound | ||
| if !errors.As(err, ¬Found) { | ||
| return fmt.Errorf("failed to lookup used token entry: %w", err) | ||
| } | ||
|
|
||
| if err := s.cache.Set(ctx, key, usedTokenMarker, store.WithExpiration(ttl)); err != nil { | ||
| return fmt.Errorf("failed to store used token entry: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func hashToken(token string) string { | ||
| sum := sha256.Sum256([]byte(token)) | ||
| return hex.EncodeToString(sum[:]) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| nbcache "github.com/netbirdio/netbird/management/server/cache" | ||
| ) | ||
|
|
||
| func newTestSessionStore(t *testing.T) *SessionStore { | ||
| t.Helper() | ||
| cacheStore, err := nbcache.NewStore(context.Background(), time.Hour, time.Hour, 100) | ||
| require.NoError(t, err) | ||
| return NewSessionStore(cacheStore) | ||
| } | ||
|
|
||
| func TestSessionStore_FirstRegisterSucceeds(t *testing.T) { | ||
| s := newTestSessionStore(t) | ||
| ctx := context.Background() | ||
|
|
||
| require.NoError(t, s.RegisterToken(ctx, "token", time.Now().Add(time.Hour))) | ||
| } | ||
|
|
||
| func TestSessionStore_RegisterSameTokenTwiceIsRejected(t *testing.T) { | ||
| s := newTestSessionStore(t) | ||
| ctx := context.Background() | ||
| token := "token" | ||
| exp := time.Now().Add(time.Hour) | ||
|
|
||
| require.NoError(t, s.RegisterToken(ctx, token, exp)) | ||
|
|
||
| err := s.RegisterToken(ctx, token, exp) | ||
| require.Error(t, err) | ||
| assert.ErrorIs(t, err, ErrTokenAlreadyUsed) | ||
| } | ||
|
|
||
| func TestSessionStore_RegisterDifferentTokensAreIndependent(t *testing.T) { | ||
| s := newTestSessionStore(t) | ||
| ctx := context.Background() | ||
| exp := time.Now().Add(time.Hour) | ||
|
|
||
| require.NoError(t, s.RegisterToken(ctx, "tokenA", exp)) | ||
| require.NoError(t, s.RegisterToken(ctx, "tokenB", exp)) | ||
| } | ||
|
|
||
| func TestSessionStore_RegisterWithPastExpiryIsRejected(t *testing.T) { | ||
| s := newTestSessionStore(t) | ||
| ctx := context.Background() | ||
| token := "token" | ||
|
|
||
| err := s.RegisterToken(ctx, token, time.Now().Add(-time.Second)) | ||
| require.Error(t, err) | ||
| assert.ErrorIs(t, err, ErrTokenExpired) | ||
| } | ||
|
|
||
| func TestSessionStore_EntryEvictsAtTTLAndAllowsReRegistration(t *testing.T) { | ||
| s := newTestSessionStore(t) | ||
| ctx := context.Background() | ||
| token := "token" | ||
|
|
||
| require.NoError(t, s.RegisterToken(ctx, token, time.Now().Add(50*time.Millisecond))) | ||
|
|
||
| err := s.RegisterToken(ctx, token, time.Now().Add(50*time.Millisecond)) | ||
| assert.ErrorIs(t, err, ErrTokenAlreadyUsed) | ||
|
|
||
| time.Sleep(120 * time.Millisecond) | ||
|
|
||
| require.NoError(t, s.RegisterToken(ctx, token, time.Now().Add(time.Hour))) | ||
| } | ||
|
|
||
| func TestHashToken_StableAndDoesNotLeak(t *testing.T) { | ||
| a := hashToken("tokenA") | ||
| b := hashToken("tokenB") | ||
| assert.Equal(t, a, hashToken("tokenA"), "hash must be deterministic") | ||
| assert.NotEqual(t, a, b, "different tokens must hash differently") | ||
| assert.Len(t, a, 64, "sha256 hex must be 64 chars") | ||
| assert.NotContains(t, a, "tokenA", "raw token must not appear in hash") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.