Skip to content

Use CryptoRandomBytes instead of CryptoRandomString #18439

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 16 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions models/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package auth
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"net/url"
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/secret"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

Expand Down Expand Up @@ -59,10 +59,14 @@ func (app *OAuth2Application) ContainsRedirectURI(redirectURI string) bool {

// GenerateClientSecret will generate the client secret and returns the plaintext and saves the hash at the database
func (app *OAuth2Application) GenerateClientSecret() (string, error) {
clientSecret, err := secret.New()
rBytes, err := util.CryptoRandomBytes(34)
if err != nil {
return "", err
}
// Add a prefix to the hexadecimal, this is in order to make it easier
// for code scanners to grab sensitive tokens.
clientSecret := "gto_" + hex.EncodeToString(rBytes)

hashedSecret, err := bcrypt.GenerateFromPassword([]byte(clientSecret), bcrypt.DefaultCost)
if err != nil {
return "", err
Expand Down Expand Up @@ -394,10 +398,14 @@ func (grant *OAuth2Grant) GenerateNewAuthorizationCode(redirectURI, codeChalleng
}

func (grant *OAuth2Grant) generateNewAuthorizationCode(e db.Engine, redirectURI, codeChallenge, codeChallengeMethod string) (code *OAuth2AuthorizationCode, err error) {
var codeSecret string
if codeSecret, err = secret.New(); err != nil {
rBytes, err := util.CryptoRandomBytes(34)
if err != nil {
return &OAuth2AuthorizationCode{}, err
}
// Add a prefix to the hexadecimal, this is in order to make it easier
// for code scanners to grab sensitive tokens.
codeSecret := "gta_" + hex.EncodeToString(rBytes)

code = &OAuth2AuthorizationCode{
Grant: grant,
GrantID: grant.ID,
Expand Down
12 changes: 0 additions & 12 deletions modules/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,8 @@ import (
"encoding/hex"
"errors"
"io"

"code.gitea.io/gitea/modules/util"
)

// New creates a new secret
func New() (string, error) {
return NewWithLength(44)
}

// NewWithLength creates a new secret for a given length
func NewWithLength(length int64) (string, error) {
return util.CryptoRandomString(length)
}

// AesEncrypt encrypts text and given key with AES.
func AesEncrypt(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
Expand Down
11 changes: 0 additions & 11 deletions modules/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
result, err := New()
assert.NoError(t, err)
assert.True(t, len(result) == 44)

result2, err := New()
assert.NoError(t, err)
// check if secrets
assert.NotEqual(t, result, result2)
}

func TestEncryptDecrypt(t *testing.T) {
var hex string
var str string
Expand Down