Skip to content

Commit 8b21e09

Browse files
authored
Merge branch 'main' into highlight-patch
2 parents e37ceb4 + 1e6fa57 commit 8b21e09

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2700
-1707
lines changed

custom/conf/app.example.ini

Lines changed: 2013 additions & 1288 deletions
Large diffs are not rendered by default.

integrations/goget_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
"testing"
11+
12+
"code.gitea.io/gitea/modules/setting"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestGoGet(t *testing.T) {
17+
defer prepareTestEnv(t)()
18+
19+
req := NewRequest(t, "GET", "/blah/glah/plah?go-get=1")
20+
resp := MakeRequest(t, req, http.StatusOK)
21+
22+
expected := fmt.Sprintf(`<!doctype html>
23+
<html>
24+
<head>
25+
<meta name="go-import" content="%[1]s:%[2]s/blah/glah git %[3]sblah/glah.git">
26+
<meta name="go-source" content="%[1]s:%[2]s/blah/glah _ %[3]sblah/glah/src/branch/master{/dir} %[3]sblah/glah/src/branch/master{/dir}/{file}#L{line}">
27+
</head>
28+
<body>
29+
go get --insecure %[1]s:%[2]s/blah/glah
30+
</body>
31+
</html>
32+
`, setting.Domain, setting.HTTPPort, setting.AppURL)
33+
34+
assert.Equal(t, expected, resp.Body.String())
35+
}

models/migrations/v71.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"crypto/sha256"
99
"fmt"
1010

11-
"code.gitea.io/gitea/modules/generate"
1211
"code.gitea.io/gitea/modules/timeutil"
12+
"code.gitea.io/gitea/modules/util"
1313

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

