Skip to content

Commit 60ddb51

Browse files
committed
Merge branch 'main' into update_content
2 parents b288976 + 7a484c0 commit 60ddb51

File tree

49 files changed

+458
-382
lines changed

Some content is hidden

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

49 files changed

+458
-382
lines changed

cmd/web.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"code.gitea.io/gitea/modules/graceful"
1717
"code.gitea.io/gitea/modules/log"
1818
"code.gitea.io/gitea/modules/setting"
19-
"code.gitea.io/gitea/modules/util"
2019
"code.gitea.io/gitea/routers"
2120
"code.gitea.io/gitea/routers/routes"
2221

@@ -152,19 +151,6 @@ func setPort(port string) error {
152151
case setting.FCGI:
153152
case setting.FCGIUnix:
154153
default:
155-
// Save LOCAL_ROOT_URL if port changed
156-
cfg := ini.Empty()
157-
isFile, err := util.IsFile(setting.CustomConf)
158-
if err != nil {
159-
log.Fatal("Unable to check if %s is a file", err)
160-
}
161-
if isFile {
162-
// Keeps custom settings if there is already something.
163-
if err := cfg.Append(setting.CustomConf); err != nil {
164-
return fmt.Errorf("Failed to load custom conf '%s': %v", setting.CustomConf, err)
165-
}
166-
}
167-
168154
defaultLocalURL := string(setting.Protocol) + "://"
169155
if setting.HTTPAddr == "0.0.0.0" {
170156
defaultLocalURL += "localhost"
@@ -173,10 +159,10 @@ func setPort(port string) error {
173159
}
174160
defaultLocalURL += ":" + setting.HTTPPort + "/"
175161

176-
cfg.Section("server").Key("LOCAL_ROOT_URL").SetValue(defaultLocalURL)
177-
if err := cfg.SaveTo(setting.CustomConf); err != nil {
178-
return fmt.Errorf("Error saving generated LOCAL_ROOT_URL to custom config: %v", err)
179-
}
162+
// Save LOCAL_ROOT_URL if port changed
163+
setting.CreateOrAppendToCustomConf(func(cfg *ini.File) {
164+
cfg.Section("server").Key("LOCAL_ROOT_URL").SetValue(defaultLocalURL)
165+
})
180166
}
181167
return nil
182168
}

integrations/links_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func TestLinksNoLogin(t *testing.T) {
3535
"/user2/repo1",
3636
"/user2/repo1/projects",
3737
"/user2/repo1/projects/1",
38+
"/assets/img/404.png",
39+
"/assets/img/500.png",
3840
}
3941

4042
for _, link := range links {

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ var migrations = []Migration{
309309
NewMigration("Add LFS columns to Mirror", addLFSMirrorColumns),
310310
// v179 -> v180
311311
NewMigration("Convert avatar url to text", convertAvatarURLToText),
312+
// v180 -> v181
313+
NewMigration("Delete credentials from past migrations", deleteMigrationCredentials),
312314
}
313315

314316
// GetCurrentDBVersion returns the current db version

models/migrations/v180.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 migrations
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/migrations/base"
10+
"code.gitea.io/gitea/modules/structs"
11+
"code.gitea.io/gitea/modules/util"
12+
13+
jsoniter "github.com/json-iterator/go"
14+
"xorm.io/builder"
15+
"xorm.io/xorm"
16+
)
17+
18+
func deleteMigrationCredentials(x *xorm.Engine) (err error) {
19+
const batchSize = 100
20+
21+
// only match migration tasks, that are not pending or running
22+
cond := builder.Eq{
23+
"type": structs.TaskTypeMigrateRepo,
24+
}.And(builder.Gte{
25+
"status": structs.TaskStatusStopped,
26+
})
27+
28+
sess := x.NewSession()
29+
defer sess.Close()
30+
31+
for start := 0; ; start += batchSize {
32+
tasks := make([]*models.Task, 0, batchSize)
33+
if err = sess.Limit(batchSize, start).Where(cond, 0).Find(&tasks); err != nil {
34+
return
35+
}
36+
if len(tasks) == 0 {
37+
break
38+
}
39+
if err = sess.Begin(); err != nil {
40+
return
41+
}
42+
for _, t := range tasks {
43+
if t.PayloadContent, err = removeCredentials(t.PayloadContent); err != nil {
44+
return
45+
}
46+
if _, err = sess.ID(t.ID).Cols("payload_content").Update(t); err != nil {
47+
return
48+
}
49+
}
50+
if err = sess.Commit(); err != nil {
51+
return
52+
}
53+
}
54+
return
55+
}
56+
57+
func removeCredentials(payload string) (string, error) {
58+
var opts base.MigrateOptions
59+
json := jsoniter.ConfigCompatibleWithStandardLibrary
60+
err := json.Unmarshal([]byte(payload), &opts)
61+
if err != nil {
62+
return "", err
63+
}
64+
65+
opts.AuthPassword = ""
66+
opts.AuthToken = ""
67+
opts.CloneAddr = util.SanitizeURLCredentials(opts.CloneAddr, true)
68+
69+
confBytes, err := json.Marshal(opts)
70+
if err != nil {
71+
return "", err
72+
}
73+
return string(confBytes), nil
74+
}

