Skip to content
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

Use single shared random string generation function #15741

Merged
merged 4 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions models/migrations/v71.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"crypto/sha256"
"fmt"

"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"golang.org/x/crypto/pbkdf2"
"xorm.io/xorm"
Expand Down Expand Up @@ -53,7 +53,7 @@ func addScratchHash(x *xorm.Engine) error {

for _, tfa := range tfas {
// generate salt
salt, err := generate.GetRandomString(10)
salt, err := util.RandomString(10)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions models/migrations/v85.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package migrations
import (
"fmt"

"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"xorm.io/xorm"
)
Expand Down Expand Up @@ -65,7 +65,7 @@ func hashAppToken(x *xorm.Engine) error {

for _, token := range tokens {
// generate salt
salt, err := generate.GetRandomString(10)
salt, err := util.RandomString(10)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions models/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"

"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

gouuid "github.com/google/uuid"
)
Expand Down Expand Up @@ -40,7 +40,7 @@ func (t *AccessToken) AfterLoad() {

// NewAccessToken creates new access token.
func NewAccessToken(t *AccessToken) error {
salt, err := generate.GetRandomString(10)
salt, err := util.RandomString(10)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions models/twofactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"encoding/base64"
"fmt"

"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/secret"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"github.com/pquerna/otp/totp"
"golang.org/x/crypto/pbkdf2"
Expand All @@ -34,11 +34,11 @@ type TwoFactor struct {

// GenerateScratchToken recreates the scratch token the user is using.
func (t *TwoFactor) GenerateScratchToken() (string, error) {
token, err := generate.GetRandomString(8)
token, err := util.RandomString(8)
if err != nil {
return "", err
}
t.ScratchSalt, _ = generate.GetRandomString(10)
t.ScratchSalt, _ = util.RandomString(10)
t.ScratchHash = hashToken(token, t.ScratchSalt)
return token, nil
}
Expand Down
3 changes: 1 addition & 2 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"unicode/utf8"

"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -746,7 +745,7 @@ func IsUserExist(uid int64, name string) (bool, error) {

// GetUserSalt returns a random user salt token.
func GetUserSalt() (string, error) {
return generate.GetRandomString(10)
return util.RandomString(10)
}

// NewGhostUser creates and returns a fake user for someone has deleted his/her account.
Expand Down
10 changes: 6 additions & 4 deletions modules/avatar/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (

// Enable PNG support:
_ "image/png"
"math/rand"
"time"

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

"github.com/issue9/identicon"
"github.com/nfnt/resize"
Expand All @@ -29,8 +28,11 @@ const AvatarSize = 290
// in custom size (height and width).
func RandomImageSize(size int, data []byte) (image.Image, error) {
randExtent := len(palette.WebSafe) - 32
rand.Seed(time.Now().UnixNano())
colorIndex := rand.Intn(randExtent)
integer, err := util.RandomInt(int64(randExtent))
if err != nil {
return nil, fmt.Errorf("identicon.New: %v", err)
silverwind marked this conversation as resolved.
Show resolved Hide resolved
}
colorIndex := int(integer)
backColorIndex := colorIndex - 1
if backColorIndex < 0 {
backColorIndex = randExtent - 1
Expand Down
13 changes: 9 additions & 4 deletions modules/avatar/avatar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_RandomImage(t *testing.T) {
_, err := RandomImage([]byte("gogs@local"))
func Test_RandomImageSize(t *testing.T) {
_, err := RandomImageSize(0, []byte("gitea@local"))
assert.Error(t, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which error ?

Copy link
Member Author

@silverwind silverwind May 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That an image of size 0 is invalid. It returns a chinese error currently coming from https://github.com/issue9/identicon:

identicon.New: 参数 size 的值(0)不能小于 16


_, err = RandomImageSize(64, []byte("gitea@local"))
assert.NoError(t, err)
}

_, err = RandomImageSize(0, []byte("gogs@local"))
assert.Error(t, err)
func Test_RandomImage(t *testing.T) {
_, err := RandomImage([]byte("gitea@local"))
assert.NoError(t, err)
}

func Test_PrepareWithPNG(t *testing.T) {
Expand Down
100 changes: 0 additions & 100 deletions modules/context/secret.go

This file was deleted.

32 changes: 2 additions & 30 deletions modules/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,12 @@ import (
"crypto/rand"
"encoding/base64"
"io"
"math/big"
"time"

"code.gitea.io/gitea/modules/util"
"github.com/dgrijalva/jwt-go"
)

// GetRandomString generate random string by specify chars.
func GetRandomString(n int) (string, error) {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

buffer := make([]byte, n)
max := big.NewInt(int64(len(alphanum)))

for i := 0; i < n; i++ {
index, err := randomInt(max)
if err != nil {
return "", err
}

buffer[i] = alphanum[index]
}

return string(buffer), nil
}

// NewInternalToken generate a new value intended to be used by INTERNAL_TOKEN.
func NewInternalToken() (string, error) {
secretBytes := make([]byte, 32)
Expand Down Expand Up @@ -69,19 +50,10 @@ func NewJwtSecret() (string, error) {

// NewSecretKey generate a new value intended to be used by SECRET_KEY.
func NewSecretKey() (string, error) {
secretKey, err := GetRandomString(64)
secretKey, err := util.RandomString(64)
if err != nil {
return "", err
}

return secretKey, nil
}

func randomInt(max *big.Int) (int, error) {
rand, err := rand.Int(rand.Reader, max)
if err != nil {
return 0, err
}

return int(rand.Int64()), nil
}
24 changes: 0 additions & 24 deletions modules/generate/generate_test.go

This file was deleted.

19 changes: 4 additions & 15 deletions modules/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,18 @@ import (
"encoding/hex"
"errors"
"io"

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

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

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

func randomBytes(len int64) ([]byte, error) {
b := make([]byte, len)
if _, err := rand.Read(b); err != nil {
return nil, err
}
return b, nil
}

func randomString(len int64) (string, error) {
b, err := randomBytes(len)
return base64.URLEncoding.EncodeToString(b), err
return util.RandomString(length)
}

// AesEncrypt encrypts text and given key with AES.
Expand Down
2 changes: 1 addition & 1 deletion modules/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestNew(t *testing.T) {
result, err := New()
assert.NoError(t, err)
assert.True(t, len(result) > 32)
assert.True(t, len(result) == 44)

result2, err := New()
assert.NoError(t, err)
Expand Down
Loading