Skip to content

Commit a8b06c6

Browse files
authored
Merge branch 'main' into tense-past
2 parents a54bcb0 + 5b9557a commit a8b06c6

File tree

29 files changed

+321
-376
lines changed

29 files changed

+321
-376
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@ var migrations = []Migration{
481481
NewMigration("Change Container Metadata", v1_20.ChangeContainerMetadataMultiArch),
482482
// v251 -> v252
483483
NewMigration("Fix incorrect owner team unit access mode", v1_20.FixIncorrectOwnerTeamUnitAccessMode),
484+
// v252 -> v253
485+
NewMigration("Fix incorrect admin team unit access mode", v1_20.FixIncorrectAdminTeamUnitAccessMode),
484486
}
485487

486488
// GetCurrentDBVersion returns the current db version

models/migrations/v1_20/v252.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_20 //nolint
5+
6+
import (
7+
"code.gitea.io/gitea/modules/log"
8+
9+
"xorm.io/xorm"
10+
)
11+
12+
func FixIncorrectAdminTeamUnitAccessMode(x *xorm.Engine) error {
13+
type UnitType int
14+
type AccessMode int
15+
16+
type TeamUnit struct {
17+
ID int64 `xorm:"pk autoincr"`
18+
OrgID int64 `xorm:"INDEX"`
19+
TeamID int64 `xorm:"UNIQUE(s)"`
20+
Type UnitType `xorm:"UNIQUE(s)"`
21+
AccessMode AccessMode
22+
}
23+
24+
const (
25+
// AccessModeAdmin admin access
26+
AccessModeAdmin = 3
27+
)
28+
29+
sess := x.NewSession()
30+
defer sess.Close()
31+
32+
if err := sess.Begin(); err != nil {
33+
return err
34+
}
35+
36+
count, err := sess.Table("team_unit").
37+
Where("team_id IN (SELECT id FROM team WHERE authorize = ?)", AccessModeAdmin).
38+
Update(&TeamUnit{
39+
AccessMode: AccessModeAdmin,
40+
})
41+
if err != nil {
42+
return err
43+
}
44+
log.Debug("Updated %d admin team unit access mode to belong to admin instead of none", count)
45+
46+
return sess.Commit()
47+
}

modules/context/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func Toggle(options *ToggleOptions) func(ctx *Context) {
6767
}
6868

