Skip to content

Commit 161e12e

Browse files
authored
Shadow the password on cache and session config on admin panel (#7300)
* shadow the password on cache and session config on admin panel * add shadow password of mysql/postgres/couchbase * fix log import
1 parent 42729b7 commit 161e12e

File tree

2 files changed

+135
-2
lines changed

2 files changed

+135
-2
lines changed

routers/admin/admin.go

+66-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

56
package admin
67

78
import (
89
"fmt"
10+
"net/url"
911
"os"
1012
"runtime"
1113
"strings"
@@ -19,6 +21,7 @@ import (
1921
"code.gitea.io/gitea/modules/context"
2022
"code.gitea.io/gitea/modules/cron"
2123
"code.gitea.io/gitea/modules/git"
24+
"code.gitea.io/gitea/modules/log"
2225
"code.gitea.io/gitea/modules/process"
2326
"code.gitea.io/gitea/modules/setting"
2427
)
@@ -202,6 +205,63 @@ func SendTestMail(ctx *context.Context) {
202205
ctx.Redirect(setting.AppSubURL + "/admin/config")
203206
}
204207

208+
func shadownPasswordKV(cfgItem, splitter string) string {
209+
fields := strings.Split(cfgItem, splitter)
210+
for i := 0; i < len(fields); i++ {
211+
if strings.HasPrefix(fields[i], "password=") {
212+
fields[i] = "password=******"
213+
break
214+
}
215+
}
216+
return strings.Join(fields, splitter)
217+
}
218+
219+
func shadownURL(provider, cfgItem string) string {
220+
u, err := url.Parse(cfgItem)
221+
if err != nil {
222+
log.Error("shodowPassword %v failed: %v", provider, err)
223+
return cfgItem
224+
}
225+
if u.User != nil {
226+
atIdx := strings.Index(cfgItem, "@")
227+
if atIdx > 0 {
228+
colonIdx := strings.LastIndex(cfgItem[:atIdx], ":")
229+
if colonIdx > 0 {
230+
return cfgItem[:colonIdx+1] + "******" + cfgItem[atIdx:]
231+
}
232+
}
233+
}
234+
return cfgItem
235+
}
236+
237+
func shadowPassword(provider, cfgItem string) string {
238+
switch provider {
239+
case "redis":
240+
return shadownPasswordKV(cfgItem, ",")
241+
case "mysql":
242+
//root:@tcp(localhost:3306)/macaron?charset=utf8
243+
atIdx := strings.Index(cfgItem, "@")
244+
if atIdx > 0 {
245+
colonIdx := strings.Index(cfgItem[:atIdx], ":")
246+
if colonIdx > 0 {
247+
return cfgItem[:colonIdx+1] + "******" + cfgItem[atIdx:]
248+
}
249+
}
250+
return cfgItem
251+
case "postgres":
252+
// user=jiahuachen dbname=macaron port=5432 sslmode=disable
253+
if !strings.HasPrefix(cfgItem, "postgres://") {
254+
return shadownPasswordKV(cfgItem, " ")
255+
}
256+
257+
// postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full
258+
// Notice: use shadwonURL
259+
}
260+
261+
// "couchbase"
262+
return shadownURL(provider, cfgItem)
263+
}
264+
205265
// Config show admin config page
206266
func Config(ctx *context.Context) {
207267
ctx.Data["Title"] = ctx.Tr("admin.config")
@@ -239,10 +299,14 @@ func Config(ctx *context.Context) {
239299

240300
ctx.Data["CacheAdapter"] = setting.CacheService.Adapter
241301
ctx.Data["CacheInterval"] = setting.CacheService.Interval
242-
ctx.Data["CacheConn"] = setting.CacheService.Conn
302+
303+
ctx.Data["CacheConn"] = shadowPassword(setting.CacheService.Adapter, setting.CacheService.Conn)
243304
ctx.Data["CacheItemTTL"] = setting.CacheService.TTL
244305

245-
ctx.Data["SessionConfig"] = setting.SessionConfig
306+
sessionCfg := setting.SessionConfig
307+
sessionCfg.ProviderConfig = shadowPassword(sessionCfg.Provider, sessionCfg.ProviderConfig)
308+
309+
ctx.Data["SessionConfig"] = sessionCfg
246310

247311
ctx.Data["DisableGravatar"] = setting.DisableGravatar
248312
ctx.Data["EnableFederatedAvatar"] = setting.EnableFederatedAvatar

routers/admin/admin_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2019 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 admin
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestShadowPassword(t *testing.T) {
14+
var kases = []struct {
15+
Provider string
16+
CfgItem string
17+
Result string
18+
}{
19+
{
20+
Provider: "redis",
21+
CfgItem: "network=tcp,addr=:6379,password=gitea,db=0,pool_size=100,idle_timeout=180",
22+
Result: "network=tcp,addr=:6379,password=******,db=0,pool_size=100,idle_timeout=180",
23+
},
24+
{
25+
Provider: "mysql",
26+
CfgItem: "root:@tcp(localhost:3306)/gitea?charset=utf8",
27+
Result: "root:******@tcp(localhost:3306)/gitea?charset=utf8",
28+
},
29+
{
30+
Provider: "mysql",
31+
CfgItem: "/gitea?charset=utf8",
32+
Result: "/gitea?charset=utf8",
33+
},
34+
{
35+
Provider: "mysql",
36+
CfgItem: "user:mypassword@/dbname",
37+
Result: "user:******@/dbname",
38+
},
39+
{
40+
Provider: "postgres",
41+
CfgItem: "user=pqgotest dbname=pqgotest sslmode=verify-full",
42+
Result: "user=pqgotest dbname=pqgotest sslmode=verify-full",
43+
},
44+
{
45+
Provider: "postgres",
46+
CfgItem: "user=pqgotest password= dbname=pqgotest sslmode=verify-full",
47+
Result: "user=pqgotest password=****** dbname=pqgotest sslmode=verify-full",
48+
},
49+
{
50+
Provider: "postgres",
51+
CfgItem: "postgres://user:pass@hostname/dbname",
52+
Result: "postgres://user:******@hostname/dbname",
53+
},
54+
{
55+
Provider: "couchbase",
56+
CfgItem: "http://dev-couchbase.example.com:8091/",
57+
Result: "http://dev-couchbase.example.com:8091/",
58+
},
59+
{
60+
Provider: "couchbase",
61+
CfgItem: "http://user:[email protected]:8091/",
62+
Result: "http://user:******@dev-couchbase.example.com:8091/",
63+
},
64+
}
65+
66+
for _, k := range kases {
67+
assert.EqualValues(t, k.Result, shadowPassword(k.Provider, k.CfgItem))
68+
}
69+
}

0 commit comments

Comments
 (0)