Skip to content

Commit 4c29c75

Browse files
authoredDec 27, 2023
Fix session key conflict with database keyword (#28613)
This is a regression from #28220 . `builder.Cond` will not add `` ` `` automatically but xorm method `Get/Find` adds `` ` ``. This PR also adds tests to prevent the method from being implemented incorrectly. The tests are added in `integrations` to test every database.
1 parent a1dfffd commit 4c29c75

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed
 

‎models/auth/session.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ func ReadSession(ctx context.Context, key string) (*Session, error) {
4141
}
4242
defer committer.Close()
4343

44-
session, exist, err := db.Get[Session](ctx, builder.Eq{"key": key})
44+
session, exist, err := db.Get[Session](ctx, builder.Eq{"`key`": key})
4545
if err != nil {
4646
return nil, err
4747
} else if !exist {
48-
session.Expiry = timeutil.TimeStampNow()
49-
if err := db.Insert(ctx, &session); err != nil {
48+
session = &Session{
49+
Key: key,
50+
Expiry: timeutil.TimeStampNow(),
51+
}
52+
if err := db.Insert(ctx, session); err != nil {
5053
return nil, err
5154
}
5255
}
@@ -56,7 +59,7 @@ func ReadSession(ctx context.Context, key string) (*Session, error) {
5659

5760
// ExistSession checks if a session exists
5861
func ExistSession(ctx context.Context, key string) (bool, error) {
59-
return db.Exist[Session](ctx, builder.Eq{"key": key})
62+
return db.Exist[Session](ctx, builder.Eq{"`key`": key})
6063
}
6164

6265
// DestroySession destroys a session
@@ -75,13 +78,13 @@ func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, er
7578
}
7679
defer committer.Close()
7780

78-
if has, err := db.Exist[Session](ctx, builder.Eq{"key": newKey}); err != nil {
81+
if has, err := db.Exist[Session](ctx, builder.Eq{"`key`": newKey}); err != nil {
7982
return nil, err
8083
} else if has {
8184
return nil, fmt.Errorf("session Key: %s already exists", newKey)
8285
}
8386

84-
if has, err := db.Exist[Session](ctx, builder.Eq{"key": oldKey}); err != nil {
87+
if has, err := db.Exist[Session](ctx, builder.Eq{"`key`": oldKey}); err != nil {
8588
return nil, err
8689
} else if !has {
8790
if err := db.Insert(ctx, &Session{
@@ -96,7 +99,7 @@ func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, er
9699
return nil, err
97100
}
98101

99-
s, _, err := db.Get[Session](ctx, builder.Eq{"key": newKey})
102+
s, _, err := db.Get[Session](ctx, builder.Eq{"`key`": newKey})
100103
if err != nil {
101104
// is not exist, it should be impossible
102105
return nil, err

‎tests/integration/session_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/auth"
10+
"code.gitea.io/gitea/models/db"
11+
"code.gitea.io/gitea/models/unittest"
12+
"code.gitea.io/gitea/tests"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func Test_RegenerateSession(t *testing.T) {
18+
defer tests.PrepareTestEnv(t)()
19+
20+
assert.NoError(t, unittest.PrepareTestDatabase())
21+
22+
key := "new_key890123456" // it must be 16 characters long
23+
key2 := "new_key890123457" // it must be 16 characters
24+
exist, err := auth.ExistSession(db.DefaultContext, key)
25+
assert.NoError(t, err)
26+
assert.False(t, exist)
27+
28+
sess, err := auth.RegenerateSession(db.DefaultContext, "", key)
29+
assert.NoError(t, err)
30+
assert.EqualValues(t, key, sess.Key)
31+
assert.Len(t, sess.Data, 0)
32+
33+
sess, err = auth.ReadSession(db.DefaultContext, key2)
34+
assert.NoError(t, err)
35+
assert.EqualValues(t, key2, sess.Key)
36+
assert.Len(t, sess.Data, 0)
37+
}

0 commit comments

Comments
 (0)