6969
if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" {
70-
ctx.csrf.Validate(ctx)
70+
ctx.Csrf.Validate(ctx)
7171
if ctx.Written() {
7272
return
7373
}
@@ -89,7 +89,7 @@ func Toggle(options *ToggleOptions) func(ctx *Context) {
8989

9090
// Redirect to log in page if auto-signin info is provided and has not signed in.
9191
if !options.SignOutRequired && !ctx.IsSigned &&
92-
len(ctx.GetCookie(setting.CookieUserName)) > 0 {
92+
len(ctx.GetSiteCookie(setting.CookieUserName)) > 0 {
9393
if ctx.Req.URL.Path != "/user/events" {
9494
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
9595
}

modules/context/context.go

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import (
4545
"golang.org/x/crypto/pbkdf2"
4646
)
4747

48+
const CookieNameFlash = "gitea_flash"
49+
4850
// Render represents a template render
4951
type Render interface {
5052
TemplateLookup(tmpl string) (*template.Template, error)
@@ -60,7 +62,7 @@ type Context struct {
6062
Render Render
6163
translation.Locale
6264
Cache cache.Cache
63-
csrf CSRFProtector
65+
Csrf CSRFProtector
6466
Flash *middleware.Flash
6567
Session session.Store
6668

@@ -478,38 +480,26 @@ func (ctx *Context) Redirect(location string, status ...int) {
478480
http.Redirect(ctx.Resp, ctx.Req, location, code)
479481
}
480482

481-
// SetCookie convenience function to set most cookies consistently
483+
// SetSiteCookie convenience function to set most cookies consistently
482484
// CSRF and a few others are the exception here
483-
func (ctx *Context) SetCookie(name, value string, expiry int) {
484-
middleware.SetCookie(ctx.Resp, name, value,
485-
expiry,
486-
setting.AppSubURL,
487-
setting.SessionConfig.Domain,
488-
setting.SessionConfig.Secure,
489-
true,
490-
middleware.SameSite(setting.SessionConfig.SameSite))
485+
func (ctx *Context) SetSiteCookie(name, value string, maxAge int) {
486+
middleware.SetSiteCookie(ctx.Resp, name, value, maxAge)
491487
}
492488

493-
// DeleteCookie convenience function to delete most cookies consistently
489+
// DeleteSiteCookie convenience function to delete most cookies consistently
494490
// CSRF and a few others are the exception here
495-
func (ctx *Context) DeleteCookie(name string) {
496-
middleware.SetCookie(ctx.Resp, name, "",
497-
-1,
498-
setting.AppSubURL,
499-
setting.SessionConfig.Domain,
500-
setting.SessionConfig.Secure,
501-
true,
502-
middleware.SameSite(setting.SessionConfig.SameSite))
491+
func (ctx *Context) DeleteSiteCookie(name string) {
492+
middleware.SetSiteCookie(ctx.Resp, name, "", -1)
503493
}
504494

505-
// GetCookie returns given cookie value from request header.
506-
func (ctx *Context) GetCookie(name string) string {
507-
return middleware.GetCookie(ctx.Req, name)
495+
// GetSiteCookie returns given cookie value from request header.
496+
func (ctx *Context) GetSiteCookie(name string) string {
497+
return middleware.GetSiteCookie(ctx.Req, name)
508498
}
509499

510500
// GetSuperSecureCookie returns given cookie value from request header with secret string.
511501
func (ctx *Context) GetSuperSecureCookie(secret, name string) (string, bool) {
512-
val := ctx.GetCookie(name)
502+
val := ctx.GetSiteCookie(name)
513503
return ctx.CookieDecrypt(secret, val)
514504
}
515505

@@ -530,10 +520,9 @@ func (ctx *Context) CookieDecrypt(secret, val string) (string, bool) {
530520
}
531521

532522
// SetSuperSecureCookie sets given cookie value to response header with secret string.
533-
func (ctx *Context) SetSuperSecureCookie(secret, name, value string, expiry int) {
523+
func (ctx *Context) SetSuperSecureCookie(secret, name, value string, maxAge int) {
534524
text := ctx.CookieEncrypt(secret, value)
535-
536-
ctx.SetCookie(name, text, expiry)
525+
ctx.SetSiteCookie(name, text, maxAge)
537526
}
538527

539528
// CookieEncrypt encrypts a given value using the provided secret
@@ -549,19 +538,19 @@ func (ctx *Context) CookieEncrypt(secret, value string) string {
549538

550539
// GetCookieInt returns cookie result in int type.
551540
func (ctx *Context) GetCookieInt(name string) int {
552-
r, _ := strconv.Atoi(ctx.GetCookie(name))
541+
r, _ := strconv.Atoi(ctx.GetSiteCookie(name))
553542
return r
554543
}
555544

556545
// GetCookieInt64 returns cookie result in int64 type.
557546
func (ctx *Context) GetCookieInt64(name string) int64 {
558-
r, _ := strconv.ParseInt(ctx.GetCookie(name), 10, 64)
547+
r, _ := strconv.ParseInt(ctx.GetSiteCookie(name), 10, 64)
559548
return r
560549
}
561550

562551
// GetCookieFloat64 returns cookie result in float64 type.
563552
func (ctx *Context) GetCookieFloat64(name string) float64 {
564-
v, _ := strconv.ParseFloat(ctx.GetCookie(name), 64)
553+
v, _ := strconv.ParseFloat(ctx.GetSiteCookie(name), 64)
565554
return v
566555
}
567556

@@ -659,7 +648,10 @@ func WithContext(req *http.Request, ctx *Context) *http.Request {
659648

660649
// GetContext retrieves install context from request
661650
func GetContext(req *http.Request) *Context {
662-
return req.Context().Value(contextKey).(*Context)
651+
if ctx, ok := req.Context().Value(contextKey).(*Context); ok {
652+
return ctx
653+
}
654+
return nil
663655
}
664656

665657
// GetContextUser returns context user
@@ -726,54 +718,32 @@ func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
726718
ctx.Data["Context"] = &ctx
727719

728720
ctx.Req = WithContext(req, &ctx)
729-
ctx.csrf = PrepareCSRFProtector(csrfOpts, &ctx)
721+
ctx.Csrf = PrepareCSRFProtector(csrfOpts, &ctx)
730722

731-
// Get flash.
732-
flashCookie := ctx.GetCookie("macaron_flash")
733-
vals, _ := url.ParseQuery(flashCookie)
734-
if len(vals) > 0 {
735-
f := &middleware.Flash{
723+
// Get the last flash message from cookie
724+
lastFlashCookie := middleware.GetSiteCookie(ctx.Req, CookieNameFlash)
725+
if vals, _ := url.ParseQuery(lastFlashCookie); len(vals) > 0 {
726+
// store last Flash message into the template data, to render it
727+
ctx.Data["Flash"] = &middleware.Flash{
736728
DataStore: &ctx,
737729
Values: vals,
738730
ErrorMsg: vals.Get("error"),
739731
SuccessMsg: vals.Get("success"),
740732
InfoMsg: vals.Get("info"),
741733
WarningMsg: vals.Get("warning"),
742734
}
743-
ctx.Data["Flash"] = f
744735
}
745736

746-
f := &middleware.Flash{
747-
DataStore: &ctx,
748-
Values: url.Values{},
749-
ErrorMsg: "",
750-
WarningMsg: "",
751-
InfoMsg: "",
752-
SuccessMsg: "",
753-
}
737+
// prepare an empty Flash message for current request
738+
ctx.Flash = &middleware.Flash{DataStore: &ctx, Values: url.Values{}}
754739
ctx.Resp.Before(func(resp ResponseWriter) {
755-
if flash := f.Encode(); len(flash) > 0 {
756-
middleware.SetCookie(resp, "macaron_flash", flash, 0,
757-
setting.SessionConfig.CookiePath,
758-
middleware.Domain(setting.SessionConfig.Domain),
759-
middleware.HTTPOnly(true),
760-
middleware.Secure(setting.SessionConfig.Secure),
761-
middleware.SameSite(setting.SessionConfig.SameSite),
762-
)
763-
return
740+
if val := ctx.Flash.Encode(); val != "" {
741+
middleware.SetSiteCookie(ctx.Resp, CookieNameFlash, val, 0)
742+
} else if lastFlashCookie != "" {
743+
middleware.SetSiteCookie(ctx.Resp, CookieNameFlash, "", -1)
764744
}
765-
766-
middleware.SetCookie(ctx.Resp, "macaron_flash", "", -1,
767-
setting.SessionConfig.CookiePath,
768-
middleware.Domain(setting.SessionConfig.Domain),
769-
middleware.HTTPOnly(true),
770-
middleware.Secure(setting.SessionConfig.Secure),
771-
middleware.SameSite(setting.SessionConfig.SameSite),
772-
)
773745
})
774746

775-
ctx.Flash = f
776-
777747
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
778748
if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") {
779749
if err := ctx.Req.ParseMultipartForm(setting.Attachment.MaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
@@ -785,7 +755,7 @@ func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
785755
httpcache.SetCacheControlInHeader(ctx.Resp.Header(), 0, "no-transform")
786756
ctx.Resp.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
787757

788-
ctx.Data["CsrfToken"] = ctx.csrf.GetToken()
758+
ctx.Data["CsrfToken"] = ctx.Csrf.GetToken()
789759
ctx.Data["CsrfTokenHtml"] = template.HTML(`<input type="hidden" name="_csrf" value="` + ctx.Data["CsrfToken"].(string) + `">`)
790760

791761
// FIXME: do we really always need these setting? There should be someway to have to avoid having to always set these

0 commit comments

Comments
 (0)