Skip to content

Commit 7d9cbf2

Browse files
committed
Only check access tokens if they are likely to be tokens (go-gitea#16164)
Backprt go-gitea#16164 Gitea will currently check every if every password is an access token even though most passwords are not and cannot be access tokens. By creation access tokens are 40 byte hexadecimal strings therefore only these should be checked. Signed-off-by: Andrew Thornton <[email protected]>
1 parent bc82bb9 commit 7d9cbf2

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

models/token.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ func GetAccessTokenBySHA(token string) (*AccessToken, error) {
5757
if token == "" {
5858
return nil, ErrAccessTokenEmpty{}
5959
}
60-
if len(token) < 8 {
60+
// A token is defined as being SHA1 sum these are 40 hexadecimal bytes long
61+
if len(token) != 40 {
6162
return nil, ErrAccessTokenNotExist{token}
6263
}
64+
for _, x := range []byte(token) {
65+
if x < '0' || (x > '9' && x < 'a') || x > 'f' {
66+
return nil, ErrAccessTokenNotExist{token}
67+
}
68+
}
6369
var tokens []AccessToken
6470
lastEight := token[len(token)-8:]
6571
err := x.Table(&AccessToken{}).Where("token_last_eight = ?", lastEight).Find(&tokens)

0 commit comments

Comments
 (0)