models/task.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import (
88
"fmt"
99

1010
migration "code.gitea.io/gitea/modules/migrations/base"
11+
"code.gitea.io/gitea/modules/secret"
12+
"code.gitea.io/gitea/modules/setting"
1113
"code.gitea.io/gitea/modules/structs"
1214
"code.gitea.io/gitea/modules/timeutil"
15+
"code.gitea.io/gitea/modules/util"
1316
jsoniter "github.com/json-iterator/go"
1417

1518
"xorm.io/builder"
@@ -110,6 +113,24 @@ func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) {
110113
if err != nil {
111114
return nil, err
112115
}
116+
117+
// decrypt credentials
118+
if opts.CloneAddrEncrypted != "" {
119+
if opts.CloneAddr, err = secret.DecryptSecret(setting.SecretKey, opts.CloneAddrEncrypted); err != nil {
120+
return nil, err
121+
}
122+
}
123+
if opts.AuthPasswordEncrypted != "" {
124+
if opts.AuthPassword, err = secret.DecryptSecret(setting.SecretKey, opts.AuthPasswordEncrypted); err != nil {
125+
return nil, err
126+
}
127+
}
128+
if opts.AuthTokenEncrypted != "" {
129+
if opts.AuthToken, err = secret.DecryptSecret(setting.SecretKey, opts.AuthTokenEncrypted); err != nil {
130+
return nil, err
131+
}
132+
}
133+
113134
return &opts, nil
114135
}
115136
return nil, fmt.Errorf("Task type is %s, not Migrate Repo", task.Type.Name())
@@ -205,12 +226,31 @@ func createTask(e Engine, task *Task) error {
205226
func FinishMigrateTask(task *Task) error {
206227
task.Status = structs.TaskStatusFinished
207228
task.EndTime = timeutil.TimeStampNow()
229+
230+
// delete credentials when we're done, they're a liability.
231+
conf, err := task.MigrateConfig()
232+
if err != nil {
233+
return err
234+
}
235+
conf.AuthPassword = ""
236+
conf.AuthToken = ""
237+
conf.CloneAddr = util.SanitizeURLCredentials(conf.CloneAddr, true)
238+
conf.AuthPasswordEncrypted = ""
239+
conf.AuthTokenEncrypted = ""
240+
conf.CloneAddrEncrypted = ""
241+
json := jsoniter.ConfigCompatibleWithStandardLibrary
242+
confBytes, err := json.Marshal(conf)
243+
if err != nil {
244+
return err
245+
}
246+
task.PayloadContent = string(confBytes)
247+
208248
sess := x.NewSession()
209249
defer sess.Close()
210250
if err := sess.Begin(); err != nil {
211251
return err
212252
}
213-
if _, err := sess.ID(task.ID).Cols("status", "end_time").Update(task); err != nil {
253+
if _, err := sess.ID(task.ID).Cols("status", "end_time", "payload_content").Update(task); err != nil {
214254
return err
215255
}
216256

modules/auth/sso/reverseproxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (r *ReverseProxy) VerifyAuthData(req *http.Request, w http.ResponseWriter,
7979

8080
// Make sure requests to API paths, attachment downloads, git and LFS do not create a new session
8181
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) && !isGitRawOrLFSPath(req) {
82-
if sess.Get("uid").(int64) != user.ID {
82+
if sess != nil && (sess.Get("uid") == nil || sess.Get("uid").(int64) != user.ID) {
8383
handleSignIn(w, req, sess, user)
8484
}
8585
}

modules/context/context.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func (ctx *Context) ParamsInt64(p string) int64 {
509509

510510
// SetParams set params into routes
511511
func (ctx *Context) SetParams(k, v string) {
512-
chiCtx := chi.RouteContext(ctx.Req.Context())
512+
chiCtx := chi.RouteContext(ctx)
513513
chiCtx.URLParams.Add(strings.TrimPrefix(k, ":"), url.PathEscape(v))
514514
}
515515

@@ -528,6 +528,26 @@ func (ctx *Context) Status(status int) {
528528
ctx.Resp.WriteHeader(status)
529529
}
530530

531+
// Deadline is part of the interface for context.Context and we pass this to the request context
532+
func (ctx *Context) Deadline() (deadline time.Time, ok bool) {
533+
return ctx.Req.Context().Deadline()
534+
}
535+
536+
// Done is part of the interface for context.Context and we pass this to the request context
537+
func (ctx *Context) Done() <-chan struct{} {
538+
return ctx.Req.Context().Done()
539+
}
540+
541+
// Err is part of the interface for context.Context and we pass this to the request context
542+
func (ctx *Context) Err() error {
543+
return ctx.Req.Context().Err()
544+
}
545+
546+
// Value is part of the interface for context.Context and we pass this to the request context
547+
func (ctx *Context) Value(key interface{}) interface{} {
548+
return ctx.Req.Context().Value(key)
549+
}
550+
531551
// Handler represents a custom handler
532552
type Handler func(*Context)
533553

modules/graceful/server_http.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package graceful
66

77
import (
8+
"context"
89
"crypto/tls"
10+
"net"
911
"net/http"
1012
)
1113

@@ -16,6 +18,7 @@ func newHTTPServer(network, address, name string, handler http.Handler) (*Server
1618
WriteTimeout: DefaultWriteTimeOut,
1719
MaxHeaderBytes: DefaultMaxHeaderBytes,
1820
Handler: handler,
21+
BaseContext: func(net.Listener) context.Context { return GetManager().HammerContext() },
1922
}
2023
server.OnShutdown = func() {
2124
httpServer.SetKeepAlivesEnabled(false)

modules/migrations/base/options.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import "code.gitea.io/gitea/modules/structs"
1111
// this is for internal usage by migrations module and func who interact with it
1212
type MigrateOptions struct {
1313
// required: true
14-
CloneAddr string `json:"clone_addr" binding:"Required"`
15-
AuthUsername string `json:"auth_username"`
16-
AuthPassword string `json:"auth_password"`
17-
AuthToken string `json:"auth_token"`
14+
CloneAddr string `json:"clone_addr" binding:"Required"`
15+
CloneAddrEncrypted string `json:"clone_addr_encrypted,omitempty"`
16+
AuthUsername string `json:"auth_username"`
17+
AuthPassword string `json:"-"`
18+
AuthPasswordEncrypted string `json:"auth_password_encrypted,omitempty"`
19+
AuthToken string `json:"-"`
20+
AuthTokenEncrypted string `json:"auth_token_encrypted,omitempty"`
1821
// required: true
1922
UID int `json:"uid" binding:"Required"`
2023
// required: true

modules/migrations/gitea_uploader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
252252

253253
// calc NumCommits if no draft
254254
if !release.Draft {
255-
commit, err := g.gitRepo.GetCommit(rel.TagName)
255+
commit, err := g.gitRepo.GetTagCommit(rel.TagName)
256256
if err != nil {
257257
return fmt.Errorf("GetCommit: %v", err)
258258
}

modules/notification/mail/mail.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ func (m *mailNotifier) NotifyNewIssue(issue *models.Issue, mentions []*models.Us
5454

5555
func (m *mailNotifier) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, isClosed bool) {
5656
var actionType models.ActionType
57-
issue.Content = ""
5857
if issue.IsPull {
5958
if isClosed {
6059
actionType = models.ActionClosePullRequest
@@ -124,7 +123,6 @@ func (m *mailNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *mode
124123
log.Error("pr.LoadIssue: %v", err)
125124
return
126125
}
127-
pr.Issue.Content = ""
128126
if err := mailer.MailParticipants(pr.Issue, doer, models.ActionMergePullRequest, nil); err != nil {
129127
log.Error("MailParticipants: %v", err)
130128
}
@@ -151,8 +149,6 @@ func (m *mailNotifier) NotifyPullRequestPushCommits(doer *models.User, pr *model
151149
if err := comment.LoadPushCommits(); err != nil {
152150
log.Error("comment.LoadPushCommits: %v", err)
153151
}
154-
comment.Content = ""
155-
156152
m.NotifyCreateIssueComment(doer, comment.Issue.Repo, comment.Issue, comment, nil)
157153
}
158154

modules/public/dynamic.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ import (
1313
"time"
1414
)
1515

16-
// Static implements the static handler for serving assets.
17-
func Static(opts *Options) func(next http.Handler) http.Handler {
18-
return opts.staticHandler(opts.Directory)
16+
func fileSystem(dir string) http.FileSystem {
17+
return http.Dir(dir)
1918
}
2019

21-
// ServeContent serve http content
22-
func ServeContent(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) {
20+
// serveContent serve http content
21+
func serveContent(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) {
2322
http.ServeContent(w, req, fi.Name(), modtime, content)
2423
}

0 commit comments

Comments
 (0)