Skip to content

Commit

Permalink
Only check access tokens if they are likely to be tokens (#16164) (#1…
Browse files Browse the repository at this point in the history
…6171)

Backprt #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 <art27@cantab.net>
  • Loading branch information
zeripath authored Jun 16, 2021
1 parent bc82bb9 commit 946eb13
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion models/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ func GetAccessTokenBySHA(token string) (*AccessToken, error) {
if token == "" {
return nil, ErrAccessTokenEmpty{}
}
if len(token) < 8 {
// A token is defined as being SHA1 sum these are 40 hexadecimal bytes long
if len(token) != 40 {
return nil, ErrAccessTokenNotExist{token}
}
for _, x := range []byte(token) {
if x < '0' || (x > '9' && x < 'a') || x > 'f' {
return nil, ErrAccessTokenNotExist{token}
}
}
var tokens []AccessToken
lastEight := token[len(token)-8:]
err := x.Table(&AccessToken{}).Where("token_last_eight = ?", lastEight).Find(&tokens)
Expand Down

0 comments on commit 946eb13

Please sign in to comment.