Skip to content

Commit b8e4ce7

Browse files
authored
Only check access tokens if they are likely to be tokens (#16164)
* Only check access tokens if they are likely to be tokens 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 3d99131 commit b8e4ce7

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)