5454
for _, tfa := range tfas {
5555
// generate salt
56-
salt, err := generate.GetRandomString(10)
56+
salt, err := util.RandomString(10)
5757
if err != nil {
5858
return err
5959
}

models/migrations/v85.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ package migrations
77
import (
88
"fmt"
99

10-
"code.gitea.io/gitea/modules/generate"
1110
"code.gitea.io/gitea/modules/log"
1211
"code.gitea.io/gitea/modules/timeutil"
12+
"code.gitea.io/gitea/modules/util"
1313

1414
"xorm.io/xorm"
1515
)
@@ -65,7 +65,7 @@ func hashAppToken(x *xorm.Engine) error {
6565

6666
for _, token := range tokens {
6767
// generate salt
68-
salt, err := generate.GetRandomString(10)
68+
salt, err := util.RandomString(10)
6969
if err != nil {
7070
return err
7171
}

models/token.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"time"
1111

1212
"code.gitea.io/gitea/modules/base"
13-
"code.gitea.io/gitea/modules/generate"
1413
"code.gitea.io/gitea/modules/timeutil"
14+
"code.gitea.io/gitea/modules/util"
1515

1616
gouuid "github.com/google/uuid"
1717
)
@@ -40,7 +40,7 @@ func (t *AccessToken) AfterLoad() {
4040

4141
// NewAccessToken creates new access token.
4242
func NewAccessToken(t *AccessToken) error {
43-
salt, err := generate.GetRandomString(10)
43+
salt, err := util.RandomString(10)
4444
if err != nil {
4545
return err
4646
}

models/twofactor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
"encoding/base64"
1212
"fmt"
1313

14-
"code.gitea.io/gitea/modules/generate"
1514
"code.gitea.io/gitea/modules/secret"
1615
"code.gitea.io/gitea/modules/setting"
1716
"code.gitea.io/gitea/modules/timeutil"
17+
"code.gitea.io/gitea/modules/util"
1818

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

3535
// GenerateScratchToken recreates the scratch token the user is using.
3636
func (t *TwoFactor) GenerateScratchToken() (string, error) {
37-
token, err := generate.GetRandomString(8)
37+
token, err := util.RandomString(8)
3838
if err != nil {
3939
return "", err
4040
}
41-
t.ScratchSalt, _ = generate.GetRandomString(10)
41+
t.ScratchSalt, _ = util.RandomString(10)
4242
t.ScratchHash = hashToken(token, t.ScratchSalt)
4343
return token, nil
4444
}

models/user.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"unicode/utf8"
2323

2424
"code.gitea.io/gitea/modules/base"
25-
"code.gitea.io/gitea/modules/generate"
2625
"code.gitea.io/gitea/modules/git"
2726
"code.gitea.io/gitea/modules/log"
2827
"code.gitea.io/gitea/modules/setting"
@@ -746,7 +745,7 @@ func IsUserExist(uid int64, name string) (bool, error) {
746745

747746
// GetUserSalt returns a random user salt token.
748747
func GetUserSalt() (string, error) {
749-
return generate.GetRandomString(10)
748+
return util.RandomString(10)
750749
}
751750

752751
// NewGhostUser creates and returns a fake user for someone has deleted his/her account.

modules/avatar/avatar.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ import (
1212

1313
// Enable PNG support:
1414
_ "image/png"
15-
"math/rand"
16-
"time"
1715

1816
"code.gitea.io/gitea/modules/setting"
17+
"code.gitea.io/gitea/modules/util"
1918

2019
"github.com/issue9/identicon"
2120
"github.com/nfnt/resize"
@@ -29,8 +28,11 @@ const AvatarSize = 290
2928
// in custom size (height and width).
3029
func RandomImageSize(size int, data []byte) (image.Image, error) {
3130
randExtent := len(palette.WebSafe) - 32
32-
rand.Seed(time.Now().UnixNano())
33-
colorIndex := rand.Intn(randExtent)
31+
integer, err := util.RandomInt(int64(randExtent))
32+
if err != nil {
33+
return nil, fmt.Errorf("util.RandomInt: %v", err)
34+
}
35+
colorIndex := int(integer)
3436
backColorIndex := colorIndex - 1
3537
if backColorIndex < 0 {
3638
backColorIndex = randExtent - 1

modules/avatar/avatar_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ import (
1313
"github.com/stretchr/testify/assert"
1414
)
1515

16-
func Test_RandomImage(t *testing.T) {
17-
_, err := RandomImage([]byte("gogs@local"))
16+
func Test_RandomImageSize(t *testing.T) {
17+
_, err := RandomImageSize(0, []byte("gitea@local"))
18+
assert.Error(t, err)
19+
20+
_, err = RandomImageSize(64, []byte("gitea@local"))
1821
assert.NoError(t, err)
22+
}
1923

20-
_, err = RandomImageSize(0, []byte("gogs@local"))
21-
assert.Error(t, err)
24+
func Test_RandomImage(t *testing.T) {
25+
_, err := RandomImage([]byte("gitea@local"))
26+
assert.NoError(t, err)
2227
}
2328

2429
func Test_PrepareWithPNG(t *testing.T) {

modules/context/repo.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,12 +905,18 @@ func (ctx *Context) IssueTemplatesFromDefaultBranch() []api.IssueTemplate {
905905
log.Debug("DataAsync: %v", err)
906906
continue
907907
}
908-
defer r.Close()
908+
closed := false
909+
defer func() {
910+
if !closed {
911+
_ = r.Close()
912+
}
913+
}()
909914
data, err := ioutil.ReadAll(r)
910915
if err != nil {
911916
log.Debug("ReadAll: %v", err)
912917
continue
913918
}
919+
_ = r.Close()
914920
var it api.IssueTemplate
915921
content, err := markdown.ExtractMetadata(string(data), &it)
916922
if err != nil {

modules/context/secret.go

Lines changed: 0 additions & 100 deletions
This file was deleted.

modules/generate/generate.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,12 @@ import (
99
"crypto/rand"
1010
"encoding/base64"
1111
"io"
12-
"math/big"
1312
"time"
1413

14+
"code.gitea.io/gitea/modules/util"
1515
"github.com/dgrijalva/jwt-go"
1616
)
1717

18-
// GetRandomString generate random string by specify chars.
19-
func GetRandomString(n int) (string, error) {
20-
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
21-
22-
buffer := make([]byte, n)
23-
max := big.NewInt(int64(len(alphanum)))
24-
25-
for i := 0; i < n; i++ {
26-
index, err := randomInt(max)
27-
if err != nil {
28-
return "", err
29-
}
30-
31-
buffer[i] = alphanum[index]
32-
}
33-
34-
return string(buffer), nil
35-
}
36-
3718
// NewInternalToken generate a new value intended to be used by INTERNAL_TOKEN.
3819
func NewInternalToken() (string, error) {
3920
secretBytes := make([]byte, 32)
@@ -69,19 +50,10 @@ func NewJwtSecret() (string, error) {
6950

7051
// NewSecretKey generate a new value intended to be used by SECRET_KEY.
7152
func NewSecretKey() (string, error) {
72-
secretKey, err := GetRandomString(64)
53+
secretKey, err := util.RandomString(64)
7354
if err != nil {
7455
return "", err
7556
}
7657

7758
return secretKey, nil
7859
}
79-
80-
func randomInt(max *big.Int) (int, error) {
81-
rand, err := rand.Int(rand.Reader, max)
82-
if err != nil {
83-
return 0, err
84-
}
85-
86-
return int(rand.Int64()), nil
87-
}

modules/generate/generate_test.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)