Skip to content

Commit 22e081a

Browse files
committed
Merge branch 'master' into fix-mediaquery
2 parents a6a0ac1 + 586bfb9 commit 22e081a

Some content is hidden

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

78 files changed

+811
-531
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
* text=auto eol=lf
22
/vendor/** -text -eol linguist-vendored
33
/public/vendor/** -text -eol linguist-vendored
4+
/templates/**/*.tmpl linguist-language=Handlebars

Dockerfile.rootless

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ RUN mkdir -p /var/lib/gitea /etc/gitea
5050
RUN chown git:git /var/lib/gitea /etc/gitea
5151

5252
COPY docker/rootless /
53-
COPY --from=build-env /go/src/code.gitea.io/gitea/gitea /usr/local/bin/gitea
54-
RUN chown root:root /usr/local/bin/* && chmod 755 /usr/local/bin/*
53+
COPY --from=build-env --chown=root:root /go/src/code.gitea.io/gitea/gitea /usr/local/bin/gitea
5554

5655
USER git:git
5756
ENV GITEA_WORK_DIR /var/lib/gitea

cmd/web.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ func runWeb(ctx *cli.Context) error {
165165
return err
166166
}
167167
}
168-
// Set up Macaron
168+
// Set up Chi routes
169169
c := routes.NewChi()
170-
routes.RegisterRoutes(c)
170+
c.Mount("/", routes.NormalRoutes())
171+
routes.DelegateToMacaron(c)
171172

172173
err := listen(c, true)
173174
<-graceful.GetManager().Done()

contrib/pr/checkout.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ func runPR() {
118118
external.RegisterParsers()
119119
markup.Init()
120120
c := routes.NewChi()
121-
routes.RegisterRoutes(c)
121+
c.Mount("/", routes.NormalRoutes())
122+
routes.DelegateToMacaron(c)
122123

123124
log.Printf("[PR] Ready for testing !\n")
124125
log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n")

docs/content/doc/features/webhooks.en-us.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ if (empty($header_signature)) {
168168
$payload_signature = hash_hmac('sha256', $payload, $secret_key, false);
169169
170170
// check payload signature against header signature
171-
if ($header_signature != $payload_signature) {
171+
if ($header_signature !== $payload_signature) {
172172
error_log('FAILED - payload signature');
173173
exit();
174174
}

integrations/api_admin_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,22 @@ func TestAPIListUsersNonAdmin(t *testing.T) {
144144
req := NewRequestf(t, "GET", "/api/v1/admin/users?token=%s", token)
145145
session.MakeRequest(t, req, http.StatusForbidden)
146146
}
147+
148+
func TestAPICreateUserInvalidEmail(t *testing.T) {
149+
defer prepareTestEnv(t)()
150+
adminUsername := "user1"
151+
session := loginUser(t, adminUsername)
152+
token := getTokenForLoggedInUser(t, session)
153+
urlStr := fmt.Sprintf("/api/v1/admin/users?token=%s", token)
154+
req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
155+
"email": "invalid_email@domain.com\r\n",
156+
"full_name": "invalid user",
157+
"login_name": "invalidUser",
158+
"must_change_password": "true",
159+
"password": "password",
160+
"send_notify": "true",
161+
"source_id": "0",
162+
"username": "invalidUser",
163+
})
164+
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
165+
}

integrations/create_no_session_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func TestSessionFileCreation(t *testing.T) {
5959
defer func() {
6060
setting.SessionConfig.ProviderConfig = oldSessionConfig
6161
c = routes.NewChi()
62-
routes.RegisterRoutes(c)
62+
c.Mount("/", routes.NormalRoutes())
63+
routes.DelegateToMacaron(c)
6364
}()
6465

6566
var config session.Options
@@ -84,7 +85,8 @@ func TestSessionFileCreation(t *testing.T) {
8485
setting.SessionConfig.ProviderConfig = string(newConfigBytes)
8586

8687
c = routes.NewChi()
87-
routes.RegisterRoutes(c)
88+
c.Mount("/", routes.NormalRoutes())
89+
routes.DelegateToMacaron(c)
8890

8991
t.Run("NoSessionOnViewIssue", func(t *testing.T) {
9092
defer PrintCurrentTest(t)()

integrations/integration_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ func TestMain(m *testing.M) {
6868

6969
initIntegrationTest()
7070
c = routes.NewChi()
71-
routes.RegisterRoutes(c)
71+
c.Mount("/", routes.NormalRoutes())
72+
routes.DelegateToMacaron(c)
7273

7374
// integration test settings...
7475
if setting.Cfg != nil {

integrations/pull_merge_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func TestCantMergeWorkInProgress(t *testing.T) {
194194
req := NewRequest(t, "GET", resp.Header().Get("Location"))
195195
resp = session.MakeRequest(t, req, http.StatusOK)
196196
htmlDoc := NewHTMLParser(t, resp.Body)
197-
text := strings.TrimSpace(htmlDoc.doc.Find(".attached.merge-section.no-header > .text.grey").Last().Text())
197+
text := strings.TrimSpace(htmlDoc.doc.Find(".merge-section > .item").Last().Text())
198198
assert.NotEmpty(t, text, "Can't find WIP text")
199199

200200
// remove <strong /> from lang

integrations/pull_status_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestPullCreate_EmptyChangesWithCommits(t *testing.T) {
114114
resp := session.MakeRequest(t, req, http.StatusOK)
115115
doc := NewHTMLParser(t, resp.Body)
116116

117-
text := strings.TrimSpace(doc.doc.Find(".item.text.green").Text())
118-
assert.EqualValues(t, "This pull request can be merged automatically.", text)
117+
text := strings.TrimSpace(doc.doc.Find(".merge-section").Text())
118+
assert.Contains(t, text, "This pull request can be merged automatically.")
119119
})
120120
}

integrations/signup_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
package integrations
66

77
import (
8+
"fmt"
89
"net/http"
10+
"strings"
911
"testing"
1012

1113
"code.gitea.io/gitea/modules/setting"
14+
"github.com/stretchr/testify/assert"
15+
"github.com/unknwon/i18n"
1216
)
1317

1418
func TestSignup(t *testing.T) {
@@ -28,3 +32,37 @@ func TestSignup(t *testing.T) {
2832
req = NewRequest(t, "GET", "/exampleUser")
2933
MakeRequest(t, req, http.StatusOK)
3034
}
35+
36+
func TestSignupEmail(t *testing.T) {
37+
defer prepareTestEnv(t)()
38+
39+
setting.Service.EnableCaptcha = false
40+
41+
tests := []struct {
42+
email string
43+
wantStatus int
44+
wantMsg string
45+
}{
46+
{"exampleUser@example.com\r\n", http.StatusOK, i18n.Tr("en", "form.email_invalid", nil)},
47+
{"exampleUser@example.com\r", http.StatusOK, i18n.Tr("en", "form.email_invalid", nil)},
48+
{"exampleUser@example.com\n", http.StatusOK, i18n.Tr("en", "form.email_invalid", nil)},
49+
{"exampleUser@example.com", http.StatusFound, ""},
50+
}
51+
52+
for i, test := range tests {
53+
req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
54+
"user_name": fmt.Sprintf("exampleUser%d", i),
55+
"email": test.email,
56+
"password": "examplePassword!1",
57+
"retype": "examplePassword!1",
58+
})
59+
resp := MakeRequest(t, req, test.wantStatus)
60+
if test.wantMsg != "" {
61+
htmlDoc := NewHTMLParser(t, resp.Body)
62+
assert.Equal(t,
63+
test.wantMsg,
64+
strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
65+
)
66+
}
67+
}
68+
}

models/error.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,21 @@ func (err ErrEmailAlreadyUsed) Error() string {
193193
return fmt.Sprintf("e-mail already in use [email: %s]", err.Email)
194194
}
195195

196+
// ErrEmailInvalid represents an error where the email address does not comply with RFC 5322
197+
type ErrEmailInvalid struct {
198+
Email string
199+
}
200+
201+
// IsErrEmailInvalid checks if an error is an ErrEmailInvalid
202+
func IsErrEmailInvalid(err error) bool {
203+
_, ok := err.(ErrEmailInvalid)
204+
return ok
205+
}
206+
207+
func (err ErrEmailInvalid) Error() string {
208+
return fmt.Sprintf("e-mail invalid [email: %s]", err.Email)
209+
}
210+
196211
// ErrOpenIDAlreadyUsed represents a "OpenIDAlreadyUsed" kind of error.
197212
type ErrOpenIDAlreadyUsed struct {
198213
OpenID string

models/user.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"errors"
1515
"fmt"
1616
_ "image/jpeg" // Needed for jpeg support
17+
"net/mail"
1718
"os"
1819
"path/filepath"
1920
"regexp"
@@ -808,6 +809,11 @@ func CreateUser(u *User) (err error) {
808809
return ErrEmailAlreadyUsed{u.Email}
809810
}
810811

812+
_, err = mail.ParseAddress(u.Email)
813+
if err != nil {
814+
return ErrEmailInvalid{u.Email}
815+
}
816+
811817
isExist, err = isEmailUsed(sess, u.Email)
812818
if err != nil {
813819
return err
@@ -951,7 +957,12 @@ func checkDupEmail(e Engine, u *User) error {
951957
}
952958

953959
func updateUser(e Engine, u *User) error {
954-
_, err := e.ID(u.ID).AllCols().Update(u)
960+
u.Email = strings.ToLower(u.Email)
961+
_, err := mail.ParseAddress(u.Email)
962+
if err != nil {
963+
return ErrEmailInvalid{u.Email}
964+
}
965+
_, err = e.ID(u.ID).AllCols().Update(u)
955966
return err
956967
}
957968

models/user_mail.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package models
88
import (
99
"errors"
1010
"fmt"
11+
"net/mail"
1112
"strings"
1213

1314
"code.gitea.io/gitea/modules/log"
@@ -143,6 +144,11 @@ func addEmailAddress(e Engine, email *EmailAddress) error {
143144
return ErrEmailAlreadyUsed{email.Email}
144145
}
145146

147+
_, err = mail.ParseAddress(email.Email)
148+
if err != nil {
149+
return ErrEmailInvalid{email.Email}
150+
}
151+
146152
_, err = e.Insert(email)
147153
return err
148154
}
@@ -167,6 +173,10 @@ func AddEmailAddresses(emails []*EmailAddress) error {
167173
} else if used {
168174
return ErrEmailAlreadyUsed{emails[i].Email}
169175
}
176+
_, err = mail.ParseAddress(emails[i].Email)
177+
if err != nil {
178+
return ErrEmailInvalid{emails[i].Email}
179+
}
170180
}
171181

172182
if _, err := x.Insert(emails); err != nil {

models/user_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,21 @@ func TestCreateUser(t *testing.T) {
329329
assert.NoError(t, DeleteUser(user))
330330
}
331331

332+
func TestCreateUserInvalidEmail(t *testing.T) {
333+
user := &User{
334+
Name: "GiteaBot",
335+
Email: "GiteaBot@gitea.io\r\n",
336+
Passwd: ";p['////..-++']",
337+
IsAdmin: false,
338+
Theme: setting.UI.DefaultTheme,
339+
MustChangePassword: false,
340+
}
341+
342+
err := CreateUser(user)
343+
assert.Error(t, err)
344+
assert.True(t, IsErrEmailInvalid(err))
345+
}
346+
332347
func TestCreateUser_Issue5882(t *testing.T) {
333348

334349
// Init settings

modules/context/api.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,61 @@ func (ctx *APIContext) NotFound(objs ...interface{}) {
259259
"errors": errors,
260260
})
261261
}
262+
263+
// RepoRefForAPI handles repository reference names when the ref name is not explicitly given
264+
func RepoRefForAPI() macaron.Handler {
265+
return func(ctx *APIContext) {
266+
// Empty repository does not have reference information.
267+
if ctx.Repo.Repository.IsEmpty {
268+
return
269+
}
270+
271+
var err error
272+
273+
if ctx.Repo.GitRepo == nil {
274+
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
275+
ctx.Repo.GitRepo, err = git.OpenRepository(repoPath)
276+
if err != nil {
277+
ctx.InternalServerError(err)
278+
return
279+
}
280+
// We opened it, we should close it
281+
defer func() {
282+
// If it's been set to nil then assume someone else has closed it.
283+
if ctx.Repo.GitRepo != nil {
284+
ctx.Repo.GitRepo.Close()
285+
}
286+
}()
287+
}
288+
289+
refName := getRefName(ctx.Context, RepoRefAny)
290+
291+
if ctx.Repo.GitRepo.IsBranchExist(refName) {
292+
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
293+
if err != nil {
294+
ctx.InternalServerError(err)
295+
return
296+
}
297+
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
298+
} else if ctx.Repo.GitRepo.IsTagExist(refName) {
299+
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refName)
300+
if err != nil {
301+
ctx.InternalServerError(err)
302+
return
303+
}
304+
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
305+
} else if len(refName) == 40 {
306+
ctx.Repo.CommitID = refName
307+
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
308+
if err != nil {
309+
ctx.NotFound("GetCommit", err)
310+
return
311+
}
312+
} else {
313+
ctx.NotFound(fmt.Errorf("not exist: '%s'", ctx.Params("*")))
314+
return
315+
}
316+
317+
ctx.Next()
318+
}
319+
}

modules/context/repo.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,6 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
716716
err error
717717
)
718718

719-
// For API calls.
720719
if ctx.Repo.GitRepo == nil {
721720
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
722721
ctx.Repo.GitRepo, err = git.OpenRepository(repoPath)
@@ -785,7 +784,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
785784

786785
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
787786
if err != nil {
788-
ctx.NotFound("GetCommit", nil)
787+
ctx.NotFound("GetCommit", err)
789788
return
790789
}
791790
} else {

modules/highlight/highlight.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414
"sync"
1515

16+
"code.gitea.io/gitea/modules/analyze"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/setting"
1819
"github.com/alecthomas/chroma/formatters/html"
@@ -117,9 +118,11 @@ func File(numLines int, fileName string, code []byte) map[int]string {
117118
fileName = "test." + val
118119
}
119120

120-
lexer := lexers.Match(fileName)
121+
language := analyze.GetCodeLanguage(fileName, code)
122+
123+
lexer := lexers.Get(language)
121124
if lexer == nil {
122-
lexer = lexers.Analyse(string(code))
125+
lexer = lexers.Match(fileName)
123126
if lexer == nil {
124127
lexer = lexers.Fallback
125128
}

modules/notification/webhook/webhook.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,3 +797,11 @@ func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *model
797797
log.Error("PrepareWebhooks: %v", err)
798798
}
799799
}
800+
801+
func (m *webhookNotifier) NotifySyncCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
802+
m.NotifyCreateRef(pusher, repo, refType, refFullName)
803+
}
804+
805+
func (m *webhookNotifier) NotifySyncDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
806+
m.NotifyDeleteRef(pusher, repo, refType, refFullName)
807+
}

0 commit comments

Comments
 (0)